-
Notifications
You must be signed in to change notification settings - Fork 0
[PB-3520]: add HeadlessPopover component with accessibility support #73
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
Open
terrerox
wants to merge
3
commits into
master
Choose a base branch
from
feat/popover
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+237
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import { Popover as HPopover, Transition } from '@headlessui/react'; | ||
| import { ReactNode } from 'react'; | ||
|
|
||
| export interface HeadlessPopoverRenderProps { | ||
| open: boolean; | ||
| close: () => void; | ||
| Button: typeof HPopover.Button; | ||
| Panel: typeof HPopover.Panel; | ||
| } | ||
|
|
||
| interface HeadlessPopoverProps { | ||
| trigger?: ReactNode; | ||
| panel?: ReactNode | ((close: () => void) => ReactNode); | ||
| className?: string; | ||
| buttonClassName?: string; | ||
| panelClassName?: string; | ||
| panelStyle?: React.CSSProperties; | ||
| buttonAs?: React.ElementType; | ||
| isAnimated?: boolean; | ||
| isStatic?: boolean; | ||
| children?: (props: HeadlessPopoverRenderProps) => ReactNode; | ||
| } | ||
|
|
||
| const DEFAULT_PANEL_CLASS = 'absolute right-0 z-50 mt-1 rounded-md border border-gray-10 bg-surface py-1.5 shadow-subtle dark:bg-gray-5'; | ||
|
|
||
| const BUTTON_CONTAINER_STYLE = { lineHeight: 0 } as const; | ||
|
|
||
| /** | ||
| * HeadlessPopover component - A flexible popover wrapper around HeadlessUI. | ||
| * | ||
| * ## Two usage modes: | ||
| * | ||
| * ### 1. Simple Mode (use trigger + panel props): | ||
| * ```tsx | ||
| * <HeadlessPopover | ||
| * trigger={<button>Click me</button>} | ||
| * panel={<div>Content</div>} | ||
| * /> | ||
| * ``` | ||
| * | ||
| * ### 2. Render Props Mode (use children for full control): | ||
| * ```tsx | ||
| * <HeadlessPopover> | ||
| * {({ open, close, Button, Panel }) => ( | ||
| * <> | ||
| * <Button>Custom Button</Button> | ||
| * <Panel>Custom Panel</Panel> | ||
| * </> | ||
| * )} | ||
| * </HeadlessPopover> | ||
| * ``` | ||
| * | ||
| * **Note:** If `children` prop is provided, all other props (trigger, panel, etc.) are ignored. | ||
|
Collaborator
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. I think that classname it applies |
||
| * | ||
| * @property {ReactNode} [trigger] | ||
| * - The content to be displayed inside the trigger button. | ||
| * | ||
| * @property {ReactNode | ((close: () => void) => ReactNode)} [panel] | ||
| * - The content to be displayed inside the popover panel. | ||
| * Can be a ReactNode or a function that receives a `close` function as a parameter. | ||
| * | ||
| * @property {string} [className] | ||
| * - Additional custom classes for the outermost container of the popover. | ||
| * | ||
| * @property {string} [buttonClassName] | ||
| * - Custom classes for the trigger button. | ||
| * | ||
| * @property {string} [panelClassName] | ||
| * - Custom classes for the panel container. | ||
| * | ||
| * @property {React.CSSProperties} [panelStyle] | ||
| * - Inline styles for the panel. | ||
| * | ||
| * @property {React.ElementType} [buttonAs] | ||
| * - Custom element type for the button (e.g., 'div', CustomComponent). | ||
| * | ||
| * @property {boolean} [isAnimated=true] | ||
| * - Whether to use transition animations when opening/closing. | ||
| * | ||
| * @property {boolean} [isStatic=false] | ||
| * - Whether to keep the panel mounted in the DOM even when closed (static mode). | ||
| * | ||
| * @property {(props: HeadlessPopoverRenderProps) => ReactNode} [children] | ||
| * - Render prop function for advanced customization. When provided, overrides all other props. | ||
| * | ||
| * @returns {JSX.Element} | ||
| * - The rendered HeadlessPopover component. | ||
| */ | ||
| export default function HeadlessPopover({ | ||
|
Collaborator
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. Is not totally headless because it already has default style :O |
||
| trigger, | ||
| panel, | ||
| className = '', | ||
| buttonClassName = '', | ||
| panelClassName, | ||
| panelStyle, | ||
| buttonAs, | ||
| isAnimated = true, | ||
| isStatic = false, | ||
| children, | ||
| }: Readonly<HeadlessPopoverProps>): JSX.Element { | ||
| if (children) { | ||
| return ( | ||
| <HPopover className={className}> | ||
| {({ open, close }: { open: boolean; close: () => void }) => <>{children({ open, close, Button: HPopover.Button, Panel: HPopover.Panel })}</>} | ||
| </HPopover> | ||
| ); | ||
| } | ||
|
|
||
| const panelElement = ( | ||
| <HPopover.Panel | ||
| className={panelClassName || DEFAULT_PANEL_CLASS} | ||
| style={panelStyle} | ||
| static={isStatic} | ||
| > | ||
| {({ close }: { close: () => void }) => ( | ||
| <>{typeof panel === 'function' ? panel(close) : panel}</> | ||
| )} | ||
| </HPopover.Panel> | ||
| ); | ||
|
|
||
| const containerClassName = `relative ${className}`; | ||
| const defaultButtonClassName = [ | ||
| 'cursor-pointer', | ||
| 'outline-none', | ||
| 'focus-visible:outline-2', | ||
| 'focus-visible:outline-offset-2', | ||
| 'focus-visible:outline-primary' | ||
| ].join(' '); | ||
| const finalButtonClassName = `${defaultButtonClassName} ${buttonClassName}`; | ||
|
|
||
| return ( | ||
| <HPopover style={BUTTON_CONTAINER_STYLE} className={containerClassName}> | ||
| <HPopover.Button as={buttonAs} className={finalButtonClassName}> | ||
| {trigger} | ||
| </HPopover.Button> | ||
|
|
||
| {isAnimated ? ( | ||
| <Transition | ||
| enter="transition duration-100 ease-out" | ||
| enterFrom="scale-95 opacity-0" | ||
| enterTo="scale-100 opacity-100" | ||
| leave="transition duration-75 ease-out" | ||
| leaveFrom="scale-100 opacity-100" | ||
| leaveTo="scale-95 opacity-0" | ||
| className="z-50" | ||
| > | ||
| {panelElement} | ||
| </Transition> | ||
| ) : ( | ||
| panelElement | ||
| )} | ||
| </HPopover> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import React from 'react'; | ||
| import { render, fireEvent } from '@testing-library/react'; | ||
| import HeadlessPopover from '../HeadlessPopover'; | ||
| import { describe, it, expect } from 'vitest'; | ||
|
|
||
| describe('HeadlessPopover', () => { | ||
| it('shows and hides panel when button is clicked', () => { | ||
| const { getByText, queryByText } = render( | ||
| <HeadlessPopover | ||
| trigger={<span>Toggle</span>} | ||
| panel={(close) => <div>Panel Content</div>} | ||
| /> | ||
| ); | ||
|
|
||
| expect(queryByText('Panel Content')).not.toBeInTheDocument(); | ||
|
|
||
| fireEvent.click(getByText('Toggle')); | ||
| expect(getByText('Panel Content')).toBeInTheDocument(); | ||
|
|
||
| fireEvent.click(getByText('Toggle')); | ||
| expect(queryByText('Panel Content')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('closes when close button inside panel is clicked', () => { | ||
| const { getByText, queryByText } = render( | ||
| <HeadlessPopover | ||
| trigger={<span>Toggle</span>} | ||
| panel={(close) => <button onClick={close}>Close</button>} | ||
| /> | ||
| ); | ||
|
|
||
| fireEvent.click(getByText('Toggle')); | ||
| expect(getByText('Close')).toBeInTheDocument(); | ||
|
|
||
| fireEvent.click(getByText('Close')); | ||
| expect(queryByText('Close')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('applies custom styling classes', () => { | ||
| const { container } = render( | ||
| <HeadlessPopover | ||
| trigger={<span>Toggle</span>} | ||
| panel={<div>Content</div>} | ||
| className="custom-container" | ||
| buttonClassName="custom-button" | ||
| /> | ||
| ); | ||
|
|
||
| expect(container.firstChild).toHaveClass('custom-container'); | ||
| const button = container.querySelector('button'); | ||
| expect(button).toHaveClass('custom-button'); | ||
| }); | ||
|
|
||
| it('works with custom children function', () => { | ||
| const { getByText, queryByText } = render( | ||
| <HeadlessPopover> | ||
| {({ open, close, Button, Panel }) => ( | ||
| <> | ||
| <Button>Open Menu</Button> | ||
| <Panel> | ||
| <button onClick={close}>Close Menu</button> | ||
| {open && <div>Menu is open</div>} | ||
| </Panel> | ||
| </> | ||
| )} | ||
| </HeadlessPopover> | ||
| ); | ||
|
|
||
| expect(queryByText('Menu is open')).not.toBeInTheDocument(); | ||
|
|
||
| fireEvent.click(getByText('Open Menu')); | ||
| expect(getByText('Menu is open')).toBeInTheDocument(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m not totally convinced about adding a new library for the headless setup, so I created a separate popover component instead of changing the existing one. Curious what you think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fewer dependencies on third parties, the better. Why use this library compared to what we already have?