英文:
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,组变体名称后缀被附加在父组选择器的末尾,如下所示:
而生成的变体通常不会附加相应的组变体名称后缀:
英文:
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("group-status", (value) => `:merge(.group[data-status=${ value }]) &`, {
values: {
active: "active",
inactive: "inactive"
}
});
<!-- 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:
答案1
得分: 1
您需要使用传递给matchVariant()
函数参数的第二个参数中的modifier
值,就像在Tailwind核心中一样:
matchVariant(
'group-aria',
(value, { modifier }) =>
modifier
? `:merge(.group\\/${modifier})[aria-${normalize(value)}] &`
: `:merge(.group)[aria-${normalize(value)}] &`,
{ values: theme('aria') ?? {} }
)
因此,要适应您的插件:
tailwind.config = {
plugins: [
tailwind.plugin(({ matchVariant }) => {
matchVariant(
'group-status',
(value, { modifier }) =>
modifier
? `:merge(.group\\/${modifier})[data-status=${value}] &`
: `:merge(.group)[data-status=${value}] &`,
{
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(
'group-aria',
(value, { modifier }) =>
modifier
? `:merge(.group\\/${modifier})[aria-${normalize(value)}] &`
: `:merge(.group)[aria-${normalize(value)}] &`,
{ values: theme('aria') ?? {} }
)
So to adapt to your plugin:
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-js -->
tailwind.config = {
plugins: [
tailwind.plugin(({ matchVariant }) => {
matchVariant(
'group-status',
(value, { modifier }) =>
modifier
? `:merge(.group\\/${modifier})[data-status=${value}] &`
: `:merge(.group)[data-status=${value}] &`,
{
values: {
inactive: 'inactive',
active: 'active',
}
}
);
}),
],
}
<!-- language: lang-html -->
<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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论