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 target: variant #4601

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
Add target: variant
The `:target` pseudo-selector can be used to style an element whose
`[id]` attribute matches the current URL's [hash][] fragment.

For example:

```html
<div id="section-1" class="border-0 target:border-1"><!-- ... --></div>

<div id="section-2" class="border-0 target:border-1"><!-- ... --></div>
```

When the URL's path is `/#section-1`, the `#section-1` element's
`target:` variant will be active (i.e. `border-1`), and the `#section-2`
element's will be inactive (i.e. `border-0`).

[:target]: https://developer.mozilla.org/en-US/docs/Web/CSS/:target
[hash]: https://developer.mozilla.org/en-US/docs/Web/API/URL/hash
  • Loading branch information
seanpdoyle committed Jun 9, 2021
commit 96b5ba0322857cfa298d24cd68c94aecb508b420
1 change: 1 addition & 0 deletions src/lib/substituteVariantsAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ const defaultVariantGenerators = (config) => ({
odd: generatePseudoClassVariant('nth-child(odd)', 'odd'),
even: generatePseudoClassVariant('nth-child(even)', 'even'),
empty: generatePseudoClassVariant('empty'),
target: generatePseudoClassVariant('target'),
})

function prependStackableVariants(atRule, variants, stackableVariants) {
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 target variants', () => {
const input = `
@variants target {
.banana { color: yellow; }
.chocolate { color: brown; }
}
`

const output = `
.banana { color: yellow; }
.chocolate { color: brown; }
.target\\:banana:target { color: yellow; }
.target\\:chocolate:target { 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