From 5f482a04761c9527d7d7179c364e36257b797da2 Mon Sep 17 00:00:00 2001 From: Talisson Costa Date: Tue, 17 Nov 2026 10:55:17 -0300 Subject: [PATCH 1/7] feat(Popover): Open as a bottom sheet on phones Below 768px a Popover's content opens in a bottom sheet (overlay, scroll lock, focus trap, Escape and outside tap to close) instead of an anchored panel. The trigger announces it with aria-haspopup="dialog" and aria-expanded. useBreakpoint('isPhone') matches Bootstrap's md breakpoint. Co-Authored-By: Claude Opus 5.5 --- .../hooks/__tests__/useBreakpoint.test.ts | 62 +++++++++++++++++++ frontend/common/hooks/useBreakpoint.ts | 28 +++++++++ .../components/Popover.stories.tsx | 19 +++++- .../components/base/Popover/BottomSheet.scss | 56 +++++++++++++++++ .../components/base/Popover/BottomSheet.tsx | 60 ++++++++++++++++++ .../web/components/base/Popover/Popover.tsx | 57 +++++++++++------ 6 files changed, 261 insertions(+), 21 deletions(-) create mode 100644 frontend/common/hooks/__tests__/useBreakpoint.test.ts create mode 100644 frontend/common/hooks/useBreakpoint.ts create mode 100644 frontend/web/components/base/Popover/BottomSheet.scss create mode 100644 frontend/web/components/base/Popover/BottomSheet.tsx diff --git a/frontend/common/hooks/__tests__/useBreakpoint.test.ts b/frontend/common/hooks/__tests__/useBreakpoint.test.ts new file mode 100644 index 000000000000..08f20a804d66 --- /dev/null +++ b/frontend/common/hooks/__tests__/useBreakpoint.test.ts @@ -0,0 +1,62 @@ +import { + BREAKPOINT_QUERIES, + matchesBreakpoint, +} from 'common/hooks/useBreakpoint' + +// jest runs in node, so stand in for window.matchMedia. +const setMatchMedia = ( + matchMedia?: (query: string) => { matches: boolean }, +) => { + ;(global as any).window = matchMedia ? { matchMedia } : {} +} + +afterEach(() => { + delete (global as any).window +}) + +describe('matchesBreakpoint', () => { + it('returns true when the breakpoint query matches', () => { + // Given + setMatchMedia((query) => ({ + matches: query === BREAKPOINT_QUERIES.isPhone, + })) + + // When + const result = matchesBreakpoint('isPhone') + + // Then + expect(result).toBe(true) + }) + + it('returns false when the breakpoint query does not match', () => { + // Given + setMatchMedia(() => ({ matches: false })) + + // When + const result = matchesBreakpoint('isPhone') + + // Then + expect(result).toBe(false) + }) + + it('returns false when matchMedia is unavailable', () => { + // Given + setMatchMedia() + + // When + const result = matchesBreakpoint('isPhone') + + // Then + expect(result).toBe(false) + }) + + it('returns false without a window', () => { + // Given no window + + // When + const result = matchesBreakpoint('isPhone') + + // Then + expect(result).toBe(false) + }) +}) diff --git a/frontend/common/hooks/useBreakpoint.ts b/frontend/common/hooks/useBreakpoint.ts new file mode 100644 index 000000000000..b0789d00027f --- /dev/null +++ b/frontend/common/hooks/useBreakpoint.ts @@ -0,0 +1,28 @@ +import { useEffect, useState } from 'react' + +// Bootstrap breakpoints, so components agree with the media queries in SCSS. +// Add a size when a component needs it. +export const BREAKPOINT_QUERIES = { + isPhone: '(max-width: 767.98px)', // below md +} as const + +export type Breakpoint = keyof typeof BREAKPOINT_QUERIES + +export const matchesBreakpoint = (breakpoint: Breakpoint): boolean => + typeof window !== 'undefined' && + !!window.matchMedia?.(BREAKPOINT_QUERIES[breakpoint]).matches + +export const useBreakpoint = (breakpoint: Breakpoint): boolean => { + const [isMatch, setIsMatch] = useState(() => matchesBreakpoint(breakpoint)) + + useEffect(() => { + const list = window.matchMedia?.(BREAKPOINT_QUERIES[breakpoint]) + if (!list) return + const onChange = () => setIsMatch(list.matches) + onChange() + list.addEventListener('change', onChange) + return () => list.removeEventListener('change', onChange) + }, [breakpoint]) + + return isMatch +} diff --git a/frontend/documentation/components/Popover.stories.tsx b/frontend/documentation/components/Popover.stories.tsx index f4b2b52aab19..290f6509c56e 100644 --- a/frontend/documentation/components/Popover.stories.tsx +++ b/frontend/documentation/components/Popover.stories.tsx @@ -40,7 +40,7 @@ const renderPopover = () => ( ) export const Default: Story = { - render: renderPopover, + render: () => renderPopover(), } export const Open: Story = { @@ -52,5 +52,20 @@ export const Open: Story = { await userEvent.click(canvas.getByRole('button', { name: 'Sort by' })) await screen.findByRole('dialog', { name: 'Sort' }) }, - render: renderPopover, + render: () => renderPopover(), +} + +// On phones the content opens as a bottom sheet +export const MobileSheet: Story = { + globals: { viewport: { isRotated: false, value: 'mobile1' } }, + parameters: { + chromatic: { delay: 300, viewports: [375] }, + docs: { story: { height: '420px', inline: false } }, + }, + play: async ({ canvasElement }: { canvasElement: HTMLElement }) => { + const canvas = within(canvasElement) + await userEvent.click(canvas.getByRole('button', { name: 'Sort by' })) + await screen.findByRole('dialog', { name: 'Sort' }) + }, + render: () => renderPopover(), } diff --git a/frontend/web/components/base/Popover/BottomSheet.scss b/frontend/web/components/base/Popover/BottomSheet.scss new file mode 100644 index 000000000000..e3c286f7c22e --- /dev/null +++ b/frontend/web/components/base/Popover/BottomSheet.scss @@ -0,0 +1,56 @@ +.ds-bottom-sheet__overlay { + // Same layer as .ds-popover: above every modal, so it works inside one. + z-index: 200000000001; + display: flex; + align-items: flex-end; + background: oklch(from var(--slate-1000) l c h / 0.5); + animation: ds-bottom-sheet-fade var(--duration-normal) var(--easing-entrance); +} + +.ds-bottom-sheet { + width: 100%; + max-height: 80vh; + overflow-y: auto; + padding: 8px 12px calc(16px + env(safe-area-inset-bottom)); + background: var(--color-surface-default); + border-radius: var(--radius-2xl) var(--radius-2xl) 0 0; + box-shadow: var(--shadow-lg); + animation: ds-bottom-sheet-in var(--duration-slow) var(--easing-entrance); + + &__handle { + width: 40px; + height: 4px; + margin: 0 auto 12px; + border-radius: var(--radius-full); + background: var(--color-border-strong); + } + + &__title { + padding: 0 12px 8px; + font-size: 13px; + font-weight: 700; + color: var(--color-text-tertiary); + } + + @media (prefers-reduced-motion: reduce) { + animation: none; + } +} + +@media (prefers-reduced-motion: reduce) { + .ds-bottom-sheet__overlay { + animation: none; + } +} + +@keyframes ds-bottom-sheet-in { + from { + transform: translateY(100%); + } +} + +@keyframes ds-bottom-sheet-fade { + from { + opacity: 0; + } +} diff --git a/frontend/web/components/base/Popover/BottomSheet.tsx b/frontend/web/components/base/Popover/BottomSheet.tsx new file mode 100644 index 000000000000..43baca60a442 --- /dev/null +++ b/frontend/web/components/base/Popover/BottomSheet.tsx @@ -0,0 +1,60 @@ +import { FC, ReactNode } from 'react' +import classNames from 'classnames' +import { + FloatingFocusManager, + FloatingOverlay, + FloatingPortal, + useDismiss, + useFloating, + useInteractions, + useRole, +} from '@floating-ui/react' +import './BottomSheet.scss' + +type BottomSheetProps = { + open: boolean + onOpenChange: (open: boolean) => void + title: string + children: ReactNode + className?: string + // Index of the focusable item to focus on open (defaults to the first) + initialFocus?: number +} + +const BottomSheet: FC = ({ + children, + className, + initialFocus = 0, + onOpenChange, + open, + title, +}) => { + const { context, refs } = useFloating({ onOpenChange, open }) + const { getFloatingProps } = useInteractions([ + useDismiss(context, { outsidePressEvent: 'mousedown' }), + useRole(context, { role: 'dialog' }), + ]) + + if (!open) return null + + return ( + + + +
+
+
{title}
+
{children}
+
+ + + + ) +} + +export default BottomSheet diff --git a/frontend/web/components/base/Popover/Popover.tsx b/frontend/web/components/base/Popover/Popover.tsx index e3b15be26acd..ed7d2a211012 100644 --- a/frontend/web/components/base/Popover/Popover.tsx +++ b/frontend/web/components/base/Popover/Popover.tsx @@ -14,6 +14,8 @@ import { useInteractions, useRole, } from '@floating-ui/react' +import { useBreakpoint } from 'common/hooks/useBreakpoint' +import BottomSheet from './BottomSheet' import './Popover.scss' export type PopoverTriggerProps = Record & { @@ -47,6 +49,8 @@ const Popover: FC = ({ }) => { const [isOpen, setIsOpen] = useState(false) const close = () => setIsOpen(false) + // Phones get a bottom sheet instead of an anchored panel + const asSheet = useBreakpoint('isPhone') const { context, floatingStyles, refs } = useFloating({ middleware: [ @@ -73,32 +77,47 @@ const Popover: FC = ({ }) const { getFloatingProps, getReferenceProps } = useInteractions([ useClick(context), - useDismiss(context), - useRole(context), + // The sheet handles its own dismiss and role + useDismiss(context, { enabled: !asSheet }), + useRole(context, { enabled: !asSheet }), ]) return ( <> {renderTrigger( - { ref: refs.setReference, ...getReferenceProps() }, + { + ref: refs.setReference, + ...getReferenceProps(), + // useRole is off for the sheet, so state the popup here + ...(asSheet && { + 'aria-expanded': isOpen, + 'aria-haspopup': 'dialog', + }), + }, isOpen, )} - {isOpen && ( - - -
- {typeof children === 'function' ? children(close) : children} -
-
-
+ {asSheet ? ( + + {typeof children === 'function' ? children(close) : children} + + ) : ( + isOpen && ( + + +
+ {typeof children === 'function' ? children(close) : children} +
+
+
+ ) )} ) From 52ece367c8ed9966fa527aee32060b849d28e585 Mon Sep 17 00:00:00 2001 From: Talisson Costa Date: Tue, 17 Nov 2026 12:22:55 -0300 Subject: [PATCH 2/7] fix(Popover): Keep the bottom sheet above the mobile browser toolbar Co-Authored-By: Claude Opus 5.5 --- frontend/web/components/base/Popover/BottomSheet.scss | 1 + 1 file changed, 1 insertion(+) diff --git a/frontend/web/components/base/Popover/BottomSheet.scss b/frontend/web/components/base/Popover/BottomSheet.scss index e3c286f7c22e..6638617562bf 100644 --- a/frontend/web/components/base/Popover/BottomSheet.scss +++ b/frontend/web/components/base/Popover/BottomSheet.scss @@ -10,6 +10,7 @@ .ds-bottom-sheet { width: 100%; max-height: 80vh; + max-height: 80dvh; overflow-y: auto; padding: 8px 12px calc(16px + env(safe-area-inset-bottom)); background: var(--color-surface-default); From ec338d1e915332c3e48902cb496aa19597b0a049 Mon Sep 17 00:00:00 2001 From: Talisson Costa Date: Tue, 17 Nov 2026 12:25:44 -0300 Subject: [PATCH 3/7] refactor(Popover): Drop the unneeded as const on breakpoint queries Co-Authored-By: Claude Opus 5.5 --- frontend/common/hooks/useBreakpoint.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/common/hooks/useBreakpoint.ts b/frontend/common/hooks/useBreakpoint.ts index b0789d00027f..590ebe328ce0 100644 --- a/frontend/common/hooks/useBreakpoint.ts +++ b/frontend/common/hooks/useBreakpoint.ts @@ -4,7 +4,7 @@ import { useEffect, useState } from 'react' // Add a size when a component needs it. export const BREAKPOINT_QUERIES = { isPhone: '(max-width: 767.98px)', // below md -} as const +} export type Breakpoint = keyof typeof BREAKPOINT_QUERIES From b0184b1418af6d3c298f7a77e35abfc5c5d06323 Mon Sep 17 00:00:00 2001 From: Talisson Costa Date: Tue, 17 Nov 2026 12:26:56 -0300 Subject: [PATCH 4/7] chore(Popover): Trim comments that repeat the code Co-Authored-By: Claude Opus 5.5 --- frontend/web/components/base/Popover/Popover.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/frontend/web/components/base/Popover/Popover.tsx b/frontend/web/components/base/Popover/Popover.tsx index ed7d2a211012..71832388c3cc 100644 --- a/frontend/web/components/base/Popover/Popover.tsx +++ b/frontend/web/components/base/Popover/Popover.tsx @@ -49,7 +49,6 @@ const Popover: FC = ({ }) => { const [isOpen, setIsOpen] = useState(false) const close = () => setIsOpen(false) - // Phones get a bottom sheet instead of an anchored panel const asSheet = useBreakpoint('isPhone') const { context, floatingStyles, refs } = useFloating({ From 85c4880db72a7e81ba11efb44ffb4940dacf5ed9 Mon Sep 17 00:00:00 2001 From: Talisson Costa Date: Tue, 17 Nov 2026 12:27:38 -0300 Subject: [PATCH 5/7] refactor(Popover): Move BottomSheet into its own folder Co-Authored-By: Claude Opus 5.5 --- .../components/base/Popover/{ => BottomSheet}/BottomSheet.scss | 0 .../components/base/Popover/{ => BottomSheet}/BottomSheet.tsx | 0 frontend/web/components/base/Popover/BottomSheet/index.ts | 1 + 3 files changed, 1 insertion(+) rename frontend/web/components/base/Popover/{ => BottomSheet}/BottomSheet.scss (100%) rename frontend/web/components/base/Popover/{ => BottomSheet}/BottomSheet.tsx (100%) create mode 100644 frontend/web/components/base/Popover/BottomSheet/index.ts diff --git a/frontend/web/components/base/Popover/BottomSheet.scss b/frontend/web/components/base/Popover/BottomSheet/BottomSheet.scss similarity index 100% rename from frontend/web/components/base/Popover/BottomSheet.scss rename to frontend/web/components/base/Popover/BottomSheet/BottomSheet.scss diff --git a/frontend/web/components/base/Popover/BottomSheet.tsx b/frontend/web/components/base/Popover/BottomSheet/BottomSheet.tsx similarity index 100% rename from frontend/web/components/base/Popover/BottomSheet.tsx rename to frontend/web/components/base/Popover/BottomSheet/BottomSheet.tsx diff --git a/frontend/web/components/base/Popover/BottomSheet/index.ts b/frontend/web/components/base/Popover/BottomSheet/index.ts new file mode 100644 index 000000000000..e08c7cb16787 --- /dev/null +++ b/frontend/web/components/base/Popover/BottomSheet/index.ts @@ -0,0 +1 @@ +export { default } from './BottomSheet' From e5f2156b3be3f652da92519aae7ef19bad05a139 Mon Sep 17 00:00:00 2001 From: Talisson Costa Date: Tue, 17 Nov 2026 12:33:25 -0300 Subject: [PATCH 6/7] fix(Popover): Drop the Chromatic viewport that clashes with the theme modes Co-Authored-By: Claude Opus 5.5 --- frontend/documentation/components/Popover.stories.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/frontend/documentation/components/Popover.stories.tsx b/frontend/documentation/components/Popover.stories.tsx index 290f6509c56e..2f069b3f71ea 100644 --- a/frontend/documentation/components/Popover.stories.tsx +++ b/frontend/documentation/components/Popover.stories.tsx @@ -59,7 +59,6 @@ export const Open: Story = { export const MobileSheet: Story = { globals: { viewport: { isRotated: false, value: 'mobile1' } }, parameters: { - chromatic: { delay: 300, viewports: [375] }, docs: { story: { height: '420px', inline: false } }, }, play: async ({ canvasElement }: { canvasElement: HTMLElement }) => { From fde57c4319304f5e67ac02d5a480353fb8b3614b Mon Sep 17 00:00:00 2001 From: Talisson Costa Date: Wed, 18 Nov 2026 10:07:48 -0300 Subject: [PATCH 7/7] fix(Popover): Focus the bottom sheet after it slides in, so it doesn't jump Co-Authored-By: Claude Opus 5.5 --- .../base/Popover/BottomSheet/BottomSheet.tsx | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/frontend/web/components/base/Popover/BottomSheet/BottomSheet.tsx b/frontend/web/components/base/Popover/BottomSheet/BottomSheet.tsx index 43baca60a442..a0ac946fff91 100644 --- a/frontend/web/components/base/Popover/BottomSheet/BottomSheet.tsx +++ b/frontend/web/components/base/Popover/BottomSheet/BottomSheet.tsx @@ -1,4 +1,4 @@ -import { FC, ReactNode } from 'react' +import { AnimationEvent, FC, ReactNode, useEffect, useRef } from 'react' import classNames from 'classnames' import { FloatingFocusManager, @@ -21,6 +21,9 @@ type BottomSheetProps = { initialFocus?: number } +const FOCUSABLE = + 'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])' + const BottomSheet: FC = ({ children, className, @@ -34,17 +37,44 @@ const BottomSheet: FC = ({ useDismiss(context, { outsidePressEvent: 'mousedown' }), useRole(context, { role: 'dialog' }), ]) + const sheetRef = useRef(null) + + const focusInitialItem = () => { + const items = sheetRef.current?.querySelectorAll(FOCUSABLE) + const target = items?.[initialFocus] ?? sheetRef.current + target?.focus({ preventScroll: true }) + } + + // Focusing during the slide-in makes the browser scroll to the item, which + // cuts the animation short (Safari also jumps). Focus once it has finished. + const onAnimationEnd = (e: AnimationEvent) => { + if (e.target === e.currentTarget) focusInitialItem() + } + + // No animation runs with reduced motion, so focus straight away. + useEffect(() => { + if (!open) return + if (window.matchMedia?.('(prefers-reduced-motion: reduce)').matches) { + focusInitialItem() + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [open]) if (!open) return null return ( - +
{ + sheetRef.current = node + refs.setFloating(node) + }} + tabIndex={-1} className={classNames('ds-bottom-sheet', className)} aria-label={title} + onAnimationEnd={onAnimationEnd} {...getFloatingProps()} >