简体   繁体   English

顺风转换延迟任意值仅适用于特定值

[英]Tailwind transition delay arbitrary value only working for specific values

I am getting really inconsistent behavior with tailwinds arbitrary value functionality, specifically in relation to the transition delay property.我的顺风任意值功能的行为确实不一致,特别是与转换延迟属性有关。 When I use any random value directly within the arbitrary value it has worked for every value I have tested so far (random positive integers).当我直接在任意值内使用任何随机值时,它对我迄今为止测试过的每个值(随机正整数)都有效。 Ex...前任...

<li className="delay-[2455]">{some text}</li>

But if I were to use a variable, the class will only occasionally have any effect depending on the value, seemingly randomly.但是,如果我要使用变量,class 只会偶尔根据值产生任何影响,看似随机。 Ex...前任...

const delay = "250ms";
return <li className={`delay-[${delay}]}`></li>

This segment above will produce a valid class but the segment below will have no effect and will not produce a valid class上面的这个段将产生一个有效的 class 但下面的段将没有效果并且不会产生一个有效的 class

const delay = "500ms";
return <li className={`delay-[${delay}]}`></li>

I am not sure if this is something that I am doing wrong or is some weird quirk in tailwind.我不确定这是我做错了什么还是顺风的一些奇怪的怪癖。 I am open to any and all suggestions.我对任何和所有建议持开放态度。 If it makes a difference I am using typescript in conjunction with react.如果它有所作为,我将 typescript 与 react 结合使用。 I am using tailwindcss version 3.0.11 and postcss version 8.4.5 These are my tailwind.config.js and my postcss.config.js files我正在使用 tailwindcss 版本 3.0.11 和 postcss 版本 8.4.5 这些是我的 tailwind.config.js 和我的 postcss.config.js 文件

module.exports = {
  content: ["./src/**/*.{js,jsx,ts,tsx}",],
  theme: {
    extend: {
      screens: {
        '3xl': '1920px',
        'xsm': '428px',
        '2xsm': '360'
      },
      fontFamily: {
        title: ['Patrick Hand'],
        body: ['Balsamiq Sans']
      },
      transitionProperty: {
        'opacity': 'opacity',
      },
    },
  },
  plugins: [],
}

module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}

Right from the docs:就在文档中:

The most important implication of how Tailwind extracts class names is that it will only find classes that exist as complete unbroken strings in your source files. Tailwind 如何提取 class 名称的最重要含义是,它只会在源文件中找到作为完整的完整字符串存在的类。

If you use string interpolation or concatenate partial class names together, Tailwind will not find them and therefore will not generate the corresponding CSS如果使用字符串插值或将部分 class 名称连接在一起,Tailwind 将找不到它们,因此不会生成相应的 CSS

It means that you need to use full class name, not dynamic concatenation of its parts.这意味着您需要使用完整的 class 名称,而不是其部分的动态连接。

For example you can make a function like that:例如,您可以像这样制作 function:

function getClassByDelay(delay) {
  return {
    250: 'delay-250',
    500: 'delay-500',
    750: 'delay-750',
  }[delay]
}

And use it like so: <li className={getClassByDelay(delay)}></li>并像这样使用它: <li className={getClassByDelay(delay)}></li>

If you really need to use dynamic classes then you can also use another approach: just add them to the safelist array in the config:如果你真的需要使用动态类,那么你也可以使用另一种方法:只需将它们添加到配置中的safelist数组中:

// tailwind.config.js
module.exports = {
  content: [
    // ...
  ],
  safelist: [
    'delay-250',
    'delay-500',
    'delay-750',
    // etc.
  ]
  // ...
}

That way Tailwind will know that it needs to generate this safelist classes anyway, regardless if it finds them in your source code or not.这样,Tailwind 就会知道它无论如何都需要生成这个safelist类,无论它是否在您的源代码中找到它们。

Obviously it has downside that you need to manage this list and not forget to delete unused classes from the config if you stop using them in the code, otherwise they will be a dead weight in your bundle.显然,如果您停止在代码中使用它们,您需要管理此列表并且不要忘记从配置中删除未使用的类,否则它们将成为您捆绑包中的死重。

More info: https://tailwindcss.com/docs/content-configuration#class-detection-in-depth更多信息: https://tailwindcss.com/docs/content-configuration#class-detection-in-depth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM