Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(ButtonGroup, InputGroup): the last-member rule is :nth-last-child(1 of S), not a sibling-relative :has() (#9394)

Cindy Zhange3bd3d547ef2
diff --git a/.changeset/group-last-member-linear-in-webkit.md b/.changeset/group-last-member-linear-in-webkit.md
new file mode 100644
index 0000000000..d2e17020ca
--- /dev/null
+++ b/.changeset/group-last-member-linear-in-webkit.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] ButtonGroup and InputGroup: the rule that rounds a group's last member no longer slows down filling a container in WebKit (as measured), on any page, groups or not. In WebKit, inserting 10,000 children into one element took about 1.15 s with the old rule on the page and about 10 ms with the new one. Corners look exactly as before: only the last member, skipping trailing popovers, templates and dialogs, rounds its trailing corners.
+
+@cixzhang
diff --git a/apps/storybook/stories/ButtonGroupCornersA11y.stories.tsx b/apps/storybook/stories/ButtonGroupCornersA11y.stories.tsx
new file mode 100644
index 0000000000..36d3f36dd1
--- /dev/null
+++ b/apps/storybook/stories/ButtonGroupCornersA11y.stories.tsx
@@ -0,0 +1,179 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file ButtonGroupCornersA11y.stories.tsx
+ * @input ButtonGroup, Button, DropdownMenu, Tooltip, InputGroup and TextInput,
+ * with layer pieces (popovers, templates, dialogs) placed after members
+ * @output Browser fixtures for ButtonGroup FR3: one group per member shape,
+ * each member marked with `data-corner-case`, so a browser spec reads which
+ * members round their trailing corners
+ * @position Read by ButtonGroupCorners.interaction.browser.spec.ts;
+ * `no-visual`, so the stable visual plan never captures these fixtures
+ */
+
+import type {ReactNode} from 'react';
+import type {Meta, StoryObj} from '@storybook/react';
+import {Button} from '@astryxdesign/core/Button';
+import {ButtonGroup} from '@astryxdesign/core/ButtonGroup';
+import {DropdownMenu} from '@astryxdesign/core/DropdownMenu';
+import {InputGroup} from '@astryxdesign/core/InputGroup';
+import {TextInput} from '@astryxdesign/core/TextInput';
+import {Tooltip} from '@astryxdesign/core/Tooltip';
+
+const meta = {
+ title: 'A11y/ButtonGroup corners',
+ tags: ['no-visual'],
+} satisfies Meta;
+
+export default meta;
+type Story = StoryObj<typeof meta>;
+
+/** `count` closed layer pieces of one kind, as inline siblings. */
+function pieces(kind: 'popover' | 'template' | 'dialog', count: number) {
+ return Array.from({length: count}, (_, index) => {
+ const key = `${kind}-${index}`;
+ if (kind === 'template') {
+ return <template key={key} />;
+ }
+ if (kind === 'dialog') {
+ return <dialog key={key} />;
+ }
+ return <div key={key} popover="manual" />;
+ });
+}
+
+/**
+ * Each group's members carry `data-corner-case="<shape>"` and
+ * `data-trailing="round"` or `"square"`: the corners ButtonGroup FR3 promises.
+ */
+function Shape({
+ name,
+ orientation = 'horizontal',
+ children,
+}: {
+ name: string;
+ orientation?: 'horizontal' | 'vertical';
+ children: ReactNode;
+}) {
+ return (
+ <div data-corner-shape={name} style={{padding: 8}}>
+ <ButtonGroup label={name} orientation={orientation}>
+ {children}
+ </ButtonGroup>
+ </div>
+ );
+}
+
+const member = (name: string, trailing: 'round' | 'square', label: string) => (
+ <Button label={label} data-corner-case={name} data-trailing={trailing} />
+);
+
+export const Shapes: Story = {
+ render: () => (
+ <div>
+ <Shape name="two">
+ {member('two', 'square', 'One')}
+ {member('two', 'round', 'Two')}
+ </Shape>
+ <Shape name="two-vertical" orientation="vertical">
+ {member('two-vertical', 'square', 'One')}
+ {member('two-vertical', 'round', 'Two')}
+ </Shape>
+ <Shape name="popover-and-template-after">
+ {member('popover-and-template-after', 'square', 'One')}
+ {member('popover-and-template-after', 'round', 'Two')}
+ {pieces('popover', 1)}
+ {pieces('template', 1)}
+ </Shape>
+ <Shape name="dialog-then-raw-child">
+ {member('dialog-then-raw-child', 'square', 'One')}
+ {pieces('dialog', 1)}
+ <button type="button">Raw</button>
+ </Shape>
+ <Shape name="contents-wrapper-follows">
+ {member('contents-wrapper-follows', 'square', 'One')}
+ <div style={{display: 'contents'}}>
+ <Button label="Wrapped" />
+ </div>
+ </Shape>
+ <Shape name="between-popovers">
+ {pieces('popover', 1)}
+ {member('between-popovers', 'round', 'Only')}
+ {pieces('popover', 1)}
+ </Shape>
+ <Shape name="four-pieces-each">
+ {member('four-pieces-each', 'square', 'One')}
+ {pieces('popover', 2)}
+ {pieces('template', 2)}
+ {member('four-pieces-each', 'round', 'Two')}
+ {pieces('popover', 2)}
+ {pieces('template', 2)}
+ </Shape>
+ {[5, 6, 7].map(count => (
+ <Shape key={count} name={`${count}-pieces-after`}>
+ {member(`${count}-pieces-after`, 'square', 'One')}
+ {member(`${count}-pieces-after`, 'round', 'Two')}
+ {pieces('popover', count)}
+ </Shape>
+ ))}
+ <Shape name="tooltip-trailing">
+ {member('tooltip-trailing', 'square', 'Save')}
+ <Button
+ label="More"
+ tooltip="More options"
+ data-corner-case="tooltip-trailing"
+ data-trailing="round"
+ />
+ </Shape>
+ <Shape name="dropdown-trailing">
+ {member('dropdown-trailing', 'square', 'Allow once')}
+ <DropdownMenu
+ button={{
+ label: 'Allow options',
+ 'data-corner-case': 'dropdown-trailing',
+ 'data-trailing': 'round',
+ }}
+ items={[{label: 'Always allow'}]}
+ />
+ </Shape>
+ <Shape name="tooltip-wrapper-follows">
+ {member('tooltip-wrapper-follows', 'square', 'Save')}
+ <Tooltip content="Rich tip">
+ <Button label="More" />
+ </Tooltip>
+ </Shape>
+ <div data-corner-shape="input-group-addon" style={{padding: 8}}>
+ <InputGroup label="Visibility">
+ <TextInput
+ label="Audience"
+ isLabelHidden
+ value="Members"
+ onChange={() => {}}
+ />
+ <Button
+ label="Save"
+ data-corner-case="input-group-addon"
+ data-trailing="round"
+ />
+ </InputGroup>
+ </div>
+ <div
+ data-corner-shape="input-group-addon-then-member"
+ style={{padding: 8}}>
+ <InputGroup label="Visibility">
+ <Button
+ label="Pick"
+ data-corner-case="input-group-addon-then-member"
+ data-trailing="square"
+ />
+ <TextInput
+ label="Audience"
+ isLabelHidden
+ value="Members"
+ onChange={() => {}}
+ />
+ </InputGroup>
+ </div>
+ </div>
+ ),
+};
diff --git a/docs/specs/components/ButtonGroup/ButtonGroup.spec.md b/docs/specs/components/ButtonGroup/ButtonGroup.spec.md
index 5d40235589..18440b3757 100644
--- a/docs/specs/components/ButtonGroup/ButtonGroup.spec.md
+++ b/docs/specs/components/ButtonGroup/ButtonGroup.spec.md
@@ -13,6 +13,7 @@ review_triggers: [public-api, behavior, layout, theming, accessibility]
verified_by:
[
packages/core/src/ButtonGroup/ButtonGroup.test.tsx,
+ packages/core/src/ButtonGroup/__tests__/ButtonGroupCorners.interaction.browser.spec.ts,
packages/core/src/Layer/layerScopedContext.test.tsx,
packages/core/src/hooks/useListFocus.test.tsx,
]
@@ -178,18 +179,18 @@ carries no anatomy-theming map.
## Verification map
-| Contract | Verification | Representative states | Mutation or failure expectation | Audit section |
-| -------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------- |
-| FR1, AR1 | `ButtonGroup.test.tsx` role, label, ref, `data-testid`, orientation | Horizontal and vertical | A missing name, `aria-orientation`, a dropped ref, or a dropped `data-testid` fails. Evidence gap: no test passes `className`, `style`, or `xstyle`. | `audit:ButtonGroup/accessibility` |
-| FR3 | `ButtonGroup.test.tsx` trailing-radius suite | Tooltip, link, DropdownMenu, HoverCard, and raw-button members | A square trailing member, or a rounded inner member, fails. Evidence gap: the suite renders wrapped members only last and asserts the member before them; no test checks a wrapped member's own corners or divider in any position. | `audit:ButtonGroup/layout` |
-| FR5, FR11 | `ButtonGroup.test.tsx` elevation suite | Every tier; member with its own elevation | Identical classes for two tiers, a mismatched `data-elevation`, or a member not reporting `none` fails. | `audit:ButtonGroup/theming` |
-| FR11 (target, size) | Evidence gap: no test asserts `astryx-button-group` or `data-size` on the root | Each size | — | `audit:ButtonGroup/theming` |
-| FR7, AR3 | `ButtonGroup.test.tsx` disabled cases | Group disabled | An operable member fails. Native-disabled and no-Tab-stop assertions match the FR7 gap. | `audit:ButtonGroup/accessibility` |
-| FR8, AR2 | `ButtonGroup.test.tsx` roving-tabindex suite | Enter and leave; disabled member; unmount; becomes disabled; link | Two Tab stops or a lost stop fails. The suite asserts that a disabled member never holds the stop and that the stop is repaired away from it, matching the FR8 gap. Evidence gap: no test of a busy or self-unmounted holder, pointer focus, or a group disabled after mount. | `audit:ButtonGroup/accessibility` |
-| FR9, FR10, AR2 | `ButtonGroup.test.tsx` keyboard-navigation suite; `useListFocus.test.tsx` "useListFocus disabled-item skipping" for `aria-disabled` items and Home and End; `layerScopedContext.test.tsx` "ends group-owned disabled state while preserving explicit and inner-group state" for the layer boundary | Arrows, Home, End, vertical, RTL, open menu, group inside a popover | Wrong direction, no wrap, landing on a disabled member, stealing a menu's keys, or group state leaking into a member's layer fails. Evidence gap: no test skips a busy or tooltip-disabled Button member. | `audit:ButtonGroup/accessibility` |
-| FR2, FR3, FR4, FR6 | Evidence gap: no assertion of gap-free layout, divider paint, the absent press scale, non-Button children keeping their own paint, or member size inheritance beyond rendering | — | — | `audit:ButtonGroup/layout` |
-| ORD2 | `ButtonGroup.test.tsx` elevation suite | Member with its own elevation | A member not reporting `none` fails. | `audit:ButtonGroup/theming` |
-| ORD1, ORD3, PR1, AR4 | Evidence gap: no test asserts member size precedence, a member `isDisabled` inside an enabled group, when the Tab stop is repaired, or the member focus indicator | — | — | `audit:ButtonGroup/behavior` |
+| Contract | Verification | Representative states | Mutation or failure expectation | Audit section |
+| -------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------- |
+| FR1, AR1 | `ButtonGroup.test.tsx` role, label, ref, `data-testid`, orientation | Horizontal and vertical | A missing name, `aria-orientation`, a dropped ref, or a dropped `data-testid` fails. Evidence gap: no test passes `className`, `style`, or `xstyle`. | `audit:ButtonGroup/accessibility` |
+| FR3 | `ButtonGroupCorners.interaction.browser.spec.ts` in the Chromium interaction lane; `ButtonGroup.test.tsx` trailing-radius suite (compiled selector shape and membership; jsdom never receives the rule) | Two members, horizontal and vertical; popover, template and dialog pieces after members, up to seven; a member between popovers; display-contents wrapper, Tooltip-wrapped and raw-button followers; tooltip'd and DropdownMenu trailing members; an InputGroup button addon last and first | A square trailing member, or a rounded inner member, fails; a compiled trailing selector that uses `:last-child` or a sibling-relative `:has()` fails. Evidence gap: the jsdom suite and the browser story render wrapped members only last and check the member before them; no test checks a wrapped member's own corners or divider in any position. | `audit:ButtonGroup/layout` |
+| FR5, FR11 | `ButtonGroup.test.tsx` elevation suite | Every tier; member with its own elevation | Identical classes for two tiers, a mismatched `data-elevation`, or a member not reporting `none` fails. | `audit:ButtonGroup/theming` |
+| FR11 (target, size) | Evidence gap: no test asserts `astryx-button-group` or `data-size` on the root | Each size | — | `audit:ButtonGroup/theming` |
+| FR7, AR3 | `ButtonGroup.test.tsx` disabled cases | Group disabled | An operable member fails. Native-disabled and no-Tab-stop assertions match the FR7 gap. | `audit:ButtonGroup/accessibility` |
+| FR8, AR2 | `ButtonGroup.test.tsx` roving-tabindex suite | Enter and leave; disabled member; unmount; becomes disabled; link | Two Tab stops or a lost stop fails. The suite asserts that a disabled member never holds the stop and that the stop is repaired away from it, matching the FR8 gap. Evidence gap: no test of a busy or self-unmounted holder, pointer focus, or a group disabled after mount. | `audit:ButtonGroup/accessibility` |
+| FR9, FR10, AR2 | `ButtonGroup.test.tsx` keyboard-navigation suite; `useListFocus.test.tsx` "useListFocus disabled-item skipping" for `aria-disabled` items and Home and End; `layerScopedContext.test.tsx` "ends group-owned disabled state while preserving explicit and inner-group state" for the layer boundary | Arrows, Home, End, vertical, RTL, open menu, group inside a popover | Wrong direction, no wrap, landing on a disabled member, stealing a menu's keys, or group state leaking into a member's layer fails. Evidence gap: no test skips a busy or tooltip-disabled Button member. | `audit:ButtonGroup/accessibility` |
+| FR2, FR3, FR4, FR6 | Evidence gap: no assertion of gap-free layout, divider paint, the absent press scale, non-Button children keeping their own paint, or member size inheritance beyond rendering | — | — | `audit:ButtonGroup/layout` |
+| ORD2 | `ButtonGroup.test.tsx` elevation suite | Member with its own elevation | A member not reporting `none` fails. | `audit:ButtonGroup/theming` |
+| ORD1, ORD3, PR1, AR4 | Evidence gap: no test asserts member size precedence, a member `isDisabled` inside an enabled group, when the Tab stop is repaired, or the member focus indicator | — | — | `audit:ButtonGroup/behavior` |
## Current-state impact
diff --git a/internal/test-utils/src/README.md b/internal/test-utils/src/README.md
index db788b8938..dffd8f8964 100644
--- a/internal/test-utils/src/README.md
+++ b/internal/test-utils/src/README.md
@@ -4,8 +4,7 @@ Source code for shared testing utilities.
<!-- SYNC: When files in this directory change, update this document. -->
-| File | Role | Purpose |
-|------|------|---------|
-| `index.ts` | Entry | Re-exports testing utilities from @testing-library/react |
-| `setup.ts` | Setup | Vitest setup file; extends matchers with jest-dom |
-
+| File | Role | Purpose |
+| ---------- | ----- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| `index.ts` | Entry | Re-exports testing utilities from @testing-library/react |
+| `setup.ts` | Setup | Vitest setup file; extends matchers with jest-dom, adds jsdom polyfills, and keeps the group last-member rule (`:nth-last-child(1 of …)`) out of jsdom's stylesheet |
diff --git a/internal/test-utils/src/setup.ts b/internal/test-utils/src/setup.ts
index 991bb056f0..d35f7bdc53 100644
--- a/internal/test-utils/src/setup.ts
+++ b/internal/test-utils/src/setup.ts
@@ -3,7 +3,9 @@
/**
* @file setup.ts
* @input Uses @testing-library/jest-dom/vitest
- * @output Extends Vitest expect with jest-dom matchers (toBeInTheDocument, etc.)
+ * @output Extends Vitest expect with jest-dom matchers (toBeInTheDocument, etc.),
+ * jsdom polyfills, and keeps the group last-member rule out of jsdom's
+ * stylesheet
* @position Test setup; loaded by vitest.config.ts before all tests
*
* SYNC: When modified, update this header and /internal/test-utils/src/README.md
@@ -36,6 +38,32 @@ afterEach(() => {
__resetClickGuardForTest();
});
+// jsdom's selector engine evaluates the group last-member rule,
+// `:nth-last-child(1 of S)`, far too slowly and differently from browsers: a
+// document of a few hundred elements took tens of seconds per
+// `getComputedStyle`, which every role-and-name query calls, and a group
+// member between two popovers did not match. So jsdom never receives that
+// rule (ButtonGroup FR3); ButtonGroupCorners.interaction.browser.spec.ts checks
+// group corners in a real browser, and jsdom tests assert group
+// membership only. Other `of S` rules, such as Table's zebra striping, still
+// reach jsdom.
+const GROUP_LAST_MEMBER = ':nth-last-child(1 of ';
+const insertRule = CSSStyleSheet.prototype.insertRule;
+CSSStyleSheet.prototype.insertRule = function insertRuleWithoutGroupLastMember(
+ rule: string,
+ index?: number,
+): number {
+ if (rule.includes(GROUP_LAST_MEMBER)) {
+ // Refuse it as a browser refuses an unsupported rule, so StyleX's
+ // injector records nothing inserted and keeps its group offsets right.
+ throw new DOMException(
+ 'jsdom does not receive the group last-member rule',
+ 'SyntaxError',
+ );
+ }
+ return insertRule.call(this, rule, index);
+};
+
// Polyfill for matchMedia (not supported in jsdom).
//
// `(hover: hover)` reports TRUE: jsdom models a desktop browser driven by a
diff --git a/packages/core/src/Button/Button.tsx b/packages/core/src/Button/Button.tsx
index b2680bbc7f..a698dadd0e 100644
--- a/packages/core/src/Button/Button.tsx
+++ b/packages/core/src/Button/Button.tsx
@@ -463,8 +463,14 @@ const loadingStyles = stylex.create({
* popover is never an in-flow member — it is `display: none` until shown, then
* promoted to the top layer. Context layers also retain an inert `<template>`
* marker so they can re-resolve their JSX position. Neither element is a group
- * member, so "last member" is: no following element sibling besides those two
- * pieces of layer infrastructure.
+ * member, so "last member" is: no following element sibling besides those
+ * pieces of layer infrastructure — the last sibling that is not one of them.
+ *
+ * Written as `:nth-last-child(1 of …)`, not `:not(:has(~ …))`: the stylesheet
+ * is global, and WebKit re-evaluates a sibling-relative `:has()` against every
+ * earlier sibling on each insertion, so filling any container on the page
+ * became quadratic (ButtonGroup FR3). A member is never layer infrastructure,
+ * so being the last such sibling is the same as having none after it.
*
* Reading it the other way round — marking the *buttons* and testing for a
* marked sibling — is the trap: it silently reclassifies anything it doesn't
@@ -480,7 +486,8 @@ const loadingStyles = stylex.create({
* The leading edge still uses `:first-child` — a member's button always precedes
* its own layer, so the first button is genuinely `:first-child`.
*/
-const IS_LAST_ITEM = ':not(:has(~ *:not([popover]):not(template):not(dialog)))';
+const IS_LAST_ITEM =
+ ':nth-last-child(1 of :not([popover]):not(template):not(dialog))';
const groupStyles = stylex.create({
horizontal: {
diff --git a/packages/core/src/ButtonGroup/ButtonGroup.test.tsx b/packages/core/src/ButtonGroup/ButtonGroup.test.tsx
index ec2eb8f106..535c5fd31d 100644
--- a/packages/core/src/ButtonGroup/ButtonGroup.test.tsx
+++ b/packages/core/src/ButtonGroup/ButtonGroup.test.tsx
@@ -5,7 +5,8 @@
* @input Uses vitest, @testing-library/react, @babel/core + @stylexjs/babel-plugin,
* ButtonGroup and Button components
* @output Unit tests for ButtonGroup, incl. the compiled-CSS contract for the
- * trailing radius (#2508)
+ * trailing radius (#2508): compiled selector shape and membership; corners
+ * in ButtonGroupCorners.interaction.browser.spec.ts
* @position Testing; validates ButtonGroup component implementation
*
* SYNC: When ButtonGroup component changes, update tests to match new behavior
@@ -209,23 +210,30 @@ describe('ButtonGroup', () => {
// DropdownMenu), and useLayer renders a marker plus the layer inline when the
// host is safe, so they steal the slot. See IS_LAST_ITEM in Button.tsx.
//
- // HOW THESE TESTS CATCH THE BUG
- // jsdom applies no StyleX CSS, so a DOM-only test cannot prove which rule
- // wins — and a test that hand-copies the selector is a tautology (revert
- // groupStyles to `:last-child` and it still passes). So the predicate is NOT
- // hand-copied here: `compileButtonRules()` runs the REAL StyleX babel plugin
- // over Button.tsx (same config as scripts/build-css.mjs) and reads the
- // trailing-radius rule's selector straight out of the emitted CSS, then
- // matches it against the real rendered DOM. Revert groupStyles to
- // `:last-child` and these go red.
+ // WHAT THESE TESTS CHECK, AND WHERE THE CORNERS ARE CHECKED
+ // The trailing predicate is `:nth-last-child(1 of S)` (S = not a layer
+ // piece), which jsdom evaluates slowly and wrongly, so jsdom never receives
+ // it (internal/test-utils/src/setup.ts). Here, `compileButtonRules()` runs
+ // the REAL StyleX babel plugin over Button.tsx and asserts the compiled
+ // trailing selector's shape (layer-skipping, never `:last-child`), and the
+ // DOM tests assert MEMBERSHIP: which element is the group's last member,
+ // with the layer pieces after it. Which corners actually round is checked in
+ // a real browser by ButtonGroupCorners.interaction.browser.spec.ts
+ // (ButtonGroup FR3).
// ===========================================================================
describe('trailing radius (#2508)', () => {
/** The group members, in DOM order (excludes invisible layer siblings). */
const items = (group: HTMLElement): Element[] =>
Array.from(
- group.querySelectorAll(':scope > *:not([popover]):not(template)'),
+ group.querySelectorAll(
+ ':scope > *:not([popover]):not(template):not(dialog)',
+ ),
);
+ /** The element is the group's last member (layer pieces may follow it). */
+ const isLastMember = (group: HTMLElement, el: Element): boolean =>
+ items(group).at(-1) === el;
+
// -- Compiled CSS, read from the source -----------------------------------
/** One atomic StyleX rule, parsed out of the plugin's emitted CSS. */
@@ -238,7 +246,7 @@ describe('ButtonGroup', () => {
property: string;
/** CSS value, e.g. `var(--radius-element)`. */
value: string;
- /** The full compiled selector, e.g. `.xp0wexf:not(:has(~ [data-...]))`. */
+ /** The full compiled selector, e.g. `.xp0wexf:nth-last-child(1 of …)`. */
selector: string;
};
@@ -355,14 +363,11 @@ describe('ButtonGroup', () => {
return rules.map(rule => rule.selector);
};
- /** Does the compiled CSS actually round `el`'s trailing corners? */
- const hasRoundedTrailingCorners = (
+ /** `el` carries both trailing-corner rounding rules (StyleX classes). */
+ const carriesTrailingRule = (
el: Element,
orientation: Orientation = 'horizontal',
- ): boolean =>
- trailingRoundingSelectors(el, orientation).every(selector =>
- el.matches(selector),
- );
+ ): boolean => trailingRoundingSelectors(el, orientation).length === 2;
// -- The compiled CSS contract --------------------------------------------
@@ -380,13 +385,18 @@ describe('ButtonGroup', () => {
for (const selector of selectors) {
// `:last-child` is the bug: an inline layer element steals the slot.
- expect(selector).not.toContain(':last-child');
+ expect(selector).not.toMatch(/:last-child/);
+ // The last sibling that is not a layer piece, written so WebKit
+ // does not re-evaluate it per sibling on every insertion (#9281).
+ expect(selector).toContain(':nth-last-child(1 of ');
+ expect(selector).not.toContain(':has(');
// `[popover]` must survive compilation *verbatim*. StyleX only
// statically evaluates a selector key from a same-file const; from a
// .stylex.ts file it compiles to a mangled selector like `[x13pbwiz]`
// that matches nothing in the DOM.
expect(selector).toContain('[popover]');
expect(selector).toContain('template');
+ expect(selector).toContain('dialog');
}
},
);
@@ -439,8 +449,9 @@ describe('ButtonGroup', () => {
expect(more).not.toBe(group.lastElementChild);
expect(items(group).at(-1)).toBe(more);
- expect(hasRoundedTrailingCorners(more, orientation)).toBe(true);
- expect(hasRoundedTrailingCorners(save, orientation)).toBe(false);
+ expect(carriesTrailingRule(more, orientation)).toBe(true);
+ expect(isLastMember(group, more)).toBe(true);
+ expect(isLastMember(group, save)).toBe(false);
},
);
@@ -465,8 +476,9 @@ describe('ButtonGroup', () => {
expect(trigger).not.toBe(group.lastElementChild);
expect(items(group).at(-1)).toBe(trigger);
- expect(hasRoundedTrailingCorners(trigger, orientation)).toBe(true);
- expect(hasRoundedTrailingCorners(allow, orientation)).toBe(false);
+ expect(carriesTrailingRule(trigger, orientation)).toBe(true);
+ expect(isLastMember(group, trigger)).toBe(true);
+ expect(isLastMember(group, allow)).toBe(false);
},
);
@@ -485,7 +497,8 @@ describe('ButtonGroup', () => {
expect(link).not.toBe(group.lastElementChild);
expect(items(group).at(-1)).toBe(link);
- expect(hasRoundedTrailingCorners(link)).toBe(true);
+ expect(carriesTrailingRule(link)).toBe(true);
+ expect(isLastMember(group, link)).toBe(true);
});
it('rounds only the last member (first/middle/last)', () => {
@@ -511,9 +524,9 @@ describe('ButtonGroup', () => {
// Middle has a tooltip layer after it, but a *marked* sibling follows too,
// so it must NOT take the trailing radius.
- expect(hasRoundedTrailingCorners(first)).toBe(false);
- expect(hasRoundedTrailingCorners(middle)).toBe(false);
- expect(hasRoundedTrailingCorners(last)).toBe(true);
+ expect(isLastMember(group, first)).toBe(false);
+ expect(isLastMember(group, middle)).toBe(false);
+ expect(isLastMember(group, last)).toBe(true);
});
it('rounds both edges of a lone tooltip’d member', () => {
@@ -523,11 +536,13 @@ describe('ButtonGroup', () => {
</ButtonGroup>,
);
+ const group = screen.getByRole('group');
const only = screen.getByRole('button', {name: 'Only'});
// Leading edge is unaffected: a member's button always precedes its layer.
expect(only.matches(':first-child')).toBe(true);
- expect(hasRoundedTrailingCorners(only)).toBe(true);
+ expect(only).not.toBe(group.lastElementChild);
+ expect(isLastMember(group, only)).toBe(true);
});
// -- Members the group does not recognise ---------------------------------
@@ -547,11 +562,12 @@ describe('ButtonGroup', () => {
</ButtonGroup>,
);
+ const group = screen.getByRole('group');
const save = screen.getByRole('button', {name: 'Save'});
// Tooltip wraps element children in a `display: contents` <div>, so the
// inner Button is a DESCENDANT of the wrapper, not a DOM sibling of Save.
- expect(hasRoundedTrailingCorners(save)).toBe(false);
+ expect(isLastMember(group, save)).toBe(false);
});
it('does not round the preceding button when a HoverCard-wrapped member follows', () => {
@@ -564,11 +580,12 @@ describe('ButtonGroup', () => {
</ButtonGroup>,
);
+ const group = screen.getByRole('group');
const save = screen.getByRole('button', {name: 'Save'});
// Button has no `hoverCard` prop, so wrapping is the ONLY way to put a
// HoverCard on a group button — this composition has no alternative.
- expect(hasRoundedTrailingCorners(save)).toBe(false);
+ expect(isLastMember(group, save)).toBe(false);
});
it('does not round the preceding button when a raw <button> follows', () => {
@@ -579,9 +596,10 @@ describe('ButtonGroup', () => {
</ButtonGroup>,
);
+ const group = screen.getByRole('group');
const save = screen.getByRole('button', {name: 'Save'});
- expect(hasRoundedTrailingCorners(save)).toBe(false);
+ expect(isLastMember(group, save)).toBe(false);
});
});
diff --git a/packages/core/src/ButtonGroup/__tests__/ButtonGroupCorners.interaction.browser.spec.ts b/packages/core/src/ButtonGroup/__tests__/ButtonGroupCorners.interaction.browser.spec.ts
new file mode 100644
index 0000000000..3be839c2a2
--- /dev/null
+++ b/packages/core/src/ButtonGroup/__tests__/ButtonGroupCorners.interaction.browser.spec.ts
@@ -0,0 +1,85 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file ButtonGroupCorners.interaction.browser.spec.ts
+ * @input Storybook story a11y-buttongroup-corners--shapes
+ * @output ButtonGroup FR3 in a real browser: in every member shape, only the
+ * group's last member (the last sibling that is not a popover, template or
+ * dialog) rounds its trailing corners, and an InputGroup button addon
+ * rounds its trailing corners only when no member follows it
+ * @position ButtonGroup FR3 and InputGroup end-cap evidence, run in the
+ * Chromium interaction lane; the jsdom tests assert membership only, since
+ * jsdom never receives these rules
+ */
+
+import {expect, test, type Page} from '@playwright/test';
+import {
+ DEFAULT_STORYBOOK_DIR,
+ serveStorybook,
+ type StaticServer,
+} from '@astryxdesign/a11y-spec/storybook';
+
+let storybook: StaticServer;
+
+test.beforeAll(async () => {
+ storybook = await serveStorybook(
+ process.env.ASTRYX_STORYBOOK_DIR ?? DEFAULT_STORYBOOK_DIR,
+ );
+});
+
+test.afterAll(async () => {
+ await storybook?.close();
+});
+
+async function openShapes(page: Page): Promise<void> {
+ await page.goto(
+ `${storybook.origin}/iframe.html?id=a11y-buttongroup-corners--shapes&viewMode=story`,
+ );
+ await page.locator('[data-corner-shape="input-group-addon"]').waitFor();
+}
+
+interface Corner {
+ shape: string;
+ label: string;
+ expected: 'round' | 'square';
+ trailing: string[];
+}
+
+/** Each marked member's two trailing-corner radii, for its orientation. */
+async function trailingCorners(page: Page): Promise<Corner[]> {
+ return page.evaluate(() =>
+ Array.from(
+ document.querySelectorAll<HTMLElement>('[data-corner-case]'),
+ ).map(element => {
+ const style = getComputedStyle(element);
+ const vertical = element.closest('[data-orientation="vertical"]') != null;
+ const trailing = vertical
+ ? [style.borderEndStartRadius, style.borderEndEndRadius]
+ : [style.borderStartEndRadius, style.borderEndEndRadius];
+ return {
+ shape: element.dataset.cornerCase ?? '',
+ label: element.textContent?.trim() ?? '',
+ expected: element.dataset.trailing as 'round' | 'square',
+ trailing,
+ };
+ }),
+ );
+}
+
+test('only the last member rounds its trailing corners, in every shape', async ({
+ page,
+}) => {
+ await openShapes(page);
+ const corners = await trailingCorners(page);
+ expect(corners).toHaveLength(24);
+ for (const {shape, label, expected, trailing} of corners) {
+ const rounded = trailing.map(value => parseFloat(value) > 0);
+ expect(rounded, `${shape} / ${label}`).toEqual(
+ expected === 'round' ? [true, true] : [false, false],
+ );
+ }
+ await page.screenshot({
+ path: `test-results/interaction-evidence/buttongroup-corners/shapes-${test.info().project.name}.png`,
+ fullPage: true,
+ });
+});
diff --git a/packages/core/src/InputGroup/groupStyles.ts b/packages/core/src/InputGroup/groupStyles.ts
index 51e821be29..9b792c16d4 100644
--- a/packages/core/src/InputGroup/groupStyles.ts
+++ b/packages/core/src/InputGroup/groupStyles.ts
@@ -26,8 +26,15 @@ const MEMBER = '[data-astryx-input-group-item]';
// any sibling; only context-layer infrastructure (popovers, inert markers,
// native dialog surfaces) is skipped. Followers are not always members, so
// this rule cannot name them by the marker.
-const FOLLOWED = ':has(~ *:not([popover]):not(template):not(dialog))';
-const IS_LAST_ITEM = `:not(${FOLLOWED})`;
+//
+// `:nth-last-child(1 of S)`, not `:not(:has(~ S))`: the stylesheet is global,
+// and WebKit re-evaluates a sibling-relative `:has()` against every earlier
+// sibling on each insertion, so filling any container became quadratic
+// (ButtonGroup FR3). A member always matches S, so being the last of S is the
+// same as having no following S.
+const IS_LAST_ITEM =
+ ':nth-last-child(1 of :not([popover]):not(template):not(dialog))';
+const FOLLOWED = `:not(${IS_LAST_ITEM})`;
// Button consumes InputGroup context anywhere below the group, including buttons
// inside an input's own implementation. Keep the visual adaptation on direct