Skip to content

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

TypeScript

fix(core): move the popup theme targets onto the surface that paints (#5009)

Cindy Zhang41f53a0c1ec7
diff --git a/.changeset/popup-surface-theme-target.md b/.changeset/popup-surface-theme-target.md
new file mode 100644
index 0000000000..07dc42d03c
--- /dev/null
+++ b/.changeset/popup-surface-theme-target.md
@@ -0,0 +1,11 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] The popup theme targets added in #4991 sat on the wrong element. `astryx-complex-selector-popup` and `astryx-multi-selector-popup` were rendered on each component's own content box — the one with the padding and the scroll — while the element that paints the popup's background, radius and elevation is the surface `usePopover` creates one level above it. A theme reaching for those classes to restyle a popup got a rule that could not paint it. Both now land on the surface, so they do what they were documented to do.
+
+`Selector` gains the matching `astryx-selector-popup`, which its sibling `MultiSelector` had and it did not.
+
+New: every popup surface carries the shared `astryx-popover-surface` class, so a theme can style all of them at once, and `usePopover` accepts a `surfaceTarget` naming the surface for a component that wants its own target there. A component cannot do this for itself — the surface belongs to `usePopover`, so any class it renders itself lands inside.
+
+@cixzhang
diff --git a/packages/cli/clients/cli/commands/build-theme.registry.test.mjs b/packages/cli/clients/cli/commands/build-theme.registry.test.mjs
index a819d06a9b..e46ed8e54a 100644
--- a/packages/cli/clients/cli/commands/build-theme.registry.test.mjs
+++ b/packages/cli/clients/cli/commands/build-theme.registry.test.mjs
@@ -74,6 +74,15 @@ function renderedClassLiterals() {
classes.add(m[1]);
}
}
+ // A component can also name its popup SURFACE — an element usePopover
+ // owns, so the class cannot be rendered from the component itself.
+ // `surfaceTarget: 'x'` puts `astryx-x` on that surface, which makes it
+ // just as rendered as a direct themeProps() call.
+ const surfaceRe = /surfaceTarget:\s*'([^']+)'/g;
+ let sm;
+ while ((sm = surfaceRe.exec(text)) !== null) {
+ classes.add(sm[1]);
+ }
// Renamed targets emit their old name too, via themeProps'
// `legacyNames`. Those classes are just as rendered as the primary
// one, so a doc entry for the old name is still backed by real output.
diff --git a/packages/core/src/ComplexSelector/ComplexSelector.test.tsx b/packages/core/src/ComplexSelector/ComplexSelector.test.tsx
index 3a7afabf71..57bd7e4916 100644
--- a/packages/core/src/ComplexSelector/ComplexSelector.test.tsx
+++ b/packages/core/src/ComplexSelector/ComplexSelector.test.tsx
@@ -177,7 +177,7 @@ describe('ComplexSelector', () => {
});
describe('ComplexSelector popup theme target', () => {
- it('puts astryx-complex-selector-popup on the content box inside the layer', async () => {
+ it('puts astryx-complex-selector-popup on the surface that paints, not the content box', async () => {
const user = userEvent.setup();
render(
<ComplexSelector label="Fruit blend" value="Apple" triggerLabel="Apple">
@@ -186,18 +186,27 @@ describe('ComplexSelector popup theme target', () => {
);
await user.click(screen.getByRole('button', {name: 'Fruit blend'}));
- const layer = document.querySelector('[popover]') as HTMLElement;
const popup = document.querySelector(
'.astryx-complex-selector-popup',
) as HTMLElement;
expect(popup).not.toBeNull();
- // The layer is a bare positioning box; the target must be the content box
- // it contains, which is what actually paints.
- expect(popup).not.toBe(layer);
- expect(layer.contains(popup)).toBe(true);
+
+ // The surface is the element usePopover renders: it carries the dialog
+ // role and the shared surface class, and the component's content box —
+ // the one with the padding and the scroll — sits INSIDE it. A target on
+ // that inner box cannot paint the popup's background or radius, which is
+ // what a theme reaches for this class to do.
+ expect(popup).toHaveAttribute('role', 'dialog');
+ expect(popup).toHaveClass('astryx-popover-surface');
+ expect(popup.querySelector('[id]')).not.toBeNull();
expect(popup).toContainElement(
screen.getByRole('button', {name: 'Done', ...h}),
);
+
+ // And it is not the bare positioning layer either.
+ const layer = document.querySelector('[popover]') as HTMLElement;
+ expect(popup).not.toBe(layer);
+ expect(layer.contains(popup)).toBe(true);
});
it('keeps the target when the consumer also passes contentXstyle', async () => {
diff --git a/packages/core/src/ComplexSelector/ComplexSelector.tsx b/packages/core/src/ComplexSelector/ComplexSelector.tsx
index 6c39a73c57..a1db1da004 100644
--- a/packages/core/src/ComplexSelector/ComplexSelector.tsx
+++ b/packages/core/src/ComplexSelector/ComplexSelector.tsx
@@ -292,6 +292,11 @@ export function ComplexSelector<Value>({
dialogLabel: label,
hasCloseButton: false,
hasAutoFocus: true,
+ // The popup's theme target belongs on the SURFACE — the element painting
+ // background, radius and elevation — which `usePopover` owns. Rendered on
+ // the content box below it, a theme's background or radius rule paints the
+ // wrong box.
+ surfaceTarget: 'complex-selector-popup',
onHide: () => {
document.getElementById(triggerId)?.focus();
},
@@ -313,16 +318,7 @@ export function ComplexSelector<Value>({
const triggerContent = triggerLabel ?? placeholder;
const content = (
- // The theme target sits here, not on the layer: the layer element is a
- // bare positioning box (useLayer zeroes its borders, padding and
- // background), so this content box is the surface a theme has to reach to
- // paint the popup.
- <div
- id={contentId}
- {...mergeProps(
- themeProps('complex-selector-popup'),
- stylex.props(styles.content, contentXstyle),
- )}>
+ <div id={contentId} {...stylex.props(styles.content, contentXstyle)}>
{children(optimisticValue, commitValue, popover.hide, {
isOpen: popover.isOpen,
isBusy,
diff --git a/packages/core/src/MultiSelector/MultiSelector.test.tsx b/packages/core/src/MultiSelector/MultiSelector.test.tsx
index 0fceec724d..d69faad2f5 100644
--- a/packages/core/src/MultiSelector/MultiSelector.test.tsx
+++ b/packages/core/src/MultiSelector/MultiSelector.test.tsx
@@ -2088,7 +2088,7 @@ describe('MultiSelector dropdown option theme target', () => {
});
describe('MultiSelector popup theme target', () => {
- it('puts astryx-multi-selector-popup on the dropdown box inside the layer', async () => {
+ it('puts astryx-multi-selector-popup on the surface that paints, not the list inside it', async () => {
const user = userEvent.setup();
render(
<MultiSelector
@@ -2100,82 +2100,17 @@ describe('MultiSelector popup theme target', () => {
);
await user.click(screen.getByRole('combobox', {name: /Fruit/}));
- const layer = document.querySelector('[popover]') as HTMLElement;
const popup = document.querySelector(
'.astryx-multi-selector-popup',
) as HTMLElement;
expect(popup).not.toBeNull();
- expect(popup).not.toBe(layer);
- expect(layer.contains(popup)).toBe(true);
+ expect(popup).toHaveClass('astryx-popover-surface');
+ // The scrolling list is a descendant, not the target itself.
expect(popup.querySelector('[role="listbox"]')).not.toBeNull();
- });
-});
-
-describe('MultiSelector indicatorPosition', () => {
- const OPTIONS = ['Apple', 'Banana', 'Cherry'];
- const rowFor = (label: string): HTMLElement =>
- screen
- .getAllByRole('option', {hidden: true})
- .find(row => row.textContent?.includes(label))!;
-
- it('draws the checkbox before the label by default', () => {
- render(
- <MultiSelector
- label="Fruit"
- options={OPTIONS}
- value={['Banana']}
- onChange={() => {}}
- isDefaultOpen
- />,
- );
- const row = rowFor('Banana');
- const checkbox = row.querySelector('.astryx-checkbox')!;
- const label = row.lastElementChild!;
- expect(label).toHaveTextContent('Banana');
- expect(
- label.compareDocumentPosition(checkbox) &
- Node.DOCUMENT_POSITION_PRECEDING,
- ).toBeTruthy();
- });
-
- it('draws the checkbox after the label when set to end', () => {
- render(
- <MultiSelector
- label="Fruit"
- options={OPTIONS}
- value={['Banana']}
- onChange={() => {}}
- indicatorPosition="end"
- isDefaultOpen
- />,
- );
- const row = rowFor('Banana');
- const checkbox = row.querySelector('.astryx-checkbox')!;
- const label = row.firstElementChild!;
- expect(label).toHaveTextContent('Banana');
- expect(
- label.compareDocumentPosition(checkbox) &
- Node.DOCUMENT_POSITION_FOLLOWING,
- ).toBeTruthy();
- });
+ expect(popup.getAttribute('role')).toBeNull();
- it('keeps the select-all row on the same edge as the options', () => {
- render(
- <MultiSelector
- label="Fruit"
- options={OPTIONS}
- value={['Banana']}
- onChange={() => {}}
- hasSelectAll
- indicatorPosition="end"
- isDefaultOpen
- />,
- );
- const row = rowFor('Select all');
- expect(
- row.firstElementChild!.compareDocumentPosition(
- row.querySelector('.astryx-checkbox')!,
- ) & Node.DOCUMENT_POSITION_FOLLOWING,
- ).toBeTruthy();
+ const layer = document.querySelector('[popover]') as HTMLElement;
+ expect(popup).not.toBe(layer);
+ expect(layer.contains(popup)).toBe(true);
});
});
diff --git a/packages/core/src/MultiSelector/MultiSelector.tsx b/packages/core/src/MultiSelector/MultiSelector.tsx
index 4c60847176..321110bdc5 100644
--- a/packages/core/src/MultiSelector/MultiSelector.tsx
+++ b/packages/core/src/MultiSelector/MultiSelector.tsx
@@ -874,6 +874,9 @@ export function MultiSelector<T extends MultiSelectorOptionType>({
// The popup's own role="listbox" is the exposed semantics; the trigger
// keeps DOM focus, so wrapping it in a modal dialog would misrepresent it.
role: 'none',
+ // The theme target belongs on the SURFACE that paints the popup, which
+ // `usePopover` owns — not on the scrolling list inside it.
+ surfaceTarget: 'multi-selector-popup',
});
// Open dropdown on mount when isDefaultOpen is true
@@ -1603,11 +1606,7 @@ export function MultiSelector<T extends MultiSelectorOptionType>({
</div>
{popover.render(
- <div
- {...mergeProps(
- themeProps('multi-selector-popup'),
- stylex.props(styles.dropdown),
- )}>
+ <div {...stylex.props(styles.dropdown)}>
{renderSearch()}
<div
id={listboxId}
diff --git a/packages/core/src/Popover/Popover.doc.mjs b/packages/core/src/Popover/Popover.doc.mjs
index 7d1db3e034..6c7d090be6 100644
--- a/packages/core/src/Popover/Popover.doc.mjs
+++ b/packages/core/src/Popover/Popover.doc.mjs
@@ -133,6 +133,7 @@ export const docs = {
theming: {
targets: [
{className: 'astryx-popover'},
+ {className: 'astryx-popover-surface'},
],
vars: [
{name: '--_popover-radius', description: 'Border radius of the popover', default: 'var(--radius-element)', private: true},
@@ -277,6 +278,7 @@ export const docsZh = {
theming: {
targets: [
{className: 'astryx-popover'},
+ {className: 'astryx-popover-surface'},
],
vars: [
{name: '--_popover-radius', description: 'Border radius of the popover', default: 'var(--radius-element)', private: true},
diff --git a/packages/core/src/Popover/usePopover.tsx b/packages/core/src/Popover/usePopover.tsx
index d213475ad2..edfb95e54d 100644
--- a/packages/core/src/Popover/usePopover.tsx
+++ b/packages/core/src/Popover/usePopover.tsx
@@ -30,6 +30,9 @@ import {Button} from '../Button';
import {rtlStyles} from '../utils';
import {useTranslator} from '../i18n';
import {useDevWarning} from '../hooks/useDevWarning';
+import {mergeProps} from '../utils/mergeProps';
+import {themeProps} from '../utils/themeProps';
+import {stableClassName} from '../naming';
const styles = stylex.create({
// Default popover surface — background, radius, shadow.
@@ -187,6 +190,22 @@ export interface UsePopoverOptions {
* @default true
*/
hasSurface?: boolean;
+
+ /**
+ * Theme-target name stamped on the popup SURFACE — the element that paints
+ * the background, radius and elevation — without the `astryx-` prefix
+ * (e.g. `'complex-selector-popup'`).
+ *
+ * The surface is created here, not by the calling component, so a component
+ * that wants its popup themeable cannot reach it on its own: a target it
+ * renders itself lands on its content INSIDE the surface, where a background
+ * or radius rule paints the wrong box. Name the surface through this option
+ * and document the class in the component's `theming.targets`.
+ *
+ * The shared `astryx-popover-surface` class is always present alongside it,
+ * so a theme can style every popup surface at once.
+ */
+ surfaceTarget?: string;
}
/**
@@ -312,6 +331,7 @@ export function usePopover(options: UsePopoverOptions = {}): UsePopoverReturn {
hasEscapeDismiss = true,
hasAutoFocus = true,
hasSurface = true,
+ surfaceTarget,
hasCloseButton = true,
closeButtonLabel: closeButtonLabelFromProps,
dialogLabel,
@@ -406,16 +426,28 @@ export function usePopover(options: UsePopoverOptions = {}): UsePopoverReturn {
// Wrapped render function that includes surface styles and optional hidden close button
const render = useCallback(
(children: ReactNode, props?: ContextRenderProps): ReactNode => {
+ // `mergeProps` is positional — a third OBJECT argument is read as
+ // `style`, not as more props — so the surface's classes are composed
+ // into one props object before merging with the StyleX result.
+ const surfaceProps = themeProps('popover-surface');
+ const surfaceClassName =
+ surfaceTarget != null
+ ? `${surfaceProps.className} ${stableClassName(surfaceTarget)}`
+ : surfaceProps.className;
+
return layer.render(
<div
ref={contentRef}
role={role === 'dialog' ? 'dialog' : undefined}
aria-modal={role === 'dialog' && isModal ? true : undefined}
aria-label={role === 'dialog' ? dialogLabel : undefined}
- {...stylex.props(
- styles.contentWrapper,
- hasSurface && styles.surface,
- xstyle,
+ {...mergeProps(
+ {...surfaceProps, className: surfaceClassName},
+ stylex.props(
+ styles.contentWrapper,
+ hasSurface && styles.surface,
+ xstyle,
+ ),
)}>
{children}
{hasCloseButton && (
@@ -439,6 +471,7 @@ export function usePopover(options: UsePopoverOptions = {}): UsePopoverReturn {
layer,
hasCloseButton,
hasSurface,
+ surfaceTarget,
closeButtonLabel,
contentRef,
dialogLabel,
diff --git a/packages/core/src/Selector/Selector.doc.mjs b/packages/core/src/Selector/Selector.doc.mjs
index 26539b5f9e..a7aefb4bd6 100644
--- a/packages/core/src/Selector/Selector.doc.mjs
+++ b/packages/core/src/Selector/Selector.doc.mjs
@@ -31,6 +31,7 @@ export const docs = {
{className: 'astryx-selector-clear-icon', deprecatedFor: 'input-clear-icon'},
{className: 'astryx-selector-indicator-icon', states: ['state']},
{className: 'astryx-selector-check'},
+ {className: 'astryx-selector-popup'},
],
},
description: 'Dropdown selector for choosing from a list of options.',
diff --git a/packages/core/src/Selector/Selector.test.tsx b/packages/core/src/Selector/Selector.test.tsx
index 7bea620380..225aa2326b 100644
--- a/packages/core/src/Selector/Selector.test.tsx
+++ b/packages/core/src/Selector/Selector.test.tsx
@@ -2739,3 +2739,40 @@ describe('Selector indicatorPosition', () => {
}
});
});
+
+describe('Selector popup theme target', () => {
+ // The surface is the same element whether or not the popup has a search
+ // field — which is the reason the target lives there. Rendered on the
+ // component's own content, it would land on the listbox in one branch and
+ // on a wrapper in the other, so one theme rule would style two different
+ // boxes.
+ it.each([
+ ['without search', false],
+ ['with search', true],
+ ])(
+ 'puts astryx-selector-popup on the painting surface, %s',
+ async (_label, hasSearch) => {
+ const user = userEvent.setup();
+ render(
+ <Selector
+ label="Fruit"
+ options={['Apple', 'Banana']}
+ value="Apple"
+ onChange={() => {}}
+ hasSearch={hasSearch}
+ />,
+ );
+ // The trigger is a combobox in the plain variant and a listbox-popup
+ // button in the search variant; the surface is the same either way.
+ await user.click(
+ screen.queryByRole('combobox') ??
+ screen.getByRole('button', {name: /Fruit/}),
+ );
+
+ const popup = document.querySelector('.astryx-selector-popup');
+ expect(popup).not.toBeNull();
+ expect(popup).toHaveClass('astryx-popover-surface');
+ expect(popup?.querySelector('[role="listbox"]')).not.toBeNull();
+ },
+ );
+});
diff --git a/packages/core/src/Selector/Selector.tsx b/packages/core/src/Selector/Selector.tsx
index 1d50dd1255..3ad734a630 100644
--- a/packages/core/src/Selector/Selector.tsx
+++ b/packages/core/src/Selector/Selector.tsx
@@ -810,6 +810,9 @@ export function Selector<T extends SelectorOptionType>(
// The popup's own role="listbox" is the exposed semantics; the trigger
// keeps DOM focus, so wrapping it in a modal dialog would misrepresent it.
role: 'none',
+ // The theme target belongs on the SURFACE that paints the popup, which
+ // `usePopover` owns — not on the scrolling list inside it.
+ surfaceTarget: 'selector-popup',
});
// Open dropdown on mount when isDefaultOpen is true