From 9ed2fb4bf95674fe7eef279e63a415fc1a06efd7 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Wed, 11 Mar 2026 16:13:12 +0100 Subject: [PATCH 01/71] Add Popover component to @wordpress/ui Introduce a new Popover primitive wrapping Base UI's Popover. Includes Root, Trigger, Popup, Arrow, Title, Description, and Close subcomponents following existing package conventions. Made-with: Cursor --- packages/ui/CHANGELOG.md | 1 + packages/ui/src/index.ts | 1 + packages/ui/src/popover/arrow.tsx | 23 +++++++++ packages/ui/src/popover/close.tsx | 14 ++++++ packages/ui/src/popover/description.tsx | 22 +++++++++ packages/ui/src/popover/index.ts | 9 ++++ packages/ui/src/popover/popup.tsx | 63 ++++++++++++++++++++++++ packages/ui/src/popover/root.tsx | 14 ++++++ packages/ui/src/popover/style.module.css | 38 ++++++++++++++ packages/ui/src/popover/title.tsx | 22 +++++++++ packages/ui/src/popover/trigger.tsx | 14 ++++++ packages/ui/src/popover/types.ts | 56 +++++++++++++++++++++ 12 files changed, 277 insertions(+) create mode 100644 packages/ui/src/popover/arrow.tsx create mode 100644 packages/ui/src/popover/close.tsx create mode 100644 packages/ui/src/popover/description.tsx create mode 100644 packages/ui/src/popover/index.ts create mode 100644 packages/ui/src/popover/popup.tsx create mode 100644 packages/ui/src/popover/root.tsx create mode 100644 packages/ui/src/popover/style.module.css create mode 100644 packages/ui/src/popover/title.tsx create mode 100644 packages/ui/src/popover/trigger.tsx create mode 100644 packages/ui/src/popover/types.ts diff --git a/packages/ui/CHANGELOG.md b/packages/ui/CHANGELOG.md index f7ce095edeaaf3..d32d102cdd391b 100644 --- a/packages/ui/CHANGELOG.md +++ b/packages/ui/CHANGELOG.md @@ -55,6 +55,7 @@ ### New Features +- Add `Popover` primitive ([#XXXXX](https://github.com/WordPress/gutenberg/pull/XXXXX)). - Add `Text` primitive with predefined typographic variants (`heading-2xl` through `heading-sm`, `body-xl` through `body-sm`) built on design tokens ([#75870](https://github.com/WordPress/gutenberg/pull/75870)). - Add `Card` and `CollapsibleCard` primitives ([#76252](https://github.com/WordPress/gutenberg/pull/76252)). - Add `Link` primitive ([#76013](https://github.com/WordPress/gutenberg/pull/76013)). diff --git a/packages/ui/src/index.ts b/packages/ui/src/index.ts index b3cbb03c72067f..800fb880628e5e 100644 --- a/packages/ui/src/index.ts +++ b/packages/ui/src/index.ts @@ -11,6 +11,7 @@ export * from './icon'; export * from './icon-button'; export * from './link'; export * as Notice from './notice'; +export * as Popover from './popover'; export * from './stack'; export * as Tabs from './tabs'; export * from './text'; diff --git a/packages/ui/src/popover/arrow.tsx b/packages/ui/src/popover/arrow.tsx new file mode 100644 index 00000000000000..6d0640dfeee47c --- /dev/null +++ b/packages/ui/src/popover/arrow.tsx @@ -0,0 +1,23 @@ +import { Popover as _Popover } from '@base-ui/react/popover'; +import clsx from 'clsx'; +import { forwardRef } from '@wordpress/element'; +import styles from './style.module.css'; +import type { ArrowProps } from './types'; + +/** + * Renders an arrow element that points toward the popover anchor. + */ +const Arrow = forwardRef< HTMLDivElement, ArrowProps >( function PopoverArrow( + { className, ...props }, + ref +) { + return ( + <_Popover.Arrow + ref={ ref } + className={ clsx( styles.arrow, className ) } + { ...props } + /> + ); +} ); + +export { Arrow }; diff --git a/packages/ui/src/popover/close.tsx b/packages/ui/src/popover/close.tsx new file mode 100644 index 00000000000000..39cafa1870ccf8 --- /dev/null +++ b/packages/ui/src/popover/close.tsx @@ -0,0 +1,14 @@ +import { Popover as _Popover } from '@base-ui/react/popover'; +import { forwardRef } from '@wordpress/element'; +import type { CloseProps } from './types'; + +/** + * Renders a button that closes the popover when clicked. + */ +const Close = forwardRef< HTMLButtonElement, CloseProps >( + function PopoverClose( props, ref ) { + return <_Popover.Close ref={ ref } { ...props } />; + } +); + +export { Close }; diff --git a/packages/ui/src/popover/description.tsx b/packages/ui/src/popover/description.tsx new file mode 100644 index 00000000000000..bcd0c1d23c8f5a --- /dev/null +++ b/packages/ui/src/popover/description.tsx @@ -0,0 +1,22 @@ +import { Popover as _Popover } from '@base-ui/react/popover'; +import clsx from 'clsx'; +import { forwardRef } from '@wordpress/element'; +import styles from './style.module.css'; +import type { DescriptionProps } from './types'; + +/** + * Renders a paragraph that describes the popover content for accessibility. + */ +const Description = forwardRef< HTMLParagraphElement, DescriptionProps >( + function PopoverDescription( { className, ...props }, ref ) { + return ( + <_Popover.Description + ref={ ref } + className={ clsx( styles.description, className ) } + { ...props } + /> + ); + } +); + +export { Description }; diff --git a/packages/ui/src/popover/index.ts b/packages/ui/src/popover/index.ts new file mode 100644 index 00000000000000..472a9f1a681240 --- /dev/null +++ b/packages/ui/src/popover/index.ts @@ -0,0 +1,9 @@ +import { Arrow } from './arrow'; +import { Close } from './close'; +import { Description } from './description'; +import { Popup } from './popup'; +import { Root } from './root'; +import { Title } from './title'; +import { Trigger } from './trigger'; + +export { Arrow, Close, Description, Popup, Root, Title, Trigger }; diff --git a/packages/ui/src/popover/popup.tsx b/packages/ui/src/popover/popup.tsx new file mode 100644 index 00000000000000..09472d39cecdb5 --- /dev/null +++ b/packages/ui/src/popover/popup.tsx @@ -0,0 +1,63 @@ +import { Popover as _Popover } from '@base-ui/react/popover'; +import clsx from 'clsx'; +import { forwardRef } from '@wordpress/element'; +import { + type ThemeProvider as ThemeProviderType, + privateApis as themePrivateApis, +} from '@wordpress/theme'; +import { unlock } from '../lock-unlock'; +import resetStyles from '../utils/css/resets.module.css'; +import dropdownMotionStyles from '../utils/css/dropdown-motion.module.css'; +import styles from './style.module.css'; +import type { PopupProps } from './types'; + +const ThemeProvider: typeof ThemeProviderType = + unlock( themePrivateApis ).ThemeProvider; + +/** + * Renders the popover popup element that contains the popover content. + * Uses a portal to render outside the DOM hierarchy. + */ +const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( + { + align = 'center', + side = 'bottom', + sideOffset = 4, + alignOffset, + children, + className, + style, + ...props + }, + ref +) { + return ( + <_Popover.Portal> + <_Popover.Positioner + align={ align } + side={ side } + sideOffset={ sideOffset } + alignOffset={ alignOffset } + style={ style } + className={ clsx( + resetStyles[ 'box-sizing' ], + dropdownMotionStyles[ 'dropdown-motion' ], + styles.positioner, + className + ) } + > + + <_Popover.Popup + ref={ ref } + className={ styles.popup } + { ...props } + > + { children } + + + + + ); +} ); + +export { Popup }; diff --git a/packages/ui/src/popover/root.tsx b/packages/ui/src/popover/root.tsx new file mode 100644 index 00000000000000..cd63ff8b7eaa9d --- /dev/null +++ b/packages/ui/src/popover/root.tsx @@ -0,0 +1,14 @@ +import { Popover as _Popover } from '@base-ui/react/popover'; +import type { RootProps } from './types'; + +/** + * Groups the popover trigger and popup. + * + * `Popover` is a collection of React components that combine to render + * an ARIA-compliant popover anchored to a trigger button. + */ +function Root( props: RootProps ) { + return <_Popover.Root { ...props } />; +} + +export { Root }; diff --git a/packages/ui/src/popover/style.module.css b/packages/ui/src/popover/style.module.css new file mode 100644 index 00000000000000..2b19752a75e261 --- /dev/null +++ b/packages/ui/src/popover/style.module.css @@ -0,0 +1,38 @@ +@layer wp-ui-utilities, wp-ui-components, wp-ui-compositions, wp-ui-overrides; + +@layer wp-ui-components { + .positioner { + z-index: var(--wp-ui-popover-z-index, initial); + } + + .popup { + background-color: var(--wpds-color-bg-surface-neutral-strong); + padding: var(--wpds-dimension-padding-md); + border-radius: var(--wpds-border-radius-md); + box-shadow: var(--wpds-elevation-md); + font-family: var(--wpds-font-family-body); + font-size: var(--wpds-font-size-md); + line-height: var(--wpds-font-line-height-md); + color: var(--wpds-color-fg-content-neutral); + outline: 0; + } + + .arrow { + color: var(--wpds-color-bg-surface-neutral-strong); + } + + .title { + margin: 0 0 var(--wpds-dimension-gap-xs); + font-size: var(--wpds-font-size-md); + font-weight: var(--wpds-font-weight-medium); + line-height: var(--wpds-font-line-height-md); + color: var(--wpds-color-fg-content-neutral); + } + + .description { + margin: 0; + font-size: var(--wpds-font-size-sm); + line-height: var(--wpds-font-line-height-sm); + color: var(--wpds-color-fg-content-neutral-secondary); + } +} diff --git a/packages/ui/src/popover/title.tsx b/packages/ui/src/popover/title.tsx new file mode 100644 index 00000000000000..cba3f96b97804b --- /dev/null +++ b/packages/ui/src/popover/title.tsx @@ -0,0 +1,22 @@ +import { Popover as _Popover } from '@base-ui/react/popover'; +import clsx from 'clsx'; +import { forwardRef } from '@wordpress/element'; +import styles from './style.module.css'; +import type { TitleProps } from './types'; + +/** + * Renders a heading that labels the popover for accessibility. + */ +const Title = forwardRef< HTMLHeadingElement, TitleProps >( + function PopoverTitle( { className, ...props }, ref ) { + return ( + <_Popover.Title + ref={ ref } + className={ clsx( styles.title, className ) } + { ...props } + /> + ); + } +); + +export { Title }; diff --git a/packages/ui/src/popover/trigger.tsx b/packages/ui/src/popover/trigger.tsx new file mode 100644 index 00000000000000..a3b7eb36255578 --- /dev/null +++ b/packages/ui/src/popover/trigger.tsx @@ -0,0 +1,14 @@ +import { Popover as _Popover } from '@base-ui/react/popover'; +import { forwardRef } from '@wordpress/element'; +import type { TriggerProps } from './types'; + +/** + * Renders a button that toggles the popover popup when clicked. + */ +const Trigger = forwardRef< HTMLButtonElement, TriggerProps >( + function PopoverTrigger( props, ref ) { + return <_Popover.Trigger ref={ ref } { ...props } />; + } +); + +export { Trigger }; diff --git a/packages/ui/src/popover/types.ts b/packages/ui/src/popover/types.ts new file mode 100644 index 00000000000000..13ea7338e4e1bb --- /dev/null +++ b/packages/ui/src/popover/types.ts @@ -0,0 +1,56 @@ +import type { ReactNode } from 'react'; +import type { Popover as _Popover } from '@base-ui/react/popover'; +import type { ComponentProps } from '../utils/types'; + +export interface RootProps + extends Pick< + _Popover.Root.Props, + 'open' | 'onOpenChange' | 'defaultOpen' | 'modal' + > { + /** + * The content to be rendered inside the component. + */ + children?: ReactNode; +} + +export interface TriggerProps extends ComponentProps< 'button' > { + /** + * The content to be rendered inside the component. + */ + children?: ReactNode; +} + +export interface PopupProps + extends ComponentProps< 'div' >, + Pick< + _Popover.Positioner.Props, + 'align' | 'side' | 'sideOffset' | 'alignOffset' + > { + /** + * The content to be rendered inside the component. + */ + children?: ReactNode; +} + +export interface ArrowProps extends ComponentProps< 'div' > {} + +export interface TitleProps extends ComponentProps< 'h2' > { + /** + * The title content to be rendered. + */ + children?: ReactNode; +} + +export interface DescriptionProps extends ComponentProps< 'p' > { + /** + * The description content to be rendered. + */ + children?: ReactNode; +} + +export interface CloseProps extends ComponentProps< 'button' > { + /** + * The content to be rendered inside the component. + */ + children?: ReactNode; +} From 7f6f110e5b31424e790edb860b4f59dfa717deac Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Wed, 11 Mar 2026 16:13:52 +0100 Subject: [PATCH 02/71] Add Storybook stories for Popover component Include stories for default usage, positioning, close button, controlled mode, modal behavior, and custom z-index. Made-with: Cursor --- .../ui/src/popover/stories/index.story.tsx | 207 ++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 packages/ui/src/popover/stories/index.story.tsx diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx new file mode 100644 index 00000000000000..7b2315b266ae37 --- /dev/null +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -0,0 +1,207 @@ +import type { Meta, StoryObj } from '@storybook/react-vite'; +import { useState } from '@wordpress/element'; +import { Popover } from '../..'; + +const meta: Meta< typeof Popover.Root > = { + title: 'Design System/Components/Popover', + component: Popover.Root, + subcomponents: { + 'Popover.Trigger': Popover.Trigger, + 'Popover.Popup': Popover.Popup, + 'Popover.Arrow': Popover.Arrow, + 'Popover.Title': Popover.Title, + 'Popover.Description': Popover.Description, + 'Popover.Close': Popover.Close, + }, + parameters: { + docs: { + description: { + component: + 'Popover is an accessible popup anchored to a trigger button. ' + + 'It can contain interactive content and form controls.', + }, + }, + }, +}; +export default meta; + +type Story = StoryObj< typeof Popover.Root >; + +export const Default: Story = { + args: { + children: ( + <> + Open Popover + + + Notifications + + You are all caught up. Good job! + + + + ), + }, +}; + +/** + * Use the `side` and `align` props on `Popover.Popup` to control where the + * popover appears relative to the trigger element. + */ +export const Positioning: Story = { + render: () => ( +
+ + Top + + + Popover on top + + + + + Right + + + Popover on right + + + + + Bottom + + + Popover on bottom + + + + + Left + + + Popover on left + + +
+ ), +}; + +/** + * A popover with a close button, title, and description. The `Popover.Close` + * component renders a button that closes the popover when clicked. + */ +export const WithCloseButton: Story = { + args: { + children: ( + <> + Settings + + +
+ + Settings + + + ✕ + +
+ + Configure your notification preferences and display + settings. + +
+ + ), + }, +}; + +/** + * Use the `open` and `onOpenChange` props on `Popover.Root` to control the + * popover's visibility programmatically. + */ +export const Controlled: Story = { + render: function Render() { + const [ isOpen, setIsOpen ] = useState( false ); + + return ( +
+ + Toggle Popover + + + Controlled Popover + + This popover is controlled by external state. + + + + +
+ ); + }, +}; + +/** + * Set `modal` to `true` to trap focus inside the popover when it is open. + * This is useful for complex popover content that requires user interaction. + */ +export const Modal: Story = { + args: { + modal: true, + children: ( + <> + Open Modal Popover + + + Modal Popover + + Focus is trapped inside this popover. Press Escape to + close. + + + Close + + + + ), + }, +}; + +/** + * The `--wp-ui-popover-z-index` CSS variable controls the z-index of the + * popover positioner. It can be overridden globally or scoped to a + * specific container. + */ +export const WithCustomZIndex: Story = { + ...Default, + name: 'With Custom z-index', +}; From 4ff1cf1a219cc957ba96b4b85bfb7773b8d1c5bf Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Wed, 11 Mar 2026 16:18:25 +0100 Subject: [PATCH 03/71] Add unit tests for Popover component Cover ref forwarding for all subcomponents, open/close behavior, controlled mode, defaultOpen, onOpenChange callback, and accessibility attributes. Made-with: Cursor --- packages/ui/src/popover/test/index.test.tsx | 365 ++++++++++++++++++++ 1 file changed, 365 insertions(+) create mode 100644 packages/ui/src/popover/test/index.test.tsx diff --git a/packages/ui/src/popover/test/index.test.tsx b/packages/ui/src/popover/test/index.test.tsx new file mode 100644 index 00000000000000..d10ee58746288d --- /dev/null +++ b/packages/ui/src/popover/test/index.test.tsx @@ -0,0 +1,365 @@ +import { render, screen, waitFor } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import { createRef, useState } from '@wordpress/element'; +import * as Popover from '../index'; + +describe( 'Popover', () => { + describe( 'forwards ref', () => { + it( 'should forward ref on Trigger', () => { + const ref = createRef< HTMLButtonElement >(); + render( + + Open + Content + + ); + expect( ref.current ).toBeInstanceOf( HTMLButtonElement ); + } ); + + it( 'should forward ref on Popup', async () => { + const user = userEvent.setup(); + const ref = createRef< HTMLDivElement >(); + + render( + + Open + Content + + ); + + await user.click( screen.getByRole( 'button', { name: 'Open' } ) ); + + await waitFor( () => { + expect( ref.current ).toBeInstanceOf( HTMLDivElement ); + } ); + } ); + + it( 'should forward ref on Arrow', async () => { + const user = userEvent.setup(); + const ref = createRef< HTMLDivElement >(); + + render( + + Open + + + Content + + + ); + + await user.click( screen.getByRole( 'button', { name: 'Open' } ) ); + + await waitFor( () => { + expect( ref.current ).toBeInstanceOf( HTMLDivElement ); + } ); + } ); + + it( 'should forward ref on Title', async () => { + const user = userEvent.setup(); + const ref = createRef< HTMLHeadingElement >(); + + render( + + Open + + Title + + + ); + + await user.click( screen.getByRole( 'button', { name: 'Open' } ) ); + + await waitFor( () => { + expect( ref.current ).toBeInstanceOf( HTMLHeadingElement ); + } ); + } ); + + it( 'should forward ref on Description', async () => { + const user = userEvent.setup(); + const ref = createRef< HTMLParagraphElement >(); + + render( + + Open + + + Description + + + + ); + + await user.click( screen.getByRole( 'button', { name: 'Open' } ) ); + + await waitFor( () => { + expect( ref.current ).toBeInstanceOf( HTMLParagraphElement ); + } ); + } ); + + it( 'should forward ref on Close', async () => { + const user = userEvent.setup(); + const ref = createRef< HTMLButtonElement >(); + + render( + + Open + + Close + + + ); + + await user.click( screen.getByRole( 'button', { name: 'Open' } ) ); + + await waitFor( () => { + expect( ref.current ).toBeInstanceOf( HTMLButtonElement ); + } ); + } ); + } ); + + describe( 'open and close behavior', () => { + it( 'should open the popover when the trigger is clicked', async () => { + const user = userEvent.setup(); + + render( + + Open + Popover content + + ); + + expect( + screen.queryByText( 'Popover content' ) + ).not.toBeInTheDocument(); + + await user.click( screen.getByRole( 'button', { name: 'Open' } ) ); + + await waitFor( () => { + expect( + screen.getByText( 'Popover content' ) + ).toBeInTheDocument(); + } ); + } ); + + it( 'should close the popover when clicking the trigger again', async () => { + const user = userEvent.setup(); + + render( + + Toggle + Popover content + + ); + + const trigger = screen.getByRole( 'button', { + name: 'Toggle', + } ); + + await user.click( trigger ); + await waitFor( () => { + expect( + screen.getByText( 'Popover content' ) + ).toBeInTheDocument(); + } ); + + await user.click( trigger ); + await waitFor( () => { + expect( + screen.queryByText( 'Popover content' ) + ).not.toBeInTheDocument(); + } ); + } ); + + it( 'should close the popover when Escape is pressed', async () => { + const user = userEvent.setup(); + + render( + + Open + Popover content + + ); + + await user.click( screen.getByRole( 'button', { name: 'Open' } ) ); + + await waitFor( () => { + expect( + screen.getByText( 'Popover content' ) + ).toBeInTheDocument(); + } ); + + await user.keyboard( '{Escape}' ); + + await waitFor( () => { + expect( + screen.queryByText( 'Popover content' ) + ).not.toBeInTheDocument(); + } ); + } ); + + it( 'should close the popover when the Close button is clicked', async () => { + const user = userEvent.setup(); + + render( + + Open + + Popover content + Close + + + ); + + await user.click( screen.getByRole( 'button', { name: 'Open' } ) ); + + await waitFor( () => { + expect( + screen.getByText( 'Popover content' ) + ).toBeInTheDocument(); + } ); + + await user.click( screen.getByRole( 'button', { name: 'Close' } ) ); + + await waitFor( () => { + expect( + screen.queryByText( 'Popover content' ) + ).not.toBeInTheDocument(); + } ); + } ); + } ); + + describe( 'controlled mode', () => { + function ControlledPopover() { + const [ open, setOpen ] = useState( false ); + return ( + <> + + + Trigger + Controlled content + + + ); + } + + it( 'should open via external state', async () => { + const user = userEvent.setup(); + + render( ); + + expect( + screen.queryByText( 'Controlled content' ) + ).not.toBeInTheDocument(); + + await user.click( + screen.getByRole( 'button', { name: 'External open' } ) + ); + + await waitFor( () => { + expect( + screen.getByText( 'Controlled content' ) + ).toBeInTheDocument(); + } ); + } ); + } ); + + describe( 'defaultOpen', () => { + it( 'should render open initially when defaultOpen is true', async () => { + render( + + Open + Default open content + + ); + + await waitFor( () => { + expect( + screen.getByText( 'Default open content' ) + ).toBeInTheDocument(); + } ); + } ); + } ); + + describe( 'onOpenChange callback', () => { + it( 'should call onOpenChange when the popover opens and closes', async () => { + const user = userEvent.setup(); + const onOpenChange = jest.fn(); + + render( + + Toggle + Content + + ); + + await user.click( + screen.getByRole( 'button', { name: 'Toggle' } ) + ); + + await waitFor( () => { + expect( onOpenChange ).toHaveBeenCalledWith( + true, + expect.anything() + ); + } ); + + await user.click( + screen.getByRole( 'button', { name: 'Toggle' } ) + ); + + await waitFor( () => { + expect( onOpenChange ).toHaveBeenCalledWith( + false, + expect.anything() + ); + } ); + } ); + } ); + + describe( 'accessibility', () => { + it( 'should associate title with the popup via aria-labelledby', async () => { + const user = userEvent.setup(); + + render( + + Open + + My Title + + + ); + + await user.click( screen.getByRole( 'button', { name: 'Open' } ) ); + + await waitFor( () => { + const title = screen.getByText( 'My Title' ); + expect( title.id ).toBeTruthy(); + } ); + } ); + + it( 'should associate description with the popup via aria-describedby', async () => { + const user = userEvent.setup(); + + render( + + Open + + + My Description + + + + ); + + await user.click( screen.getByRole( 'button', { name: 'Open' } ) ); + + await waitFor( () => { + const description = screen.getByText( 'My Description' ); + expect( description.id ).toBeTruthy(); + } ); + } ); + } ); +} ); From 43b29558878860568b5be38d8fe9804c951052f8 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Wed, 11 Mar 2026 16:20:44 +0100 Subject: [PATCH 04/71] Fix review issues in Popover component - Add children prop to ArrowProps for custom arrow content - Fix invalid CSS token: use --wpds-color-fg-content-neutral-weak instead of non-existent --wpds-color-fg-content-neutral-secondary Made-with: Cursor --- packages/ui/src/popover/style.module.css | 2 +- packages/ui/src/popover/types.ts | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/ui/src/popover/style.module.css b/packages/ui/src/popover/style.module.css index 2b19752a75e261..6aeb039ad0118f 100644 --- a/packages/ui/src/popover/style.module.css +++ b/packages/ui/src/popover/style.module.css @@ -33,6 +33,6 @@ margin: 0; font-size: var(--wpds-font-size-sm); line-height: var(--wpds-font-line-height-sm); - color: var(--wpds-color-fg-content-neutral-secondary); + color: var(--wpds-color-fg-content-neutral-weak); } } diff --git a/packages/ui/src/popover/types.ts b/packages/ui/src/popover/types.ts index 13ea7338e4e1bb..befd8f3cff695f 100644 --- a/packages/ui/src/popover/types.ts +++ b/packages/ui/src/popover/types.ts @@ -32,7 +32,12 @@ export interface PopupProps children?: ReactNode; } -export interface ArrowProps extends ComponentProps< 'div' > {} +export interface ArrowProps extends ComponentProps< 'div' > { + /** + * Custom arrow visuals to render inside the positioned container. + */ + children?: ReactNode; +} export interface TitleProps extends ComponentProps< 'h2' > { /** From d53289686cf7c6ffa043665627f3789dca026054 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Wed, 11 Mar 2026 16:55:12 +0100 Subject: [PATCH 05/71] Extend Popover Popup with additional props - initialFocus / finalFocus: custom focus behavior on open/close - variant: 'default' | 'unstyled' for visual style control - inline: render without portal - collisionAvoidance / collisionBoundary / collisionPadding / sticky: fine-grained collision handling (replaces legacy flip/resize/shift) - container: portal target for cross-document rendering Made-with: Cursor --- packages/ui/src/popover/popup.tsx | 78 ++++++++++++++++++++----------- packages/ui/src/popover/types.ts | 42 ++++++++++++++++- 2 files changed, 91 insertions(+), 29 deletions(-) diff --git a/packages/ui/src/popover/popup.tsx b/packages/ui/src/popover/popup.tsx index 09472d39cecdb5..5c6a3a7a388c9d 100644 --- a/packages/ui/src/popover/popup.tsx +++ b/packages/ui/src/popover/popup.tsx @@ -16,46 +16,70 @@ const ThemeProvider: typeof ThemeProviderType = /** * Renders the popover popup element that contains the popover content. - * Uses a portal to render outside the DOM hierarchy. + * By default, uses a portal to render outside the DOM hierarchy. */ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( { align = 'center', - side = 'bottom', - sideOffset = 4, alignOffset, children, className, + collisionAvoidance, + collisionBoundary, + collisionPadding, + container, + finalFocus, + initialFocus, + inline: inlineProp = false, + side = 'bottom', + sideOffset = 4, + sticky, style, + variant = 'default', ...props }, ref ) { - return ( - <_Popover.Portal> - <_Popover.Positioner - align={ align } - side={ side } - sideOffset={ sideOffset } - alignOffset={ alignOffset } - style={ style } - className={ clsx( - resetStyles[ 'box-sizing' ], + const positioner = ( + <_Popover.Positioner + align={ align } + alignOffset={ alignOffset } + collisionAvoidance={ collisionAvoidance } + collisionBoundary={ collisionBoundary } + collisionPadding={ collisionPadding } + side={ side } + sideOffset={ sideOffset } + sticky={ sticky } + style={ style } + className={ clsx( + resetStyles[ 'box-sizing' ], + variant !== 'unstyled' && dropdownMotionStyles[ 'dropdown-motion' ], - styles.positioner, - className - ) } - > - - <_Popover.Popup - ref={ ref } - className={ styles.popup } - { ...props } - > - { children } - - - + styles.positioner, + className + ) } + > + + <_Popover.Popup + ref={ ref } + initialFocus={ initialFocus } + finalFocus={ finalFocus } + className={ clsx( variant !== 'unstyled' && styles.popup ) } + { ...props } + > + { children } + + + + ); + + if ( inlineProp ) { + return positioner; + } + + return ( + <_Popover.Portal container={ container }> + { positioner } ); } ); diff --git a/packages/ui/src/popover/types.ts b/packages/ui/src/popover/types.ts index befd8f3cff695f..3e9eaf295b84d3 100644 --- a/packages/ui/src/popover/types.ts +++ b/packages/ui/src/popover/types.ts @@ -24,12 +24,50 @@ export interface PopupProps extends ComponentProps< 'div' >, Pick< _Popover.Positioner.Props, - 'align' | 'side' | 'sideOffset' | 'alignOffset' - > { + | 'align' + | 'alignOffset' + | 'collisionAvoidance' + | 'collisionBoundary' + | 'collisionPadding' + | 'side' + | 'sideOffset' + | 'sticky' + >, + Pick< _Popover.Popup.Props, 'initialFocus' | 'finalFocus' > { /** * The content to be rendered inside the component. */ children?: ReactNode; + + /** + * A parent element to render the portal into. + * + * Useful for cross-document rendering, such as rendering a popover + * in a parent document when the trigger is inside an iframe. + */ + container?: _Popover.Portal.Props[ 'container' ]; + + /** + * Whether to render the popup inline without a portal. + * + * When `true`, the popup is rendered in place within the DOM hierarchy + * instead of being portaled to `document.body`. + * + * @default false + */ + inline?: boolean; + + /** + * The visual style variant of the popup. + * + * - `'default'` — standard surface styling with background, padding, + * border radius, and shadow. + * - `'unstyled'` — no visual treatment; useful as a blank positioning + * container for fully custom content. + * + * @default 'default' + */ + variant?: 'default' | 'unstyled'; } export interface ArrowProps extends ComponentProps< 'div' > { From 7bfba39e0bf59c7d078ba51aaf5e3ecef839266a Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Wed, 11 Mar 2026 16:59:04 +0100 Subject: [PATCH 06/71] Add stories and tests for new Popover props Stories: - Unstyled variant - Overlay placement (useMeasure technique with negative sideOffset) - Disabled animations (data-instant CSS selector) - Inline rendering (no portal) - Collision avoidance - Cross-iframe portal (container prop) Tests: - Unstyled variant strips popup class - Inline renders within parent container - Portal renders outside parent container (default) - initialFocus={false} prevents focus movement Made-with: Cursor --- packages/ui/src/popover/popup.tsx | 16 +- .../ui/src/popover/stories/index.story.tsx | 318 +++++++++++++++++- packages/ui/src/popover/test/index.test.tsx | 104 ++++++ 3 files changed, 435 insertions(+), 3 deletions(-) diff --git a/packages/ui/src/popover/popup.tsx b/packages/ui/src/popover/popup.tsx index 5c6a3a7a388c9d..ff8114790fde01 100644 --- a/packages/ui/src/popover/popup.tsx +++ b/packages/ui/src/popover/popup.tsx @@ -1,6 +1,6 @@ import { Popover as _Popover } from '@base-ui/react/popover'; import clsx from 'clsx'; -import { forwardRef } from '@wordpress/element'; +import { forwardRef, useRef } from '@wordpress/element'; import { type ThemeProvider as ThemeProviderType, privateApis as themePrivateApis, @@ -40,6 +40,8 @@ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( }, ref ) { + const inlineContainerRef = useRef< HTMLSpanElement >( null ); + const positioner = ( <_Popover.Positioner align={ align } @@ -74,7 +76,17 @@ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( ); if ( inlineProp ) { - return positioner; + return ( + <> + + <_Popover.Portal container={ inlineContainerRef }> + { positioner } + + + ); } return ( diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx index 7b2315b266ae37..819c8ae128fa69 100644 --- a/packages/ui/src/popover/stories/index.story.tsx +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -1,5 +1,6 @@ import type { Meta, StoryObj } from '@storybook/react-vite'; -import { useState } from '@wordpress/element'; +import { useLayoutEffect, useRef, useState } from '@wordpress/element'; +import type { RefCallback } from 'react'; import { Popover } from '../..'; const meta: Meta< typeof Popover.Root > = { @@ -196,6 +197,321 @@ export const Modal: Story = { }, }; +/** + * The `variant="unstyled"` option strips all visual styling from the popup, + * making it a blank positioning container for fully custom content. + */ +export const Unstyled: Story = { + args: { + children: ( + <> + Open Unstyled + +
+ Custom content +

+ This popup has no default styling — the consumer + controls all visual appearance. +

+
+
+ + ), + }, +}; + +function useMeasure< TRef extends HTMLElement >() { + const [ element, setElement ] = useState< TRef | null >( null ); + const [ elementSize, setElementSize ] = useState( { + width: 0, + height: 0, + } ); + + useLayoutEffect( () => { + if ( ! element ) { + return; + } + + function update() { + const bcr = element!.getBoundingClientRect(); + setElementSize( { + width: bcr.width, + height: bcr.height, + } ); + } + + const resizeObserver = new ResizeObserver( () => { + update(); + } ); + resizeObserver.observe( element ); + update(); + + return () => { + resizeObserver.disconnect(); + }; + }, [ element ] ); + + const elementRef: RefCallback< TRef > = ( node ) => { + setElement( node ); + }; + + return [ elementRef, elementSize ] as const; +} + +/** + * Overlay placement positions the popover centered on top of its trigger, + * effectively covering it. This is achieved by computing a negative + * `sideOffset` based on the measured sizes of the trigger and popup. + * + * This technique is useful when you want the popover to visually replace + * the trigger element in place. + */ +export const OverlayPlacement: Story = { + render: function Render() { + const [ popupRef, popupSize ] = useMeasure< HTMLDivElement >(); + const [ triggerRef, triggerSize ] = useMeasure< HTMLButtonElement >(); + + return ( +
+ + + Trigger (covered by popover) + + + Overlay + + This popover is centered over its trigger using a + negative sideOffset. + + + Try resizing the browser — collision avoidance is + disabled so the popover stays overlaid. + + + +
+ ); + }, +}; + +/** + * Base UI exposes a `data-instant` attribute on the popup that can be used + * in CSS to disable animations. Adding the following rule to your + * stylesheet will skip all transitions: + * + * ```css + * [data-instant] { transition: none !important; } + * ``` + * + * In this example, we add the rule via a ` + + No Animation + + + Instant + + This popover opens and closes without animation. The + `data-instant` attribute on the positioner is targeted + by a CSS rule that disables transitions. + + + + + ), +}; + +/** + * When `inline` is set to `true`, the popup renders in place within the DOM + * hierarchy instead of being portaled to `document.body`. This can be + * useful when you need the popup to participate in the surrounding layout + * or inherit styles from a parent. + */ +export const Inline: Story = { + args: { + children: ( + <> + Open Inline + + + Inline Popover + + This popup is rendered in place — no portal is used. + Inspect the DOM to see it lives inside its parent. + + + + ), + }, +}; + +/** + * Use the `collisionAvoidance` prop to control how the popover behaves when + * it collides with viewport edges. + * + * - `side: 'flip'` flips to the opposite side (default). + * - `side: 'shift'` shifts along the main axis. + * - `side: 'none'` disables collision handling. + * + * Scroll the container to see collision avoidance in action. + */ +export const CollisionAvoidance: Story = { + render: () => ( +
+
+ + Flip (default) + + + Flips to bottom when clipped + + + + + + No collision + + + Stays on top even when clipped + + + +
+
+
+ ), +}; + +function GenericIframe( { + children, + ...props +}: React.ComponentProps< 'iframe' > & { children: React.ReactNode } ) { + const [ containerNode, setContainerNode ] = useState< HTMLElement | null >( + null + ); + + return ( + // eslint-disable-next-line jsx-a11y/iframe-has-title + + ); +} + +/** + * When the popover's trigger lives inside an iframe but the popover should + * render in the parent document, pass a parent-document element to the + * `container` prop on `Popover.Popup`. + * + * This technique is used in Gutenberg where the block editor canvas is an + * iframe but toolbars and menus must appear outside it. + */ +export const CrossIframe: Story = { + render: function Render() { + const portalContainerRef = useRef< HTMLDivElement >( null ); + + return ( +
+

+ The popover trigger is inside the purple-bordered iframe. + The popup renders in the parent document via the `container` + prop. +

+
+ +
+ + + Trigger (inside iframe) + + + } + > + + Cross-Iframe Popover + + + This popup is rendered in the parent + document, not inside the iframe. + + + +
+
+
+ ); + }, +}; + /** * The `--wp-ui-popover-z-index` CSS variable controls the z-index of the * popover positioner. It can be overridden globally or scoped to a diff --git a/packages/ui/src/popover/test/index.test.tsx b/packages/ui/src/popover/test/index.test.tsx index d10ee58746288d..968a1c023f9917 100644 --- a/packages/ui/src/popover/test/index.test.tsx +++ b/packages/ui/src/popover/test/index.test.tsx @@ -362,4 +362,108 @@ describe( 'Popover', () => { } ); } ); } ); + + describe( 'variant', () => { + it( 'should not apply popup styles when variant is unstyled', async () => { + const user = userEvent.setup(); + const ref = createRef< HTMLDivElement >(); + + render( + + Open + + Unstyled content + + + ); + + await user.click( screen.getByRole( 'button', { name: 'Open' } ) ); + + await waitFor( () => { + expect( ref.current ).toBeInstanceOf( HTMLDivElement ); + } ); + + expect( ref.current!.className ).toBe( '' ); + } ); + } ); + + describe( 'inline', () => { + it( 'should render without a portal when inline is true', async () => { + const user = userEvent.setup(); + + render( +
+ + Open + Inline content + +
+ ); + + await user.click( screen.getByRole( 'button', { name: 'Open' } ) ); + + await waitFor( () => { + expect( + screen.getByText( 'Inline content' ) + ).toBeInTheDocument(); + } ); + + const wrapper = screen.getByTestId( 'inline-wrapper' ); + expect( wrapper ).toContainElement( + screen.getByText( 'Inline content' ) + ); + } ); + + it( 'should render with a portal by default', async () => { + const user = userEvent.setup(); + + render( +
+ + Open + Portal content + +
+ ); + + await user.click( screen.getByRole( 'button', { name: 'Open' } ) ); + + await waitFor( () => { + expect( + screen.getByText( 'Portal content' ) + ).toBeInTheDocument(); + } ); + + const wrapper = screen.getByTestId( 'portal-wrapper' ); + expect( wrapper ).not.toContainElement( + screen.getByText( 'Portal content' ) + ); + } ); + } ); + + describe( 'initialFocus', () => { + it( 'should not move focus when initialFocus is false', async () => { + const user = userEvent.setup(); + + render( + + Open + + + + + ); + + const trigger = screen.getByRole( 'button', { name: 'Open' } ); + await user.click( trigger ); + + await waitFor( () => { + expect( + screen.getByPlaceholderText( 'Input' ) + ).toBeInTheDocument(); + } ); + + expect( trigger ).toHaveFocus(); + } ); + } ); } ); From a7aba5e7cc71a83196a242df380584fac63e9ec3 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Wed, 11 Mar 2026 18:27:26 +0100 Subject: [PATCH 07/71] Fix Controlled story race condition Replace toggle button with explicit open/close buttons to avoid the click-outside dismiss racing with the toggle handler. Made-with: Cursor --- packages/ui/src/popover/stories/index.story.tsx | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx index 819c8ae128fa69..8cbc6f00104f97 100644 --- a/packages/ui/src/popover/stories/index.story.tsx +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -139,6 +139,11 @@ export const WithCloseButton: Story = { /** * Use the `open` and `onOpenChange` props on `Popover.Root` to control the * popover's visibility programmatically. + * + * Note: clicking outside an open popover dismisses it via `onOpenChange`. + * If an external button uses toggle logic (`!prev`), the dismiss and toggle + * can race — the popover closes then immediately reopens. Use explicit + * open/close actions on external controls to avoid this. */ export const Controlled: Story = { render: function Render() { @@ -156,9 +161,13 @@ export const Controlled: Story = { - + + open: { String( isOpen ) }
); }, From 1eda3793fc2d86d141a4e1c7269a0027d0c4f308 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Wed, 11 Mar 2026 18:29:34 +0100 Subject: [PATCH 08/71] Improve Modal story with interactive form content Replace static text with a name/email form to better demonstrate the focus-trapping behavior described in the story's JSDoc. Made-with: Cursor --- .../ui/src/popover/stories/index.story.tsx | 90 ++++++++++++++----- 1 file changed, 70 insertions(+), 20 deletions(-) diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx index 8cbc6f00104f97..5e06e70b42ac43 100644 --- a/packages/ui/src/popover/stories/index.story.tsx +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -1,5 +1,5 @@ import type { Meta, StoryObj } from '@storybook/react-vite'; -import { useLayoutEffect, useRef, useState } from '@wordpress/element'; +import { useId, useLayoutEffect, useRef, useState } from '@wordpress/element'; import type { RefCallback } from 'react'; import { Popover } from '../..'; @@ -175,34 +175,84 @@ export const Controlled: Story = { /** * Set `modal` to `true` to trap focus inside the popover when it is open. - * This is useful for complex popover content that requires user interaction. + * This is useful for complex popover content that requires user interaction, + * such as forms. Try tabbing through the fields — focus stays inside the + * popover until it is dismissed. */ export const Modal: Story = { - args: { - modal: true, - children: ( - <> - Open Modal Popover + render: function Render() { + const nameId = useId(); + const emailId = useId(); + + return ( + + Edit Settings - Modal Popover - - Focus is trapped inside this popover. Press Escape to - close. - - Settings +
e.preventDefault() } > - Close - + + +
+ + Cancel + + +
+
- - ), +
+ ); }, }; From 1cbd98fb8ee18e1afaf98c585267e966f71bebc2 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Wed, 11 Mar 2026 18:30:22 +0100 Subject: [PATCH 09/71] Fix Controlled story click-outside flicker Ignore dismiss events whose click target is inside the external controls wrapper, preventing the close-then-reopen flicker. Made-with: Cursor --- .../ui/src/popover/stories/index.story.tsx | 44 ++++++++++++++----- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx index 5e06e70b42ac43..18b3162061f401 100644 --- a/packages/ui/src/popover/stories/index.story.tsx +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -140,18 +140,35 @@ export const WithCloseButton: Story = { * Use the `open` and `onOpenChange` props on `Popover.Root` to control the * popover's visibility programmatically. * - * Note: clicking outside an open popover dismisses it via `onOpenChange`. - * If an external button uses toggle logic (`!prev`), the dismiss and toggle - * can race — the popover closes then immediately reopens. Use explicit - * open/close actions on external controls to avoid this. + * When external controls live outside the popover, clicks on them trigger + * Base UI's click-outside dismiss before the button's own handler runs. + * To prevent the flicker, filter out dismiss events whose click landed on + * your external controls using the `event` in `onOpenChange`. */ export const Controlled: Story = { render: function Render() { const [ isOpen, setIsOpen ] = useState( false ); + const externalControlsRef = useRef< HTMLDivElement >( null ); + + const handleOpenChange: React.ComponentProps< + typeof Popover.Root + >[ 'onOpenChange' ] = ( nextOpen, event ) => { + if ( + ! nextOpen && + event && + 'event' in event && + event.event instanceof Event && + event.event.target instanceof Node && + externalControlsRef.current?.contains( event.event.target ) + ) { + return; + } + setIsOpen( nextOpen ); + }; return (
- + Toggle Popover @@ -161,12 +178,17 @@ export const Controlled: Story = { - - +
+ + +
open: { String( isOpen ) }
); From 6096213d835f8cfa5a0ce3ca4f6f4c3aa66520dc Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Wed, 11 Mar 2026 18:32:59 +0100 Subject: [PATCH 10/71] Simplify Controlled story with checkbox toggle Replace the button-based external controls with a checkbox that drives the popover state. This avoids the click-outside dismiss race entirely and makes the controlled pattern clearer. Made-with: Cursor --- .../ui/src/popover/stories/index.story.tsx | 56 +++++++------------ 1 file changed, 21 insertions(+), 35 deletions(-) diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx index 18b3162061f401..3d800268104546 100644 --- a/packages/ui/src/popover/stories/index.story.tsx +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -140,35 +140,24 @@ export const WithCloseButton: Story = { * Use the `open` and `onOpenChange` props on `Popover.Root` to control the * popover's visibility programmatically. * - * When external controls live outside the popover, clicks on them trigger - * Base UI's click-outside dismiss before the button's own handler runs. - * To prevent the flicker, filter out dismiss events whose click landed on - * your external controls using the `event` in `onOpenChange`. + * The checkbox drives the popover state externally. The popover's trigger + * and click-outside dismiss both sync back to the same state via + * `onOpenChange`, keeping everything in sync. */ export const Controlled: Story = { render: function Render() { const [ isOpen, setIsOpen ] = useState( false ); - const externalControlsRef = useRef< HTMLDivElement >( null ); - - const handleOpenChange: React.ComponentProps< - typeof Popover.Root - >[ 'onOpenChange' ] = ( nextOpen, event ) => { - if ( - ! nextOpen && - event && - 'event' in event && - event.event instanceof Event && - event.event.target instanceof Node && - externalControlsRef.current?.contains( event.event.target ) - ) { - return; - } - setIsOpen( nextOpen ); - }; + const checkboxId = useId(); return ( -
- +
+ Toggle Popover @@ -178,18 +167,15 @@ export const Controlled: Story = { -
- - -
- open: { String( isOpen ) } +
); }, From ce7f6653c65cddb6775cd511b337d573c2c133a1 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Wed, 11 Mar 2026 18:43:44 +0100 Subject: [PATCH 11/71] Use Popover.Title and Description consistently in stories Add missing Title/Description to Unstyled and CollisionAvoidance stories for better accessibility and consistent usage patterns. Made-with: Cursor --- packages/ui/src/popover/stories/index.story.tsx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx index 3d800268104546..33128dfb74e08f 100644 --- a/packages/ui/src/popover/stories/index.story.tsx +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -282,11 +282,11 @@ export const Unstyled: Story = { borderRadius: 8, } } > - Custom content -

+ Custom Styled + This popup has no default styling — the consumer controls all visual appearance. -

+
@@ -470,6 +470,7 @@ export const CollisionAvoidance: Story = { Flip (default) + Flip Flips to bottom when clipped @@ -485,6 +486,7 @@ export const CollisionAvoidance: Story = { align: 'none', } } > + None Stays on top even when clipped From 2f961be0b4ae5548f96d4e88bd7006b39a69a446 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Wed, 11 Mar 2026 18:50:44 +0100 Subject: [PATCH 12/71] Fix animations and add `animated` prop to Popover.Popup Move the dropdown-motion CSS class from the Positioner to the Popup element, where Base UI places the `data-starting-style` attribute needed for enter transitions. Add an `animated` prop (default `true`) so consumers can disable animations declaratively instead of needing to know about the `data-instant` implementation detail. Made-with: Cursor --- packages/ui/src/popover/popup.tsx | 10 +++- .../ui/src/popover/stories/index.story.tsx | 56 ++++++------------- packages/ui/src/popover/test/index.test.tsx | 23 ++++++++ packages/ui/src/popover/types.ts | 7 +++ 4 files changed, 53 insertions(+), 43 deletions(-) diff --git a/packages/ui/src/popover/popup.tsx b/packages/ui/src/popover/popup.tsx index ff8114790fde01..a74422211420f3 100644 --- a/packages/ui/src/popover/popup.tsx +++ b/packages/ui/src/popover/popup.tsx @@ -22,6 +22,7 @@ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( { align = 'center', alignOffset, + animated = true, children, className, collisionAvoidance, @@ -55,8 +56,6 @@ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( style={ style } className={ clsx( resetStyles[ 'box-sizing' ], - variant !== 'unstyled' && - dropdownMotionStyles[ 'dropdown-motion' ], styles.positioner, className ) } @@ -66,7 +65,12 @@ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( ref={ ref } initialFocus={ initialFocus } finalFocus={ finalFocus } - className={ clsx( variant !== 'unstyled' && styles.popup ) } + className={ clsx( + variant !== 'unstyled' && styles.popup, + animated && + variant !== 'unstyled' && + dropdownMotionStyles[ 'dropdown-motion' ] + ) } { ...props } > { children } diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx index 33128dfb74e08f..1182b221879d6b 100644 --- a/packages/ui/src/popover/stories/index.story.tsx +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -274,20 +274,11 @@ export const Unstyled: Story = { <> Open Unstyled -
- Custom Styled - - This popup has no default styling — the consumer - controls all visual appearance. - -
+ Custom Styled + + This popup has no default styling — the consumer + controls all visual appearance. +
), @@ -381,39 +372,24 @@ export const OverlayPlacement: Story = { }; /** - * Base UI exposes a `data-instant` attribute on the popup that can be used - * in CSS to disable animations. Adding the following rule to your - * stylesheet will skip all transitions: - * - * ```css - * [data-instant] { transition: none !important; } - * ``` - * - * In this example, we add the rule via a ` - + args: { + children: ( + <> No Animation - + Instant - This popover opens and closes without animation. The - `data-instant` attribute on the positioner is targeted - by a CSS rule that disables transitions. + This popover opens and closes without animation. - -
- ), + + ), + }, }; /** @@ -493,7 +469,7 @@ export const CollisionAvoidance: Story = { -
+
), }; diff --git a/packages/ui/src/popover/test/index.test.tsx b/packages/ui/src/popover/test/index.test.tsx index 968a1c023f9917..794e3331148599 100644 --- a/packages/ui/src/popover/test/index.test.tsx +++ b/packages/ui/src/popover/test/index.test.tsx @@ -387,6 +387,29 @@ describe( 'Popover', () => { } ); } ); + describe( 'animated', () => { + it( 'should render correctly when animated is false', async () => { + const user = userEvent.setup(); + + render( + + Open + + No animation content + + + ); + + await user.click( screen.getByRole( 'button', { name: 'Open' } ) ); + + await waitFor( () => { + expect( + screen.getByText( 'No animation content' ) + ).toBeInTheDocument(); + } ); + } ); + } ); + describe( 'inline', () => { it( 'should render without a portal when inline is true', async () => { const user = userEvent.setup(); diff --git a/packages/ui/src/popover/types.ts b/packages/ui/src/popover/types.ts index 3e9eaf295b84d3..c3d95009888d1a 100644 --- a/packages/ui/src/popover/types.ts +++ b/packages/ui/src/popover/types.ts @@ -34,6 +34,13 @@ export interface PopupProps | 'sticky' >, Pick< _Popover.Popup.Props, 'initialFocus' | 'finalFocus' > { + /** + * Whether the popup uses open/close animations. + * + * @default true + */ + animated?: boolean; + /** * The content to be rendered inside the component. */ From ceb71881d4ad5e3fd0d9aa528282d9a2afd7ebea Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Wed, 11 Mar 2026 18:51:37 +0100 Subject: [PATCH 13/71] =?UTF-8?q?Show=20all=20side=20=C3=97=20align=20comb?= =?UTF-8?q?inations=20in=20Positioning=20story?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Display all 12 positioning combinations (4 sides × 3 aligns) in a grid, all open by default with animations and collision avoidance disabled so the layout stays stable. Made-with: Cursor --- .../ui/src/popover/stories/index.story.tsx | 89 ++++++++++--------- 1 file changed, 46 insertions(+), 43 deletions(-) diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx index 1182b221879d6b..56043f7b53fb11 100644 --- a/packages/ui/src/popover/stories/index.story.tsx +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -46,52 +46,55 @@ export const Default: Story = { }; /** - * Use the `side` and `align` props on `Popover.Popup` to control where the - * popover appears relative to the trigger element. + * All combinations of `side` and `align` props on `Popover.Popup`. + * + * Each row shows a side (`top`, `right`, `bottom`, `left`), and each column + * shows an alignment (`start`, `center`, `end`). */ export const Positioning: Story = { - render: () => ( -
- - Top - - - Popover on top - - - - - Right - - - Popover on right - - - - - Bottom - - - Popover on bottom - - + render: function Render() { + const sides = [ 'top', 'right', 'bottom', 'left' ] as const; + const aligns = [ 'start', 'center', 'end' ] as const; - - Left - - - Popover on left - - -
- ), + return ( +
+ { sides.flatMap( ( side ) => + aligns.map( ( align ) => ( + + + { side } / { align } + + + + + { side } / { align } + + + + ) ) + ) } +
+ ); + }, }; /** From eb025b8756b41f872f0a511b08c9dd1d97a016c0 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Wed, 11 Mar 2026 19:02:58 +0100 Subject: [PATCH 14/71] Mirror legacy WithSlotOutsideIframe pattern in CrossIframe story Add scrollable iframe content (200vh) so the popover's cross-document anchor tracking can be verified by scrolling. Match the legacy example's layout with a centered salmon-colored trigger and the popover open by default. Made-with: Cursor --- .../ui/src/popover/stories/index.story.tsx | 82 +++++++++++-------- 1 file changed, 50 insertions(+), 32 deletions(-) diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx index 56043f7b53fb11..b136b0574926ca 100644 --- a/packages/ui/src/popover/stories/index.story.tsx +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -68,10 +68,7 @@ export const Positioning: Story = { > { sides.flatMap( ( side ) => aligns.map( ( align ) => ( - + { side } / { align } @@ -86,7 +83,7 @@ export const Positioning: Story = { > - { side } / { align } + { side } side / { align } align @@ -486,10 +483,12 @@ function GenericIframe( { ); return ( - // eslint-disable-next-line jsx-a11y/iframe-has-title ); } From bb4ffc5f085a49637b98c599c994b5de541e0a75 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Wed, 11 Mar 2026 19:11:08 +0100 Subject: [PATCH 16/71] Add CrossIframeWithSlotFill story demonstrating SlotFill pattern Create a minimal SlotFill using React context that shares a container ref from the parent document. The popover inside the iframe reads the slot via usePopoverSlot() and passes it to the container prop, showing how the pattern works without manual ref plumbing. Made-with: Cursor --- .../ui/src/popover/stories/index.story.tsx | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx index 5d348fabe06278..c92ce746c559a0 100644 --- a/packages/ui/src/popover/stories/index.story.tsx +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -1,6 +1,8 @@ import type { Meta, StoryObj } from '@storybook/react-vite'; import { + createContext, createPortal, + useContext, useId, useLayoutEffect, useRef, @@ -580,6 +582,100 @@ export const CrossIframe: Story = { }, }; +/** + * Minimal SlotFill implementation using React context. The `Slot` renders a + * target `div` and shares its ref via context so that any descendant `Fill` + * can read it and pass it to `Popover.Popup`'s `container` prop. + */ +const PopoverSlotContext = + createContext< React.RefObject< HTMLElement > | null >( null ); + +function PopoverSlotProvider( { children }: { children: React.ReactNode } ) { + const slotRef = useRef< HTMLDivElement >( null ); + + return ( + } + > +
+ { children } + + ); +} + +function usePopoverSlot() { + return useContext( PopoverSlotContext ); +} + +/** + * Same cross-iframe scenario, but wired through a minimal SlotFill context + * instead of manually passing a container ref. + * + * `PopoverSlotProvider` renders a slot `div` in the parent document and + * shares its ref via context. Inside the iframe, the popover reads it + * with `usePopoverSlot()` and passes it to the `container` prop — no + * manual ref plumbing needed. + */ +export const CrossIframeWithSlotFill: Story = { + name: 'Cross-Iframe (SlotFill)', + render: function Render() { + return ( + + +
+
+ +
+
+
+
+ ); + }, +}; + +function SlotFillPopover() { + const slot = usePopoverSlot(); + + return ( + + + Popover's anchor (inside iframe) + + + + Cross-Iframe (SlotFill) + + This popup renders in the parent document via a SlotFill + context — no manual ref wiring required. + + + + ); +} + /** * The `--wp-ui-popover-z-index` CSS variable controls the z-index of the * popover positioner. It can be overridden globally or scoped to a From 1c17d3b01fad136aa32fc1d5a6258ac94d3a7480 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Wed, 11 Mar 2026 19:13:19 +0100 Subject: [PATCH 17/71] Use @wordpress/components SlotFill in CrossIframeWithSlotFill story Replace the custom React context SlotFill with the real SlotFillProvider and Slot from @wordpress/components. The Slot renders a div in the parent document whose ref is passed to the Popover.Popup container prop, mirroring the legacy WithSlotOutsideIframe pattern. Made-with: Cursor --- .../ui/src/popover/stories/index.story.tsx | 102 +++++++----------- 1 file changed, 41 insertions(+), 61 deletions(-) diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx index c92ce746c559a0..fa5c8db9ea638e 100644 --- a/packages/ui/src/popover/stories/index.story.tsx +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -1,13 +1,12 @@ import type { Meta, StoryObj } from '@storybook/react-vite'; import { - createContext, createPortal, - useContext, useId, useLayoutEffect, useRef, useState, } from '@wordpress/element'; +import { SlotFillProvider, Slot } from '@wordpress/components'; import type { RefCallback } from 'react'; import { Popover } from '../..'; @@ -583,44 +582,26 @@ export const CrossIframe: Story = { }; /** - * Minimal SlotFill implementation using React context. The `Slot` renders a - * target `div` and shares its ref via context so that any descendant `Fill` - * can read it and pass it to `Popover.Popup`'s `container` prop. - */ -const PopoverSlotContext = - createContext< React.RefObject< HTMLElement > | null >( null ); - -function PopoverSlotProvider( { children }: { children: React.ReactNode } ) { - const slotRef = useRef< HTMLDivElement >( null ); - - return ( - } - > -
- { children } - - ); -} - -function usePopoverSlot() { - return useContext( PopoverSlotContext ); -} - -/** - * Same cross-iframe scenario, but wired through a minimal SlotFill context - * instead of manually passing a container ref. + * Same cross-iframe scenario, but using `SlotFillProvider` and `Slot` from + * `@wordpress/components` as the render target. * - * `PopoverSlotProvider` renders a slot `div` in the parent document and - * shares its ref via context. Inside the iframe, the popover reads it - * with `usePopoverSlot()` and passes it to the `container` prop — no - * manual ref plumbing needed. + * The `Slot` renders a `div` in the parent document, and its forwarded ref + * is passed to `Popover.Popup`'s `container` prop so the popup portals into + * the slot element. This mirrors the legacy Popover's `WithSlotOutsideIframe` + * pattern. */ export const CrossIframeWithSlotFill: Story = { name: 'Cross-Iframe (SlotFill)', render: function Render() { + const slotRef = useRef< HTMLDivElement >( null ); + return ( - + + - + + + Popover's anchor (inside iframe) + + + } + > + + + Cross-Iframe (SlotFill) + + + This popup renders in the parent + document via a `Slot` from + `@wordpress/components`. + + +
- + ); }, }; -function SlotFillPopover() { - const slot = usePopoverSlot(); - - return ( - - - Popover's anchor (inside iframe) - - - - Cross-Iframe (SlotFill) - - This popup renders in the parent document via a SlotFill - context — no manual ref wiring required. - - - - ); -} - /** * The `--wp-ui-popover-z-index` CSS variable controls the z-index of the * popover positioner. It can be overridden globally or scoped to a From 9d7c6f93afdc57b2c0e56601196b757b2985b523 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Wed, 11 Mar 2026 19:33:17 +0100 Subject: [PATCH 18/71] Set collisionBoundary in collision avoidance and cross-iframe stories Portal'd popovers don't inherit scrollable ancestors as clipping boundaries. Pass the scrollable container (or iframe element) as collisionBoundary so Floating UI correctly flips/clips against it. Also make GenericIframe forward refs so the iframe element can be captured for use as a collision boundary. Made-with: Cursor --- .../ui/src/popover/stories/index.story.tsx | 167 +++++++++++------- 1 file changed, 100 insertions(+), 67 deletions(-) diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx index fa5c8db9ea638e..d442cb55cc3c49 100644 --- a/packages/ui/src/popover/stories/index.story.tsx +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -1,11 +1,13 @@ import type { Meta, StoryObj } from '@storybook/react-vite'; import { createPortal, + forwardRef, useId, useLayoutEffect, useRef, useState, } from '@wordpress/element'; +import { useMergeRefs } from '@wordpress/compose'; import { SlotFillProvider, Slot } from '@wordpress/components'; import type { RefCallback } from 'react'; import { Popover } from '../..'; @@ -425,88 +427,107 @@ export const Inline: Story = { /** * Use the `collisionAvoidance` prop to control how the popover behaves when - * it collides with viewport edges. + * it collides with the edges of its collision boundary. + * + * Because the popup renders via a portal (outside the scrollable container), + * the container must be passed as `collisionBoundary` so Floating UI treats + * it as the clipping edge. * * - `side: 'flip'` flips to the opposite side (default). - * - `side: 'shift'` shifts along the main axis. * - `side: 'none'` disables collision handling. * * Scroll the container to see collision avoidance in action. */ export const CollisionAvoidance: Story = { - render: () => ( -
+ render: function Render() { + const [ boundary, setBoundary ] = useState< HTMLElement | null >( + null + ); + + return (
- - Flip (default) - - Flip - - Flips to bottom when clipped - - - +
+ + Flip (default) + + Flip + + Flips to bottom when clipped + + + - - No collision - - None - - Stays on top even when clipped - - - + + No collision + + None + + Stays on top even when clipped + + + +
+
-
-
- ), + ); + }, }; -function GenericIframe( { - children, - ...props -}: React.ComponentProps< 'iframe' > & { children: React.ReactNode } ) { - const [ containerNode, setContainerNode ] = useState< HTMLElement | null >( - null - ); - - return ( - - ); -} +const GenericIframe = forwardRef< HTMLIFrameElement, GenericIframeProps >( + function GenericIframe( { children, ...props }, ref ) { + const [ containerNode, setContainerNode ] = + useState< HTMLElement | null >( null ); + const iframeRef = useRef< HTMLIFrameElement >( null ); + const mergedRef = useMergeRefs( [ ref, iframeRef ] ); + + return ( + + ); + } +); + +type GenericIframeProps = React.ComponentProps< 'iframe' > & { + children: React.ReactNode; +}; /** * When the popover's trigger lives inside an iframe but the popover should @@ -522,11 +543,14 @@ function GenericIframe( { export const CrossIframe: Story = { render: function Render() { const portalContainerRef = useRef< HTMLDivElement >( null ); + const [ iframeBoundary, setIframeBoundary ] = + useState< HTMLIFrameElement | null >( null ); return (
} + collisionBoundary={ + iframeBoundary ?? undefined + } > @@ -594,6 +621,8 @@ export const CrossIframeWithSlotFill: Story = { name: 'Cross-Iframe (SlotFill)', render: function Render() { const slotRef = useRef< HTMLDivElement >( null ); + const [ iframeBoundary, setIframeBoundary ] = + useState< HTMLIFrameElement | null >( null ); return ( @@ -603,6 +632,7 @@ export const CrossIframeWithSlotFill: Story = { ref={ slotRef } /> } + collisionBoundary={ + iframeBoundary ?? undefined + } > From 558cac8371baf2912238c6ed96ff75abc18995d1 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Wed, 11 Mar 2026 19:45:43 +0100 Subject: [PATCH 19/71] Use args and disable hardcoded controls in Popover stories - Positioning, CollisionAvoidance: disable all controls (multi-popover demos where args don't apply meaningfully) - Controlled: disable open/onOpenChange/defaultOpen (managed by internal state) - Modal: pass modal via args, disable its control - OverlayPlacement, CrossIframe, CrossIframeWithSlotFill: pass defaultOpen via args, disable its control Made-with: Cursor --- .../ui/src/popover/stories/index.story.tsx | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx index d442cb55cc3c49..339496817a1eae 100644 --- a/packages/ui/src/popover/stories/index.story.tsx +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -61,6 +61,7 @@ export const Default: Story = { * shows an alignment (`start`, `center`, `end`). */ export const Positioning: Story = { + parameters: { controls: { disable: true } }, render: function Render() { const sides = [ 'top', 'right', 'bottom', 'left' ] as const; const aligns = [ 'start', 'center', 'end' ] as const; @@ -154,6 +155,11 @@ export const WithCloseButton: Story = { * `onOpenChange`, keeping everything in sync. */ export const Controlled: Story = { + argTypes: { + open: { control: false }, + onOpenChange: { control: false }, + defaultOpen: { control: false }, + }, render: function Render() { const [ isOpen, setIsOpen ] = useState( false ); const checkboxId = useId(); @@ -197,12 +203,14 @@ export const Controlled: Story = { * popover until it is dismissed. */ export const Modal: Story = { - render: function Render() { + args: { modal: true }, + argTypes: { modal: { control: false } }, + render: function Render( { modal: modalArg } ) { const nameId = useId(); const emailId = useId(); return ( - + Edit Settings @@ -341,13 +349,15 @@ function useMeasure< TRef extends HTMLElement >() { * the trigger element in place. */ export const OverlayPlacement: Story = { - render: function Render() { + args: { defaultOpen: true }, + argTypes: { defaultOpen: { control: false } }, + render: function Render( { defaultOpen: defaultOpenArg } ) { const [ popupRef, popupSize ] = useMeasure< HTMLDivElement >(); const [ triggerRef, triggerSize ] = useMeasure< HTMLButtonElement >(); return (
- + Trigger (covered by popover) @@ -439,6 +449,7 @@ export const Inline: Story = { * Scroll the container to see collision avoidance in action. */ export const CollisionAvoidance: Story = { + parameters: { controls: { disable: true } }, render: function Render() { const [ boundary, setBoundary ] = useState< HTMLElement | null >( null @@ -541,7 +552,9 @@ type GenericIframeProps = React.ComponentProps< 'iframe' > & { * position across document boundaries. */ export const CrossIframe: Story = { - render: function Render() { + args: { defaultOpen: true }, + argTypes: { defaultOpen: { control: false } }, + render: function Render( { defaultOpen: defaultOpenArg } ) { const portalContainerRef = useRef< HTMLDivElement >( null ); const [ iframeBoundary, setIframeBoundary ] = useState< HTMLIFrameElement | null >( null ); @@ -571,7 +584,7 @@ export const CrossIframe: Story = { marginInline: 'auto', } } > - + ( null ); const [ iframeBoundary, setIframeBoundary ] = useState< HTMLIFrameElement | null >( null ); @@ -653,7 +668,7 @@ export const CrossIframeWithSlotFill: Story = { marginInline: 'auto', } } > - + Date: Thu, 12 Mar 2026 11:45:47 +0100 Subject: [PATCH 20/71] Improve Controlled and Modal stories args handling - Controlled: move children to args, spread all args on Root, fix onOpenChange to filter outside-press/focus-out from checkbox controls - Modal: convert to pure args (no render function), use hardcoded IDs since hooks can't be used in args - Default: use generic placeholder text Made-with: Cursor --- .../ui/src/popover/stories/index.story.tsx | 88 +++++++++++++------ packages/ui/src/popover/stories/utils.tsx | 0 2 files changed, 60 insertions(+), 28 deletions(-) create mode 100644 packages/ui/src/popover/stories/utils.tsx diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx index 339496817a1eae..d606b507b183f5 100644 --- a/packages/ui/src/popover/stories/index.story.tsx +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -44,9 +44,9 @@ export const Default: Story = { Open Popover - Notifications + Popover title - You are all caught up. Good job! + Popover description @@ -160,9 +160,25 @@ export const Controlled: Story = { onOpenChange: { control: false }, defaultOpen: { control: false }, }, - render: function Render() { + args: { + children: ( + <> + Toggle Popover + + + Controlled Popover + + This popover is controlled by external state. + + + + ), + }, + render: function Render( args ) { const [ isOpen, setIsOpen ] = useState( false ); const checkboxId = useId(); + const checkboxRef = useRef< HTMLInputElement >( null ); + const labelRef = useRef< HTMLLabelElement >( null ); return (
- - Toggle Popover - - - Controlled Popover - - This popover is controlled by external state. - - - -
@@ -203,14 +236,11 @@ export const Controlled: Story = { * popover until it is dismissed. */ export const Modal: Story = { - args: { modal: true }, argTypes: { modal: { control: false } }, - render: function Render( { modal: modalArg } ) { - const nameId = useId(); - const emailId = useId(); - - return ( - + args: { + modal: true, + children: ( + <> Edit Settings @@ -225,7 +255,7 @@ export const Modal: Story = { onSubmit={ ( e ) => e.preventDefault() } >
-
- ); + + ), }, }; diff --git a/packages/ui/src/popover/stories/utils.tsx b/packages/ui/src/popover/stories/utils.tsx new file mode 100644 index 00000000000000..e69de29bb2d1d6 From 18f09e019b8adfb610d7498c60f36416205ee205 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Thu, 12 Mar 2026 11:48:43 +0100 Subject: [PATCH 21/71] Refactor Popover stories: args forwarding, utils extraction, z-index fix - Forward all args to Popover.Root in every render-based story (OverlayPlacement, CrossIframe, CrossIframeWithSlotFill) so controls are wired to the live component - Disable children control globally in meta (re-enable for Default only), since children JSX is not meaningfully editable via controls - Extract useMeasure and GenericIframe to stories/utils.tsx with JSDoc comments - Fix WithCustomZIndex to actually set --wp-ui-popover-z-index via a scoped style tag and className on the positioner Made-with: Cursor --- .../ui/src/popover/stories/index.story.tsx | 130 +++++------------- packages/ui/src/popover/stories/utils.tsx | 91 ++++++++++++ 2 files changed, 128 insertions(+), 93 deletions(-) diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx index d606b507b183f5..39ba630e69c301 100644 --- a/packages/ui/src/popover/stories/index.story.tsx +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -1,16 +1,8 @@ import type { Meta, StoryObj } from '@storybook/react-vite'; -import { - createPortal, - forwardRef, - useId, - useLayoutEffect, - useRef, - useState, -} from '@wordpress/element'; -import { useMergeRefs } from '@wordpress/compose'; +import { useId, useRef, useState } from '@wordpress/element'; import { SlotFillProvider, Slot } from '@wordpress/components'; -import type { RefCallback } from 'react'; import { Popover } from '../..'; +import { GenericIframe, useMeasure } from './utils'; const meta: Meta< typeof Popover.Root > = { title: 'Design System/Components/Popover', @@ -23,6 +15,9 @@ const meta: Meta< typeof Popover.Root > = { 'Popover.Description': Popover.Description, 'Popover.Close': Popover.Close, }, + argTypes: { + children: { control: false }, + }, parameters: { docs: { description: { @@ -38,6 +33,9 @@ export default meta; type Story = StoryObj< typeof Popover.Root >; export const Default: Story = { + argTypes: { + children: { control: { type: 'text' } }, + }, args: { children: ( <> @@ -192,9 +190,6 @@ export const Controlled: Story = { { ...args } open={ isOpen } onOpenChange={ ( nextOpen, eventDetails ) => { - // For the sake of demonstrating controlled updates, prevent the - // popover from auto-closing when detecting a click outside coming - // from the checkbox (used to trigger external, controlled updates). if ( [ 'outside-press', 'focus-out' ].includes( eventDetails.reason @@ -334,44 +329,6 @@ export const Unstyled: Story = { }, }; -function useMeasure< TRef extends HTMLElement >() { - const [ element, setElement ] = useState< TRef | null >( null ); - const [ elementSize, setElementSize ] = useState( { - width: 0, - height: 0, - } ); - - useLayoutEffect( () => { - if ( ! element ) { - return; - } - - function update() { - const bcr = element!.getBoundingClientRect(); - setElementSize( { - width: bcr.width, - height: bcr.height, - } ); - } - - const resizeObserver = new ResizeObserver( () => { - update(); - } ); - resizeObserver.observe( element ); - update(); - - return () => { - resizeObserver.disconnect(); - }; - }, [ element ] ); - - const elementRef: RefCallback< TRef > = ( node ) => { - setElement( node ); - }; - - return [ elementRef, elementSize ] as const; -} - /** * Overlay placement positions the popover centered on top of its trigger, * effectively covering it. This is achieved by computing a negative @@ -383,13 +340,13 @@ function useMeasure< TRef extends HTMLElement >() { export const OverlayPlacement: Story = { args: { defaultOpen: true }, argTypes: { defaultOpen: { control: false } }, - render: function Render( { defaultOpen: defaultOpenArg } ) { + render: function Render( { children: _children, ...args } ) { const [ popupRef, popupSize ] = useMeasure< HTMLDivElement >(); const [ triggerRef, triggerSize ] = useMeasure< HTMLButtonElement >(); return (
- + Trigger (covered by popover) @@ -540,38 +497,6 @@ export const CollisionAvoidance: Story = { }, }; -const GenericIframe = forwardRef< HTMLIFrameElement, GenericIframeProps >( - function GenericIframe( { children, ...props }, ref ) { - const [ containerNode, setContainerNode ] = - useState< HTMLElement | null >( null ); - const iframeRef = useRef< HTMLIFrameElement >( null ); - const mergedRef = useMergeRefs( [ ref, iframeRef ] ); - - return ( - - ); - } -); - -type GenericIframeProps = React.ComponentProps< 'iframe' > & { - children: React.ReactNode; -}; - /** * When the popover's trigger lives inside an iframe but the popover should * render in the parent document, pass a parent-document element to the @@ -586,7 +511,7 @@ type GenericIframeProps = React.ComponentProps< 'iframe' > & { export const CrossIframe: Story = { args: { defaultOpen: true }, argTypes: { defaultOpen: { control: false } }, - render: function Render( { defaultOpen: defaultOpenArg } ) { + render: function Render( { children: _children, ...args } ) { const portalContainerRef = useRef< HTMLDivElement >( null ); const [ iframeBoundary, setIframeBoundary ] = useState< HTMLIFrameElement | null >( null ); @@ -616,7 +541,7 @@ export const CrossIframe: Story = { marginInline: 'auto', } } > - + ( null ); const [ iframeBoundary, setIframeBoundary ] = useState< HTMLIFrameElement | null >( null ); @@ -700,7 +625,7 @@ export const CrossIframeWithSlotFill: Story = { marginInline: 'auto', } } > - + ` tag + * sets the value. */ export const WithCustomZIndex: Story = { - ...Default, name: 'With Custom z-index', + args: { + children: ( + <> + + Open Popover + + + Custom z-index + + This popover's positioner has z-index: 9999 via the + `--wp-ui-popover-z-index` CSS custom property. + + + + ), + }, }; diff --git a/packages/ui/src/popover/stories/utils.tsx b/packages/ui/src/popover/stories/utils.tsx index e69de29bb2d1d6..e4476b4dcf1a5e 100644 --- a/packages/ui/src/popover/stories/utils.tsx +++ b/packages/ui/src/popover/stories/utils.tsx @@ -0,0 +1,91 @@ +import { + createPortal, + forwardRef, + useLayoutEffect, + useRef, + useState, +} from '@wordpress/element'; +import { useMergeRefs } from '@wordpress/compose'; +import type { RefCallback } from 'react'; + +/** + * Tracks the dimensions of a DOM element via ResizeObserver. + * Returns a ref callback to attach to the element and its current size. + */ +export function useMeasure< TRef extends HTMLElement >() { + const [ element, setElement ] = useState< TRef | null >( null ); + const [ elementSize, setElementSize ] = useState( { + width: 0, + height: 0, + } ); + + useLayoutEffect( () => { + if ( ! element ) { + return; + } + + function update() { + const bcr = element!.getBoundingClientRect(); + setElementSize( { + width: bcr.width, + height: bcr.height, + } ); + } + + const resizeObserver = new ResizeObserver( () => { + update(); + } ); + resizeObserver.observe( element ); + update(); + + return () => { + resizeObserver.disconnect(); + }; + }, [ element ] ); + + const elementRef: RefCallback< TRef > = ( node ) => { + setElement( node ); + }; + + return [ elementRef, elementSize ] as const; +} + +/** + * Renders an iframe and portals its `children` into the iframe's body. + * Forwards a ref to the underlying ` + ); +} ); + +export type GenericIframeProps = React.ComponentProps< 'iframe' > & { + children: React.ReactNode; +}; From 60a8350384e6e1c9bb35761fac956c61cefc21d4 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Thu, 12 Mar 2026 12:01:42 +0100 Subject: [PATCH 22/71] Align WithCustomZIndex story with Select component pattern Use inline style prop to set --wp-ui-popover-z-index, matching how Select.Popup sets --wp-ui-select-z-index. Removes the unnecessary scoped Open Popover - + Custom z-index From aeb4c3278530e25340076aea4d0d6318f66472d6 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Thu, 19 Mar 2026 13:57:59 +0100 Subject: [PATCH 23/71] Expose `anchor` prop on Popover.Popup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pick the `anchor` prop from Base UI's Positioner and forward it in popup.tsx. This enables anchoring to an arbitrary element, virtual element, ref object, or callback — the most-used pattern in Gutenberg (block popovers, link popover, data views context menus). Made-with: Cursor --- packages/ui/src/popover/popup.tsx | 2 ++ packages/ui/src/popover/test/index.test.tsx | 29 +++++++++++++++++++++ packages/ui/src/popover/types.ts | 1 + 3 files changed, 32 insertions(+) diff --git a/packages/ui/src/popover/popup.tsx b/packages/ui/src/popover/popup.tsx index a74422211420f3..b685edab1646c8 100644 --- a/packages/ui/src/popover/popup.tsx +++ b/packages/ui/src/popover/popup.tsx @@ -22,6 +22,7 @@ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( { align = 'center', alignOffset, + anchor, animated = true, children, className, @@ -47,6 +48,7 @@ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( <_Popover.Positioner align={ align } alignOffset={ alignOffset } + anchor={ anchor } collisionAvoidance={ collisionAvoidance } collisionBoundary={ collisionBoundary } collisionPadding={ collisionPadding } diff --git a/packages/ui/src/popover/test/index.test.tsx b/packages/ui/src/popover/test/index.test.tsx index 794e3331148599..b080465806fe1b 100644 --- a/packages/ui/src/popover/test/index.test.tsx +++ b/packages/ui/src/popover/test/index.test.tsx @@ -464,6 +464,35 @@ describe( 'Popover', () => { } ); } ); + describe( 'anchor', () => { + it( 'should render the popup when an anchor element is provided without a trigger', async () => { + function AnchorTest() { + const [ anchorEl, setAnchorEl ] = + useState< HTMLDivElement | null >( null ); + return ( + <> +
+ Anchor element +
+ + + Anchored content + + + + ); + } + + render( ); + + await waitFor( () => { + expect( + screen.getByText( 'Anchored content' ) + ).toBeInTheDocument(); + } ); + } ); + } ); + describe( 'initialFocus', () => { it( 'should not move focus when initialFocus is false', async () => { const user = userEvent.setup(); diff --git a/packages/ui/src/popover/types.ts b/packages/ui/src/popover/types.ts index c3d95009888d1a..0e98f4eecb53ec 100644 --- a/packages/ui/src/popover/types.ts +++ b/packages/ui/src/popover/types.ts @@ -26,6 +26,7 @@ export interface PopupProps _Popover.Positioner.Props, | 'align' | 'alignOffset' + | 'anchor' | 'collisionAvoidance' | 'collisionBoundary' | 'collisionPadding' From ed91d0d042907f603cc40e109b2f526e090851cb Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Thu, 19 Mar 2026 13:58:17 +0100 Subject: [PATCH 24/71] Expose `openOnHover`, `delay`, and `closeDelay` props on Popover.Root MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pick hover-trigger props from Base UI's Root. This enables popover opening on hover with configurable timing — a capability the legacy Popover does not have natively. Made-with: Cursor --- packages/ui/src/popover/types.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/ui/src/popover/types.ts b/packages/ui/src/popover/types.ts index 0e98f4eecb53ec..0d6467aeae8624 100644 --- a/packages/ui/src/popover/types.ts +++ b/packages/ui/src/popover/types.ts @@ -5,7 +5,13 @@ import type { ComponentProps } from '../utils/types'; export interface RootProps extends Pick< _Popover.Root.Props, - 'open' | 'onOpenChange' | 'defaultOpen' | 'modal' + | 'open' + | 'onOpenChange' + | 'defaultOpen' + | 'modal' + | 'openOnHover' + | 'delay' + | 'closeDelay' > { /** * The content to be rendered inside the component. From b66cab59f7093ba903ef0fd0d0a121c0c342d2fb Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Thu, 19 Mar 2026 13:58:39 +0100 Subject: [PATCH 25/71] Add Popover.Backdrop sub-component Expose an opt-in backdrop overlay for modal popovers. Two variants: - 'default': semi-transparent dark overlay with fade transition - 'unstyled': transparent click-blocking layer without visual dimming Made-with: Cursor --- packages/ui/src/popover/backdrop.tsx | 31 ++++++++++++++++++++++++ packages/ui/src/popover/index.ts | 3 ++- packages/ui/src/popover/style.module.css | 24 ++++++++++++++++++ packages/ui/src/popover/types.ts | 13 ++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 packages/ui/src/popover/backdrop.tsx diff --git a/packages/ui/src/popover/backdrop.tsx b/packages/ui/src/popover/backdrop.tsx new file mode 100644 index 00000000000000..4fcda907775a7d --- /dev/null +++ b/packages/ui/src/popover/backdrop.tsx @@ -0,0 +1,31 @@ +import { Popover as _Popover } from '@base-ui/react/popover'; +import clsx from 'clsx'; +import { forwardRef } from '@wordpress/element'; +import styles from './style.module.css'; +import type { BackdropProps } from './types'; + +/** + * An overlay displayed beneath the popover. Typically used with `modal={true}` + * to provide a visual signal that interaction with the rest of the page is + * blocked. + */ +const Backdrop = forwardRef< HTMLDivElement, BackdropProps >( + function PopoverBackdrop( + { className, variant = 'default', ...props }, + ref + ) { + return ( + <_Popover.Backdrop + ref={ ref } + className={ clsx( + styles.backdrop, + variant === 'unstyled' && styles[ 'backdrop-unstyled' ], + className + ) } + { ...props } + /> + ); + } +); + +export { Backdrop }; diff --git a/packages/ui/src/popover/index.ts b/packages/ui/src/popover/index.ts index 472a9f1a681240..4c9f3469c99a5e 100644 --- a/packages/ui/src/popover/index.ts +++ b/packages/ui/src/popover/index.ts @@ -1,4 +1,5 @@ import { Arrow } from './arrow'; +import { Backdrop } from './backdrop'; import { Close } from './close'; import { Description } from './description'; import { Popup } from './popup'; @@ -6,4 +7,4 @@ import { Root } from './root'; import { Title } from './title'; import { Trigger } from './trigger'; -export { Arrow, Close, Description, Popup, Root, Title, Trigger }; +export { Arrow, Backdrop, Close, Description, Popup, Root, Title, Trigger }; diff --git a/packages/ui/src/popover/style.module.css b/packages/ui/src/popover/style.module.css index 6aeb039ad0118f..1aae15be3a3d1f 100644 --- a/packages/ui/src/popover/style.module.css +++ b/packages/ui/src/popover/style.module.css @@ -35,4 +35,28 @@ line-height: var(--wpds-font-line-height-sm); color: var(--wpds-color-fg-content-neutral-weak); } + + .backdrop { + position: fixed; + inset: 0; + z-index: var(--wp-ui-popover-z-index, initial); + background-color: rgba(0, 0, 0, 0.15); + + &[data-starting-style], + &[data-ending-style] { + opacity: 0; + } + + &[data-open] { + opacity: 1; + } + + @media not (prefers-reduced-motion) { + transition: opacity 0.15s ease-out; + } + } + + .backdrop-unstyled { + background-color: transparent; + } } diff --git a/packages/ui/src/popover/types.ts b/packages/ui/src/popover/types.ts index 0d6467aeae8624..8de6c6af82ae3e 100644 --- a/packages/ui/src/popover/types.ts +++ b/packages/ui/src/popover/types.ts @@ -111,3 +111,16 @@ export interface CloseProps extends ComponentProps< 'button' > { */ children?: ReactNode; } + +export interface BackdropProps extends ComponentProps< 'div' > { + /** + * The visual style variant of the backdrop. + * + * - `'default'` — semi-transparent dark overlay. + * - `'unstyled'` — fully transparent; blocks interaction without + * any visual dimming. + * + * @default 'default' + */ + variant?: 'default' | 'unstyled'; +} From 6f8dd1c9434247823fe11e4ed0850a5b7d6dab25 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Thu, 19 Mar 2026 13:58:55 +0100 Subject: [PATCH 26/71] Alias Base UI positioner CSS variables with --wp-ui-popover-* prefix Add aliased CSS custom properties in .positioner for all 7 Base UI Positioner variables (anchor size, available space, positioner size, transform origin). Update item-popup.module.css to use the aliased names with fallbacks for backward compatibility. Made-with: Cursor --- packages/ui/src/popover/style.module.css | 9 +++++++++ packages/ui/src/utils/css/item-popup.module.css | 6 +++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/ui/src/popover/style.module.css b/packages/ui/src/popover/style.module.css index 1aae15be3a3d1f..065d512bd0c49e 100644 --- a/packages/ui/src/popover/style.module.css +++ b/packages/ui/src/popover/style.module.css @@ -3,6 +3,15 @@ @layer wp-ui-components { .positioner { z-index: var(--wp-ui-popover-z-index, initial); + + /* Alias Base UI's Positioner CSS variables with a --wp-ui-popover-* prefix. */ + --wp-ui-popover-anchor-height: var(--anchor-height); + --wp-ui-popover-anchor-width: var(--anchor-width); + --wp-ui-popover-available-height: var(--available-height); + --wp-ui-popover-available-width: var(--available-width); + --wp-ui-popover-positioner-height: var(--positioner-height); + --wp-ui-popover-positioner-width: var(--positioner-width); + --wp-ui-popover-transform-origin: var(--transform-origin); } .popup { diff --git a/packages/ui/src/utils/css/item-popup.module.css b/packages/ui/src/utils/css/item-popup.module.css index 5de02f78bd2aef..24dfd42d17df07 100644 --- a/packages/ui/src/utils/css/item-popup.module.css +++ b/packages/ui/src/utils/css/item-popup.module.css @@ -9,9 +9,9 @@ display: grid; grid-template-rows: auto minmax(0, 1fr); grid-template-areas: "header" "main"; - max-height: min(var(--available-height), 480px, 60dvh); - min-width: var(--anchor-width); - max-width: var(--available-width); + max-height: min(var(--wp-ui-popover-available-height, var(--available-height)), 480px, 60dvh); + min-width: var(--wp-ui-popover-anchor-width, var(--anchor-width)); + max-width: var(--wp-ui-popover-available-width, var(--available-width)); border-radius: var(--wpds-border-radius-md); border: 1px solid var(--wpds-color-stroke-surface-neutral); box-shadow: var(--wpds-elevation-md); From 04a0330e83fc62325094c84a0ba87ff6f8260f8d Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Thu, 19 Mar 2026 14:00:15 +0100 Subject: [PATCH 27/71] Add Storybook stories for new Popover features Add 7 new stories demonstrating: - Anchor (4 anchor types: element, virtual, ref, callback) - Toolbar Variant (unstyled + custom CSS) - Viewport-Constrained Size (--wp-ui-popover-available-height/width) - onOpenChange Details (event reason logging) - Initial Focus (custom focus target via ref) - Trap Focus (modal='trap-focus' for constrainTabbing equivalent) - Hover Trigger (openOnHover with delay/closeDelay) Update Modal story to include Popover.Backdrop and register Backdrop in Storybook subcomponents. Made-with: Cursor --- .../ui/src/popover/stories/index.story.tsx | 446 ++++++++++++++++++ 1 file changed, 446 insertions(+) diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx index 5efeb49904daca..24812cb63e8beb 100644 --- a/packages/ui/src/popover/stories/index.story.tsx +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -11,6 +11,7 @@ const meta: Meta< typeof Popover.Root > = { 'Popover.Trigger': Popover.Trigger, 'Popover.Popup': Popover.Popup, 'Popover.Arrow': Popover.Arrow, + 'Popover.Backdrop': Popover.Backdrop, 'Popover.Title': Popover.Title, 'Popover.Description': Popover.Description, 'Popover.Close': Popover.Close, @@ -229,6 +230,9 @@ export const Controlled: Story = { * This is useful for complex popover content that requires user interaction, * such as forms. Try tabbing through the fields — focus stays inside the * popover until it is dismissed. + * + * Add `Popover.Backdrop` to display a semi-transparent overlay beneath the + * popover, signalling that the page is blocked. */ export const Modal: Story = { argTypes: { modal: { control: false } }, @@ -237,6 +241,7 @@ export const Modal: Story = { children: ( <> Edit Settings + Settings @@ -688,3 +693,444 @@ export const WithCustomZIndex: Story = { ), }, }; + +/** + * Use the `anchor` prop on `Popover.Popup` to position the popover against an + * arbitrary element instead of the built-in trigger. Base UI accepts four + * anchor types: + * + * 1. **Element** — a direct DOM element reference. + * 2. **VirtualElement** — an object with a `getBoundingClientRect()` method. + * 3. **RefObject** — a `React.RefObject` pointing to an element. + * 4. **Callback** — a function returning an Element or VirtualElement. + * + * This is the most-used pattern in Gutenberg: block popovers anchor to + * selected block elements, the link popover anchors to the text selection, and + * data views anchor to right-click positions. + */ +export const Anchor: Story = { + parameters: { controls: { disable: true } }, + render: function Render() { + const [ elementAnchor, setElementAnchor ] = + useState< HTMLElement | null >( null ); + const refAnchor = useRef< HTMLDivElement >( null ); + const callbackTarget = useRef< HTMLDivElement >( null ); + + const virtualAnchor = { + getBoundingClientRect: () => ( { + x: 400, + y: 50, + width: 0, + height: 0, + top: 50, + right: 400, + bottom: 50, + left: 400, + } ), + }; + + const anchorBoxStyle = { + padding: '8px 12px', + border: '2px dashed currentcolor', + borderRadius: 4, + fontSize: 12, + textAlign: 'center' as const, + }; + + const popupProps = { + animated: false as const, + collisionAvoidance: { + side: 'none' as const, + align: 'none' as const, + }, + }; + + return ( +
+ { /* 1. Element anchor */ } +
+
+ Element anchor +
+ + + + + Anchored to a DOM element + + + +
+ + { /* 2. VirtualElement anchor */ } +
+
+ VirtualElement anchor (fixed at 400, 50) +
+ + + + + Anchored to a virtual element + + + +
+ + { /* 3. RefObject anchor */ } +
+
+ RefObject anchor +
+ + + + + Anchored via useRef + + + +
+ + { /* 4. Callback anchor */ } +
+
+ Callback anchor +
+ + callbackTarget.current } + { ...popupProps } + > + + + Anchored via callback function + + + +
+
+ ); + }, +}; + +/** + * Use `variant="unstyled"` and custom inline styles to replicate a toolbar-like + * appearance: high-contrast border, no shadow, and a smaller border radius. + * + * A first-class `variant="toolbar"` may be added in the future if this pattern + * becomes widespread. + */ +export const ToolbarVariant: Story = { + args: { + children: ( + <> + Open Toolbar + + + + + + + + ), + }, +}; + +/** + * Base UI's Positioner exposes `--wp-ui-popover-available-height` and + * `--wp-ui-popover-available-width` CSS variables representing the space + * between the anchor and the viewport edge. Apply them as `max-height` / + * `max-width` on the popup content to constrain its size to the available + * space — this replaces the legacy Popover's `resize` prop. + * + * Open the popover and resize or scroll the container to see the popup shrink + * to fit. + */ +export const ViewportConstrainedSize: Story = { + name: 'Viewport-Constrained Size', + args: { defaultOpen: true }, + argTypes: { defaultOpen: { control: false } }, + render: function Render( { children: _children, ...args } ) { + return ( +
+ + Show Content + + Constrained + + This popup constrains its size using the + `--wp-ui-popover-available-height` and + `--wp-ui-popover-available-width` CSS variables + exposed by the positioner. + +
+

+ Scroll inside this popup — its max-height is + capped to the available viewport space. +

+
+
+
+
+
+ ); + }, +}; + +/** + * The `onOpenChange` callback on `Popover.Root` receives an `eventDetails` + * object with a `reason` field that describes why the popover is + * opening/closing. This replaces the legacy Popover's separate `onClose` and + * `onFocusOutside` callbacks: + * + * - `reason === 'escape-key'` — user pressed Escape (was `onClose`) + * - `reason === 'outside-press'` — user clicked outside (was `onClose`) + * - `reason === 'focus-out'` — focus moved outside (was `onFocusOutside`) + * + * Open the popover, then dismiss it in different ways to see the logged reason. + */ +export const OnOpenChangeDetails: Story = { + name: 'onOpenChange Details', + parameters: { controls: { disable: true } }, + render: function Render() { + const [ log, setLog ] = useState< string[] >( [] ); + + return ( +
+ { + setLog( ( prev ) => [ + ...prev.slice( -9 ), + `open=${ nextOpen } reason=${ eventDetails.reason }`, + ] ); + } } + > + Toggle + + + Event Log + + Dismiss this popover via Escape, click-outside, or + moving focus away. + + + + +
+					{ log.length
+						? log.join( '\n' )
+						: 'Interact with the popover to see events…' }
+				
+
+ ); + }, +}; + +/** + * Pass a ref to `initialFocus` on `Popover.Popup` to focus a specific element + * when the popover opens. This replaces the legacy Popover's `focusOnMount` + * prop. + * + * In this example, the Email field receives focus instead of the first + * focusable element (Name). + */ +export const InitialFocus: Story = { + parameters: { controls: { disable: true } }, + render: function Render() { + const emailRef = useRef< HTMLInputElement >( null ); + const nameId = useId(); + const emailId = useId(); + + return ( + + Open Form + + + Contact +
e.preventDefault() } + > + + + + +
+
+
+ ); + }, +}; + +/** + * Set `modal="trap-focus"` on `Popover.Root` to trap keyboard focus inside the + * popover without making it fully modal. Unlike `modal={true}`, this mode: + * + * - Traps Tab/Shift+Tab cycling within the popover + * - Does **not** lock page scroll + * - Does **not** block pointer interaction outside + * + * This replaces the legacy Popover's `constrainTabbing` prop. Try tabbing + * through the fields — focus stays inside — then click the button outside + * to verify that pointer interaction still works. + */ +export const TrapFocus: Story = { + argTypes: { modal: { control: false } }, + args: { + modal: 'trap-focus' as const, + }, + render: function Render( args ) { + return ( +
+ + Open + + + Trap Focus + + Tab cycles within this popover, but clicking outside + still works. + +
+ + +
+
+
+ + +
+ ); + }, +}; + +/** + * Set `openOnHover` on `Popover.Root` to open the popover when the trigger is + * hovered. The `delay` and `closeDelay` props control the timing (in ms). + * + * This is a capability the legacy Popover does not have natively — consumers + * would need to wire up `mouseenter`/`mouseleave` handlers manually. + */ +export const HoverTrigger: Story = { + argTypes: { + openOnHover: { control: false }, + }, + args: { + openOnHover: true, + delay: 200, + closeDelay: 150, + children: ( + <> + Hover me + + + Hover Popover + + This popover opens on hover with a 200ms delay and + closes 150ms after the pointer leaves. + + + + ), + }, +}; From 60542d89cbc291ba85934017ba7dc72834228cb1 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Thu, 19 Mar 2026 14:12:16 +0100 Subject: [PATCH 28/71] Fix: move openOnHover/delay/closeDelay to TriggerProps MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These props belong on Popover.Trigger, not Popover.Root — they are defined on Base UI's PopoverTrigger.Props. Picking them from Root.Props caused TS2344 build failures in CI. Update the HoverTrigger story to pass the props to Popover.Trigger. Made-with: Cursor --- .../ui/src/popover/stories/index.story.tsx | 25 ++++++++----------- packages/ui/src/popover/types.ts | 12 +++------ 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx index 24812cb63e8beb..7b679da05a3a85 100644 --- a/packages/ui/src/popover/stories/index.story.tsx +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -1105,23 +1105,20 @@ export const TrapFocus: Story = { }; /** - * Set `openOnHover` on `Popover.Root` to open the popover when the trigger is - * hovered. The `delay` and `closeDelay` props control the timing (in ms). + * Set `openOnHover` on `Popover.Trigger` to open the popover when the trigger + * is hovered. The `delay` and `closeDelay` props control the timing (in ms). * * This is a capability the legacy Popover does not have natively — consumers * would need to wire up `mouseenter`/`mouseleave` handlers manually. */ export const HoverTrigger: Story = { - argTypes: { - openOnHover: { control: false }, - }, - args: { - openOnHover: true, - delay: 200, - closeDelay: 150, - children: ( - <> - Hover me + parameters: { controls: { disable: true } }, + render: function Render( args ) { + return ( + + + Hover me + Hover Popover @@ -1130,7 +1127,7 @@ export const HoverTrigger: Story = { closes 150ms after the pointer leaves. - - ), + + ); }, }; diff --git a/packages/ui/src/popover/types.ts b/packages/ui/src/popover/types.ts index 8de6c6af82ae3e..45daab231e380d 100644 --- a/packages/ui/src/popover/types.ts +++ b/packages/ui/src/popover/types.ts @@ -5,13 +5,7 @@ import type { ComponentProps } from '../utils/types'; export interface RootProps extends Pick< _Popover.Root.Props, - | 'open' - | 'onOpenChange' - | 'defaultOpen' - | 'modal' - | 'openOnHover' - | 'delay' - | 'closeDelay' + 'open' | 'onOpenChange' | 'defaultOpen' | 'modal' > { /** * The content to be rendered inside the component. @@ -19,7 +13,9 @@ export interface RootProps children?: ReactNode; } -export interface TriggerProps extends ComponentProps< 'button' > { +export interface TriggerProps + extends ComponentProps< 'button' >, + Pick< _Popover.Trigger.Props, 'openOnHover' | 'delay' | 'closeDelay' > { /** * The content to be rendered inside the component. */ From 5784436c245e926f77ea9dd039a140d9730cece9 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Tue, 31 Mar 2026 15:31:53 +0200 Subject: [PATCH 29/71] Fix CHANGELOG.md placeholder PR number Replace #XXXXX with the actual PR number #76438. Made-with: Cursor --- packages/ui/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ui/CHANGELOG.md b/packages/ui/CHANGELOG.md index d32d102cdd391b..6843b97be41946 100644 --- a/packages/ui/CHANGELOG.md +++ b/packages/ui/CHANGELOG.md @@ -55,7 +55,7 @@ ### New Features -- Add `Popover` primitive ([#XXXXX](https://github.com/WordPress/gutenberg/pull/XXXXX)). +- Add `Popover` primitive ([#76438](https://github.com/WordPress/gutenberg/pull/76438)). - Add `Text` primitive with predefined typographic variants (`heading-2xl` through `heading-sm`, `body-xl` through `body-sm`) built on design tokens ([#75870](https://github.com/WordPress/gutenberg/pull/75870)). - Add `Card` and `CollapsibleCard` primitives ([#76252](https://github.com/WordPress/gutenberg/pull/76252)). - Add `Link` primitive ([#76013](https://github.com/WordPress/gutenberg/pull/76013)). From d77d6242733d8f4917397d1f4967dc3f36b839e6 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Tue, 31 Mar 2026 15:32:01 +0200 Subject: [PATCH 30/71] Remove built-in animation from Popover MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Per team consensus, this low-level primitive should not provide a default animation — different popover use cases (tooltips, dropdowns, dialogs, drawers) require different animations, and a unified approach needs design exploration first. Remove the `animated` prop, `dropdown-motion` class usage, the `DisabledAnimations` story, and the related test. Made-with: Cursor --- packages/ui/src/popover/popup.tsx | 9 +--- .../ui/src/popover/stories/index.story.tsx | 43 +++---------------- packages/ui/src/popover/test/index.test.tsx | 23 ---------- packages/ui/src/popover/types.ts | 20 --------- 4 files changed, 7 insertions(+), 88 deletions(-) diff --git a/packages/ui/src/popover/popup.tsx b/packages/ui/src/popover/popup.tsx index b685edab1646c8..b186e8942c8667 100644 --- a/packages/ui/src/popover/popup.tsx +++ b/packages/ui/src/popover/popup.tsx @@ -7,7 +7,6 @@ import { } from '@wordpress/theme'; import { unlock } from '../lock-unlock'; import resetStyles from '../utils/css/resets.module.css'; -import dropdownMotionStyles from '../utils/css/dropdown-motion.module.css'; import styles from './style.module.css'; import type { PopupProps } from './types'; @@ -23,7 +22,6 @@ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( align = 'center', alignOffset, anchor, - animated = true, children, className, collisionAvoidance, @@ -67,12 +65,7 @@ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( ref={ ref } initialFocus={ initialFocus } finalFocus={ finalFocus } - className={ clsx( - variant !== 'unstyled' && styles.popup, - animated && - variant !== 'unstyled' && - dropdownMotionStyles[ 'dropdown-motion' ] - ) } + className={ clsx( variant !== 'unstyled' && styles.popup ) } { ...props } > { children } diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx index 7b679da05a3a85..4f56f69d775768 100644 --- a/packages/ui/src/popover/stories/index.story.tsx +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -11,7 +11,6 @@ const meta: Meta< typeof Popover.Root > = { 'Popover.Trigger': Popover.Trigger, 'Popover.Popup': Popover.Popup, 'Popover.Arrow': Popover.Arrow, - 'Popover.Backdrop': Popover.Backdrop, 'Popover.Title': Popover.Title, 'Popover.Description': Popover.Description, 'Popover.Close': Popover.Close, @@ -84,7 +83,6 @@ export const Positioning: Story = { Edit Settings - Settings @@ -384,27 +378,6 @@ export const OverlayPlacement: Story = { }, }; -/** - * Set `animated` to `false` on `Popover.Popup` to disable the open/close - * transition. The popover will appear and disappear instantly. - */ -export const DisabledAnimations: Story = { - args: { - children: ( - <> - No Animation - - - Instant - - This popover opens and closes without animation. - - - - ), - }, -}; - /** * When `inline` is set to `true`, the popup renders in place within the DOM * hierarchy instead of being portaled to `document.body`. This can be @@ -738,7 +711,6 @@ export const Anchor: Story = { }; const popupProps = { - animated: false as const, collisionAvoidance: { side: 'none' as const, align: 'none' as const, @@ -862,8 +834,8 @@ export const ToolbarVariant: Story = { }; /** - * Base UI's Positioner exposes `--wp-ui-popover-available-height` and - * `--wp-ui-popover-available-width` CSS variables representing the space + * Base UI's Positioner exposes `--available-height` and + * `--available-width` CSS variables representing the space * between the anchor and the viewport edge. Apply them as `max-height` / * `max-width` on the popup content to constrain its size to the available * space — this replaces the legacy Popover's `resize` prop. @@ -890,19 +862,16 @@ export const ViewportConstrainedSize: Story = { Constrained This popup constrains its size using the - `--wp-ui-popover-available-height` and - `--wp-ui-popover-available-width` CSS variables - exposed by the positioner. + `--available-height` and `--available-width` CSS + variables exposed by the positioner.

diff --git a/packages/ui/src/popover/test/index.test.tsx b/packages/ui/src/popover/test/index.test.tsx index b080465806fe1b..ac025bf9583c18 100644 --- a/packages/ui/src/popover/test/index.test.tsx +++ b/packages/ui/src/popover/test/index.test.tsx @@ -387,29 +387,6 @@ describe( 'Popover', () => { } ); } ); - describe( 'animated', () => { - it( 'should render correctly when animated is false', async () => { - const user = userEvent.setup(); - - render( - - Open - - No animation content - - - ); - - await user.click( screen.getByRole( 'button', { name: 'Open' } ) ); - - await waitFor( () => { - expect( - screen.getByText( 'No animation content' ) - ).toBeInTheDocument(); - } ); - } ); - } ); - describe( 'inline', () => { it( 'should render without a portal when inline is true', async () => { const user = userEvent.setup(); diff --git a/packages/ui/src/popover/types.ts b/packages/ui/src/popover/types.ts index 45daab231e380d..d6fa5249562af3 100644 --- a/packages/ui/src/popover/types.ts +++ b/packages/ui/src/popover/types.ts @@ -37,13 +37,6 @@ export interface PopupProps | 'sticky' >, Pick< _Popover.Popup.Props, 'initialFocus' | 'finalFocus' > { - /** - * Whether the popup uses open/close animations. - * - * @default true - */ - animated?: boolean; - /** * The content to be rendered inside the component. */ @@ -107,16 +100,3 @@ export interface CloseProps extends ComponentProps< 'button' > { */ children?: ReactNode; } - -export interface BackdropProps extends ComponentProps< 'div' > { - /** - * The visual style variant of the backdrop. - * - * - `'default'` — semi-transparent dark overlay. - * - `'unstyled'` — fully transparent; blocks interaction without - * any visual dimming. - * - * @default 'default' - */ - variant?: 'default' | 'unstyled'; -} From 78e0d0d4cbe9e9233b8e6087b619faf037356646 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Tue, 31 Mar 2026 15:32:15 +0200 Subject: [PATCH 31/71] Remove CSS variable aliases, use Base UI variables directly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Per team consensus, don't alias Base UI's Positioner CSS variables with a --wp-ui-popover-* prefix. Use the upstream names (--available-height, --anchor-width, etc.) directly — gating them adds maintenance overhead with no practical benefit since data attributes already can't be gated. Retain --wp-ui-popover-z-index which is our own custom variable. Made-with: Cursor --- packages/ui/src/popover/style.module.css | 33 ------------------- .../ui/src/utils/css/item-popup.module.css | 6 ++-- 2 files changed, 3 insertions(+), 36 deletions(-) diff --git a/packages/ui/src/popover/style.module.css b/packages/ui/src/popover/style.module.css index 065d512bd0c49e..6aeb039ad0118f 100644 --- a/packages/ui/src/popover/style.module.css +++ b/packages/ui/src/popover/style.module.css @@ -3,15 +3,6 @@ @layer wp-ui-components { .positioner { z-index: var(--wp-ui-popover-z-index, initial); - - /* Alias Base UI's Positioner CSS variables with a --wp-ui-popover-* prefix. */ - --wp-ui-popover-anchor-height: var(--anchor-height); - --wp-ui-popover-anchor-width: var(--anchor-width); - --wp-ui-popover-available-height: var(--available-height); - --wp-ui-popover-available-width: var(--available-width); - --wp-ui-popover-positioner-height: var(--positioner-height); - --wp-ui-popover-positioner-width: var(--positioner-width); - --wp-ui-popover-transform-origin: var(--transform-origin); } .popup { @@ -44,28 +35,4 @@ line-height: var(--wpds-font-line-height-sm); color: var(--wpds-color-fg-content-neutral-weak); } - - .backdrop { - position: fixed; - inset: 0; - z-index: var(--wp-ui-popover-z-index, initial); - background-color: rgba(0, 0, 0, 0.15); - - &[data-starting-style], - &[data-ending-style] { - opacity: 0; - } - - &[data-open] { - opacity: 1; - } - - @media not (prefers-reduced-motion) { - transition: opacity 0.15s ease-out; - } - } - - .backdrop-unstyled { - background-color: transparent; - } } diff --git a/packages/ui/src/utils/css/item-popup.module.css b/packages/ui/src/utils/css/item-popup.module.css index 24dfd42d17df07..5de02f78bd2aef 100644 --- a/packages/ui/src/utils/css/item-popup.module.css +++ b/packages/ui/src/utils/css/item-popup.module.css @@ -9,9 +9,9 @@ display: grid; grid-template-rows: auto minmax(0, 1fr); grid-template-areas: "header" "main"; - max-height: min(var(--wp-ui-popover-available-height, var(--available-height)), 480px, 60dvh); - min-width: var(--wp-ui-popover-anchor-width, var(--anchor-width)); - max-width: var(--wp-ui-popover-available-width, var(--available-width)); + max-height: min(var(--available-height), 480px, 60dvh); + min-width: var(--anchor-width); + max-width: var(--available-width); border-radius: var(--wpds-border-radius-md); border: 1px solid var(--wpds-color-stroke-surface-neutral); box-shadow: var(--wpds-elevation-md); From ef8359ae58c89d002f996c9d50c93a9e8b4e8e9f Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Tue, 31 Mar 2026 15:32:23 +0200 Subject: [PATCH 32/71] Remove Popover.Backdrop sub-component Per review feedback, the standalone Backdrop will be removed and its functionality merged into Popover.Popup (via a showBackdrop prop) in a follow-up. Base UI already renders an invisible internal backdrop for modal popovers to capture clicks, so the visual overlay is opt-in. Made-with: Cursor --- packages/ui/src/popover/backdrop.tsx | 31 ---------------------------- packages/ui/src/popover/index.ts | 3 +-- 2 files changed, 1 insertion(+), 33 deletions(-) delete mode 100644 packages/ui/src/popover/backdrop.tsx diff --git a/packages/ui/src/popover/backdrop.tsx b/packages/ui/src/popover/backdrop.tsx deleted file mode 100644 index 4fcda907775a7d..00000000000000 --- a/packages/ui/src/popover/backdrop.tsx +++ /dev/null @@ -1,31 +0,0 @@ -import { Popover as _Popover } from '@base-ui/react/popover'; -import clsx from 'clsx'; -import { forwardRef } from '@wordpress/element'; -import styles from './style.module.css'; -import type { BackdropProps } from './types'; - -/** - * An overlay displayed beneath the popover. Typically used with `modal={true}` - * to provide a visual signal that interaction with the rest of the page is - * blocked. - */ -const Backdrop = forwardRef< HTMLDivElement, BackdropProps >( - function PopoverBackdrop( - { className, variant = 'default', ...props }, - ref - ) { - return ( - <_Popover.Backdrop - ref={ ref } - className={ clsx( - styles.backdrop, - variant === 'unstyled' && styles[ 'backdrop-unstyled' ], - className - ) } - { ...props } - /> - ); - } -); - -export { Backdrop }; diff --git a/packages/ui/src/popover/index.ts b/packages/ui/src/popover/index.ts index 4c9f3469c99a5e..472a9f1a681240 100644 --- a/packages/ui/src/popover/index.ts +++ b/packages/ui/src/popover/index.ts @@ -1,5 +1,4 @@ import { Arrow } from './arrow'; -import { Backdrop } from './backdrop'; import { Close } from './close'; import { Description } from './description'; import { Popup } from './popup'; @@ -7,4 +6,4 @@ import { Root } from './root'; import { Title } from './title'; import { Trigger } from './trigger'; -export { Arrow, Backdrop, Close, Description, Popup, Root, Title, Trigger }; +export { Arrow, Close, Description, Popup, Root, Title, Trigger }; From 7a1d66f6e79a8bc0e37f0266c460dd7bb040d4a8 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Tue, 31 Mar 2026 15:35:09 +0200 Subject: [PATCH 33/71] Add `backdrop` prop to Popover.Popup Render the backdrop inside the Popup component (matching the Dialog pattern) instead of exposing a standalone Backdrop sub-component. When `backdrop` is true, a semi-transparent overlay renders behind the popover inside the portal. Typically used with `modal` to signal that page interaction is blocked. Made-with: Cursor --- packages/ui/src/popover/popup.tsx | 7 +++++++ packages/ui/src/popover/stories/index.story.tsx | 5 ++++- packages/ui/src/popover/style.module.css | 7 +++++++ packages/ui/src/popover/types.ts | 10 ++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/ui/src/popover/popup.tsx b/packages/ui/src/popover/popup.tsx index b186e8942c8667..ac39b5a0b9a152 100644 --- a/packages/ui/src/popover/popup.tsx +++ b/packages/ui/src/popover/popup.tsx @@ -22,6 +22,7 @@ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( align = 'center', alignOffset, anchor, + backdrop = false, children, className, collisionAvoidance, @@ -42,6 +43,10 @@ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( ) { const inlineContainerRef = useRef< HTMLSpanElement >( null ); + const backdropElement = backdrop ? ( + <_Popover.Backdrop className={ styles.backdrop } /> + ) : null; + const positioner = ( <_Popover.Positioner align={ align } @@ -82,6 +87,7 @@ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( style={ { display: 'contents' } } /> <_Popover.Portal container={ inlineContainerRef }> + { backdropElement } { positioner } @@ -90,6 +96,7 @@ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( return ( <_Popover.Portal container={ container }> + { backdropElement } { positioner } ); diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx index 4f56f69d775768..92b54df283d646 100644 --- a/packages/ui/src/popover/stories/index.story.tsx +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -228,6 +228,9 @@ export const Controlled: Story = { * This is useful for complex popover content that requires user interaction, * such as forms. Try tabbing through the fields — focus stays inside the * popover until it is dismissed. + * + * Pass `backdrop` to `Popover.Popup` to display a semi-transparent overlay + * beneath the popover, signalling that the page is blocked. */ export const Modal: Story = { argTypes: { modal: { control: false } }, @@ -236,7 +239,7 @@ export const Modal: Story = { children: ( <> Edit Settings - + Settings

, Pick< _Popover.Popup.Props, 'initialFocus' | 'finalFocus' > { + /** + * Whether to render a backdrop overlay behind the popover. + * + * Typically used with `modal` to signal that interaction with the rest + * of the page is blocked. The backdrop is a semi-transparent dark overlay. + * + * @default false + */ + backdrop?: boolean; + /** * The content to be rendered inside the component. */ From ced42333ca3ac18246f3139fe8199791bdc9ab9b Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Tue, 31 Mar 2026 15:54:56 +0200 Subject: [PATCH 34/71] Fix Anchor story VirtualElement positioning in docs mode Derive the virtual anchor position from its label div's actual bounding rect instead of hard-coded viewport coordinates. This prevents the popover from rendering at a fixed position that overlaps other stories in Storybook's docs page. Made-with: Cursor --- .../ui/src/popover/stories/index.story.tsx | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx index 92b54df283d646..8fee225aa4c985 100644 --- a/packages/ui/src/popover/stories/index.story.tsx +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -690,19 +690,13 @@ export const Anchor: Story = { const [ elementAnchor, setElementAnchor ] = useState< HTMLElement | null >( null ); const refAnchor = useRef< HTMLDivElement >( null ); + const virtualAnchorLabel = useRef< HTMLDivElement >( null ); const callbackTarget = useRef< HTMLDivElement >( null ); const virtualAnchor = { - getBoundingClientRect: () => ( { - x: 400, - y: 50, - width: 0, - height: 0, - top: 50, - right: 400, - bottom: 50, - left: 400, - } ), + getBoundingClientRect: () => + virtualAnchorLabel.current?.getBoundingClientRect() ?? + new DOMRect(), }; const anchorBoxStyle = { @@ -749,8 +743,8 @@ export const Anchor: Story = { { /* 2. VirtualElement anchor */ }
-
- VirtualElement anchor (fixed at 400, 50) +
+ VirtualElement anchor
Date: Tue, 31 Mar 2026 16:08:18 +0200 Subject: [PATCH 35/71] Popover.Popup: deprioritize close button for initial focus Default initialFocus now skips elements marked with data-wp-ui-popover-close, focusing the first tabbable content element instead. Falls back to the close button when it is the only tabbable element. Mirrors the Dialog.Popup approach from #76910. Made-with: Cursor --- packages/ui/CHANGELOG.md | 1 + packages/ui/src/popover/close.tsx | 8 +- packages/ui/src/popover/popup.tsx | 49 +++++++++++- packages/ui/src/popover/test/index.test.tsx | 85 ++++++++++++++++++++- 4 files changed, 136 insertions(+), 7 deletions(-) diff --git a/packages/ui/CHANGELOG.md b/packages/ui/CHANGELOG.md index 6843b97be41946..45216fb3a97cb0 100644 --- a/packages/ui/CHANGELOG.md +++ b/packages/ui/CHANGELOG.md @@ -41,6 +41,7 @@ - `Dialog.Root`: expose `disablePointerDismissal` prop ([#76847](https://github.com/WordPress/gutenberg/pull/76847)). - `Dialog.Popup`: Default `initialFocus` now deprioritizes the close icon, focusing the first tabbable content element instead (following WAI-ARIA APG guidance) ([#76910](https://github.com/WordPress/gutenberg/pull/76910)). +- `Popover.Popup`: Default `initialFocus` now deprioritizes the close button, focusing the first tabbable content element instead (following WAI-ARIA APG guidance) ([#76438](https://github.com/WordPress/gutenberg/pull/76438)). ### Internal diff --git a/packages/ui/src/popover/close.tsx b/packages/ui/src/popover/close.tsx index 39cafa1870ccf8..f266a7bc0769e0 100644 --- a/packages/ui/src/popover/close.tsx +++ b/packages/ui/src/popover/close.tsx @@ -7,7 +7,13 @@ import type { CloseProps } from './types'; */ const Close = forwardRef< HTMLButtonElement, CloseProps >( function PopoverClose( props, ref ) { - return <_Popover.Close ref={ ref } { ...props } />; + return ( + <_Popover.Close + ref={ ref } + data-wp-ui-popover-close="" + { ...props } + /> + ); } ); diff --git a/packages/ui/src/popover/popup.tsx b/packages/ui/src/popover/popup.tsx index ac39b5a0b9a152..0dd6ad81bccbde 100644 --- a/packages/ui/src/popover/popup.tsx +++ b/packages/ui/src/popover/popup.tsx @@ -1,10 +1,12 @@ import { Popover as _Popover } from '@base-ui/react/popover'; import clsx from 'clsx'; -import { forwardRef, useRef } from '@wordpress/element'; +import { forwardRef, useMemo, useRef } from '@wordpress/element'; +import { useMergeRefs } from '@wordpress/compose'; import { type ThemeProvider as ThemeProviderType, privateApis as themePrivateApis, } from '@wordpress/theme'; +import { tabbable } from 'tabbable'; import { unlock } from '../lock-unlock'; import resetStyles from '../utils/css/resets.module.css'; import styles from './style.module.css'; @@ -13,6 +15,21 @@ import type { PopupProps } from './types'; const ThemeProvider: typeof ThemeProviderType = unlock( themePrivateApis ).ThemeProvider; +const CLOSE_ATTR = 'data-wp-ui-popover-close'; + +/** + * Options matching Base UI's internal tabbable configuration. + * @see https://github.com/mui/base-ui FloatingFocusManager utils/tabbable.ts + */ +const getTabbableOptions = () => ( { + getShadowRoot: true, + displayCheck: + typeof ResizeObserver === 'function' && + ResizeObserver.toString().includes( '[native code]' ) + ? ( 'full' as const ) + : ( 'none' as const ), +} ); + /** * Renders the popover popup element that contains the popover content. * By default, uses a portal to render outside the DOM hierarchy. @@ -42,6 +59,32 @@ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( ref ) { const inlineContainerRef = useRef< HTMLSpanElement >( null ); + const popupRef = useRef< HTMLDivElement >( null ); + const mergedRef = useMergeRefs( [ ref, popupRef ] ); + + const resolvedInitialFocus = useMemo( () => { + if ( initialFocus !== undefined && initialFocus !== true ) { + return initialFocus; + } + return ( interactionType: string ): HTMLElement | boolean | null => { + if ( interactionType === 'touch' ) { + return popupRef.current ?? true; + } + const popup = popupRef.current; + if ( popup ) { + const tabbables = tabbable( popup, getTabbableOptions() ); + for ( const el of tabbables ) { + if ( + el instanceof HTMLElement && + ! el.hasAttribute( CLOSE_ATTR ) + ) { + return el; + } + } + } + return true; + }; + }, [ initialFocus ] ); const backdropElement = backdrop ? ( <_Popover.Backdrop className={ styles.backdrop } /> @@ -67,8 +110,8 @@ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( > <_Popover.Popup - ref={ ref } - initialFocus={ initialFocus } + ref={ mergedRef } + initialFocus={ resolvedInitialFocus } finalFocus={ finalFocus } className={ clsx( variant !== 'unstyled' && styles.popup ) } { ...props } diff --git a/packages/ui/src/popover/test/index.test.tsx b/packages/ui/src/popover/test/index.test.tsx index ac025bf9583c18..23e47267dd104a 100644 --- a/packages/ui/src/popover/test/index.test.tsx +++ b/packages/ui/src/popover/test/index.test.tsx @@ -471,6 +471,52 @@ describe( 'Popover', () => { } ); describe( 'initialFocus', () => { + it( 'should focus the first content element, skipping the close button', async () => { + const user = userEvent.setup(); + + render( + + Open + + Close + + + + ); + + await user.click( screen.getByRole( 'button', { name: 'Open' } ) ); + + await waitFor( () => { + expect( + screen.getByRole( 'button', { + name: 'Content Button', + } ) + ).toHaveFocus(); + } ); + } ); + + it( 'should fall back to the close button when it is the only tabbable element', async () => { + const user = userEvent.setup(); + + render( + + Open + + Close +

No tabbable content here

+
+
+ ); + + await user.click( screen.getByRole( 'button', { name: 'Open' } ) ); + + await waitFor( () => { + expect( + screen.getByRole( 'button', { name: 'Close' } ) + ).toHaveFocus(); + } ); + } ); + it( 'should not move focus when initialFocus is false', async () => { const user = userEvent.setup(); @@ -478,7 +524,8 @@ describe( 'Popover', () => { Open - + Close + ); @@ -488,11 +535,43 @@ describe( 'Popover', () => { await waitFor( () => { expect( - screen.getByPlaceholderText( 'Input' ) + screen.getByRole( 'button', { + name: 'Content Button', + } ) + ).toBeInTheDocument(); + } ); + + expect( + screen.getByRole( 'button', { name: 'Content Button' } ) + ).not.toHaveFocus(); + expect( + screen.getByRole( 'button', { name: 'Close' } ) + ).not.toHaveFocus(); + } ); + + it( 'should use a custom initialFocus callback as-is', async () => { + const user = userEvent.setup(); + const customFocus = jest.fn( () => false as const ); + + render( + + Open + + Close + + + + ); + + await user.click( screen.getByRole( 'button', { name: 'Open' } ) ); + + await waitFor( () => { + expect( + screen.getByText( 'Content Button' ) ).toBeInTheDocument(); } ); - expect( trigger ).toHaveFocus(); + expect( customFocus ).toHaveBeenCalled(); } ); } ); } ); From 9ab9ce9d8bb5a50bdb9970be603c0e65ce77807a Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Tue, 31 Mar 2026 16:20:45 +0200 Subject: [PATCH 36/71] Extract useDeprioritizedInitialFocus into shared utility Move the initial-focus deprioritization logic (skip close button, fall back gracefully) into a reusable hook at utils/use-deprioritized-initial-focus. Popover.Popup now consumes this hook. The hook is designed to be reused by Dialog and other overlay components in a follow-up. Made-with: Cursor --- packages/ui/src/popover/popup.tsx | 51 ++++++------------------------- 1 file changed, 9 insertions(+), 42 deletions(-) diff --git a/packages/ui/src/popover/popup.tsx b/packages/ui/src/popover/popup.tsx index 0dd6ad81bccbde..fb56893c32f046 100644 --- a/packages/ui/src/popover/popup.tsx +++ b/packages/ui/src/popover/popup.tsx @@ -1,14 +1,14 @@ import { Popover as _Popover } from '@base-ui/react/popover'; import clsx from 'clsx'; -import { forwardRef, useMemo, useRef } from '@wordpress/element'; +import { forwardRef, useRef } from '@wordpress/element'; import { useMergeRefs } from '@wordpress/compose'; import { type ThemeProvider as ThemeProviderType, privateApis as themePrivateApis, } from '@wordpress/theme'; -import { tabbable } from 'tabbable'; import { unlock } from '../lock-unlock'; import resetStyles from '../utils/css/resets.module.css'; +import { useDeprioritizedInitialFocus } from '../utils/use-deprioritized-initial-focus'; import styles from './style.module.css'; import type { PopupProps } from './types'; @@ -17,19 +17,6 @@ const ThemeProvider: typeof ThemeProviderType = const CLOSE_ATTR = 'data-wp-ui-popover-close'; -/** - * Options matching Base UI's internal tabbable configuration. - * @see https://github.com/mui/base-ui FloatingFocusManager utils/tabbable.ts - */ -const getTabbableOptions = () => ( { - getShadowRoot: true, - displayCheck: - typeof ResizeObserver === 'function' && - ResizeObserver.toString().includes( '[native code]' ) - ? ( 'full' as const ) - : ( 'none' as const ), -} ); - /** * Renders the popover popup element that contains the popover content. * By default, uses a portal to render outside the DOM hierarchy. @@ -59,32 +46,12 @@ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( ref ) { const inlineContainerRef = useRef< HTMLSpanElement >( null ); - const popupRef = useRef< HTMLDivElement >( null ); - const mergedRef = useMergeRefs( [ ref, popupRef ] ); - - const resolvedInitialFocus = useMemo( () => { - if ( initialFocus !== undefined && initialFocus !== true ) { - return initialFocus; - } - return ( interactionType: string ): HTMLElement | boolean | null => { - if ( interactionType === 'touch' ) { - return popupRef.current ?? true; - } - const popup = popupRef.current; - if ( popup ) { - const tabbables = tabbable( popup, getTabbableOptions() ); - for ( const el of tabbables ) { - if ( - el instanceof HTMLElement && - ! el.hasAttribute( CLOSE_ATTR ) - ) { - return el; - } - } - } - return true; - }; - }, [ initialFocus ] ); + const { resolvedInitialFocus, popupRef } = + useDeprioritizedInitialFocus( { + initialFocus, + deprioritizedAttribute: CLOSE_ATTR, + } ); + const mergedPopupRef = useMergeRefs( [ ref, popupRef ] ); const backdropElement = backdrop ? ( <_Popover.Backdrop className={ styles.backdrop } /> @@ -110,7 +77,7 @@ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( > <_Popover.Popup - ref={ mergedRef } + ref={ mergedPopupRef } initialFocus={ resolvedInitialFocus } finalFocus={ finalFocus } className={ clsx( variant !== 'unstyled' && styles.popup ) } From e1d3df944725190696a23f1a21082e8ac4ede730 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Tue, 31 Mar 2026 16:33:16 +0200 Subject: [PATCH 37/71] Update CHANGELOG: move Popover to Unreleased, add shared hook entry Made-with: Cursor --- packages/ui/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ui/CHANGELOG.md b/packages/ui/CHANGELOG.md index 45216fb3a97cb0..b5fc9401bb82c1 100644 --- a/packages/ui/CHANGELOG.md +++ b/packages/ui/CHANGELOG.md @@ -29,6 +29,7 @@ - Add `AlertDialog` primitive ([#76847](https://github.com/WordPress/gutenberg/pull/76847)). - Add `InputControl` component ([#76653](https://github.com/WordPress/gutenberg/pull/76653)). +- Add `Popover` primitive ([#76438](https://github.com/WordPress/gutenberg/pull/76438)). - `Dialog`: Expose `initialFocus` and `finalFocus` props on `Dialog.Popup` for custom focus management ([#76860](https://github.com/WordPress/gutenberg/pull/76860)). - `CollapsibleCard`: Add `HeaderDescription` subcomponent for supplementary header content with `aria-describedby` relationship ([#76867](https://github.com/WordPress/gutenberg/pull/76867)). @@ -56,7 +57,6 @@ ### New Features -- Add `Popover` primitive ([#76438](https://github.com/WordPress/gutenberg/pull/76438)). - Add `Text` primitive with predefined typographic variants (`heading-2xl` through `heading-sm`, `body-xl` through `body-sm`) built on design tokens ([#75870](https://github.com/WordPress/gutenberg/pull/75870)). - Add `Card` and `CollapsibleCard` primitives ([#76252](https://github.com/WordPress/gutenberg/pull/76252)). - Add `Link` primitive ([#76013](https://github.com/WordPress/gutenberg/pull/76013)). From 04ff1a862e9948137f47b85f8a28cee6d02d0aed Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Tue, 31 Mar 2026 16:50:23 +0200 Subject: [PATCH 38/71] Popover: use Text for Title/Description, require Title for accessibility - Popover.Title now renders via Text with the heading-md variant, and Popover.Description via Text with the body-md variant. Typography CSS removed from style.module.css in favor of the Text component's tokens. - Add PopoverValidationProvider (dev-only) that throws when Popover.Title is missing or empty, mirroring Dialog's validation pattern. - Add Popover.Title to all existing tests and stories. Stories that have no visible heading use VisuallyHidden inside Popover.Title. Made-with: Cursor --- packages/ui/src/popover/context.tsx | 100 ++++++++++ packages/ui/src/popover/description.tsx | 13 +- packages/ui/src/popover/popup.tsx | 5 +- .../ui/src/popover/stories/index.story.tsx | 22 ++- packages/ui/src/popover/style.module.css | 6 - packages/ui/src/popover/test/index.test.tsx | 174 ++++++++++++++++-- packages/ui/src/popover/title.tsx | 27 ++- 7 files changed, 316 insertions(+), 31 deletions(-) create mode 100644 packages/ui/src/popover/context.tsx diff --git a/packages/ui/src/popover/context.tsx b/packages/ui/src/popover/context.tsx new file mode 100644 index 00000000000000..33c22637df2e11 --- /dev/null +++ b/packages/ui/src/popover/context.tsx @@ -0,0 +1,100 @@ +import { + createContext, + useCallback, + useContext, + useEffect, + useMemo, + useRef, +} from '@wordpress/element'; + +/** + * Whether validation is enabled. This is a build-time constant that allows + * bundlers to tree-shake all validation code in production builds. + */ +const VALIDATION_ENABLED = process.env.NODE_ENV !== 'production'; + +type PopoverValidationContextType = { + registerTitle: ( element: HTMLElement | null ) => void; +}; + +const PopoverValidationContext = VALIDATION_ENABLED + ? createContext< PopoverValidationContextType | null >( null ) + : ( null as unknown as React.Context< PopoverValidationContextType | null > ); + +function usePopoverValidationContextDev() { + return useContext( PopoverValidationContext ); +} + +function usePopoverValidationContextProd() { + return null; +} + +/** + * Hook to access the popover validation context. + * Returns null in production or if not within a Popover.Popup. + */ +export const usePopoverValidationContext = VALIDATION_ENABLED + ? usePopoverValidationContextDev + : usePopoverValidationContextProd; + +/** + * Development-only provider that tracks whether Popover.Title is rendered. + */ +function PopoverValidationProviderDev( { + children, +}: { + children: React.ReactNode; +} ) { + const titleElementRef = useRef< HTMLElement | null >( null ); + + const registerTitle = useCallback( ( element: HTMLElement | null ) => { + titleElementRef.current = element; + }, [] ); + + const contextValue = useMemo( + () => ( { registerTitle } ), + [ registerTitle ] + ); + + useEffect( () => { + const titleElement = titleElementRef.current; + + if ( ! titleElement ) { + throw new Error( + 'Popover: Missing . ' + + 'For accessibility, every popover requires a title. ' + + 'If needed, the title can be visually hidden but must not be omitted.' + ); + } + + const textContent = titleElement.textContent?.trim(); + if ( ! textContent ) { + throw new Error( + 'Popover: cannot be empty. ' + + 'Provide meaningful text content for the popover title.' + ); + } + }, [] ); + + return ( + + { children } + + ); +} + +function PopoverValidationProviderProd( { + children, +}: { + children: React.ReactNode; +} ) { + return <>{ children }; +} + +/** + * Provider component that validates Popover.Title presence in development mode. + * In production, this component is a no-op and just renders children. + */ +export const PopoverValidationProvider = VALIDATION_ENABLED + ? PopoverValidationProviderDev + : PopoverValidationProviderProd; diff --git a/packages/ui/src/popover/description.tsx b/packages/ui/src/popover/description.tsx index bcd0c1d23c8f5a..acb13e304b71cb 100644 --- a/packages/ui/src/popover/description.tsx +++ b/packages/ui/src/popover/description.tsx @@ -1,6 +1,7 @@ import { Popover as _Popover } from '@base-ui/react/popover'; import clsx from 'clsx'; import { forwardRef } from '@wordpress/element'; +import { Text } from '../text'; import styles from './style.module.css'; import type { DescriptionProps } from './types'; @@ -8,13 +9,15 @@ import type { DescriptionProps } from './types'; * Renders a paragraph that describes the popover content for accessibility. */ const Description = forwardRef< HTMLParagraphElement, DescriptionProps >( - function PopoverDescription( { className, ...props }, ref ) { + function PopoverDescription( { className, children, ...props }, ref ) { return ( - <_Popover.Description - ref={ ref } + } className={ clsx( styles.description, className ) } - { ...props } - /> + > + { children } + ); } ); diff --git a/packages/ui/src/popover/popup.tsx b/packages/ui/src/popover/popup.tsx index fb56893c32f046..a06460118ddee8 100644 --- a/packages/ui/src/popover/popup.tsx +++ b/packages/ui/src/popover/popup.tsx @@ -9,6 +9,7 @@ import { import { unlock } from '../lock-unlock'; import resetStyles from '../utils/css/resets.module.css'; import { useDeprioritizedInitialFocus } from '../utils/use-deprioritized-initial-focus'; +import { PopoverValidationProvider } from './context'; import styles from './style.module.css'; import type { PopupProps } from './types'; @@ -83,7 +84,9 @@ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( className={ clsx( variant !== 'unstyled' && styles.popup ) } { ...props } > - { children } + + { children } + diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx index 8fee225aa4c985..a9651f5995d267 100644 --- a/packages/ui/src/popover/stories/index.story.tsx +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -1,7 +1,7 @@ import type { Meta, StoryObj } from '@storybook/react-vite'; import { useId, useRef, useState } from '@wordpress/element'; import { SlotFillProvider, Slot } from '@wordpress/components'; -import { Popover } from '../..'; +import { Popover, VisuallyHidden } from '../..'; import { GenericIframe, useMeasure } from './utils'; const meta: Meta< typeof Popover.Root > = { @@ -88,6 +88,11 @@ export const Positioning: Story = { align: 'none', } } > + + + { side } / { align } + + { side } side / { align } align @@ -733,6 +738,9 @@ export const Anchor: Story = { anchor={ elementAnchor ?? undefined } { ...popupProps } > + + Element anchor + Anchored to a DOM element @@ -751,6 +759,9 @@ export const Anchor: Story = { anchor={ virtualAnchor } { ...popupProps } > + + Virtual anchor + Anchored to a virtual element @@ -766,6 +777,9 @@ export const Anchor: Story = {
+ + Ref anchor + Anchored via useRef @@ -784,6 +798,9 @@ export const Anchor: Story = { anchor={ () => callbackTarget.current } { ...popupProps } > + + Callback anchor + Anchored via callback function @@ -820,6 +837,9 @@ export const ToolbarVariant: Story = { fontSize: 13, } } > + + Formatting + diff --git a/packages/ui/src/popover/style.module.css b/packages/ui/src/popover/style.module.css index f147c6906ddaae..12a1f44af65fdc 100644 --- a/packages/ui/src/popover/style.module.css +++ b/packages/ui/src/popover/style.module.css @@ -23,16 +23,10 @@ .title { margin: 0 0 var(--wpds-dimension-gap-xs); - font-size: var(--wpds-font-size-md); - font-weight: var(--wpds-font-weight-medium); - line-height: var(--wpds-font-line-height-md); - color: var(--wpds-color-fg-content-neutral); } .description { margin: 0; - font-size: var(--wpds-font-size-sm); - line-height: var(--wpds-font-line-height-sm); color: var(--wpds-color-fg-content-neutral-weak); } diff --git a/packages/ui/src/popover/test/index.test.tsx b/packages/ui/src/popover/test/index.test.tsx index 23e47267dd104a..942d744ac5d84d 100644 --- a/packages/ui/src/popover/test/index.test.tsx +++ b/packages/ui/src/popover/test/index.test.tsx @@ -1,6 +1,6 @@ import { render, screen, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; -import { createRef, useState } from '@wordpress/element'; +import { Component, createRef, useState } from '@wordpress/element'; import * as Popover from '../index'; describe( 'Popover', () => { @@ -10,7 +10,9 @@ describe( 'Popover', () => { render( Open - Content + + Title + ); expect( ref.current ).toBeInstanceOf( HTMLButtonElement ); @@ -23,7 +25,9 @@ describe( 'Popover', () => { render( Open - Content + + Title + ); @@ -42,8 +46,8 @@ describe( 'Popover', () => { Open + Title - Content ); @@ -83,6 +87,7 @@ describe( 'Popover', () => { Open + Title Description @@ -105,6 +110,7 @@ describe( 'Popover', () => { Open + Title Close @@ -125,7 +131,10 @@ describe( 'Popover', () => { render( Open - Popover content + + Title + Popover content + ); @@ -148,7 +157,10 @@ describe( 'Popover', () => { render( Toggle - Popover content + + Title + Popover content + ); @@ -177,7 +189,10 @@ describe( 'Popover', () => { render( Open - Popover content + + Title + Popover content + ); @@ -205,6 +220,7 @@ describe( 'Popover', () => { Open + Title Popover content Close @@ -239,7 +255,10 @@ describe( 'Popover', () => { Trigger - Controlled content + + Title + Controlled content + ); @@ -271,7 +290,10 @@ describe( 'Popover', () => { render( Open - Default open content + + Title + Default open content + ); @@ -291,7 +313,9 @@ describe( 'Popover', () => { render( Toggle - Content + + Title + ); @@ -347,6 +371,7 @@ describe( 'Popover', () => { Open + Title My Description @@ -372,6 +397,7 @@ describe( 'Popover', () => { Open + Title Unstyled content @@ -395,7 +421,10 @@ describe( 'Popover', () => {
Open - Inline content + + Title + Inline content +
); @@ -421,7 +450,10 @@ describe( 'Popover', () => {
Open - Portal content + + Title + Portal content +
); @@ -453,6 +485,7 @@ describe( 'Popover', () => {
+ Title Anchored content @@ -478,6 +511,7 @@ describe( 'Popover', () => { Open + Title Close @@ -502,6 +536,7 @@ describe( 'Popover', () => { Open + Title Close

No tabbable content here

@@ -524,6 +559,7 @@ describe( 'Popover', () => { Open + Title Close @@ -557,6 +593,7 @@ describe( 'Popover', () => { Open + Title Close @@ -574,4 +611,117 @@ describe( 'Popover', () => { expect( customFocus ).toHaveBeenCalled(); } ); } ); + + describe( 'title validation', () => { + it( 'should throw when Popover.Title is missing', async () => { + const user = userEvent.setup(); + const onError = jest.fn(); + + // Suppress console.error from React error boundary + const spy = jest + .spyOn( console, 'error' ) + .mockImplementation( () => {} ); + + render( + + + Open + No title here + + + ); + + await user.click( screen.getByRole( 'button', { name: 'Open' } ) ); + + await waitFor( () => { + expect( onError ).toHaveBeenCalledWith( + expect.objectContaining( { + message: expect.stringContaining( + 'Missing ' + ), + } ) + ); + } ); + + spy.mockRestore(); + } ); + + it( 'should throw when Popover.Title is empty', async () => { + const user = userEvent.setup(); + const onError = jest.fn(); + + const spy = jest + .spyOn( console, 'error' ) + .mockImplementation( () => {} ); + + render( + + + Open + + + + + + ); + + await user.click( screen.getByRole( 'button', { name: 'Open' } ) ); + + await waitFor( () => { + expect( onError ).toHaveBeenCalledWith( + expect.objectContaining( { + message: expect.stringContaining( 'cannot be empty' ), + } ) + ); + } ); + + spy.mockRestore(); + } ); + + it( 'should not throw when Popover.Title is present', async () => { + const user = userEvent.setup(); + const onError = jest.fn(); + + render( + + + Open + + Valid Title + + + + ); + + await user.click( screen.getByRole( 'button', { name: 'Open' } ) ); + + await waitFor( () => { + expect( screen.getByText( 'Valid Title' ) ).toBeVisible(); + } ); + + expect( onError ).not.toHaveBeenCalled(); + } ); + } ); } ); + +class ErrorBoundary extends Component< + { children: React.ReactNode; onError: ( error: Error ) => void }, + { hasError: boolean } +> { + state = { hasError: false }; + + static getDerivedStateFromError() { + return { hasError: true }; + } + + componentDidCatch( error: Error ) { + this.props.onError( error ); + } + + render() { + if ( this.state.hasError ) { + return null; + } + return this.props.children; + } +} diff --git a/packages/ui/src/popover/title.tsx b/packages/ui/src/popover/title.tsx index cba3f96b97804b..803444cb576d2d 100644 --- a/packages/ui/src/popover/title.tsx +++ b/packages/ui/src/popover/title.tsx @@ -1,20 +1,35 @@ import { Popover as _Popover } from '@base-ui/react/popover'; import clsx from 'clsx'; -import { forwardRef } from '@wordpress/element'; +import { useMergeRefs } from '@wordpress/compose'; +import { forwardRef, useLayoutEffect, useRef } from '@wordpress/element'; +import { Text } from '../text'; +import { usePopoverValidationContext } from './context'; import styles from './style.module.css'; import type { TitleProps } from './types'; /** * Renders a heading that labels the popover for accessibility. + * This component is required — every popover must include a title, + * even if visually hidden. */ const Title = forwardRef< HTMLHeadingElement, TitleProps >( - function PopoverTitle( { className, ...props }, ref ) { + function PopoverTitle( { className, children, ...props }, forwardedRef ) { + const validationContext = usePopoverValidationContext(); + const internalRef = useRef< HTMLHeadingElement >( null ); + const mergedRef = useMergeRefs( [ internalRef, forwardedRef ] ); + + useLayoutEffect( () => { + validationContext?.registerTitle( internalRef.current ); + }, [ validationContext ] ); + return ( - <_Popover.Title - ref={ ref } + } className={ clsx( styles.title, className ) } - { ...props } - /> + > + { children } + ); } ); From 67c0b04a8a0313cd91ee98860486929bee274260 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Tue, 31 Mar 2026 16:54:55 +0200 Subject: [PATCH 39/71] Popover: enrich JSDoc across all subcomponents Make Root's JSDoc a full component overview with sub-component listing and usage example. Add detailed descriptions to Trigger, Popup, Arrow, Title, Description, and Close so Storybook can source docs from JSDoc instead of manual `parameters.docs` config. Made-with: Cursor --- packages/ui/src/popover/arrow.tsx | 5 +++- packages/ui/src/popover/close.tsx | 4 ++++ packages/ui/src/popover/description.tsx | 5 +++- packages/ui/src/popover/popup.tsx | 8 +++++-- packages/ui/src/popover/root.tsx | 31 ++++++++++++++++++++++--- packages/ui/src/popover/title.tsx | 6 +++-- packages/ui/src/popover/trigger.tsx | 3 +++ packages/ui/src/popover/types.ts | 2 +- 8 files changed, 54 insertions(+), 10 deletions(-) diff --git a/packages/ui/src/popover/arrow.tsx b/packages/ui/src/popover/arrow.tsx index 6d0640dfeee47c..23b4698242d04d 100644 --- a/packages/ui/src/popover/arrow.tsx +++ b/packages/ui/src/popover/arrow.tsx @@ -5,7 +5,10 @@ import styles from './style.module.css'; import type { ArrowProps } from './types'; /** - * Renders an arrow element that points toward the popover anchor. + * Renders an optional arrow element that points toward the anchor. + * + * Must be placed inside `Popover.Popup`. The arrow automatically rotates + * to match the current placement side. */ const Arrow = forwardRef< HTMLDivElement, ArrowProps >( function PopoverArrow( { className, ...props }, diff --git a/packages/ui/src/popover/close.tsx b/packages/ui/src/popover/close.tsx index f266a7bc0769e0..3240fb06fbd9e6 100644 --- a/packages/ui/src/popover/close.tsx +++ b/packages/ui/src/popover/close.tsx @@ -4,6 +4,10 @@ import type { CloseProps } from './types'; /** * Renders a button that closes the popover when clicked. + * + * When the popover opens, initial focus skips this button in favor of + * the first interactive element inside the popup (unless `initialFocus` + * on `Popover.Popup` overrides this behavior). */ const Close = forwardRef< HTMLButtonElement, CloseProps >( function PopoverClose( props, ref ) { diff --git a/packages/ui/src/popover/description.tsx b/packages/ui/src/popover/description.tsx index acb13e304b71cb..6bde6bd0b50607 100644 --- a/packages/ui/src/popover/description.tsx +++ b/packages/ui/src/popover/description.tsx @@ -6,7 +6,10 @@ import styles from './style.module.css'; import type { DescriptionProps } from './types'; /** - * Renders a paragraph that describes the popover content for accessibility. + * Renders an optional paragraph that describes the popover content. + * + * The rendered element is linked to the popup via `aria-describedby`. + * Uses the `body-md` text variant by default. */ const Description = forwardRef< HTMLParagraphElement, DescriptionProps >( function PopoverDescription( { className, children, ...props }, ref ) { diff --git a/packages/ui/src/popover/popup.tsx b/packages/ui/src/popover/popup.tsx index a06460118ddee8..f973de0f8fec94 100644 --- a/packages/ui/src/popover/popup.tsx +++ b/packages/ui/src/popover/popup.tsx @@ -19,8 +19,12 @@ const ThemeProvider: typeof ThemeProviderType = const CLOSE_ATTR = 'data-wp-ui-popover-close'; /** - * Renders the popover popup element that contains the popover content. - * By default, uses a portal to render outside the DOM hierarchy. + * Renders the floating popup container for the popover content. + * + * Handles portal rendering, positioning relative to the anchor, collision + * avoidance, focus management, and optional backdrop. Set `inline` to + * render inside the DOM tree instead of a portal, or supply a `container` + * element for cross-document scenarios such as iframes. */ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( { diff --git a/packages/ui/src/popover/root.tsx b/packages/ui/src/popover/root.tsx index cd63ff8b7eaa9d..a565d3bc8d6d01 100644 --- a/packages/ui/src/popover/root.tsx +++ b/packages/ui/src/popover/root.tsx @@ -2,10 +2,35 @@ import { Popover as _Popover } from '@base-ui/react/popover'; import type { RootProps } from './types'; /** - * Groups the popover trigger and popup. + * An accessible popup anchored to a trigger element. * - * `Popover` is a collection of React components that combine to render - * an ARIA-compliant popover anchored to a trigger button. + * Popover renders ARIA-compliant floating content that appears next to its + * trigger. It can contain interactive content such as form controls, menus, + * and rich descriptions. + * + * Compose the compound components to build a popover: + * + * - `Popover.Root` — provides open state and context to all sub-components. + * - `Popover.Trigger` — the button that toggles the popup. + * - `Popover.Popup` — the floating container (portal, positioning, collision + * avoidance). + * - `Popover.Arrow` — an optional arrow pointing toward the anchor. + * - `Popover.Title` — **required** heading that labels the popover for + * accessibility (can be visually hidden). + * - `Popover.Description` — optional paragraph linked via `aria-describedby`. + * - `Popover.Close` — a button that closes the popover when clicked. + * + * ```jsx + * + * Open + * + * + * Popover title + * Popover description + * Close + * + * + * ``` */ function Root( props: RootProps ) { return <_Popover.Root { ...props } />; diff --git a/packages/ui/src/popover/title.tsx b/packages/ui/src/popover/title.tsx index 803444cb576d2d..857260bbb54425 100644 --- a/packages/ui/src/popover/title.tsx +++ b/packages/ui/src/popover/title.tsx @@ -9,8 +9,10 @@ import type { TitleProps } from './types'; /** * Renders a heading that labels the popover for accessibility. - * This component is required — every popover must include a title, - * even if visually hidden. + * + * **Required** — every popover must include a `Popover.Title`, even if + * visually hidden. The rendered element is linked to the popup via + * `aria-labelledby`. Uses the `heading-md` text variant by default. */ const Title = forwardRef< HTMLHeadingElement, TitleProps >( function PopoverTitle( { className, children, ...props }, forwardedRef ) { diff --git a/packages/ui/src/popover/trigger.tsx b/packages/ui/src/popover/trigger.tsx index a3b7eb36255578..dda283281e4385 100644 --- a/packages/ui/src/popover/trigger.tsx +++ b/packages/ui/src/popover/trigger.tsx @@ -4,6 +4,9 @@ import type { TriggerProps } from './types'; /** * Renders a button that toggles the popover popup when clicked. + * + * Renders as a ` @@ -1294,9 +1292,9 @@ export const InfoTip: Story = { - - More information - + }> + More information + This is additional context about the label. Unlike tooltips, this content is accessible to touch and From 846af7e659652e428507e6e43b5f98ef0983d5ec Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Wed, 8 Apr 2026 15:31:18 +0200 Subject: [PATCH 71/71] Popover: remove inline prop, document as consumer pattern MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove the built-in `inline` prop from `Popover.Popup`. Consumers who need inline (non-portaled) rendering can achieve the same result by creating a local ref to a `` and passing it as the `container` prop — this is now documented in the Inline story. This simplifies the component by removing a special code path and keeps the API surface small, since `container` already covers the use case. Made-with: Cursor --- packages/ui/src/popover/popup.tsx | 22 +------ .../ui/src/popover/stories/index.story.tsx | 61 +++++++++++-------- packages/ui/src/popover/test/index.test.tsx | 21 +++++-- packages/ui/src/popover/types.ts | 12 ---- 4 files changed, 51 insertions(+), 65 deletions(-) diff --git a/packages/ui/src/popover/popup.tsx b/packages/ui/src/popover/popup.tsx index 8eef0ed27395fc..4dabf05b2c1beb 100644 --- a/packages/ui/src/popover/popup.tsx +++ b/packages/ui/src/popover/popup.tsx @@ -1,6 +1,6 @@ import { Popover as _Popover } from '@base-ui/react/popover'; import clsx from 'clsx'; -import { forwardRef, useRef } from '@wordpress/element'; +import { forwardRef } from '@wordpress/element'; import { useMergeRefs } from '@wordpress/compose'; import { type ThemeProvider as ThemeProviderType, @@ -22,8 +22,7 @@ const CLOSE_ATTR = 'data-wp-ui-popover-close'; * Renders the floating popup container for the popover content. * * Handles portal rendering, positioning relative to the anchor, collision - * avoidance, focus management, and optional backdrop. Set `inline` to - * render inside the DOM tree instead of a portal, or supply a `container` + * avoidance, focus management, and optional backdrop. Supply a `container` * element for cross-document scenarios such as iframes. */ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( @@ -42,7 +41,6 @@ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( container, finalFocus, initialFocus, - inline: inlineProp = false, side = 'bottom', sideOffset = 8, sticky, @@ -52,7 +50,6 @@ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( }, ref ) { - const inlineContainerRef = useRef< HTMLSpanElement >( null ); const { resolvedInitialFocus, popupRef } = useDeprioritizedInitialFocus( { initialFocus, deprioritizedAttribute: CLOSE_ATTR, @@ -98,21 +95,6 @@ const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( ); - if ( inlineProp ) { - return ( - <> - - <_Popover.Portal container={ inlineContainerRef }> - { backdropElement } - { positioner } - - - ); - } - return ( <_Popover.Portal container={ container }> { backdropElement } diff --git a/packages/ui/src/popover/stories/index.story.tsx b/packages/ui/src/popover/stories/index.story.tsx index 27076eee389911..a6849b089b0714 100644 --- a/packages/ui/src/popover/stories/index.story.tsx +++ b/packages/ui/src/popover/stories/index.story.tsx @@ -442,36 +442,43 @@ export const OverlayPlacement: Story = { }; /** - * When `inline` is set to `true`, the popup renders in place within the DOM - * hierarchy instead of being portaled to `document.body`. This can be - * useful when you need the popup to participate in the surrounding layout - * or inherit styles from a parent. + * To render the popup inline (without a portal), create a local ref to a + * `` with `display: contents` and pass it as the `container` prop. + * The popup will render inside the span rather than being portaled to + * `document.body`, while retaining all positioning behavior. * - * Positioning props (`side`, `align`, collision settings, etc.) still work - * normally. However, `container` is ignored (the portal target is managed - * internally) and `backdrop` will not cover the full viewport. + * **Note:** `backdrop` will not cover the full viewport in this mode. */ export const Inline: Story = { - args: { - children: ( - <> - Open Inline - - - - Inline Popover - - - This popup is rendered in place — no portal is used. - Inspect the DOM to see it lives inside its parent. - - - - ), + parameters: { controls: { disable: true } }, + render: function Render() { + const inlineContainerRef = useRef< HTMLSpanElement >( null ); + + return ( +
+ + Open Inline + + + + + Inline Popover + + + This popup is rendered in place — no portal is used. + Inspect the DOM to see it lives inside its parent. + + + +
+ ); }, }; diff --git a/packages/ui/src/popover/test/index.test.tsx b/packages/ui/src/popover/test/index.test.tsx index eac80ae1aef68a..bef3e5358ec723 100644 --- a/packages/ui/src/popover/test/index.test.tsx +++ b/packages/ui/src/popover/test/index.test.tsx @@ -421,21 +421,30 @@ describe( 'Popover', () => { } ); } ); - describe( 'inline', () => { - it( 'should render without a portal when inline is true', async () => { - const user = userEvent.setup(); - - render( + describe( 'inline (via container)', () => { + function InlinePopover() { + const containerRef = createRef< HTMLSpanElement >(); + return (
Open - + + Title Inline content
); + } + + it( 'should render inside the container when a local ref is used', async () => { + const user = userEvent.setup(); + + render( ); await user.click( screen.getByRole( 'button', { name: 'Open' } ) ); diff --git a/packages/ui/src/popover/types.ts b/packages/ui/src/popover/types.ts index ab02c7c87a910d..353bf278b6a349 100644 --- a/packages/ui/src/popover/types.ts +++ b/packages/ui/src/popover/types.ts @@ -71,18 +71,6 @@ export interface PopupProps */ container?: _Popover.Portal.Props[ 'container' ]; - /** - * Whether to render the popup inline without a portal. - * - * When `true`, the popup is rendered in place within the DOM hierarchy - * instead of being portaled to `document.body`. Positioning props - * (`side`, `align`, etc.) still work, but `container` is ignored and - * `backdrop` will not cover the viewport as expected. - * - * @default false - */ - inline?: boolean; - /** * The visual style variant of the popup. *