Skip to content

Commit

Permalink
Add aria variants (#9557)
Browse files Browse the repository at this point in the history
* Add aria variants

* Add group and peer variants to test

* Add support for group and peer modifiers
  • Loading branch information
reinink committed Oct 14, 2022
1 parent 66f39a4 commit 2cae042
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/corePlugins.js
Expand Up @@ -381,6 +381,26 @@ export let variantPlugins = {
)
},

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

orientationVariants: ({ addVariant }) => {
addVariant('portrait', '@media (orientation: portrait)')
addVariant('landscape', '@media (orientation: landscape)')
Expand Down
1 change: 1 addition & 0 deletions src/lib/setupContextUtils.js
Expand Up @@ -717,6 +717,7 @@ function resolvePlugins(context, root) {
let beforeVariants = [
variantPlugins['pseudoElementVariants'],
variantPlugins['pseudoClassVariants'],
variantPlugins['ariaVariants'],
]
let afterVariants = [
variantPlugins['supportsVariants'],
Expand Down
10 changes: 10 additions & 0 deletions stubs/defaultConfig.stub.js
Expand Up @@ -112,6 +112,16 @@ module.exports = {
pulse: 'pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite',
bounce: 'bounce 1s infinite',
},
aria: {
checked: 'checked="true"',
disabled: 'disabled="true"',
expanded: 'expanded="true"',
hidden: 'hidden="true"',
pressed: 'pressed="true"',
readonly: 'readonly="true"',
required: 'required="true"',
selected: 'selected="true"',
},
aspectRatio: {
auto: 'auto',
square: '1 / 1',
Expand Down
63 changes: 63 additions & 0 deletions tests/arbitrary-variants.test.js
Expand Up @@ -616,6 +616,69 @@ test('classes in the same arbitrary variant should not be prefixed', () => {
})
})

it('should support aria variants', () => {
let config = {
content: [
{
raw: html`
<div>
<div class="aria-checked:underline"></div>
<div class="aria-[sort=ascending]:underline"></div>
<div class="group-aria-checked:underline"></div>
<div class="peer-aria-checked:underline"></div>
<div class="group-aria-checked/foo:underline"></div>
<div class="peer-aria-checked/foo:underline"></div>
<div class="group-aria-[sort=ascending]:underline"></div>
<div class="peer-aria-[sort=ascending]:underline"></div>
<div class="group-aria-[sort=ascending]/foo:underline"></div>
<div class="peer-aria-[sort=ascending]/foo:underline"></div>
</div>
`,
},
],
corePlugins: { preflight: false },
}

let input = css`
@tailwind utilities;
`

return run(input, config).then((result) => {
expect(result.css).toMatchFormattedCss(css`
.aria-checked\:underline[aria-checked='true'] {
text-decoration-line: underline;
}
.aria-\[sort\=ascending\]\:underline[aria-sort='ascending'] {
text-decoration-line: underline;
}
.group[aria-checked='true'] .group-aria-checked\:underline {
text-decoration-line: underline;
}
.group\/foo[aria-checked='true'] .group-aria-checked\/foo\:underline {
text-decoration-line: underline;
}
.group[aria-sort='ascending'] .group-aria-\[sort\=ascending\]\:underline {
text-decoration-line: underline;
}
.group\/foo[aria-sort='ascending'] .group-aria-\[sort\=ascending\]\/foo\:underline {
text-decoration-line: underline;
}
.peer[aria-checked='true'] ~ .peer-aria-checked\:underline {
text-decoration-line: underline;
}
.peer\/foo[aria-checked='true'] ~ .peer-aria-checked\/foo\:underline {
text-decoration-line: underline;
}
.peer[aria-sort='ascending'] ~ .peer-aria-\[sort\=ascending\]\:underline {
text-decoration-line: underline;
}
.peer\/foo[aria-sort='ascending'] ~ .peer-aria-\[sort\=ascending\]\/foo\:underline {
text-decoration-line: underline;
}
`)
})
})

it('should support supports', () => {
let config = {
theme: {
Expand Down

0 comments on commit 2cae042

Please sign in to comment.