gooee 0.0.0-stage → 0.41.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +99 -2
- package/package.json +36 -4
- package/scripts/add.js +533 -0
- package/scripts/pro-components.json +82 -0
- package/src/base.css +39 -0
- package/src/components/Accordion/Accordion.module.css +90 -0
- package/src/components/Accordion/Accordion.tsx +125 -0
- package/src/components/Accordion/index.ts +1 -0
- package/src/components/Alert/Alert.module.css +121 -0
- package/src/components/Alert/Alert.tsx +90 -0
- package/src/components/Alert/index.ts +1 -0
- package/src/components/AlertDialog/AlertDialog.module.css +2 -0
- package/src/components/AlertDialog/AlertDialog.tsx +108 -0
- package/src/components/AlertDialog/index.ts +1 -0
- package/src/components/AnimatedNumber/AnimatedNumber.module.css +48 -0
- package/src/components/AnimatedNumber/AnimatedNumber.tsx +52 -0
- package/src/components/AnimatedNumber/index.ts +1 -0
- package/src/components/AspectRatio/AspectRatio.module.css +15 -0
- package/src/components/AspectRatio/AspectRatio.tsx +12 -0
- package/src/components/AspectRatio/index.ts +1 -0
- package/src/components/Avatar/Avatar.module.css +62 -0
- package/src/components/Avatar/Avatar.tsx +55 -0
- package/src/components/Avatar/index.ts +3 -0
- package/src/components/AvatarGroup/AvatarGroup.module.css +53 -0
- package/src/components/AvatarGroup/AvatarGroup.tsx +43 -0
- package/src/components/AvatarGroup/index.ts +1 -0
- package/src/components/Badge/Badge.module.css +69 -0
- package/src/components/Badge/Badge.tsx +25 -0
- package/src/components/Badge/index.ts +1 -0
- package/src/components/Breadcrumb/Breadcrumb.module.css +104 -0
- package/src/components/Breadcrumb/Breadcrumb.tsx +76 -0
- package/src/components/Breadcrumb/index.ts +1 -0
- package/src/components/Button/Button.module.css +191 -0
- package/src/components/Button/Button.tsx +148 -0
- package/src/components/Button/index.ts +1 -0
- package/src/components/ButtonGroup/ButtonGroup.module.css +59 -0
- package/src/components/ButtonGroup/ButtonGroup.tsx +45 -0
- package/src/components/ButtonGroup/index.ts +1 -0
- package/src/components/Card/Card.module.css +82 -0
- package/src/components/Card/Card.tsx +34 -0
- package/src/components/Card/index.ts +1 -0
- package/src/components/Center/Center.module.css +15 -0
- package/src/components/Center/Center.tsx +28 -0
- package/src/components/Center/index.ts +1 -0
- package/src/components/Checkbox/Checkbox.module.css +120 -0
- package/src/components/Checkbox/Checkbox.tsx +133 -0
- package/src/components/Checkbox/index.ts +1 -0
- package/src/components/CheckboxGroup/CheckboxGroup.module.css +30 -0
- package/src/components/CheckboxGroup/CheckboxGroup.tsx +88 -0
- package/src/components/CheckboxGroup/index.ts +1 -0
- package/src/components/Cluster/Cluster.module.css +25 -0
- package/src/components/Cluster/Cluster.tsx +26 -0
- package/src/components/Cluster/index.ts +1 -0
- package/src/components/CodeBlock/CodeBlock.module.css +74 -0
- package/src/components/CodeBlock/CodeBlock.tsx +53 -0
- package/src/components/CodeBlock/highlight.ts +73 -0
- package/src/components/CodeBlock/index.ts +1 -0
- package/src/components/Collapsible/Collapsible.module.css +75 -0
- package/src/components/Collapsible/Collapsible.tsx +71 -0
- package/src/components/Collapsible/index.ts +1 -0
- package/src/components/Container/Container.module.css +14 -0
- package/src/components/Container/Container.tsx +19 -0
- package/src/components/Container/index.ts +1 -0
- package/src/components/CopyButton/CopyButton.module.css +125 -0
- package/src/components/CopyButton/CopyButton.tsx +160 -0
- package/src/components/CopyButton/index.ts +1 -0
- package/src/components/Dial/Dial.module.css +344 -0
- package/src/components/Dial/Dial.tsx +332 -0
- package/src/components/Dial/index.ts +1 -0
- package/src/components/Dialog/Dialog.module.css +190 -0
- package/src/components/Dialog/Dialog.tsx +100 -0
- package/src/components/Dialog/index.ts +1 -0
- package/src/components/Divider/Divider.module.css +39 -0
- package/src/components/Divider/Divider.tsx +34 -0
- package/src/components/Divider/index.ts +1 -0
- package/src/components/DropdownMenu/DropdownMenu.module.css +134 -0
- package/src/components/DropdownMenu/DropdownMenu.tsx +215 -0
- package/src/components/DropdownMenu/index.ts +1 -0
- package/src/components/EmptyState/EmptyState.module.css +66 -0
- package/src/components/EmptyState/EmptyState.tsx +38 -0
- package/src/components/EmptyState/index.ts +1 -0
- package/src/components/Fab/Fab.module.css +176 -0
- package/src/components/Fab/Fab.tsx +117 -0
- package/src/components/Fab/index.ts +1 -0
- package/src/components/Fieldset/Fieldset.module.css +34 -0
- package/src/components/Fieldset/Fieldset.tsx +41 -0
- package/src/components/Fieldset/index.ts +1 -0
- package/src/components/FormField/FormField.module.css +62 -0
- package/src/components/FormField/FormField.tsx +91 -0
- package/src/components/FormField/index.ts +2 -0
- package/src/components/Grid/Grid.module.css +39 -0
- package/src/components/Grid/Grid.tsx +35 -0
- package/src/components/Grid/index.ts +1 -0
- package/src/components/HoldToConfirm/HoldToConfirm.module.css +175 -0
- package/src/components/HoldToConfirm/HoldToConfirm.tsx +334 -0
- package/src/components/HoldToConfirm/index.ts +1 -0
- package/src/components/IconButton/IconButton.module.css +78 -0
- package/src/components/IconButton/IconButton.tsx +35 -0
- package/src/components/IconButton/index.ts +1 -0
- package/src/components/Input/Input.module.css +157 -0
- package/src/components/Input/Input.tsx +133 -0
- package/src/components/Input/index.ts +1 -0
- package/src/components/InputGroup/InputGroup.module.css +146 -0
- package/src/components/InputGroup/InputGroup.tsx +90 -0
- package/src/components/InputGroup/index.ts +1 -0
- package/src/components/Kbd/Kbd.module.css +28 -0
- package/src/components/Kbd/Kbd.tsx +38 -0
- package/src/components/Kbd/index.ts +1 -0
- package/src/components/Link/Link.module.css +65 -0
- package/src/components/Link/Link.tsx +41 -0
- package/src/components/Link/index.ts +1 -0
- package/src/components/LivePill/LivePill.module.css +207 -0
- package/src/components/LivePill/LivePill.tsx +182 -0
- package/src/components/LivePill/index.ts +1 -0
- package/src/components/Meter/Meter.module.css +91 -0
- package/src/components/Meter/Meter.tsx +109 -0
- package/src/components/Meter/index.ts +1 -0
- package/src/components/MorphButton/MorphButton.module.css +152 -0
- package/src/components/MorphButton/MorphButton.tsx +203 -0
- package/src/components/MorphButton/index.ts +1 -0
- package/src/components/NumberInput/NumberInput.module.css +104 -0
- package/src/components/NumberInput/NumberInput.tsx +252 -0
- package/src/components/NumberInput/index.ts +2 -0
- package/src/components/Pagination/Pagination.module.css +88 -0
- package/src/components/Pagination/Pagination.tsx +126 -0
- package/src/components/Pagination/index.ts +1 -0
- package/src/components/PasswordInput/PasswordInput.module.css +64 -0
- package/src/components/PasswordInput/PasswordInput.tsx +63 -0
- package/src/components/PasswordInput/index.ts +2 -0
- package/src/components/PickerWheel/PickerWheel.module.css +123 -0
- package/src/components/PickerWheel/PickerWheel.tsx +326 -0
- package/src/components/PickerWheel/index.ts +1 -0
- package/src/components/Popover/Popover.module.css +78 -0
- package/src/components/Popover/Popover.tsx +117 -0
- package/src/components/Popover/index.ts +1 -0
- package/src/components/ProgressBar/ProgressBar.module.css +61 -0
- package/src/components/ProgressBar/ProgressBar.tsx +48 -0
- package/src/components/ProgressBar/index.ts +1 -0
- package/src/components/Prose/Prose.module.css +115 -0
- package/src/components/Prose/Prose.tsx +14 -0
- package/src/components/Prose/index.ts +1 -0
- package/src/components/Radio/Radio.module.css +106 -0
- package/src/components/Radio/Radio.tsx +115 -0
- package/src/components/Radio/index.ts +1 -0
- package/src/components/Retime/Retime.tsx +116 -0
- package/src/components/Retime/index.ts +1 -0
- package/src/components/RulerScrubber/RulerScrubber.module.css +146 -0
- package/src/components/RulerScrubber/RulerScrubber.tsx +209 -0
- package/src/components/RulerScrubber/index.ts +1 -0
- package/src/components/ScrollArea/ScrollArea.module.css +72 -0
- package/src/components/ScrollArea/ScrollArea.tsx +172 -0
- package/src/components/ScrollArea/index.ts +1 -0
- package/src/components/SearchInput/SearchInput.module.css +53 -0
- package/src/components/SearchInput/SearchInput.tsx +104 -0
- package/src/components/SearchInput/index.ts +2 -0
- package/src/components/SegmentedControl/SegmentedControl.module.css +150 -0
- package/src/components/SegmentedControl/SegmentedControl.tsx +169 -0
- package/src/components/SegmentedControl/index.ts +1 -0
- package/src/components/Select/Select.module.css +204 -0
- package/src/components/Select/Select.tsx +254 -0
- package/src/components/Select/index.ts +1 -0
- package/src/components/Sheet/Sheet.module.css +228 -0
- package/src/components/Sheet/Sheet.tsx +449 -0
- package/src/components/Sheet/index.ts +1 -0
- package/src/components/Skeleton/Skeleton.module.css +56 -0
- package/src/components/Skeleton/Skeleton.tsx +37 -0
- package/src/components/Skeleton/index.ts +1 -0
- package/src/components/Slider/Slider.module.css +155 -0
- package/src/components/Slider/Slider.tsx +200 -0
- package/src/components/Slider/index.ts +1 -0
- package/src/components/Spinner/Spinner.module.css +50 -0
- package/src/components/Spinner/Spinner.tsx +29 -0
- package/src/components/Spinner/index.ts +1 -0
- package/src/components/Split/Split.module.css +29 -0
- package/src/components/Split/Split.tsx +33 -0
- package/src/components/Split/index.ts +1 -0
- package/src/components/SplitFlap/SplitFlap.module.css +107 -0
- package/src/components/SplitFlap/SplitFlap.tsx +225 -0
- package/src/components/SplitFlap/index.ts +1 -0
- package/src/components/Stack/Stack.module.css +17 -0
- package/src/components/Stack/Stack.tsx +19 -0
- package/src/components/Stack/index.ts +1 -0
- package/src/components/SwipeDeck/SwipeDeck.module.css +250 -0
- package/src/components/SwipeDeck/SwipeDeck.tsx +350 -0
- package/src/components/SwipeDeck/index.ts +1 -0
- package/src/components/Switch/Switch.module.css +97 -0
- package/src/components/Switch/Switch.tsx +147 -0
- package/src/components/Switch/index.ts +1 -0
- package/src/components/Table/Table.module.css +369 -0
- package/src/components/Table/Table.tsx +368 -0
- package/src/components/Table/index.ts +1 -0
- package/src/components/TableOfContents/TableOfContents.module.css +107 -0
- package/src/components/TableOfContents/TableOfContents.tsx +93 -0
- package/src/components/TableOfContents/index.ts +1 -0
- package/src/components/Tabs/Tabs.module.css +101 -0
- package/src/components/Tabs/Tabs.tsx +190 -0
- package/src/components/Tabs/index.ts +1 -0
- package/src/components/Tag/Tag.module.css +120 -0
- package/src/components/Tag/Tag.tsx +109 -0
- package/src/components/Tag/index.ts +1 -0
- package/src/components/Textarea/Textarea.module.css +79 -0
- package/src/components/Textarea/Textarea.tsx +130 -0
- package/src/components/Textarea/index.ts +1 -0
- package/src/components/ThemeToggle/ThemeToggle.module.css +21 -0
- package/src/components/ThemeToggle/ThemeToggle.tsx +69 -0
- package/src/components/ThemeToggle/index.ts +1 -0
- package/src/components/Toast/Toast.module.css +202 -0
- package/src/components/Toast/Toast.tsx +473 -0
- package/src/components/Toast/index.ts +1 -0
- package/src/components/Toggle/Toggle.module.css +94 -0
- package/src/components/Toggle/Toggle.tsx +50 -0
- package/src/components/Toggle/index.ts +1 -0
- package/src/components/Tooltip/Tooltip.module.css +34 -0
- package/src/components/Tooltip/Tooltip.tsx +154 -0
- package/src/components/Tooltip/index.ts +1 -0
- package/src/components/VisuallyHidden/VisuallyHidden.module.css +13 -0
- package/src/components/VisuallyHidden/VisuallyHidden.tsx +12 -0
- package/src/components/VisuallyHidden/index.ts +1 -0
- package/src/internal/FieldMessage.module.css +49 -0
- package/src/internal/FieldMessage.tsx +44 -0
- package/src/internal/Portal.tsx +7 -0
- package/src/internal/Roll.module.css +31 -0
- package/src/internal/Roll.tsx +20 -0
- package/src/internal/cx.ts +3 -0
- package/src/internal/feel.ts +355 -0
- package/src/internal/fling.ts +118 -0
- package/src/internal/focus.ts +94 -0
- package/src/internal/icons.module.css +62 -0
- package/src/internal/icons.tsx +511 -0
- package/src/internal/keyboard.ts +62 -0
- package/src/internal/layers.ts +85 -0
- package/src/internal/liquid.ts +248 -0
- package/src/internal/liquidMatter.ts +168 -0
- package/src/internal/liquidMore.ts +686 -0
- package/src/internal/liquidPath.ts +519 -0
- package/src/internal/mergeRefs.ts +10 -0
- package/src/internal/motion.ts +37 -0
- package/src/internal/relativeTime.ts +26 -0
- package/src/internal/scrollFrame.ts +157 -0
- package/src/internal/softPress.ts +299 -0
- package/src/internal/space.ts +6 -0
- package/src/internal/useControllable.ts +21 -0
- package/src/internal/useFloating.ts +85 -0
- package/src/internal/usePresence.ts +15 -0
- package/src/internal/useScrub.ts +160 -0
- package/src/tokens.css +727 -0
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useEffect, useId, useLayoutEffect, useRef, useState, type FocusEvent, type KeyboardEvent, type PointerEvent, type ReactNode, type RefObject } from 'react'
|
|
4
|
+
import { cx } from '../../internal/cx'
|
|
5
|
+
import { release, rubber, speedTracker, useDragClose } from '../../internal/fling'
|
|
6
|
+
import { returnTarget, useFocusTrap } from '../../internal/focus'
|
|
7
|
+
import { XIcon } from '../../internal/icons'
|
|
8
|
+
import { useDismiss, useScrollLock } from '../../internal/layers'
|
|
9
|
+
import { Portal } from '../../internal/Portal'
|
|
10
|
+
import { usePresence } from '../../internal/usePresence'
|
|
11
|
+
import styles from './Sheet.module.css'
|
|
12
|
+
|
|
13
|
+
/** A height a sheet can rest at: low with only its title showing, half the screen, or nearly all of it. */
|
|
14
|
+
export type SheetDetent = 'peek' | 'half' | 'full'
|
|
15
|
+
|
|
16
|
+
export type SheetProps = {
|
|
17
|
+
/** Whether the sheet is open. You always control it. */
|
|
18
|
+
open: boolean
|
|
19
|
+
/** Called when the sheet asks to close, by Esc, outside tap, drag or close button, with false. */
|
|
20
|
+
onOpenChange: (open: boolean) => void
|
|
21
|
+
/** Visible heading, also the dialog's accessible name. */
|
|
22
|
+
title: ReactNode
|
|
23
|
+
/** Text under the title, also read as the dialog's description. */
|
|
24
|
+
description?: ReactNode
|
|
25
|
+
/** Main content of the sheet. */
|
|
26
|
+
children?: ReactNode
|
|
27
|
+
/** Sticky actions at the bottom of the sheet. */
|
|
28
|
+
footer?: ReactNode
|
|
29
|
+
/** Screen reader name for the close button. */
|
|
30
|
+
closeLabel?: string
|
|
31
|
+
/** Element to focus first. Defaults to the first focusable element in the content. */
|
|
32
|
+
initialFocus?: RefObject<HTMLElement | null>
|
|
33
|
+
/** Extra class for the panel. */
|
|
34
|
+
className?: string
|
|
35
|
+
/** Heights the sheet rests at, such as ['peek', 'half', 'full']. Below the highest one it is not modal: the page behind stays usable. */
|
|
36
|
+
detents?: SheetDetent[]
|
|
37
|
+
/** The height it rests at, when you control it. */
|
|
38
|
+
detent?: SheetDetent
|
|
39
|
+
/** The height it opens at when you do not control it. Defaults to the lowest. */
|
|
40
|
+
defaultDetent?: SheetDetent
|
|
41
|
+
/** Called with the new height after a drag, a tap or a key on the grabber, or Esc. */
|
|
42
|
+
onDetentChange?: (detent: SheetDetent) => void
|
|
43
|
+
/** Screen reader name for the grabber, which sets the height. */
|
|
44
|
+
heightLabel?: string
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
type BaseProps = Omit<SheetProps, 'detents' | 'detent' | 'defaultDetent' | 'onDetentChange' | 'heightLabel' | 'closeLabel'> & { closeLabel: string }
|
|
48
|
+
|
|
49
|
+
/** With detents, the grabber becomes a control for the height, and the sheet is modal only at its highest. */
|
|
50
|
+
export function Sheet({
|
|
51
|
+
open,
|
|
52
|
+
onOpenChange,
|
|
53
|
+
title,
|
|
54
|
+
description,
|
|
55
|
+
children,
|
|
56
|
+
footer,
|
|
57
|
+
closeLabel = 'Close',
|
|
58
|
+
initialFocus,
|
|
59
|
+
className,
|
|
60
|
+
detents,
|
|
61
|
+
detent,
|
|
62
|
+
defaultDetent,
|
|
63
|
+
onDetentChange,
|
|
64
|
+
heightLabel = 'Sheet height',
|
|
65
|
+
}: SheetProps) {
|
|
66
|
+
const base = { open, onOpenChange, title, description, children, footer, closeLabel, initialFocus, className }
|
|
67
|
+
if (!detents?.length) return <PlainSheet {...base} />
|
|
68
|
+
return <DetentSheet {...base} detents={detents} detent={detent} defaultDetent={defaultDetent} onDetentChange={onDetentChange} heightLabel={heightLabel} />
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function PlainSheet({ open, onOpenChange, title, description, children, footer, closeLabel, initialFocus, className }: BaseProps) {
|
|
72
|
+
const id = useId()
|
|
73
|
+
const panelRef = useRef<HTMLDivElement>(null)
|
|
74
|
+
const { mounted, state } = usePresence(open, 'morph-out')
|
|
75
|
+
const grab = useDragClose(panelRef, open, () => onOpenChange(false))
|
|
76
|
+
|
|
77
|
+
useScrollLock(open)
|
|
78
|
+
useDismiss(open, [panelRef], () => onOpenChange(false))
|
|
79
|
+
useFocusTrap(panelRef, open && mounted, { initialFocus })
|
|
80
|
+
|
|
81
|
+
if (!mounted) return null
|
|
82
|
+
return (
|
|
83
|
+
<Portal>
|
|
84
|
+
<div className={styles.overlay} data-state={state} aria-hidden="true" />
|
|
85
|
+
<div
|
|
86
|
+
ref={panelRef}
|
|
87
|
+
role="dialog"
|
|
88
|
+
aria-modal="true"
|
|
89
|
+
aria-labelledby={`${id}-title`}
|
|
90
|
+
aria-describedby={description ? `${id}-desc` : undefined}
|
|
91
|
+
tabIndex={-1}
|
|
92
|
+
className={cx(styles.sheet, className)}
|
|
93
|
+
data-state={state}
|
|
94
|
+
onAnimationEnd={(e) => {
|
|
95
|
+
if (e.target === panelRef.current && state === 'open') panelRef.current.style.transform = ''
|
|
96
|
+
}}
|
|
97
|
+
>
|
|
98
|
+
<div
|
|
99
|
+
className={styles.grab}
|
|
100
|
+
{...grab}
|
|
101
|
+
>
|
|
102
|
+
<span className={styles.handle} aria-hidden="true" />
|
|
103
|
+
<header className={styles.header}>
|
|
104
|
+
<div className={styles.titles}>
|
|
105
|
+
<h2 id={`${id}-title`} className={styles.title}>{title}</h2>
|
|
106
|
+
{description && <p id={`${id}-desc`} className={styles.description}>{description}</p>}
|
|
107
|
+
</div>
|
|
108
|
+
<button type="button" className={styles.close} aria-label={closeLabel} data-skip-initial-focus onClick={() => onOpenChange(false)}>
|
|
109
|
+
<XIcon />
|
|
110
|
+
</button>
|
|
111
|
+
</header>
|
|
112
|
+
</div>
|
|
113
|
+
{children && <div className={styles.body}>{children}</div>}
|
|
114
|
+
{footer && <footer className={styles.footer}>{footer}</footer>}
|
|
115
|
+
</div>
|
|
116
|
+
</Portal>
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Shares of the screen, from the heights Apple Maps on the web and Vaul rest at.
|
|
121
|
+
const SHARE: Record<SheetDetent, number> = { peek: 0.19, half: 0.5, full: 0.96 }
|
|
122
|
+
const ORDER: SheetDetent[] = ['peek', 'half', 'full']
|
|
123
|
+
const NAME: Record<SheetDetent, string> = { peek: 'Low', half: 'Half', full: 'Full' }
|
|
124
|
+
// px per second: faster than this, a let-go is a flick and moves one height its way.
|
|
125
|
+
const FLICK = 400
|
|
126
|
+
// How far ahead, in seconds, a slow let-go is carried before the nearest height is picked.
|
|
127
|
+
const PROJECT = 0.2
|
|
128
|
+
// A press is not a drag until it moves this far, so taps on the content still work.
|
|
129
|
+
const SLOP = 6
|
|
130
|
+
|
|
131
|
+
const nearest = (v: number, hs: number[]) => hs.reduce((best, h, i) => (Math.abs(h - v) < Math.abs(hs[best] - v) ? i : best), 0)
|
|
132
|
+
|
|
133
|
+
// A flick moves one height from where the drag began, never two, even when the hand has already
|
|
134
|
+
// passed the next one. Real sheets measured all do this.
|
|
135
|
+
function pick(pos: number, v: number, start: number, hs: number[]) {
|
|
136
|
+
if (Math.abs(v) <= FLICK) return nearest(pos + v * PROJECT, hs)
|
|
137
|
+
return Math.max(0, Math.min(hs.length - 1, start + Math.sign(v)))
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
type DetentProps = BaseProps & Required<Pick<SheetProps, 'detents' | 'heightLabel'>> & Pick<SheetProps, 'detent' | 'defaultDetent' | 'onDetentChange'>
|
|
141
|
+
|
|
142
|
+
function DetentSheet({ open, onOpenChange, title, description, children, footer, closeLabel, initialFocus, className, detents, detent, defaultDetent, onDetentChange, heightLabel }: DetentProps) {
|
|
143
|
+
const id = useId()
|
|
144
|
+
const order = ORDER.filter((d) => detents.includes(d))
|
|
145
|
+
const top = order.length - 1
|
|
146
|
+
const [inner, setInner] = useState<SheetDetent>(defaultDetent ?? order[0])
|
|
147
|
+
const index = Math.max(0, order.indexOf(detent ?? inner))
|
|
148
|
+
const atTop = index === top
|
|
149
|
+
const { mounted, state } = usePresence(open, 'morph-out')
|
|
150
|
+
const [vh, setVh] = useState(() => (typeof window === 'undefined' ? 0 : window.innerHeight))
|
|
151
|
+
const heights = order.map((d) => Math.round(vh * SHARE[d]))
|
|
152
|
+
const [dragging, setDragging] = useState(false)
|
|
153
|
+
const [target, setTarget] = useState(index)
|
|
154
|
+
const [said, setSaid] = useState('')
|
|
155
|
+
|
|
156
|
+
const panelRef = useRef<HTMLDivElement>(null)
|
|
157
|
+
const overlayRef = useRef<HTMLDivElement>(null)
|
|
158
|
+
const grabRef = useRef<HTMLDivElement>(null)
|
|
159
|
+
const bodyRef = useRef<HTMLDivElement>(null)
|
|
160
|
+
const at = useRef(0)
|
|
161
|
+
const aimed = useRef(-1)
|
|
162
|
+
const stopGlide = useRef(() => {})
|
|
163
|
+
const unlisten = useRef(() => {})
|
|
164
|
+
const opener = useRef<HTMLElement | null>(null)
|
|
165
|
+
const trapStart = useRef<HTMLElement | null>(null)
|
|
166
|
+
const drag = useRef<{ y0: number; at0: number; start: number; pointer: number; live: boolean; speed: ReturnType<typeof speedTracker> } | null>(null)
|
|
167
|
+
const live = useRef({ heights, index, open })
|
|
168
|
+
live.current = { heights, index, open }
|
|
169
|
+
|
|
170
|
+
const change = (i: number) => {
|
|
171
|
+
if (detent === undefined) setInner(order[i])
|
|
172
|
+
onDetentChange?.(order[i])
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const paint = (v: number) => {
|
|
176
|
+
at.current = v
|
|
177
|
+
const hs = live.current.heights
|
|
178
|
+
const panel = panelRef.current
|
|
179
|
+
if (!panel) return
|
|
180
|
+
panel.style.transform = `translateY(${hs[hs.length - 1] - v}px)`
|
|
181
|
+
// The dim follows the height between the two highest, so it never jumps.
|
|
182
|
+
const lo = hs.length > 1 ? hs[hs.length - 2] : 0
|
|
183
|
+
const dim = Math.max(0, Math.min(1, (v - lo) / (hs[hs.length - 1] - lo || 1)))
|
|
184
|
+
overlayRef.current?.style.setProperty('opacity', String(dim))
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const settle = (to: number, v: number, done?: () => void) => {
|
|
188
|
+
stopGlide.current()
|
|
189
|
+
stopGlide.current = release(at.current, to, v, paint, done)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const shownHidden = () => {
|
|
193
|
+
const active = document.activeElement as HTMLElement | null
|
|
194
|
+
return !!active && !!bodyRef.current?.contains(active) && active.getBoundingClientRect().top > window.innerHeight - 8
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const go = (i: number, v = 0, spoken = true) => {
|
|
198
|
+
const hs = live.current.heights
|
|
199
|
+
if (i < 0) {
|
|
200
|
+
// A glide carries it off at the hand's speed, so the CSS close stays out of the way.
|
|
201
|
+
panelRef.current?.setAttribute('data-fluid', '')
|
|
202
|
+
settle(0, v)
|
|
203
|
+
onOpenChange(false)
|
|
204
|
+
requestAnimationFrame(() => {
|
|
205
|
+
if (live.current.open) {
|
|
206
|
+
panelRef.current?.removeAttribute('data-fluid')
|
|
207
|
+
settle(hs[live.current.index], 0)
|
|
208
|
+
}
|
|
209
|
+
})
|
|
210
|
+
return
|
|
211
|
+
}
|
|
212
|
+
aimed.current = i
|
|
213
|
+
setTarget(i)
|
|
214
|
+
if (i !== live.current.index) {
|
|
215
|
+
change(i)
|
|
216
|
+
if (spoken) setSaid(`${NAME[order[i]]} height`)
|
|
217
|
+
}
|
|
218
|
+
// Shrunk below a focused control: focus goes to the grabber, so it never sits on something hidden.
|
|
219
|
+
settle(hs[i], v, () => shownHidden() && grabRef.current?.focus({ preventScroll: true }))
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const stepDown = () => (live.current.index > 0 ? go(live.current.index - 1) : onOpenChange(false))
|
|
223
|
+
|
|
224
|
+
useEffect(() => {
|
|
225
|
+
const onResize = () => setVh(window.innerHeight)
|
|
226
|
+
window.addEventListener('resize', onResize)
|
|
227
|
+
return () => window.removeEventListener('resize', onResize)
|
|
228
|
+
}, [])
|
|
229
|
+
|
|
230
|
+
useLayoutEffect(() => {
|
|
231
|
+
if (!open) return
|
|
232
|
+
opener.current = returnTarget()
|
|
233
|
+
aimed.current = -1
|
|
234
|
+
}, [open])
|
|
235
|
+
|
|
236
|
+
useLayoutEffect(() => {
|
|
237
|
+
if (!open || !mounted) return
|
|
238
|
+
if (aimed.current === -1) {
|
|
239
|
+
aimed.current = index
|
|
240
|
+
setTarget(index)
|
|
241
|
+
paint(heights[index])
|
|
242
|
+
if (!atTop) (initialFocus?.current ?? grabRef.current)?.focus({ preventScroll: true })
|
|
243
|
+
return
|
|
244
|
+
}
|
|
245
|
+
if (drag.current?.live) return
|
|
246
|
+
if (aimed.current !== index) {
|
|
247
|
+
aimed.current = index
|
|
248
|
+
setTarget(index)
|
|
249
|
+
settle(heights[index], 0)
|
|
250
|
+
} else paint(heights[index])
|
|
251
|
+
}, [open, mounted, index, vh])
|
|
252
|
+
|
|
253
|
+
// Closed, it gives focus back to its opener, and opens next time at the default height.
|
|
254
|
+
useEffect(() => {
|
|
255
|
+
if (open) return
|
|
256
|
+
setInner(defaultDetent ?? order[0])
|
|
257
|
+
const panel = panelRef.current
|
|
258
|
+
const active = document.activeElement
|
|
259
|
+
if (panel && opener.current?.isConnected && (!active || active === document.body || panel.contains(active))) opener.current.focus({ preventScroll: true })
|
|
260
|
+
}, [open])
|
|
261
|
+
|
|
262
|
+
useEffect(() => () => stopGlide.current(), [])
|
|
263
|
+
|
|
264
|
+
// Before the trap's own effect: at the top, focus stays where it is in the sheet.
|
|
265
|
+
useLayoutEffect(() => {
|
|
266
|
+
if (!atTop || !open) return
|
|
267
|
+
const active = document.activeElement as HTMLElement | null
|
|
268
|
+
trapStart.current = active && panelRef.current?.contains(active) ? active : (initialFocus?.current ?? null)
|
|
269
|
+
}, [atTop, open])
|
|
270
|
+
useScrollLock(open && atTop)
|
|
271
|
+
useFocusTrap(panelRef, open && mounted && atTop, { initialFocus: trapStart, restore: false })
|
|
272
|
+
useDismiss(open, [panelRef], stepDown, { outside: atTop })
|
|
273
|
+
|
|
274
|
+
const onPointerMove = (e: globalThis.PointerEvent) => {
|
|
275
|
+
const d = drag.current
|
|
276
|
+
const hs = live.current.heights
|
|
277
|
+
if (!d || d.pointer !== e.pointerId) return
|
|
278
|
+
const dy = d.y0 - e.clientY
|
|
279
|
+
if (!d.live) {
|
|
280
|
+
if (Math.abs(dy) < SLOP) return
|
|
281
|
+
if (bodyRef.current?.contains(e.target as Node) && live.current.index === top && dy > 0) return endDrag()
|
|
282
|
+
// A tap must not stop a settle half way, so the glide is caught only once it is a drag.
|
|
283
|
+
stopGlide.current()
|
|
284
|
+
d.live = true
|
|
285
|
+
d.at0 = at.current
|
|
286
|
+
d.start = nearest(at.current, hs)
|
|
287
|
+
d.speed.add(at.current)
|
|
288
|
+
d.y0 = e.clientY + (dy > 0 ? SLOP : -SLOP)
|
|
289
|
+
// Only now, so a tap on a button inside still clicks it.
|
|
290
|
+
if (e.target instanceof Element && panelRef.current?.contains(e.target)) panelRef.current.setPointerCapture?.(e.pointerId)
|
|
291
|
+
window.addEventListener('selectstart', block)
|
|
292
|
+
setDragging(true)
|
|
293
|
+
}
|
|
294
|
+
// A drag moves the sheet, never selects the page's text: a selection left over from one drag
|
|
295
|
+
// would turn the next press on it into the browser's own drag, which cancels the pointer. Safari
|
|
296
|
+
// keeps growing the selection it began on the press, so it is cleared on every move.
|
|
297
|
+
const selection = window.getSelection()
|
|
298
|
+
if (selection && !selection.isCollapsed) selection.removeAllRanges()
|
|
299
|
+
let raw = d.at0 + (d.y0 - e.clientY)
|
|
300
|
+
const full = hs[hs.length - 1]
|
|
301
|
+
if (raw > full) raw = full + rubber(raw - full, window.innerHeight - full)
|
|
302
|
+
if (raw < 0) raw = 0
|
|
303
|
+
d.speed.add(raw)
|
|
304
|
+
paint(raw)
|
|
305
|
+
const next = pick(raw, d.speed.speed(), d.start, hs)
|
|
306
|
+
setTarget((t) => (t === next ? t : next))
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const onPointerUp = (e: globalThis.PointerEvent) => {
|
|
310
|
+
const d = drag.current
|
|
311
|
+
if (!d || d.pointer !== e.pointerId) return
|
|
312
|
+
endDrag()
|
|
313
|
+
if (!d.live) return
|
|
314
|
+
setDragging(false)
|
|
315
|
+
const hs = live.current.heights
|
|
316
|
+
const cancelled = e.type === 'pointercancel'
|
|
317
|
+
const v = cancelled ? 0 : d.speed.speed()
|
|
318
|
+
const pos = at.current
|
|
319
|
+
if (!cancelled && d.start === 0 && (v < -FLICK || pos < hs[0] * 0.5)) return go(-1, v)
|
|
320
|
+
go(pick(pos, v, d.start, hs), v)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// The hand often leaves the sheet before the drag counts as one, so the moves are followed on the window.
|
|
324
|
+
const block = (e: Event) => e.preventDefault()
|
|
325
|
+
|
|
326
|
+
const endDrag = () => {
|
|
327
|
+
drag.current = null
|
|
328
|
+
unlisten.current()
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const onPointerDown = (e: PointerEvent<HTMLDivElement>) => {
|
|
332
|
+
if ((e.pointerType === 'mouse' && e.button !== 0) || drag.current) return
|
|
333
|
+
const el = e.target as Element
|
|
334
|
+
if (el.closest('input, textarea, select, [contenteditable="true"]')) return
|
|
335
|
+
const hs = live.current.heights
|
|
336
|
+
// At the top the content scrolls; a drag on it moves the sheet only from the top of the content.
|
|
337
|
+
if (bodyRef.current?.contains(el) && live.current.index === top && (bodyRef.current?.scrollTop ?? 0) > 0) return
|
|
338
|
+
drag.current = { y0: e.clientY, at0: at.current, start: nearest(at.current, hs), pointer: e.pointerId, live: false, speed: speedTracker() }
|
|
339
|
+
const move = onPointerMove
|
|
340
|
+
const up = onPointerUp
|
|
341
|
+
window.addEventListener('pointermove', move)
|
|
342
|
+
window.addEventListener('pointerup', up)
|
|
343
|
+
window.addEventListener('pointercancel', up)
|
|
344
|
+
window.addEventListener('dragstart', block)
|
|
345
|
+
// Safari starts a text selection on the press itself, so a press outside the content (the grabber,
|
|
346
|
+
// the edges) blocks it at once. Text in the content can still be selected by dragging inside it.
|
|
347
|
+
if (!bodyRef.current?.contains(el)) window.addEventListener('selectstart', block)
|
|
348
|
+
unlisten.current = () => {
|
|
349
|
+
window.removeEventListener('pointermove', move)
|
|
350
|
+
window.removeEventListener('pointerup', up)
|
|
351
|
+
window.removeEventListener('pointercancel', up)
|
|
352
|
+
window.removeEventListener('dragstart', block)
|
|
353
|
+
window.removeEventListener('selectstart', block)
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
useEffect(() => () => unlisten.current(), [])
|
|
358
|
+
|
|
359
|
+
// A touch at the top of the content, moving down, belongs to the sheet: the browser would scroll it.
|
|
360
|
+
useEffect(() => {
|
|
361
|
+
const body = bodyRef.current
|
|
362
|
+
if (!body) return
|
|
363
|
+
let y = 0
|
|
364
|
+
const start = (e: TouchEvent) => (y = e.touches[0].clientY)
|
|
365
|
+
const move = (e: TouchEvent) => {
|
|
366
|
+
if (drag.current?.live || (live.current.index === live.current.heights.length - 1 && body.scrollTop <= 0 && e.touches[0].clientY > y)) e.preventDefault()
|
|
367
|
+
}
|
|
368
|
+
body.addEventListener('touchstart', start, { passive: true })
|
|
369
|
+
body.addEventListener('touchmove', move, { passive: false })
|
|
370
|
+
return () => {
|
|
371
|
+
body.removeEventListener('touchstart', start)
|
|
372
|
+
body.removeEventListener('touchmove', move)
|
|
373
|
+
}
|
|
374
|
+
}, [mounted])
|
|
375
|
+
|
|
376
|
+
const onGrabKey = (e: KeyboardEvent<HTMLDivElement>) => {
|
|
377
|
+
const i = live.current.index
|
|
378
|
+
if (e.key === 'Enter' || e.key === ' ') {
|
|
379
|
+
e.preventDefault()
|
|
380
|
+
return go(i === top ? 0 : i + 1, 0, false)
|
|
381
|
+
}
|
|
382
|
+
const keys: Record<string, number> = { ArrowUp: i + 1, ArrowRight: i + 1, ArrowDown: i - 1, ArrowLeft: i - 1, Home: 0, End: top, PageUp: top, PageDown: 0 }
|
|
383
|
+
if (!(e.key in keys)) return
|
|
384
|
+
e.preventDefault()
|
|
385
|
+
go(Math.max(0, Math.min(top, keys[e.key])), 0, false)
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// Tabbing onto a control hidden below the screen's edge grows the sheet to the top, so it shows.
|
|
389
|
+
const onFocus = (e: FocusEvent<HTMLDivElement>) => {
|
|
390
|
+
if (!bodyRef.current?.contains(e.target) || live.current.index === top) return
|
|
391
|
+
if (e.target.getBoundingClientRect().bottom > window.innerHeight - 4) go(top)
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if (!mounted) return null
|
|
395
|
+
const shown = order[dragging ? target : index]
|
|
396
|
+
return (
|
|
397
|
+
<Portal>
|
|
398
|
+
<div ref={overlayRef} className={styles.overlay} data-state={state} data-detents data-top={atTop || undefined} aria-hidden="true" />
|
|
399
|
+
<div
|
|
400
|
+
ref={panelRef}
|
|
401
|
+
role="dialog"
|
|
402
|
+
aria-modal={atTop || undefined}
|
|
403
|
+
aria-labelledby={`${id}-title`}
|
|
404
|
+
aria-describedby={description ? `${id}-desc` : undefined}
|
|
405
|
+
tabIndex={-1}
|
|
406
|
+
id={`${id}-sheet`}
|
|
407
|
+
className={cx(styles.sheet, className)}
|
|
408
|
+
data-state={state}
|
|
409
|
+
data-detents
|
|
410
|
+
data-top={atTop || undefined}
|
|
411
|
+
data-dragging={dragging || undefined}
|
|
412
|
+
style={{ height: heights[top] }}
|
|
413
|
+
onPointerDown={onPointerDown}
|
|
414
|
+
onFocus={onFocus}
|
|
415
|
+
>
|
|
416
|
+
<div
|
|
417
|
+
ref={grabRef}
|
|
418
|
+
className={styles.grabber}
|
|
419
|
+
role="slider"
|
|
420
|
+
tabIndex={0}
|
|
421
|
+
aria-label={heightLabel}
|
|
422
|
+
aria-orientation="vertical"
|
|
423
|
+
aria-valuemin={0}
|
|
424
|
+
aria-valuemax={top}
|
|
425
|
+
aria-valuenow={index}
|
|
426
|
+
aria-valuetext={NAME[order[index]]}
|
|
427
|
+
aria-controls={`${id}-sheet`}
|
|
428
|
+
data-detent={shown}
|
|
429
|
+
onKeyDown={onGrabKey}
|
|
430
|
+
onClick={() => go(index === top ? 0 : index + 1)}
|
|
431
|
+
>
|
|
432
|
+
<span className={styles.pill} />
|
|
433
|
+
</div>
|
|
434
|
+
<header className={styles.header}>
|
|
435
|
+
<div className={styles.titles}>
|
|
436
|
+
<h2 id={`${id}-title`} className={styles.title}>{title}</h2>
|
|
437
|
+
{description && <p id={`${id}-desc`} className={styles.description}>{description}</p>}
|
|
438
|
+
</div>
|
|
439
|
+
<button type="button" className={styles.close} aria-label={closeLabel} data-skip-initial-focus onClick={() => onOpenChange(false)}>
|
|
440
|
+
<XIcon />
|
|
441
|
+
</button>
|
|
442
|
+
</header>
|
|
443
|
+
{children && <div ref={bodyRef} className={styles.body}>{children}</div>}
|
|
444
|
+
{footer && <footer className={styles.footer}>{footer}</footer>}
|
|
445
|
+
<p className={styles.srOnly} aria-live="polite">{said}</p>
|
|
446
|
+
</div>
|
|
447
|
+
</Portal>
|
|
448
|
+
)
|
|
449
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Sheet'
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
.lines {
|
|
2
|
+
display: flex;
|
|
3
|
+
flex-direction: column;
|
|
4
|
+
gap: var(--gooee-space-3);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
.bone {
|
|
8
|
+
position: relative;
|
|
9
|
+
display: block;
|
|
10
|
+
overflow: hidden;
|
|
11
|
+
background: var(--gooee-color-surface-active);
|
|
12
|
+
border-radius: var(--gooee-radius-sm);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
.bone::after {
|
|
16
|
+
content: "";
|
|
17
|
+
position: absolute;
|
|
18
|
+
inset: 0;
|
|
19
|
+
transform: translateX(-100%);
|
|
20
|
+
background: linear-gradient(90deg, transparent, var(--gooee-color-shimmer), transparent);
|
|
21
|
+
animation: shimmer var(--gooee-motion-shimmer) var(--gooee-ease-in-out) infinite;
|
|
22
|
+
animation-delay: calc(var(--i, 0) * var(--gooee-motion-stagger) * 3);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
.text { height: 0.8em; border-radius: var(--gooee-radius-xs); }
|
|
26
|
+
.rect { width: 100%; height: var(--gooee-skeleton-block); border-radius: var(--gooee-radius-md); }
|
|
27
|
+
.circle { width: var(--gooee-skeleton-avatar); height: var(--gooee-skeleton-avatar); border-radius: var(--gooee-radius-full); }
|
|
28
|
+
|
|
29
|
+
/* A sketch: the shapes as dashed pencil outlines, no fill. Text becomes dashed baselines. The lines
|
|
30
|
+
themselves fade gently instead of a shimmer, which would light up the empty box behind them. */
|
|
31
|
+
.sketch {
|
|
32
|
+
background: none;
|
|
33
|
+
border: var(--gooee-line) dashed var(--gooee-color-border-strong);
|
|
34
|
+
animation: sketching var(--gooee-motion-shimmer) var(--gooee-ease-in-out) infinite alternate;
|
|
35
|
+
animation-delay: calc(var(--i, 0) * var(--gooee-motion-stagger) * 3);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
.sketch::after { display: none; }
|
|
39
|
+
|
|
40
|
+
.sketch.text {
|
|
41
|
+
border-width: 0 0 var(--gooee-line);
|
|
42
|
+
border-radius: 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
@keyframes sketching {
|
|
46
|
+
from { border-color: var(--gooee-color-border); }
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
@keyframes shimmer {
|
|
50
|
+
to { transform: translateX(100%); }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
@media (prefers-reduced-motion: reduce) {
|
|
54
|
+
.bone::after { animation: none; display: none; }
|
|
55
|
+
.sketch { animation: none; }
|
|
56
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { CSSProperties } from 'react'
|
|
2
|
+
import { cx } from '../../internal/cx'
|
|
3
|
+
import styles from './Skeleton.module.css'
|
|
4
|
+
|
|
5
|
+
export type SkeletonProps = {
|
|
6
|
+
/** Text draws lines, rect a box, circle a round shape such as an avatar. */
|
|
7
|
+
shape?: 'text' | 'rect' | 'circle'
|
|
8
|
+
/** Any CSS width, such as 120 or "50%". */
|
|
9
|
+
width?: CSSProperties['width']
|
|
10
|
+
/** Any CSS height. Ignored for text, where lines set the height. */
|
|
11
|
+
height?: CSSProperties['height']
|
|
12
|
+
/** For text: number of lines. The last one is shorter, like real text. */
|
|
13
|
+
lines?: number
|
|
14
|
+
/** Solid grey shapes, or a sketch: the same shapes as dashed outlines, like a pencil layout. */
|
|
15
|
+
variant?: 'solid' | 'sketch'
|
|
16
|
+
/** Extra class for the root element. */
|
|
17
|
+
className?: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Decorative only. Mark the loading container with aria-busy and a label instead.
|
|
21
|
+
export function Skeleton({ shape = 'rect', width, height, lines = 1, variant = 'solid', className }: SkeletonProps) {
|
|
22
|
+
const look = variant === 'sketch' && styles.sketch
|
|
23
|
+
if (shape === 'text') {
|
|
24
|
+
return (
|
|
25
|
+
<span className={cx(styles.lines, className)} style={{ width }} aria-hidden="true">
|
|
26
|
+
{Array.from({ length: lines }, (_, i) => (
|
|
27
|
+
<span
|
|
28
|
+
key={i}
|
|
29
|
+
className={cx(styles.bone, styles.text, look)}
|
|
30
|
+
style={{ width: lines > 1 && i === lines - 1 ? '62%' : '100%', ['--i' as string]: i }}
|
|
31
|
+
/>
|
|
32
|
+
))}
|
|
33
|
+
</span>
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
return <span className={cx(styles.bone, styles[shape], look, className)} style={{ width, height }} aria-hidden="true" />
|
|
37
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Skeleton'
|