Tailwind brings dynamic flair to styling

When attempting to create a custom cursor, I encountered an issue with setting the x and y position values (obtained from mousepos) as the top and left positions using tailwind. For some reason, the `

top-[${mousepos.y}] ,left-[${mousepos.x}]
` stylings are not working, although the others work perfectly.

Here is the code snippet:

import React, { useEffect, useState } from "react";

const Practice = () => {
  const [mousepos, setMousepos] = useState({ x: 0, y: 0 });
  useEffect(() => {
    const mouseMove = (e) => {
      setMousepos({
        x: e.clientX,
        y: e.clientY,
      });
    };
    window.addEventListener("mousemove", mouseMove);

    return () => {
      window.removeEventListener("mousemove", mouseMove);
    };
  }, []);
  console.log(mousepos);

  return (
    <div>
      <div
        className={`fixed pointer-events-none rounded-full border-2 w-[50px] h-[50px] border-violet-600 top-[${mousepos.y}] left-[${mousepos.x}]`}
      ></div>
    </div>
  );
};

export default Practice;

Answer №1

If you're looking to dynamically generate classes with TailwindCSS, you may run into some limitations. When attempting to create a class using the following code snippet...

top-[${mousepos.y}] left-[${mousepos.x}]

...TailwindCSS won't recognize it as a valid class and won't apply the necessary CSS styles.

Instead, you'll need to explicitly define the full class name in your source code. You can achieve this by returning the complete value like so:

function  myFunc() {
  return `top-[${mousepos.y}] left-[${mousepos.x}]` ;
}

Then, you can use it in your className attribute like this:

className={`fixed pointer-events-none rounded-full border-2 w-[50px] h-[50px] border-violet-600 ${myFunc}`}

By including the entire class string directly in your source code, TailwindCSS will be able to correctly generate the corresponding CSS rules.

For more information, check out: https://tailwindcss.com/docs/content-configuration#class-detection-in-depth

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Animate an element to shift towards the lower left corner

My goal is to smoothly move a <p> element to the bottom left of the page using CSS animation. However, I'm encountering issues with the animation as it starts at the original place and abruptly ends at the bottom. I need the animation to be seam ...

The 'Bfrip' button is missing from the DOM

Having some issues with displaying table content using DataTables. Everything seems to be working smoothly except for the Buttons, which are not appearing as expected. <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.1 ...

"Enhance your user interface with Bootstrap tabs featuring a convenient next arrow for navigating

Seeking assistance with updating Bootstrap tabs to the latest version (Bootstrap 4). The current source code can be found here: https://codepen.io/srees/pen/pgVLbm The issue lies in the fact that it utilizes Bootstrap 3 and lacks tab content. I am aiming ...

I keep running into errors whenever I try to run npm install in my React JS project. The only way for me to successfully install dependencies is by using npm install --force. How can I go about resolving these

I am encountering this error message while working on my project: npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: @mui/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="681b1c11040d1b ...

Using Chartjs with Next.js - Dynamically input data to generate graphs

Struggling with integrating user input into my Chart.js graph in Next.js. The main issue is that every time the dataset values change, the entire graph redraws causing a flashing effect due to an image background reload. I believe I'm close to a solu ...

Transforming react.js into HTML and CSS programming languages

I have a small experiment I need help with. As I am not familiar with react.js, I would like to convert my main.jsx file into pure HTML/CSS/JS without using react. The issue I'm facing is how to handle form data submission to the server in vanilla HTM ...

The collapse feature in Bootstrap Reactstrap is having issues when used with a set of components

Struggling to understand why my list of components isn't collapsing at all. It seems like I'm missing a tiny detail because no matter what I try with <div> and <p> tags, nothing changes. component import React, { useState } from "rea ...

When using TouchableWithoutFeedback, I encountered the following error message: "Error: React.Children.only expected to receive a single React element child."

I am encountering the following issue: Error: React.Children.only expected to receive a single React element child. Whenever I incorporate the 'TouchableWithoutFeedback' component in my ReactNative project: return ( <TouchableWithoutFeed ...

The issue of `window.scrollTo(0, 240)` not functioning properly when used in combination with

I am currently in the process of developing a website using Reactjs and Gatsby. The issue I'm facing is that when I have window.scrollTo(0, 240) within the componentDidMount() function, everything works fine when I refresh the site. However, when I n ...

Tips for customizing css for image tags

Here is the CSS code I used for images: img { border: none; max-width: 100%; height: auto !important } To override specific image styles, I added this at the bottom: .locals-list>img { height: 35px; } Below is the HTML code I am work ...

Adjust the height of videos automatically on columns

I need help figuring out how to display two videos with different dimensions side by side without any weird gaps. I've already tried using flex display with no luck. My current code looks like this: <div class="row"> <d ...

What is the best way to display API response data in a tabular format using React.js?

My goal is to display the data from an API response in a table format. I've written some code using Axios for making the API request, but the response is not showing up in the table as expected and there are no errors displayed. I suspect that I may h ...

Importing image assets dynamically in a React application

One of the challenges I face in my ReactJS component is handling imported images. Here's an example of how I currently import and use images in my code: import client01 from '../../assets/images/site/client-01.png'; import client02 from &ap ...

Issue with JQuery Datepicker theme not rendering properly

I have integrated the jQuery UI 1.11.4 with a customized Sunny theme. Take a look at how the date picker should appear below: https://i.sstatic.net/1eEMU.png To keep the current website style intact, I am using CSS Scope while applying the downloaded the ...

I need assistance with customizing the appearance of a menu control in Asp.Net

Currently, I am in the process of creating a user control that will include a menu control. The menu is oriented horizontally and consists of 4 gifs as individual menu items with spacing of about 10px between them. While this setup is relatively simple, I ...

The smooth transition of my collapsible item is compromised when closing, causing the bottom border to abruptly jump to the top

Recently, I implemented a collapsible feature using React and it's functioning well. However, one issue I encountered is that when the collapsible div closes, it doesn't smoothly collapse with the border bottom moving up as smoothly as it opens. ...

Tips for formatting JSON using jQuery

Imagine my JSON data with 3 different sets of information: [ { "Pair":"", "Id":"8ca2df56-2523-4bc3-a648-61ec4debcaaf", "PubDate":"/Date(1463775846000)/", "Provider":null, "Market":"" }, { "Pair":"", ...

Quick guide on prepending text to an IconButton

Is there a way to include custom text before the IconButton in Material UI? Here is the code for the button: <div className={classes.toolbar}> <IconButton onClick={handleDrawerClose}> {theme.direction === 'rtl' ? <Chevron ...

What is the best way to align text in the middle on smaller devices like phones and iPads?

I have been working on a login screen and encountered a problem with the layout on smaller devices. Despite centering all the content perfectly on my computer's browser, when I inspect it using developer tools for smaller devices, everything shifts to ...

Modifying the cursor appearance during object dragging is a simple task that can enhance

html .container, .container * { cursor: url('img/arrowPointer.png'), auto; } javascript $('html').on('dragstart', function () { $('html').addClass('container'); }); $('html').on('d ...