Skip to content

Commit 2cae042

Browse files
authored
Add aria variants (#9557)
* Add aria variants * Add group and peer variants to test * Add support for group and peer modifiers
1 parent 66f39a4 commit 2cae042

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

src/corePlugins.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,26 @@ export let variantPlugins = {
381381
)
382382
},
383383

384+
ariaVariants: ({ matchVariant, theme }) => {
385+
matchVariant('aria', (value) => `&[aria-${value}]`, { values: theme('aria') ?? {} })
386+
matchVariant(
387+
'group-aria',
388+
(value, { modifier }) =>
389+
modifier
390+
? `:merge(.group\\/${modifier})[aria-${value}] &`
391+
: `:merge(.group)[aria-${value}] &`,
392+
{ values: theme('aria') ?? {} }
393+
)
394+
matchVariant(
395+
'peer-aria',
396+
(value, { modifier }) =>
397+
modifier
398+
? `:merge(.peer\\/${modifier})[aria-${value}] ~ &`
399+
: `:merge(.peer)[aria-${value}] ~ &`,
400+
{ values: theme('aria') ?? {} }
401+
)
402+
},
403+
384404
orientationVariants: ({ addVariant }) => {
385405
addVariant('portrait', '@media (orientation: portrait)')
386406
addVariant('landscape', '@media (orientation: landscape)')

src/lib/setupContextUtils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@ function resolvePlugins(context, root) {
717717
let beforeVariants = [
718718
variantPlugins['pseudoElementVariants'],
719719
variantPlugins['pseudoClassVariants'],
720+
variantPlugins['ariaVariants'],
720721
]
721722
let afterVariants = [
722723
variantPlugins['supportsVariants'],

stubs/defaultConfig.stub.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ module.exports = {
112112
pulse: 'pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite',
113113
bounce: 'bounce 1s infinite',
114114
},
115+
aria: {
116+
checked: 'checked="true"',
117+
disabled: 'disabled="true"',
118+
expanded: 'expanded="true"',
119+
hidden: 'hidden="true"',
120+
pressed: 'pressed="true"',
121+
readonly: 'readonly="true"',
122+
required: 'required="true"',
123+
selected: 'selected="true"',
124+
},
115125
aspectRatio: {
116126
auto: 'auto',
117127
square: '1 / 1',

tests/arbitrary-variants.test.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,69 @@ test('classes in the same arbitrary variant should not be prefixed', () => {
616616
})
617617
})
618618

619+
it('should support aria variants', () => {
620+
let config = {
621+
content: [
622+
{
623+
raw: html`
624+
<div>
625+
<div class="aria-checked:underline"></div>
626+
<div class="aria-[sort=ascending]:underline"></div>
627+
<div class="group-aria-checked:underline"></div>
628+
<div class="peer-aria-checked:underline"></div>
629+
<div class="group-aria-checked/foo:underline"></div>
630+
<div class="peer-aria-checked/foo:underline"></div>
631+
<div class="group-aria-[sort=ascending]:underline"></div>
632+
<div class="peer-aria-[sort=ascending]:underline"></div>
633+
<div class="group-aria-[sort=ascending]/foo:underline"></div>
634+
<div class="peer-aria-[sort=ascending]/foo:underline"></div>
635+
</div>
636+
`,
637+
},
638+
],
639+
corePlugins: { preflight: false },
640+
}
641+
642+
let input = css`
643+
@tailwind utilities;
644+
`
645+
646+
return run(input, config).then((result) => {
647+
expect(result.css).toMatchFormattedCss(css`
648+
.aria-checked\:underline[aria-checked='true'] {
649+
text-decoration-line: underline;
650+
}
651+
.aria-\[sort\=ascending\]\:underline[aria-sort='ascending'] {
652+
text-decoration-line: underline;
653+
}
654+
.group[aria-checked='true'] .group-aria-checked\:underline {
655+
text-decoration-line: underline;
656+
}
657+
.group\/foo[aria-checked='true'] .group-aria-checked\/foo\:underline {
658+
text-decoration-line: underline;
659+
}
660+
.group[aria-sort='ascending'] .group-aria-\[sort\=ascending\]\:underline {
661+
text-decoration-line: underline;
662+
}
663+
.group\/foo[aria-sort='ascending'] .group-aria-\[sort\=ascending\]\/foo\:underline {
664+
text-decoration-line: underline;
665+
}
666+
.peer[aria-checked='true'] ~ .peer-aria-checked\:underline {
667+
text-decoration-line: underline;
668+
}
669+
.peer\/foo[aria-checked='true'] ~ .peer-aria-checked\/foo\:underline {
670+
text-decoration-line: underline;
671+
}
672+
.peer[aria-sort='ascending'] ~ .peer-aria-\[sort\=ascending\]\:underline {
673+
text-decoration-line: underline;
674+
}
675+
.peer\/foo[aria-sort='ascending'] ~ .peer-aria-\[sort\=ascending\]\/foo\:underline {
676+
text-decoration-line: underline;
677+
}
678+
`)
679+
})
680+
})
681+
619682
it('should support supports', () => {
620683
let config = {
621684
theme: {

0 commit comments

Comments
 (0)