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

Normalize attribute selectors for data-* and aria-* modifiers #14040

Merged
merged 7 commits into from
Jul 24, 2024

Conversation

philipp-spiess
Copy link
Contributor

@philipp-spiess philipp-spiess commented Jul 23, 2024

Fixes #14026
See #14037 for the v3 fix

When translating data- and aria- modifiers with attribute selectors, we currently do not wrap the target attribute in quotes. This only works for keywords (purely alphabetic words) but breaks as soon as there are numbers or things like spaces in the attribute:

<div data-id="foo" class="data-[id=foo]:underline">underlined</div>
<div data-id="f1" class="data-[id=1]:underline">not underlined</div>
<div data-id="foo bar" class="data-[id=foo_bar]:underline">not underlined</div>

Since it's fairly common to have attribute selectors with data- and aria- modifiers, this PR will now wrap the attribute in quotes unless these are already wrapped.

Tailwind Modifier CSS Selector
.data-[id=foo] [data-id='foo']
.data-[id='foo'] [data-id='foo']
.data-[id=foo_i] [data-id='foo i']
.data-[id='foo'_i] [data-id='foo' i]
.data-[id=123] [data-id='123']

@philipp-spiess philipp-spiess changed the title Normalize attribute selectors for data- and aria- modifiers Normalize attribute selectors for data- and aria- modifiers Jul 23, 2024
Copy link
Member

@RobinMalfait RobinMalfait left a comment

Choose a reason for hiding this comment

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

Looks good, just a few small questions / nitpicks

packages/tailwindcss/src/variants.test.ts Outdated Show resolved Hide resolved
packages/tailwindcss/src/variants.test.ts Outdated Show resolved Hide resolved
packages/tailwindcss/src/variants.test.ts Outdated Show resolved Hide resolved
packages/tailwindcss/src/variants.ts Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
philipp-spiess and others added 2 commits July 23, 2024 15:06
Co-authored-by: Robin Malfait <malfait.robin@gmail.com>
@RobinMalfait RobinMalfait changed the title Normalize attribute selectors for data- and aria- modifiers Normalize attribute selectors for data-* and aria-* modifiers Jul 23, 2024
@philipp-spiess philipp-spiess merged commit 300524b into next Jul 24, 2024
1 check passed
@philipp-spiess philipp-spiess deleted the fix/issue-14037/next branch July 24, 2024 14:55
philipp-spiess added a commit that referenced this pull request Jul 24, 2024
)

Fixes #14026 
See #14040 for the v4 fix

When translating `data-` and `aria-` modifiers with attribute selectors,
we currently do not wrap the target attribute in quotes. This only works
for keywords (purely alphabetic words) but breaks as soon as there are
numbers or things like spaces in the attribute:

```html
<div data-id="foo" class="data-[id=foo]:underline">underlined</div>
<div data-id="f1" class="data-[id=1]:underline">not underlined</div>
<div data-id="foo bar" class="data-[id=foo_bar]:underline">not underlined</div>
```

Since it's fairly common to have attribute selectors with `data-` and
`aria-` modifiers, this PR will now wrap the attribute in quotes unless
these are already wrapped.

| Tailwind Modifier  | CSS Selector |
| ------------- | ------------- |
| `.data-[id=foo]`  | `[data-id='foo']`  |
| `.data-[id='foo']`  | `[data-id='foo']`  |
| `.data-[id=foo_i]`  | `[data-id='foo i']`  |
| `.data-[id='foo'_i]`  | `[data-id='foo' i]`  |
| `.data-[id=123]`  | `[data-id='123']`  |

---------

Co-authored-by: Robin Malfait <malfait.robin@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Data attribute modifier doesn't work with numeric values
2 participants