Skip to content

Component Lacks Tailwind Styling When Injected Dynamically #1444

Closed Answered by iatomic1
ntdgo asked this question in Q&A
Discussion options

You must be logged in to vote

I've been able to fix it by just injecting the style element with the tailwind classes as a string

// lib/utils.ts
export function injectStyles(css: string): void {
  const style = document.createElement("style");
  style.textContent = css;
  document.head.appendChild(style);
}
//component.tsx
import { createPortal } from "react-dom";
import { useEffect, useState } from "react";
import { Button } from "@/components/ui/button";
import tailwindStyles from "~/assets/main.css?inline";
import { injectStyles } from "@/lib/utils";


export default function X() {
  const [leaderboardTargetEl, setLeaderboardTargetEl] =
    useState<HTMLElement | null>(null);

  useEffect(() => {
    injectStyles(t…

Replies: 1 comment 14 replies

Comment options

You must be logged in to vote
14 replies
@ntdgo
Comment options

@iatomic1
Comment options

@aklinker1
Comment options

@iatomic1
Comment options

Answer selected by ntdgo
@ntdgo
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question A question about usage, not a bug
3 participants
Converted from issue

This discussion was converted from issue #1438 on February 16, 2025 16:28.