Repository navigation
@wordpress/ui: add Popover
#76438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@wordpress/ui: add Popover
#76438
Changes from all commits
9ed2fb4
7f6f110
4ff1cf1
43b2955
d532896
7bfba39
a7aba5e
1eda379
1cbd98f
6096213
ce7f665
2f961be
ceb7188
eb025b8
d5a808c
bb4ffc5
1c17d3b
9d7c6f9
558cac8
bfc5a21
18f09e0
60a8350
aeb4c32
ed91d0d
b66cab5
6f8dd1c
04a0330
60542d8
5784436
d77d624
78e0d0d
ef8359a
7a1d66f
ced4233
0b75701
9ab9ce9
e1d3df9
04ff1a8
67c0b04
6ad1976
5d86895
669aa15
c1caa5b
6b0b046
3be5ee0
b8e47fc
29c50ae
4967044
fdb22c0
3160f55
3298ab4
c55a81b
868b4e9
65b3f09
58ef1f2
50be8ed
e916d6b
bf43cd4
27da335
5812506
0bebc5f
11a06e8
553bf3d
40168bc
4852686
fe1e3ad
b46121f
196c19c
43fb33e
7b4dc50
846af7e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| 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'; | ||
|
|
||
| function DefaultArrowSvg( props: React.ComponentProps< 'svg' > ) { | ||
| return ( | ||
| <svg | ||
| width="20" | ||
| height="10" | ||
| viewBox="0 0 20 10" | ||
| fill="none" | ||
| { ...props } | ||
| > | ||
| <path | ||
| d="M20 10H0V8h1.465a4 4 0 0 0 2.676-1.027l5.19-4.388c.378-.341.96-.341 1.338 0l5.19 4.388A4 4 0 0 0 18.535 8H20z" | ||
| className={ styles[ 'arrow-fill' ] } | ||
| /> | ||
| <path | ||
| d="M10 3.097 4.81 7.486A5 5 0 0 1 1.465 8.77H0V8h1.465a4 4 0 0 0 2.676-1.027l5.19-4.388c.378-.341.96-.341 1.338 0l5.19 4.388A4 4 0 0 0 18.535 8H20v.77h-1.465a5 5 0 0 1-3.345-1.284z" | ||
| className={ styles[ 'arrow-stroke' ] } | ||
| /> | ||
| </svg> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * 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( | ||
| { children, className, ...props }, | ||
| ref | ||
| ) { | ||
| return ( | ||
| <_Popover.Arrow | ||
| ref={ ref } | ||
| className={ clsx( styles.arrow, className ) } | ||
| { ...props } | ||
| > | ||
| { children ?? <DefaultArrowSvg /> } | ||
| </_Popover.Arrow> | ||
| ); | ||
| } ); | ||
|
|
||
| export { Arrow }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| 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. | ||
| * | ||
| * 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 ) { | ||
| return ( | ||
| <_Popover.Close | ||
| ref={ ref } | ||
| data-wp-ui-popover-close="" | ||
| { ...props } | ||
| /> | ||
| ); | ||
| } | ||
| ); | ||
|
|
||
| export { Close }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <Popover.Title>. ' + | ||
| '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: <Popover.Title> cannot be empty. ' + | ||
| 'Provide meaningful text content for the popover title.' | ||
| ); | ||
| } | ||
| }, [] ); | ||
|
|
||
| return ( | ||
| <PopoverValidationContext.Provider value={ contextValue }> | ||
| { children } | ||
| </PopoverValidationContext.Provider> | ||
| ); | ||
| } | ||
|
|
||
| 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; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| 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'; | ||
|
|
||
| /** | ||
| * 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, render, ...props }, | ||
| ref | ||
| ) { | ||
| return ( | ||
| <Text | ||
| ref={ ref } | ||
| variant="body-md" | ||
| render={ | ||
| <_Popover.Description render={ render } { ...props } /> | ||
| } | ||
| className={ clsx( styles.description, className ) } | ||
| > | ||
| { children } | ||
| </Text> | ||
| ); | ||
| } | ||
| ); | ||
|
|
||
| export { Description }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import { Popover as _Popover } from '@base-ui/react/popover'; | ||
| import clsx from 'clsx'; | ||
| import { forwardRef } from '@wordpress/element'; | ||
| import { useMergeRefs } from '@wordpress/compose'; | ||
| 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 { useDeprioritizedInitialFocus } from '../utils/use-deprioritized-initial-focus'; | ||
| import { PopoverValidationProvider } from './context'; | ||
| import styles from './style.module.css'; | ||
| import type { PopupProps } from './types'; | ||
|
|
||
| const ThemeProvider: typeof ThemeProviderType = | ||
| unlock( themePrivateApis ).ThemeProvider; | ||
|
|
||
| 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. Supply a `container` | ||
| * element for cross-document scenarios such as iframes. | ||
| */ | ||
| const Popup = forwardRef< HTMLDivElement, PopupProps >( function PopoverPopup( | ||
| { | ||
| align = 'center', | ||
| alignOffset, | ||
| anchor, | ||
| // Matches the popup's border-radius (--wpds-border-radius-md). | ||
| arrowPadding = 8, | ||
|
Comment on lines
+33
to
+34
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tricky one to avoid hard-coding 🤔 I don't suppose there's an easy way to evaluate the actual token value in this context. Not a blocker, but may make future maintenance a bit harder, or if we add some theme configuration that affects radii.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. More than the token value, the correct solution would be to resolve the value of the border radius at runtime (and watch for changes), but that feels like an overengineered solution. For now, I made the comment even more explicit. We can evaluate more involved solutions if the need arises
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we got #76604 working, we would be able to to at least get that default |
||
| backdrop = false, | ||
| children, | ||
| className, | ||
| collisionAvoidance, | ||
| collisionBoundary, | ||
| collisionPadding, | ||
| container, | ||
| finalFocus, | ||
| initialFocus, | ||
| side = 'bottom', | ||
| sideOffset = 8, | ||
| sticky, | ||
| style, | ||
| variant = 'default', | ||
| ...props | ||
| }, | ||
| ref | ||
| ) { | ||
| const { resolvedInitialFocus, popupRef } = useDeprioritizedInitialFocus( { | ||
| initialFocus, | ||
| deprioritizedAttribute: CLOSE_ATTR, | ||
| } ); | ||
| const mergedPopupRef = useMergeRefs( [ ref, popupRef ] ); | ||
|
|
||
| const backdropElement = backdrop ? ( | ||
| <_Popover.Backdrop className={ styles.backdrop } /> | ||
| ) : null; | ||
|
|
||
| const positioner = ( | ||
| <_Popover.Positioner | ||
| align={ align } | ||
| alignOffset={ alignOffset } | ||
| anchor={ anchor } | ||
| arrowPadding={ arrowPadding } | ||
| collisionAvoidance={ collisionAvoidance } | ||
| collisionBoundary={ collisionBoundary } | ||
| collisionPadding={ collisionPadding } | ||
| side={ side } | ||
| sideOffset={ sideOffset } | ||
| sticky={ sticky } | ||
| style={ style } | ||
| className={ clsx( | ||
| resetStyles[ 'box-sizing' ], | ||
| styles.positioner, | ||
| className | ||
| ) } | ||
| > | ||
| <ThemeProvider> | ||
|
ciampo marked this conversation as resolved.
Resolved
|
||
| <_Popover.Popup | ||
| ref={ mergedPopupRef } | ||
| initialFocus={ resolvedInitialFocus } | ||
| finalFocus={ finalFocus } | ||
| className={ clsx( variant !== 'unstyled' && styles.popup ) } | ||
| { ...props } | ||
| > | ||
| <PopoverValidationProvider> | ||
| { children } | ||
| </PopoverValidationProvider> | ||
| </_Popover.Popup> | ||
| </ThemeProvider> | ||
| </_Popover.Positioner> | ||
| ); | ||
|
|
||
| return ( | ||
| <_Popover.Portal container={ container }> | ||
| { backdropElement } | ||
| { positioner } | ||
| </_Popover.Portal> | ||
| ); | ||
| } ); | ||
|
|
||
| export { Popup }; | ||
Uh oh!
There was an error while loading. Please reload this page.