我正在将 tailwind 类字符串从 utils 导入到应用程序文件夹中的索引页面,但它不起作用。直接在应用程序文件夹中用作类名是可行的

问题描述 投票:0回答:1

utils/responsiveBox

const responsiveBoxClass = "border-2 border-[red] xxsmScr:border-tahitiii xsmScr:border-[blue] smScr:border-[green] midScr:border-[orange] bigScr:border-[white] biggerScr:border-[brown] biggestScr:border-[grey]";
export default responsiveBoxClass;

app/page.tsx

import { AvatarContainer } from "@/components/home/avatarContainer";
import { RenderBox } from "@/components/home/renderBox";
import responsiveBoxClass from "@/utils/responsiveBox";

export default function Home() {
  return (
    <div className="w-screen h-[300vh]" style={{ backgroundImage: `url("/home/gvHomeBg.svg")`, backgroundSize: 'cover', backgroundRepeat: 'no-repeat' }}>
      <div className="flex justify-center items-start w-screen">
        <RenderBox className={`bg-primary ${responsiveBoxClass} my-[40px] h-[800px]`}>
          <AvatarContainer />
        </RenderBox>
      </div>

    </div>
  );
}

tailwind.config.ts

/** @type {import('tailwindcss').Config} */
import type { Config } from "tailwindcss";

const config: Config = {
  content: [
    "./pages/**/*.{js,ts,jsx,tsx,mdx}",
    "./components/**/*.{js,ts,jsx,tsx,mdx}",
    "./app/**/*.{js,ts,jsx,tsx,mdx}"
  ],
  theme: {
    screens: {
      'xxsmScr': "360px",
      // => @media (min-width: 360px and max-width: 449px) { ... }
      'xsmScr': "450px",
      // => @media (min-width: 450px and max-width: 639px) { ... }
      'smScr': "640px",
      // => @media (min-width: 640px and max-width: 767px) { ... }

      'midScr': "768px",
      // => @media (min-width: 768px and max-width: 1023px) { ... }

      'bigScr': "1024px",
      // => @media (min-width: 1024px and max-width: 1279px) { ... }

      'biggerScr': "1280px",
      // => @media (min-width: 1280px and max-width: 1535px) { ... }

      'biggestScr': "1536px",
      // => @media (min-width: 1536px) { ... }
    },
    extend: {
      backgroundImage: {
        "gradient-radial": "radial-gradient(var(--tw-gradient-stops))",
        "gradient-conic":
          "conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))",
      },
      colors: {
        primary: {
          foreground: "white",
          DEFAULT: "black",
        },
        tahitiii: {
          foreground: "#FF5733",
          DEFAULT: "#20575A",
        }
      }
    },
  },
  plugins: [],
};

export default config;

当我将 utils 中的字符串 className 包装在应用程序组件文件中 html 元素的 className 下时,我期望它能够工作,但它不起作用。但是直接在 html 元素的类名称中使用 tailwind 类名字符串是可行的。 为什么我不能使用类名称字符串作为模板文本中的导入资源?

reactjs tailwind-css next.js13 postcss tailwind-ui
1个回答
0
投票

Tailwind 为其“看到”的类名生成 CSS 规则。它只查找与

content
文件全局匹配的文件。它不执行任何代码,也不知道导入。它将所有文件视为纯文本文件,就像它们都具有
.txt
扩展名一样。

因此,为了使其正常工作,我们将

utils/responsiveBox
文件添加到您的
content
文件团中,以便 Tailwind 扫描此文件以查找它包含的类名:

/** @type {import('tailwindcss').Config} */
import type { Config } from "tailwindcss";

const config: Config = {
  content: [
    "./pages/**/*.{js,ts,jsx,tsx,mdx}",
    "./components/**/*.{js,ts,jsx,tsx,mdx}",
    "./app/**/*.{js,ts,jsx,tsx,mdx}"
    "utils/responsiveBox",
  ],
© www.soinside.com 2019 - 2024. All rights reserved.