Issue with Dynamic Tailwind Classes Not Being Generated When Applied via Function in React

Question:

Hi, I’m fairly new to frontend development, especially with React and JavaScript frameworks like Next.js and Tailwind CSS. I’ve started working on a couple of small projects to improve my understanding, one of which is a UI kit that includes components like buttons, alerts, dropdowns, and autocomplete fields.

One of the challenges I’ve been working on is implementing dynamic positioning for DropDownContent and AutoCompleteContent components. I wanted to pass a position prop to these components to control the absolute positioning of the content div (e.g., bottom-center, top-right, right-middle, etc.). Additionally, I wanted to incorporate conditional animation classes that control how the content opens and closes.

The functionality works, but I’m having trouble when trying to abstract the logic into a reusable function. Here’s an example of the function I created for dynamically assigning these classes:

export function getShowDynamicAbsolutePosition(
  position: AbsolutePositionProps,
  prefixShow: string,
): string {
  let result: string = prefixShow + "DynamicContentShow";

  if (position === "bottom-center" || position === "top-center") {
    result = prefixShow + "DynamicContentShowCenter";
  } else if (position === "left-middle" || position === "right-middle") {
    result = prefixShow + "DynamicContentShowMiddle";
  }

  return result;
}

For example, here’s how I’m using it in the AutoCompleteContent component:

function getAutoCompleteContentStyle(props: AutoCompleteContentProps): string {
  const radius: string = getRadius(props.radius);
  const shadow: string = getShadow(props.shadow);
  const bgColor: string = getColor(
    false,
    props.color,
    props.variant,
    props.blurProps,
  );
  const position: AbsolutePositionProps = props.position
    ? props.position
    : "bottom-center";
  const showDynamicPosition: string = getShowDynamicAbsolutePosition(
    position,
    "peer-[.focus]:",
  );
  const hideDynamicPosition: string = getHideDynamicAbsolutePosition(
    position,
    "peer-[.notfocus]:",
  );

  return (
    radius +
    " " +
    shadow +
    " " +
    bgColor +
    " " +
    showDynamicPosition +
    " " +
    hideDynamicPosition +
    " " +
    position
  );
}

The Issue:

When I manually include the showDynamicPosition value directly in the className, like this:

className={
  "h-fit absolute p-2 peer-[.focus]:DynamicContentShowCenter peer-[.notfocus]:DynamicContentHiddenCenter " +
  style +
  " " +
  props.className
}

Everything works perfectly. The Tailwind CSS classes are generated correctly, and the animations are applied as expected.

However, when the class value is coming from the getAutoCompleteContentStyle function, Tailwind doesn’t generate the necessary styles for those classes.

Browser Inspector Differences:

  • When directly included in className:
    Tailwind generates the appropriate classes in the inspector:

    .peer.notfocus ~ .peer-[.notfocus]:DynamicContentHiddenCenter {
        visibility: hidden;
        animation: fadeOutToNoneCenter 0.2s ease-in-out;
    }
    
    .peer.focus ~ .peer-[.focus]:DynamicContentShowCenter {
        visibility: visible;
        animation: fadeInFromNoneCenter 0.2s ease-in-out;
    }
    
  • When coming from the getAutoCompleteContentStyle function:
    The classes aren’t generated at all. Tailwind doesn’t apply any of the dynamic styles, even though the elements have the same class names when inspecting them in the browser.

Question:

Why does Tailwind fail to generate the necessary dynamic styles when applied via the getAutoCompleteContentStyle function? How can I fix this so that Tailwind generates the classes correctly when they are applied through a function like this?


tailwind.config.js:

my tailwind config maybe needed so :

import type { Config } from "tailwindcss";

const config: Config = {
mode: "jit",
darkMode: "selector",
content: [
  "./app/**/*.{js,ts,jsx,tsx,mdx}",
  "./pages/**/*.{js,ts,jsx,tsx,mdx}",
  "./components/**/*.{js,ts,jsx,tsx,mdx}",
  "./future-ui/**/*.{js,ts,jsx,tsx,mdx}",
],
theme: {
  extend: {
    colors: {...},
  },
},
plugins: [],
};

export default config;

Any help or insights would be greatly appreciated! Thanks!

Initially, I thought the issue might be related to the timing of when Tailwind’s Just-in-Time (JIT) mode compiles the styles. I suspected that the className was being applied too late for Tailwind to detect and compile the necessary styles. To resolve this, I attempted to optimize the code execution, ensuring that the className was assigned faster or in sync with Tailwind’s compilation process. However, these efforts didn’t seem to have any effect, and the issue persisted.

1

Tailwind won’t generate dynamic classnames

In your case it comes from a function

https://tailwindcss.com/docs/content-configuration#dynamic-class-names

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật