Skip to content

Commit 483a8f4

Browse files
authored
Merge pull request #33605 from hpohlmeyer/fix/tw4-selectors
Addon Pseudo-states: Process all nested css rules
2 parents bbca9fc + dc787d2 commit 483a8f4

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

code/addons/pseudo-states/src/preview/rewriteStyleSheet.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,24 +198,28 @@ const rewriteRuleContainer = (
198198
// @ts-expect-error We're adding this nonstandard property below
199199
numRewritten = cssRule.__pseudoStatesRewrittenCount;
200200
} else {
201-
if ('cssRules' in cssRule && (cssRule.cssRules as CSSRuleList).length) {
202-
numRewritten = rewriteRuleContainer(
203-
cssRule as CSSGroupingRule,
204-
rewriteLimit - count,
205-
forShadowDOM
206-
);
207-
} else {
208-
if (!('selectorText' in cssRule)) {
209-
continue;
210-
}
211-
const styleRule = cssRule as CSSStyleRule;
201+
let styleRule = cssRule as CSSStyleRule;
202+
203+
// Modify the rule, if it contains a pseudo state
204+
if ('selectorText' in styleRule) {
212205
if (matchOne.test(styleRule.selectorText)) {
213206
const newRule = rewriteRule(styleRule, forShadowDOM);
214207
ruleContainer.deleteRule(index);
215208
ruleContainer.insertRule(newRule, index);
209+
styleRule = ruleContainer.cssRules[index] as CSSStyleRule;
216210
numRewritten = 1;
217211
}
218212
}
213+
214+
// If it has nested rules, check them as well
215+
if ('cssRules' in styleRule && (styleRule.cssRules as CSSRuleList).length) {
216+
numRewritten = rewriteRuleContainer(
217+
styleRule as CSSGroupingRule,
218+
rewriteLimit - count,
219+
forShadowDOM
220+
);
221+
}
222+
219223
// @ts-expect-error We're adding this nonstandard property
220224
cssRule.__processed = true;
221225
// @ts-expect-error We're adding this nonstandard property
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { Meta, StoryObj } from '@storybook/react-vite';
2+
3+
import { Button } from './NestedRules';
4+
5+
const meta = {
6+
title: 'NestedRules',
7+
component: Button,
8+
render: (args, context) => <Button {...args}>{context.name}</Button>,
9+
} satisfies Meta<typeof Button>;
10+
11+
export default meta;
12+
13+
type Story = StoryObj<typeof meta>;
14+
15+
export const NestedHover: Story = {
16+
parameters: {
17+
pseudo: { focusVisible: true },
18+
},
19+
// TODO: Use this test once the pseudostates addon uses the beforeEach API
20+
// play: async ({ canvas }) => {
21+
// const button = canvas.getByRole('button')!;
22+
// await expect(getComputedStyle(button).textDecorationLine).toBe('underline');
23+
// await expect(getComputedStyle(button).textDecorationColor).toBe('rgb(255, 0, 0)');
24+
// },
25+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from 'react';
2+
3+
import './nested.css';
4+
5+
export const Button = (props: React.ButtonHTMLAttributes<HTMLButtonElement>) => (
6+
<button className="nested-focus-visible" {...props} />
7+
);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
button {
2+
display: inline-block;
3+
cursor: pointer;
4+
border: 0;
5+
border-radius: 3em;
6+
background-color: #1ea7fd;
7+
padding: 11px 20px;
8+
color: white;
9+
font-weight: 700;
10+
font-size: 14px;
11+
line-height: 1;
12+
font-family: 'Nunito Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif;
13+
}
14+
15+
.nested-focus-visible {
16+
&:focus-visible {
17+
@supports (color: color-mix(in lab, red, red)) {
18+
text-decoration: underline red;
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)