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 [open] variant #4627

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 11 additions & 0 deletions src/jit/corePlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,17 @@ export default {
)
}
},
booleanAttributeVariants: function ({ addVariant }) {
let booleanAttributeVariants = ['open']

for (let variant of booleanAttributeVariants) {
addVariant(variant, ({ config, modifySelectors }) => {
modifySelectors(({ className }) => {
return `.${e(`${variant}${config('separator')}${className}`)}[${variant}]`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamwathan in addition to styling the elements that declare [open] themselves (e.g. <details>), it'd be really valuable to also integrate the open: variant with the .group and .peer utilities.

For example:

<details class="group peer" open>
  <summary>
    <span class="hidden group-open:inline">Close me, I'm open</span>
    <span class="inline group-open:hidden">Open me, I'm closed</span>
  </summary>
</details>

<span class="hidden peer-open:inline">I'm next to an open element</span>
<span class="inline peer-open:hidden">I'm next to a closed element</span>

Since the booleanAttributeVariants key is new, I wasn't sure how to best integrate with the existing group and peer support from the pseudoElementVariants without copy-pasting. Is there an existing utility we could re-use, or would this require extracting a new one?

})
})
}
},
directionVariants: function ({ config, addVariant }) {
addVariant(
'ltr',
Expand Down
6 changes: 5 additions & 1 deletion src/jit/lib/setupContextUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,11 @@ function resolvePlugins(context, tailwindDirectives, root) {

// TODO: This is a workaround for backwards compatibility, since custom variants
// were historically sorted before screen/stackable variants.
let beforeVariants = [corePlugins['pseudoElementVariants'], corePlugins['pseudoClassVariants']]
let beforeVariants = [
corePlugins['pseudoElementVariants'],
corePlugins['pseudoClassVariants'],
corePlugins['booleanAttributeVariants'],
]
let afterVariants = [
corePlugins['directionVariants'],
corePlugins['reducedMotionVariants'],
Expand Down
21 changes: 21 additions & 0 deletions tests/variantsAtRule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,27 @@ test('it can generate read-only variants', () => {
})
})

test('it can generate open variants', () => {
const input = `
@variants open {
.banana { color: yellow; }
.chocolate { color: brown; }
}
`

const output = `
.banana { color: yellow; }
.chocolate { color: brown; }
.open\\:banana[open] { color: yellow; }
.open\\:chocolate[open] { color: brown; }
`

return run(input).then((result) => {
expect(result.css).toMatchCss(output)
expect(result.warnings().length).toBe(0)
})
})

test('it can generate hover, active and focus variants for multiple classes in one rule', () => {
const input = `
@variants hover, focus, active {
Expand Down