TailwindCSS: Generating variants through "matchVariants" does not generate correct CSS for nested groups and peers

huangapple go评论68阅读模式
英文:

TailwindCSS: Generating variants through "matchVariants" does not generate correct CSS for nested groups and peers

问题

我尝试生成适合我的应用程序需求的变体,但我有嵌套的组类需要命名以避免歧义。只要在元素的类中不添加类似“group/any-name-here”这样的名称后缀,生成的类就能正常工作。

在tailwind.config.js中,我导入了“tailwind/plugin”以访问核心tailwind提供的“matchVariant”函数。
以下是我目前创建的代码片段...

// begin snippet: js hide: false console: true babel: false

// language: lang-js
matchVariant("group-status", (value) => `:merge(.group[data-status=${value}]) &`, {
   values: {
      active: "active",
      inactive: "inactive"
   }
});

// end snippet

我想知道为什么以及如何添加生成的样式;因为在tailwind的核心变体中,例如hover,组变体名称后缀被附加在父组选择器的末尾,如下所示:

Core Tailwind Variant CSS示例

而生成的变体通常不会附加相应的组变体名称后缀:

生成的变体CSS示例

英文:

I tried generating variants which suits the needs of my app, however, I have nested group classes which require names to avoid ambiguity. The generated classes works fine as long as no name suffix, like "group/any-name-here", is added to the group variant within the element's class.

In tailwind.config.js, I imported "tailwind/plugin" to access the "matchVariant" function as provided by core tailwind.
.
Here is the current snippet I have created...

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

 matchVariant(&quot;group-status&quot;, (value) =&gt; `:merge(.group[data-status=${ value }]) &amp;`, {
    values: {
       active: &quot;active&quot;,
       inactive: &quot;inactive&quot;
    }
 });

<!-- end snippet -->

I want to know why and how to add in the generated styles; because in core variants from tailwind such as hover the group variant name suffix is appended at the end of the parent group selector, as shown below:

Sample from Core Tailwind Variant CSS

While the variants generated tend to not have appended the respective group variant name suffix:

Sample of Generated Variant CSS

答案1

得分: 1

您需要使用传递给matchVariant()函数参数的第二个参数中的modifier值,就像在Tailwind核心中一样:

matchVariant(
  'group-aria',
  (value, { modifier }) =>
    modifier
      ? `:merge(.group\\/${modifier})[aria-${normalize(value)}] &amp;`
      : `:merge(.group)[aria-${normalize(value)}] &amp;`,
  { values: theme('aria') ?? {} }
)

因此,要适应您的插件:

tailwind.config = {
  plugins: [
    tailwind.plugin(({ matchVariant }) => {
      matchVariant(
        'group-status',
        (value, { modifier }) =>
          modifier
            ? `:merge(.group\\/${modifier})[data-status=${value}] &amp;`
            : `:merge(.group)[data-status=${value}] &amp;`,
        {
          values: {
            inactive: 'inactive',
            active: 'active',
          }
        }
      );
    }),
  ],
}
<script src="https://cdn.tailwindcss.com/3.3.2"></script>

<div data-status="inactive" class="group/foo">
  <div data-status="active" class="group/bar">
    <div class="w-10 h-10 group-status-inactive/foo:bg-red-500"></div>
    <div class="w-10 h-10 group-status-active/bar:bg-green-500"></div>
  </div>
</div>
英文:

You'd need to use the modifier value from the second parameter passed to the function parameter in matchVariant(), just like in Tailwind core:

matchVariant(
  &#39;group-aria&#39;,
  (value, { modifier }) =&gt;
    modifier
      ? `:merge(.group\\/${modifier})[aria-${normalize(value)}] &amp;`
      : `:merge(.group)[aria-${normalize(value)}] &amp;`,
  { values: theme(&#39;aria&#39;) ?? {} }
)

So to adapt to your plugin:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

tailwind.config = {
  plugins: [
    tailwind.plugin(({ matchVariant }) =&gt; {
      matchVariant(
        &#39;group-status&#39;,
        (value, { modifier }) =&gt;
          modifier
            ? `:merge(.group\\/${modifier})[data-status=${value}] &amp;`
            : `:merge(.group)[data-status=${value}] &amp;`,
        {
          values: {
            inactive: &#39;inactive&#39;,
            active: &#39;active&#39;,
          }
        }
      );
    }),
  ],
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdn.tailwindcss.com/3.3.2&quot;&gt;&lt;/script&gt;

&lt;div data-status=&quot;inactive&quot; class=&quot;group/foo&quot;&gt;
  &lt;div data-status=&quot;active&quot; class=&quot;group/bar&quot;&gt;
    &lt;div class=&quot;w-10 h-10 group-status-inactive/foo:bg-red-500&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;w-10 h-10 group-status-active/bar:bg-green-500&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月18日 14:24:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定