Repository navigation
feat(Popover): Open as a bottom sheet on phones #8995
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
Merged
+351
−25
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
75646c6
feat(Popover): Open as a bottom sheet on phones
talissoncosta 836fa39
fix(Popover): Keep the bottom sheet above the mobile browser toolbar
talissoncosta 6179293
refactor(Popover): Drop the unneeded as const on breakpoint queries
talissoncosta 23a2529
chore(Popover): Trim comments that repeat the code
talissoncosta af65009
refactor(Popover): Move BottomSheet into its own folder
talissoncosta 222896a
fix(Popover): Drop the Chromatic viewport that clashes with the theme…
talissoncosta 21d1f52
fix(Popover): Focus the bottom sheet after it slides in, so it doesn'…
talissoncosta f563824
feat(Popover): Give the bottom sheet a header with a close button
talissoncosta 88712c2
fix(AuditLog): Fit the scope filter to the bottom sheet on phones
talissoncosta 94ed0f2
refactor(Popover): Standardise the sheet breakpoint, touch target and…
talissoncosta 2f3fdde
fix(Popover): Only render sheet content while it is open
talissoncosta ebbe978
refactor(Popover): Use HeadingText for the bottom sheet title
talissoncosta 999b6c9
fix(Popover): Keep desktop panel classes off the phone sheet
talissoncosta 9df2864
refactor(Popover): Size the panel itself instead of borrowing an Inli…
talissoncosta e1ce968
fix(Popover): Let the focus manager focus the sheet with reduced motion
talissoncosta 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| 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 }, | ||
| ) => { | ||
| Object.defineProperty(globalThis, 'window', { | ||
| configurable: true, | ||
| value: matchMedia ? { matchMedia } : {}, | ||
| }) | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| Reflect.deleteProperty(globalThis, '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) | ||
| }) | ||
| }) |
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,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 | ||
| } | ||
|
|
||
| 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 | ||
| } |
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
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
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
46 changes: 46 additions & 0 deletions
46
frontend/web/components/base/Popover/BottomSheet/BottomSheet.scss
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,46 @@ | ||
| .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; | ||
| max-height: 80dvh; | ||
| 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); | ||
|
|
||
| &__header { | ||
| padding: 0 0 8px 12px; | ||
| } | ||
|
|
||
| @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; | ||
| } | ||
| } |
100 changes: 100 additions & 0 deletions
100
frontend/web/components/base/Popover/BottomSheet/BottomSheet.tsx
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,100 @@ | ||
| import { AnimationEvent, FC, ReactNode, useId, useRef } from 'react' | ||
| import { HeadingText } from 'components/base/typography' | ||
| import IconButton from 'components/base/IconButton' | ||
| import Icon from 'components/icons/Icon' | ||
| 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 | ||
| // Index of the focusable item to focus on open (defaults to the first) | ||
| 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<BottomSheetProps> = ({ | ||
| children, | ||
| initialFocus = 0, | ||
| onOpenChange, | ||
| open, | ||
| title, | ||
| }) => { | ||
| const { context, refs } = useFloating({ onOpenChange, open }) | ||
| const { getFloatingProps } = useInteractions([ | ||
| useDismiss(context, { outsidePressEvent: 'mousedown' }), | ||
| useRole(context, { role: 'dialog' }), | ||
| ]) | ||
| const sheetRef = useRef<HTMLDivElement | null>(null) | ||
| const titleId = useId() | ||
|
|
||
| const focusInitialItem = () => { | ||
| const items = sheetRef.current?.querySelectorAll<HTMLElement>(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<HTMLDivElement>) => { | ||
| if (e.target === e.currentTarget) focusInitialItem() | ||
| } | ||
|
|
||
| if (!open) return null | ||
|
|
||
| // No animation runs with reduced motion, so the focus manager focuses on open | ||
| const prefersReducedMotion = !!window.matchMedia?.( | ||
| '(prefers-reduced-motion: reduce)', | ||
| ).matches | ||
|
|
||
| return ( | ||
| <FloatingPortal> | ||
| <FloatingOverlay className='ds-bottom-sheet__overlay' lockScroll> | ||
| <FloatingFocusManager | ||
| context={context} | ||
| initialFocus={prefersReducedMotion ? initialFocus : -1} | ||
| > | ||
| <div | ||
| ref={(node) => { | ||
| sheetRef.current = node | ||
| refs.setFloating(node) | ||
| }} | ||
| tabIndex={-1} | ||
| className='ds-bottom-sheet' | ||
| aria-labelledby={titleId} | ||
| onAnimationEnd={onAnimationEnd} | ||
| {...getFloatingProps()} | ||
| > | ||
| <div className='ds-bottom-sheet__header d-flex align-items-center justify-content-between gap-2'> | ||
| <HeadingText id={titleId} level={2} size='sm'> | ||
| {title} | ||
| </HeadingText> | ||
| <IconButton | ||
| aria-label='Close' | ||
| variant='ghost' | ||
| onClick={() => onOpenChange(false)} | ||
| > | ||
| <Icon name='close' width={20} /> | ||
| </IconButton> | ||
| </div> | ||
| {children} | ||
| </div> | ||
| </FloatingFocusManager> | ||
| </FloatingOverlay> | ||
| </FloatingPortal> | ||
| ) | ||
| } | ||
|
|
||
| export default BottomSheet |
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 @@ | ||
| export { default } from './BottomSheet' |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.