mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-23 16:34:27 +00:00
🧩 feat: Export Popover Parts From @librechat/client (#17751)
* feat: Export Unstyled Popover Parts From @librechat/client * feat: Give PopoverContent the Shared Panel Surface
This commit is contained in:
parent
ed85f15735
commit
c1bedeb386
3 changed files with 105 additions and 0 deletions
64
packages/client/src/components/Popover.spec.tsx
Normal file
64
packages/client/src/components/Popover.spec.tsx
Normal file
|
|
@ -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 (
|
||||
<Popover modal={modal}>
|
||||
<PopoverTrigger asChild>
|
||||
<button type="button">Open</button>
|
||||
</PopoverTrigger>
|
||||
<PopoverPortal>
|
||||
<PopoverContent role="dialog" aria-label="Details" className="custom-panel">
|
||||
<p>Body</p>
|
||||
<PopoverClose asChild>
|
||||
<button type="button">Close</button>
|
||||
</PopoverClose>
|
||||
</PopoverContent>
|
||||
</PopoverPortal>
|
||||
</Popover>
|
||||
);
|
||||
}
|
||||
|
||||
describe('Popover', () => {
|
||||
it('opens from its trigger and closes from its close part', () => {
|
||||
render(<Example />);
|
||||
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(<Example modal />);
|
||||
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<HTMLDivElement>();
|
||||
render(
|
||||
<Popover open>
|
||||
<PopoverTrigger>Open</PopoverTrigger>
|
||||
<PopoverContent ref={ref}>Body</PopoverContent>
|
||||
</Popover>,
|
||||
);
|
||||
|
||||
expect(ref.current).toBe(screen.getByText('Body'));
|
||||
});
|
||||
});
|
||||
40
packages/client/src/components/Popover.tsx
Normal file
40
packages/client/src/components/Popover.tsx
Normal file
|
|
@ -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.PopoverProps> = PopoverPrimitive.Root;
|
||||
|
||||
const PopoverTrigger: React.ForwardRefExoticComponent<
|
||||
PopoverPrimitive.PopoverTriggerProps & React.RefAttributes<HTMLButtonElement>
|
||||
> = PopoverPrimitive.Trigger;
|
||||
|
||||
const PopoverPortal: React.FC<PopoverPrimitive.PopoverPortalProps> = PopoverPrimitive.Portal;
|
||||
|
||||
const PopoverContent: React.ForwardRefExoticComponent<
|
||||
PopoverPrimitive.PopoverContentProps & React.RefAttributes<HTMLDivElement>
|
||||
> = React.forwardRef<
|
||||
React.ElementRef<typeof PopoverPrimitive.Content>,
|
||||
React.ComponentPropsWithoutRef<typeof PopoverPrimitive.Content>
|
||||
>(({ className, ...props }, ref) => (
|
||||
<PopoverPrimitive.Content
|
||||
ref={ref}
|
||||
className={cn(
|
||||
'border-border-light bg-surface-secondary text-text-primary data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 rounded-theme-popover z-50 origin-[--radix-popover-content-transform-origin] border shadow-xl motion-reduce:animate-none',
|
||||
className,
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
));
|
||||
PopoverContent.displayName = 'PopoverContent';
|
||||
|
||||
const PopoverClose: React.ForwardRefExoticComponent<
|
||||
PopoverPrimitive.PopoverCloseProps & React.RefAttributes<HTMLButtonElement>
|
||||
> = PopoverPrimitive.Close;
|
||||
|
||||
export { Popover, PopoverTrigger, PopoverPortal, PopoverContent, PopoverClose };
|
||||
|
|
@ -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';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue