diff --git a/src/components/ui/Select/fragments/SelectPortal.tsx b/src/components/ui/Select/fragments/SelectPortal.tsx index 3a4ee2ede..8ea97cb2b 100644 --- a/src/components/ui/Select/fragments/SelectPortal.tsx +++ b/src/components/ui/Select/fragments/SelectPortal.tsx @@ -13,7 +13,10 @@ type SelectPortalPrimitiveProps = React.ComponentPropsWithoutRef(({ children, container, ...props }, forwardedRef) => { const themeContext = useContext(ThemeContext); - const portalContainer = container ?? themeContext?.portalRootRef.current; + const portalContainer = container + ?? themeContext?.portalRootRef.current + ?? themeContext?.containerRef.current + ?? undefined; return ( diff --git a/src/components/ui/Select/tests/Select.portal.test.tsx b/src/components/ui/Select/tests/Select.portal.test.tsx new file mode 100644 index 000000000..516bce07a --- /dev/null +++ b/src/components/ui/Select/tests/Select.portal.test.tsx @@ -0,0 +1,44 @@ +import React from 'react'; +import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import Select from '../Select'; +import Theme from '~/components/ui/Theme/Theme'; + +const mockMatchMedia = () => { + if ('matchMedia' in window && typeof window.matchMedia === 'function') return; + Object.defineProperty(window, 'matchMedia', { + writable: true, + value: jest.fn().mockImplementation(() => ({ + matches: false, + addEventListener: jest.fn(), + removeEventListener: jest.fn() + })) + }); +}; + +describe('Select portal', () => { + beforeEach(() => mockMatchMedia()); + + test('portals listbox into Theme portal root', async() => { + const user = userEvent.setup(); + + render( + + + + + + Apple + + + + + ); + + const portalRoot = document.querySelector('[data-rad-ui-portal-root]') as HTMLElement; + expect(portalRoot).toBeTruthy(); + + await user.click(screen.getByRole('combobox')); + expect(portalRoot).toContainElement(screen.getByText('Apple')); + }); +});