-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to list multiple variants in arbitrary variant #10576
Comments
Hey! So in your expected CSS the span,
.\[span\,\&_h1\]\:font-bold h1 {
font-weight: 700;
} That's the same as this: span {
font-weight: 700;
}
.\[span\,\&_h1\]\:font-bold h1 {
font-weight: 700;
} I'm not really sure what the expected output should be here personally, I kinda just don't think anyone should do this 😅 What do you actually want to achieve here? I think there's a bug here in that the |
@adamwathan My use of the <h1 class="[span,h1]:font-bold">Test</h1>
<div class="[span,&_h1]:font-bold"><h1>Test</h1></div> Neither My specific use case here is essentially— match the element that this utility is injected onto if it is an If multiple selectors aren't natively supported yet but you think they'd be useful, I'd be happy to whip up/collab on a PR to get that added. My guess is it'd likely require a relatively lightweight adjustment to the In other words…
I already have a little helper function that can effectively do the splitting for only commas that appear in a string and are not wrapped in parentheses, brackets, braces, etc.: function splitUnwrappedCommas(str) {
const wrapperPairs = { '(' : ')', '[' : ']', '{' : '}' };
const stack = [];
const result = [];
let current = '';
for (const c of str) {
if (c in wrapperPairs) {
current += c;
stack.push(c);
} else if (Object.values(wrapperPairs).includes(c)) {
if (stack.length === 0) {
throw new Error(`Unexpected closing bracket: ${c}`);
}
const last = stack.pop();
if (last in wrapperPairs && c !== wrapperPairs[last]) {
throw new Error(`Mismatched brackets: ${last} and ${c}`);
}
current += c;
} else if (c === ',' && stack.length === 0) {
result.push(current);
current = '';
} else {
current += c;
}
}
if (stack.length > 0) {
throw new Error(`Unclosed brackets: ${stack.join('')}`);
}
if (current !== '') {
result.push(current.trim());
}
return result;
}; Using this function: splitUnwrappedCommas('test') -> (length: 1) [ 'test' ]
splitUnwrappedCommas('&[attr="1,2,3"]') -> (length: 1) [ '&[attr="1,2,3"]' ]
splitUnwrappedCommas('&:is(test,other)') -> (length: 1) [ '&:is(test,other)' ]
splitUnwrappedCommas('test,other') -> (length: 2) [ 'test', 'other' ] After that, each item in the arbitrary variant array(s) can be iterated over, with an arbitrary variant generated for each. In the case I mentioned earlier, that could look like this: // variant = '[span,h1,&_h1]'
// Register arbitrary variants
if (isArbitraryValue(variant) && !context.variantMap.has(variant)) {
let selectors = splitUnwrappedCommas(normalize(variant.slice(1, -1))) // [ 'span', 'h1', '&_h1' ]
if (!selectors.every(isValidVariantFormatString)) {
return []
}
let fns = selectors.map(parseVariant)
let sort = context.offsets.recordVariant(variant)
context.variantMap.set(variant, [[sort, fns]])
} I'm sure I'm probably oversimplifying the example here, maybe even grossly, but that's the general idea. ☝🏼 |
Hey, we talked this through and the gist is:
I've merged in #10655 that ensures we don't generate any rules in this case. Thanks for reporting this! ✨ |
What version of Tailwind CSS are you using?
v3.2.6
What build tool (or framework if it abstracts the build tool) are you using?
postcss-cli v8.4.19
What version of Node.js are you using?
v18.12.1
What browser are you using?
Chrome v109
What operating system are you using?
macOS Ventura
Reproduction URL
https://play.tailwindcss.com/7TUEdEbhJH
Describe your issue
I'd expect to be able to do something like
[span,h1,&_h1]:font-bold
using an arbitrary variant, but that doesn't appear to work.To better demonstrate the issue, I'll compare the CSS generated by the above utility vs. what I think would be expected:
Expected:
Reality:
This is inconvenient for a couple of reasons:
h1
in the arbitrary variant gets completely ignoredThe text was updated successfully, but these errors were encountered: