An open source design system that's fully customizable and agent ready
TypeScript
fix(Popover): embed surface styles in useXDSPopover hook (#964)
Cindy Zhang5b5438a791b8
diff --git a/packages/core/src/DateInput/XDSDateInput.tsx b/packages/core/src/DateInput/XDSDateInput.tsxindex da4ccc6ef9..d5001edc45 100644--- a/packages/core/src/DateInput/XDSDateInput.tsx+++ b/packages/core/src/DateInput/XDSDateInput.tsx@@ -28,7 +28,6 @@ import {colorVars,sizeVars,radiusVars,- shadowVars,typographyVars,typeScaleVars,borderVars,@@ -97,11 +96,6 @@ const styles = stylex.create({inputInvalid: {color: colorVars['--color-text-secondary'],},- popover: {- backgroundColor: colorVars['--color-background-popover'],- borderRadius: radiusVars['--radius-container'],- boxShadow: shadowVars['--shadow-low'],- },});const sizeStyles = stylex.create({@@ -340,7 +334,6 @@ export function XDSDateInput({: parseDateInput(pendingInput) !== null;const popover = useXDSPopover({- xstyle: styles.popover,dialogLabel: 'Choose date',closeButtonLabel: 'Close calendar',onHide: () => inputRef.current?.focus(),diff --git a/packages/core/src/Popover/XDSPopover.tsx b/packages/core/src/Popover/XDSPopover.tsxindex 5c1cf31e14..a90b92a739 100644--- a/packages/core/src/Popover/XDSPopover.tsx+++ b/packages/core/src/Popover/XDSPopover.tsx@@ -26,14 +26,7 @@ import {xdsClassName, mergeProps} from '../utils';import type {StyleXStyles} from '@stylexjs/stylex';import {useXDSPopover} from './useXDSPopover';import type {LayerAlignment, LayerPlacement} from '../Layer/useXDSLayer';-import {- colorVars,- durationVars,- easeVars,- spacingVars,- radiusVars,- shadowVars,-} from '../theme/tokens.stylex';+import {durationVars, easeVars, spacingVars} from '../theme/tokens.stylex';// =============================================================================// Helpers@@ -191,12 +184,6 @@ const styles = stylex.create({},},// Visual styles for the inner content container- container: {- backgroundColor: colorVars['--color-background-surface'],- color: colorVars['--color-text-primary'],- borderRadius: radiusVars['--radius-element'],- boxShadow: shadowVars['--shadow-low'],- },contentPadding: {paddingBlockStart: spacingVars['--spacing-3'],paddingBlockEnd: spacingVars['--spacing-3'],@@ -437,7 +424,7 @@ export function XDSPopover({data-testid={testId}{...mergeProps(xdsClassName('popover'),- stylex.props(styles.container, styles.contentPadding, xstyle),+ stylex.props(styles.contentPadding, xstyle),className,style,)}>@@ -463,7 +450,7 @@ export function XDSPopover({data-testid={testId}{...mergeProps(xdsClassName('popover'),- stylex.props(styles.container, styles.contentPadding, xstyle),+ stylex.props(styles.contentPadding, xstyle),className,style,)}>diff --git a/packages/core/src/Popover/useXDSPopover.tsx b/packages/core/src/Popover/useXDSPopover.tsxindex f062f9f3af..f0d6a80a08 100644--- a/packages/core/src/Popover/useXDSPopover.tsx+++ b/packages/core/src/Popover/useXDSPopover.tsx@@ -33,10 +33,19 @@ import {colorVars,spacingVars,radiusVars,+ shadowVars,typeScaleVars,} from '../theme/tokens.stylex';const styles = stylex.create({+ // Default popover surface — background, radius, shadow.+ // Applied automatically unless hasSurface is false.+ // Consumers that need a raw positioned layer should use useXDSLayer instead.+ surface: {+ backgroundColor: colorVars['--color-background-popover'],+ borderRadius: radiusVars['--radius-container'],+ boxShadow: shadowVars['--shadow-low'],+ },// Focus trap containercontentWrapper: {position: 'relative',@@ -100,7 +109,12 @@ export interface UseXDSPopoverOptions {onHide?: () => void;/**- * StyleX styles for the popover container+ * StyleX styles applied to the popover's content wrapper.+ * Merges after the surface styles (when hasSurface is true), so these+ * can override background, radius, etc.+ *+ * For styles on the layer's positioned element (e.g., animations using+ * `:popover-open`), pass `xstyle` via the `render()` call's props instead.*/xstyle?: StyleXStyles;@@ -136,6 +150,18 @@ export interface UseXDSPopoverOptions {* Required for screen readers to announce the dialog purpose.*/dialogLabel?: string;++ /**+ * Whether to apply the default popover surface (background, border-radius,+ * box-shadow) to the content wrapper.+ *+ * Set to false when the popover content provides its own surface styling+ * (e.g., mega menus with custom layouts). If you find yourself opting out,+ * consider whether useXDSLayer is a better fit.+ *+ * @default true+ */+ hasSurface?: boolean;}/**@@ -262,6 +288,7 @@ export function useXDSPopover(hasLightDismiss = true,hasAutoFocus = true,hasCloseButton = true,+ hasSurface = true,closeButtonLabel = 'Close popover',dialogLabel,} = options;@@ -344,7 +371,7 @@ export function useXDSPopover('aria-controls': layer.id,};- // Wrapped render function that includes optional hidden close button+ // Wrapped render function that includes surface styles and optional hidden close buttonconst render = useCallback((children: ReactNode, props?: ContextRenderProps) => {return layer.render(@@ -353,7 +380,11 @@ export function useXDSPopover(role="dialog"aria-modal="true"aria-label={dialogLabel}- {...stylex.props(styles.contentWrapper)}>+ {...stylex.props(+ styles.contentWrapper,+ hasSurface && styles.surface,+ xstyle,+ )}>{children}{hasCloseButton && (<button@@ -370,12 +401,13 @@ export function useXDSPopover(</button>)}</div>,- {...props, xstyle: xstyle ?? props?.xstyle},+ {...props, xstyle: props?.xstyle},);},[layer,hasCloseButton,+ hasSurface,closeButtonLabel,isCloseButtonFocused,contentRef,diff --git a/packages/core/src/SideNav/XDSSideNavHeading.tsx b/packages/core/src/SideNav/XDSSideNavHeading.tsxindex 2ca31a3bda..2dbe98f93e 100644--- a/packages/core/src/SideNav/XDSSideNavHeading.tsx+++ b/packages/core/src/SideNav/XDSSideNavHeading.tsx@@ -24,7 +24,6 @@ import {spacingVars,fontWeightVars,radiusVars,- shadowVars,typeScaleVars,} from '../theme/tokens.stylex';// TODO(#264): Lazy-load useXDSPopover so the popover/layer bundle is not@@ -128,9 +127,6 @@ const styles = stylex.create({},popoverContent: {padding: spacingVars['--spacing-1'],- borderRadius: radiusVars['--radius-element'],- backgroundColor: colorVars['--color-background-surface'],- boxShadow: shadowVars['--shadow-low'],overflow: 'hidden',},popover: {diff --git a/packages/core/src/SideNav/XDSSideNavItem.tsx b/packages/core/src/SideNav/XDSSideNavItem.tsxindex 4e8f597e92..af8c56995f 100644--- a/packages/core/src/SideNav/XDSSideNavItem.tsx+++ b/packages/core/src/SideNav/XDSSideNavItem.tsx@@ -20,12 +20,10 @@ import * as stylex from '@stylexjs/stylex';import {colorVars,spacingVars,- radiusVars,fontWeightVars,typeScaleVars,durationVars,easeVars,- shadowVars,borderVars,} from '../theme/tokens.stylex';import {XDSIcon} from '../Icon';@@ -99,12 +97,9 @@ const styles = stylex.create({},// Popover surface for collapsed items with childrenpopoverSurface: {- backgroundColor: colorVars['--color-background-popover'],borderWidth: borderVars['--border-width'],borderStyle: 'solid',borderColor: colorVars['--color-border'],- borderRadius: radiusVars['--radius-element'],- boxShadow: shadowVars['--shadow-low'],paddingBlock: spacingVars['--spacing-1'],paddingInline: spacingVars['--spacing-1'],marginInlineStart: spacingVars['--spacing-1'],diff --git a/packages/core/src/TopNav/XDSTopNavMegaMenu.tsx b/packages/core/src/TopNav/XDSTopNavMegaMenu.tsxindex b9649d4f56..cb72f08cb1 100644--- a/packages/core/src/TopNav/XDSTopNavMegaMenu.tsx+++ b/packages/core/src/TopNav/XDSTopNavMegaMenu.tsx@@ -338,7 +338,10 @@ function DefaultMegaMenu({const popover = useXDSPopover({dialogLabel: label,- xstyle: styles.panelAnimation,+ // hasSurface: false — mega menu provides its own surface (panelContainer)+ // with border-top and custom overflow. Animation is applied via the+ // render() call's xstyle prop (panelAnimation), not the hook options.+ hasSurface: false,onShow: handlePopoverShow,onHide: handlePopoverHide,});diff --git a/packages/core/src/TopNav/XDSTopNavMenu.tsx b/packages/core/src/TopNav/XDSTopNavMenu.tsxindex 910a141631..fa5a1dfc6b 100644--- a/packages/core/src/TopNav/XDSTopNavMenu.tsx+++ b/packages/core/src/TopNav/XDSTopNavMenu.tsx@@ -35,7 +35,6 @@ import {durationVars,easeVars,fontWeightVars,- shadowVars,typeScaleVars,} from '../theme/tokens.stylex';@@ -97,9 +96,6 @@ const styles = stylex.create({gap: spacingVars['--spacing-1'],minWidth: 280,padding: spacingVars['--spacing-1'],- backgroundColor: colorVars['--color-background-popover'],- borderRadius: radiusVars['--radius-container'],- boxShadow: shadowVars['--shadow-low'],},menuOffset: {marginBlockStart: spacingVars['--spacing-1'],