diff --git a/packages/client/src/components/Popover.spec.tsx b/packages/client/src/components/Popover.spec.tsx new file mode 100644 index 0000000000..6758c9c285 --- /dev/null +++ b/packages/client/src/components/Popover.spec.tsx @@ -0,0 +1,64 @@ +import React from 'react'; +import { fireEvent, render, screen } from '@testing-library/react'; +import { Popover, PopoverClose, PopoverPortal, PopoverContent, PopoverTrigger } from './Popover'; + +function Example({ modal = false }: { modal?: boolean }) { + return ( + + + + + + +

Body

+ + + +
+
+
+ ); +} + +describe('Popover', () => { + it('opens from its trigger and closes from its close part', () => { + render(); + const trigger = screen.getByRole('button', { name: 'Open' }); + expect(screen.queryByRole('dialog', { name: 'Details' })).not.toBeInTheDocument(); + + fireEvent.click(trigger); + expect(trigger).toHaveAttribute('aria-expanded', 'true'); + expect(screen.getByRole('dialog', { name: 'Details' })).toHaveTextContent('Body'); + + fireEvent.click(screen.getByRole('button', { name: 'Close' })); + expect(screen.queryByRole('dialog', { name: 'Details' })).not.toBeInTheDocument(); + expect(trigger).toHaveAttribute('aria-expanded', 'false'); + }); + + it('portals the content out of the trigger tree on the shared panel surface', () => { + const { container } = render(); + fireEvent.click(screen.getByRole('button', { name: 'Open' })); + + const panel = screen.getByRole('dialog', { name: 'Details' }); + expect(container).not.toContainElement(panel); + expect(panel).toHaveClass( + 'bg-surface-secondary', + 'border-border-light', + 'rounded-theme-popover', + 'motion-reduce:animate-none', + 'custom-panel', + ); + }); + + it('forwards its ref to the content element', () => { + const ref = React.createRef(); + render( + + Open + Body + , + ); + + expect(ref.current).toBe(screen.getByText('Body')); + }); +}); diff --git a/packages/client/src/components/Popover.tsx b/packages/client/src/components/Popover.tsx new file mode 100644 index 0000000000..116e0ad0e2 --- /dev/null +++ b/packages/client/src/components/Popover.tsx @@ -0,0 +1,40 @@ +import * as React from 'react'; +import * as PopoverPrimitive from '@radix-ui/react-popover'; +import { cn } from '~/utils'; + +/** + * Radix popover parts, so a consumer of this library (the chat package among them) composes an + * anchored, focus-managed popup without its own dependency on Radix. The root, trigger, portal + * and close parts are Radix's own; `PopoverContent` adds the panel's surface, edge, radius, + * shadow and enter/exit motion, and callers add only size and layout through `className`. + */ +const Popover: React.FC = PopoverPrimitive.Root; + +const PopoverTrigger: React.ForwardRefExoticComponent< + PopoverPrimitive.PopoverTriggerProps & React.RefAttributes +> = PopoverPrimitive.Trigger; + +const PopoverPortal: React.FC = PopoverPrimitive.Portal; + +const PopoverContent: React.ForwardRefExoticComponent< + PopoverPrimitive.PopoverContentProps & React.RefAttributes +> = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>(({ className, ...props }, ref) => ( + +)); +PopoverContent.displayName = 'PopoverContent'; + +const PopoverClose: React.ForwardRefExoticComponent< + PopoverPrimitive.PopoverCloseProps & React.RefAttributes +> = PopoverPrimitive.Close; + +export { Popover, PopoverTrigger, PopoverPortal, PopoverContent, PopoverClose }; diff --git a/packages/client/src/components/index.ts b/packages/client/src/components/index.ts index 1ec8bab41e..18157d1651 100644 --- a/packages/client/src/components/index.ts +++ b/packages/client/src/components/index.ts @@ -7,6 +7,7 @@ export * from './SeriesLabel'; export * from './Checkbox'; export * from './DisclosureChevron'; export * from './HoverCard'; +export * from './Popover'; export * from './Field'; export * from './Focus'; export * from './Input';