Skip to content
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

Add * variant for targeting direct children #12551

Merged
merged 5 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add `forced-colors` variant ([#11694](https://github.com/tailwindlabs/tailwindcss/pull/11694))
- Add `appearance-auto` utility ([#12404](https://github.com/tailwindlabs/tailwindcss/pull/12404))
- Add logical property values for `float` and `clear` utilities ([#12480](https://github.com/tailwindlabs/tailwindcss/pull/12480))
- Add `*` variant for targeting direct children ([#12551](https://github.com/tailwindlabs/tailwindcss/pull/12551))
- [Oxide] New Rust template parsing engine ([#10252](https://github.com/tailwindlabs/tailwindcss/pull/10252))
- [Oxide] Support `@import "tailwindcss"` using top-level `index.css` file ([#11205](https://github.com/tailwindlabs/tailwindcss/pull/11205), ([#11260](https://github.com/tailwindlabs/tailwindcss/pull/11260)))
- [Oxide] Use `lightningcss` for nesting and vendor prefixes in PostCSS plugin ([#10399](https://github.com/tailwindlabs/tailwindcss/pull/10399))
Expand Down
13 changes: 11 additions & 2 deletions oxide/crates/core/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ impl<'a> Extractor<'a> {
}

// Allowed first characters.
b'@' | b'!' | b'-' | b'<' | b'>' | b'0'..=b'9' | b'a'..=b'z' | b'A'..=b'Z' => {
b'@' | b'!' | b'-' | b'<' | b'>' | b'0'..=b'9' | b'a'..=b'z' | b'A'..=b'Z' | b'*' => {
// TODO: A bunch of characters that we currently support but maybe we only want it behind
// a flag. E.g.: '<sm'
// | '$' | '^' | '_'
Expand Down Expand Up @@ -473,7 +473,7 @@ impl<'a> Extractor<'a> {
b'%' => return ParseAction::Skip,

// < and > can only be part of a variant and only be the first or last character
b'<' | b'>' => {
b'<' | b'>' | b'*' => {
// Can only be the first or last character
// E.g.:
// - <sm:underline
Expand Down Expand Up @@ -797,6 +797,15 @@ mod test {
assert_eq!(candidates, vec!["hover:underline"]);
}

#[test]
fn it_can_parse_start_variants() {
let candidates = run("*:underline", false);
assert_eq!(candidates, vec!["*:underline"]);

let candidates = run("hover:*:underline", false);
assert_eq!(candidates, vec!["hover:*:underline"]);
}

#[test]
fn it_can_parse_simple_candidates_with_stacked_variants() {
let candidates = run("focus:hover:underline", false);
Expand Down
3 changes: 3 additions & 0 deletions src/corePlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import { normalize } from './util/dataTypes'
import { INTERNAL_FEATURES } from './lib/setupContextUtils'

export let variantPlugins = {
childVariant: ({ addVariant }) => {
addVariant('*', '& > *')
},
pseudoElementVariants: ({ addVariant }) => {
addVariant('first-letter', '&::first-letter')
addVariant('first-line', '&::first-line')
Expand Down
1 change: 1 addition & 0 deletions src/lib/setupContextUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,7 @@ function resolvePlugins(context, root) {
// TODO: This is a workaround for backwards compatibility, since custom variants
// were historically sorted before screen/stackable variants.
let beforeVariants = [
variantPlugins['childVariant'],
variantPlugins['pseudoElementVariants'],
variantPlugins['pseudoClassVariants'],
variantPlugins['hasVariants'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should test the 'childVariant' plugin 1`] = `
"
.\\*\\:flex > * {
display: flex;
}
"
`;
3 changes: 3 additions & 0 deletions tests/plugins/variants/childVariant.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { quickVariantPluginTest } from '../../util/run'

quickVariantPluginTest('childVariant').toMatchSnapshot()
33 changes: 33 additions & 0 deletions tests/variants.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1167,3 +1167,36 @@ test('stacking dark and rtl variants with pseudo elements', async () => {
}
`)
})

test('* is matched by the parser as the children variant', async () => {
let config = {
content: [
{
raw: html`
<div class="*:italic" />
<div class="*:hover:italic" />
<div class="hover:*:italic" />
<div class="data-[slot=label]:*:hover:italic" />
<div class="[&_p]:*:hover:italic" />
`,
},
],
corePlugins: { preflight: false },
}

let input = css`
@tailwind utilities;
`

let result = await run(input, config)

expect(result.css).toMatchFormattedCss(css`
.\*\:italic > *,
.\*\:hover\:italic:hover > *,
.hover\:\*\:italic > :hover,
.data-\[slot\=label\]\:\*\:hover\:italic:hover > [data-slot='label'],
.\[\&_p\]\:\*\:hover\:italic:hover > * p {
font-style: italic;
}
`)
})