@fabricator-ui/react-native 0.0.0-stage → 0.10.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/README.md +8 -2
- package/dist/LICENSE.md +25 -0
- package/dist/NOTICE.md +24 -0
- package/dist/lib/data-variants.ts +23 -0
- package/dist/lib/density-scale.ts +208 -0
- package/dist/lib/fabricator/css-object.ts +2 -0
- package/dist/lib/fabricator/density-scale.ts +210 -0
- package/dist/lib/fabricator/density-variables.ts +37 -0
- package/dist/lib/fabricator/motion-speeds.ts +566 -0
- package/dist/lib/fabricator/radius.ts +240 -0
- package/dist/lib/feedback.ts +124 -0
- package/dist/lib/field-frame.ts +53 -0
- package/dist/lib/field-model.ts +112 -0
- package/dist/lib/fluid-hover-model.ts +142 -0
- package/dist/lib/fluid-hover.tsx +788 -0
- package/dist/lib/icon-stroke.ts +38 -0
- package/dist/lib/motion-tokens.ts +97 -0
- package/dist/lib/motion.ts +292 -0
- package/dist/lib/overlay-model.ts +205 -0
- package/dist/lib/press-wash.tsx +73 -0
- package/dist/lib/render.tsx +53 -0
- package/dist/lib/select-model.ts +87 -0
- package/dist/lib/sheet-model.ts +101 -0
- package/dist/lib/sheet-surface.tsx +429 -0
- package/dist/lib/sound-recipes.ts +449 -0
- package/dist/lib/spring-model.ts +214 -0
- package/dist/lib/surface-color.tsx +27 -0
- package/dist/lib/text-children.ts +43 -0
- package/dist/styles.css +610 -0
- package/dist/ui/badge.tsx +108 -0
- package/dist/ui/button.tsx +285 -0
- package/dist/ui/card.tsx +365 -0
- package/dist/ui/checkbox.tsx +230 -0
- package/dist/ui/collapsible.tsx +194 -0
- package/dist/ui/density.tsx +79 -0
- package/dist/ui/dialog.tsx +232 -0
- package/dist/ui/dropdown-menu.tsx +687 -0
- package/dist/ui/fabricator-root.tsx +68 -0
- package/dist/ui/input.tsx +63 -0
- package/dist/ui/label.tsx +22 -0
- package/dist/ui/select.tsx +616 -0
- package/dist/ui/switch.tsx +177 -0
- package/dist/ui/tabs.tsx +272 -0
- package/dist/ui/text.tsx +125 -0
- package/dist/ui/textarea.tsx +65 -0
- package/package.json +54 -4
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
import { Slot } from "@rn-primitives/slot"
|
|
3
|
+
|
|
4
|
+
export type RenderProps = {
|
|
5
|
+
/** Renders this element instead, with this part's props merged onto it. */
|
|
6
|
+
render?: React.ReactElement<{ children?: React.ReactNode }>
|
|
7
|
+
/** Radix's form of `render`: the only child takes this part's props. */
|
|
8
|
+
asChild?: boolean
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The composition APIs both web bases use. Returns the element to render when
|
|
13
|
+
* `render` or `asChild` is set, or null to render the part itself.
|
|
14
|
+
*/
|
|
15
|
+
export function renderSlot(
|
|
16
|
+
{ render, asChild }: RenderProps,
|
|
17
|
+
props: Record<string, unknown>,
|
|
18
|
+
children?: React.ReactNode
|
|
19
|
+
) {
|
|
20
|
+
if (render) {
|
|
21
|
+
return (
|
|
22
|
+
<Slot {...props}>
|
|
23
|
+
{React.cloneElement(
|
|
24
|
+
render,
|
|
25
|
+
undefined,
|
|
26
|
+
children ?? render.props.children
|
|
27
|
+
)}
|
|
28
|
+
</Slot>
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
if (asChild) return <Slot {...props}>{children}</Slot>
|
|
32
|
+
return null
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* For parts built on a primitive that has `asChild`: `render` as the primitive
|
|
37
|
+
* takes it.
|
|
38
|
+
*/
|
|
39
|
+
export function resolveRender({
|
|
40
|
+
render,
|
|
41
|
+
asChild,
|
|
42
|
+
children,
|
|
43
|
+
}: RenderProps & { children?: React.ReactNode }) {
|
|
44
|
+
if (!render) return { asChild, children }
|
|
45
|
+
return {
|
|
46
|
+
asChild: true,
|
|
47
|
+
children: React.cloneElement(
|
|
48
|
+
render,
|
|
49
|
+
undefined,
|
|
50
|
+
children ?? render.props.children
|
|
51
|
+
),
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// A Select's values and labels, and how it opens. No React Native imports, so
|
|
2
|
+
// these stay testable anywhere.
|
|
3
|
+
|
|
4
|
+
import type * as React from "react"
|
|
5
|
+
|
|
6
|
+
type SelectEntry = { label: React.ReactNode; value: unknown }
|
|
7
|
+
type SelectGroupEntry = { value: unknown; items: readonly SelectEntry[] }
|
|
8
|
+
|
|
9
|
+
/** Base UI's `items`: a record of labels, a list of them, or groups of them. */
|
|
10
|
+
export type SelectItems =
|
|
11
|
+
| Record<string, React.ReactNode>
|
|
12
|
+
| readonly (SelectEntry | SelectGroupEntry)[]
|
|
13
|
+
|
|
14
|
+
// Tailwind's sm: the width from which a popover has room beside a finger.
|
|
15
|
+
const SM_BREAKPOINT = 640
|
|
16
|
+
|
|
17
|
+
export function getSelectPresentation(windowWidth: number) {
|
|
18
|
+
return windowWidth >= SM_BREAKPOINT ? "popover" : "sheet"
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function toggleSelectValue(
|
|
22
|
+
current: unknown,
|
|
23
|
+
value: unknown,
|
|
24
|
+
multiple: boolean
|
|
25
|
+
) {
|
|
26
|
+
if (!multiple) return value
|
|
27
|
+
const list: readonly unknown[] = Array.isArray(current) ? current : []
|
|
28
|
+
return list.includes(value)
|
|
29
|
+
? list.filter((entry) => entry !== value)
|
|
30
|
+
: [...list, value]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function isValueSelected(
|
|
34
|
+
current: unknown,
|
|
35
|
+
value: unknown,
|
|
36
|
+
multiple: boolean
|
|
37
|
+
) {
|
|
38
|
+
if (multiple) return Array.isArray(current) && current.includes(value)
|
|
39
|
+
return Object.is(current, value)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function isSelectValueEmpty(value: unknown) {
|
|
43
|
+
if (Array.isArray(value)) return value.length === 0
|
|
44
|
+
return value === null || value === undefined
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function isItemList(
|
|
48
|
+
items: SelectItems
|
|
49
|
+
): items is readonly (SelectEntry | SelectGroupEntry)[] {
|
|
50
|
+
return Array.isArray(items)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function isGroup(
|
|
54
|
+
entry: SelectEntry | SelectGroupEntry
|
|
55
|
+
): entry is SelectGroupEntry {
|
|
56
|
+
return "items" in entry
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function findLabel(items: SelectItems, value: unknown): React.ReactNode {
|
|
60
|
+
if (!isItemList(items)) {
|
|
61
|
+
return typeof value === "string" ? items[value] : undefined
|
|
62
|
+
}
|
|
63
|
+
for (const entry of items) {
|
|
64
|
+
if (isGroup(entry)) {
|
|
65
|
+
const label = findLabel(entry.items, value)
|
|
66
|
+
if (label !== undefined) return label
|
|
67
|
+
} else if (Object.is(entry.value, value ?? null)) {
|
|
68
|
+
return entry.label
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return undefined
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** What a value reads as. A null item in `items` labels the empty value. */
|
|
75
|
+
export function getSelectLabel(
|
|
76
|
+
items: SelectItems | undefined,
|
|
77
|
+
value: unknown
|
|
78
|
+
): React.ReactNode {
|
|
79
|
+
if (!items) return undefined
|
|
80
|
+
if (Array.isArray(value)) {
|
|
81
|
+
const labels = value.map((entry) => findLabel(items, entry))
|
|
82
|
+
return labels.every((label) => label !== undefined)
|
|
83
|
+
? labels.join(", ")
|
|
84
|
+
: undefined
|
|
85
|
+
}
|
|
86
|
+
return findLabel(items, value)
|
|
87
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// What a sheet does under a finger. No React Native imports, so it stays testable
|
|
2
|
+
// anywhere; the "worklet" directive lets the gesture run it on the UI thread.
|
|
3
|
+
|
|
4
|
+
export type SheetSide = "top" | "right" | "bottom" | "left"
|
|
5
|
+
export type SwipeDirection = "up" | "right" | "down" | "left"
|
|
6
|
+
|
|
7
|
+
// The website's drawer dismisses at these: half its size, or a flick of 0.5pt/ms.
|
|
8
|
+
const DISMISS_FRACTION = 0.5
|
|
9
|
+
const MIN_DISMISS_DISTANCE = 10
|
|
10
|
+
const FLICK_VELOCITY = 500
|
|
11
|
+
|
|
12
|
+
const MIN_RELEASE_SCALE = 0.1
|
|
13
|
+
|
|
14
|
+
// Pulling a sheet past open gives way less and less, up to this far.
|
|
15
|
+
const MAX_OVERDRAG = 40
|
|
16
|
+
const OVERDRAG_SOFTNESS = 120
|
|
17
|
+
|
|
18
|
+
// The shadow ring is drawn outside the panel; this keeps it off screen too.
|
|
19
|
+
const CLOSED_MARGIN = 24
|
|
20
|
+
|
|
21
|
+
const SWIPE_SIDES: Record<SwipeDirection, SheetSide> = {
|
|
22
|
+
up: "top",
|
|
23
|
+
right: "right",
|
|
24
|
+
down: "bottom",
|
|
25
|
+
left: "left",
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The side a Drawer sits on: it swipes away toward its own edge. */
|
|
29
|
+
export function getSideForSwipe(direction: SwipeDirection) {
|
|
30
|
+
return SWIPE_SIDES[direction]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function getSwipeAxis(side: SheetSide): "x" | "y" {
|
|
34
|
+
"worklet"
|
|
35
|
+
return side === "top" || side === "bottom" ? "y" : "x"
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The sign of a movement toward the edge the sheet closes against. */
|
|
39
|
+
function getClosingSign(side: SheetSide) {
|
|
40
|
+
"worklet"
|
|
41
|
+
return side === "bottom" || side === "right" ? 1 : -1
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Where a sheet of this size rests when it's closed. */
|
|
45
|
+
export function getClosedOffset(side: SheetSide, size: number) {
|
|
46
|
+
"worklet"
|
|
47
|
+
return getClosingSign(side) * (size + CLOSED_MARGIN)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Where the panel sits for a finger this far from where it started. */
|
|
51
|
+
export function getDragOffset(side: SheetSide, translation: number) {
|
|
52
|
+
"worklet"
|
|
53
|
+
const sign = getClosingSign(side)
|
|
54
|
+
const distance = translation * sign
|
|
55
|
+
if (distance >= 0) return sign * distance
|
|
56
|
+
return -sign * MAX_OVERDRAG * (1 - 1 / (1 - distance / OVERDRAG_SOFTNESS))
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Whether letting go dismisses the sheet. `translation` and `velocity` are the
|
|
61
|
+
* gesture's own, on the sheet's axis; `size` is the panel's along it.
|
|
62
|
+
*/
|
|
63
|
+
export function shouldDismissSheet({
|
|
64
|
+
side,
|
|
65
|
+
size,
|
|
66
|
+
translation,
|
|
67
|
+
velocity,
|
|
68
|
+
}: {
|
|
69
|
+
side: SheetSide
|
|
70
|
+
size: number
|
|
71
|
+
translation: number
|
|
72
|
+
velocity: number
|
|
73
|
+
}) {
|
|
74
|
+
"worklet"
|
|
75
|
+
if (size <= 0) return false
|
|
76
|
+
const sign = getClosingSign(side)
|
|
77
|
+
const distance = translation * sign
|
|
78
|
+
const speed = velocity * sign
|
|
79
|
+
if (distance <= 0 || speed <= -FLICK_VELOCITY) return false
|
|
80
|
+
if (speed >= FLICK_VELOCITY) return true
|
|
81
|
+
return distance >= Math.max(size * DISMISS_FRACTION, MIN_DISMISS_DISTANCE)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* How long a released sheet takes to leave: the harder the flick, the quicker,
|
|
86
|
+
* so it carries the finger's speed. Never longer than the role's own timing.
|
|
87
|
+
*/
|
|
88
|
+
export function getDismissDuration({
|
|
89
|
+
remaining,
|
|
90
|
+
velocity,
|
|
91
|
+
duration,
|
|
92
|
+
}: {
|
|
93
|
+
remaining: number
|
|
94
|
+
velocity: number
|
|
95
|
+
duration: number
|
|
96
|
+
}) {
|
|
97
|
+
"worklet"
|
|
98
|
+
if (velocity <= 0) return duration
|
|
99
|
+
const flight = (remaining / velocity) * 1000
|
|
100
|
+
return Math.min(duration, Math.max(flight, duration * MIN_RELEASE_SCALE))
|
|
101
|
+
}
|
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
import {
|
|
3
|
+
useAccessibilityFocus,
|
|
4
|
+
useEffectEvent as useLatestCallback,
|
|
5
|
+
} from "@rn-primitives/hooks"
|
|
6
|
+
import { Portal } from "@rn-primitives/portal"
|
|
7
|
+
import { cn } from "cn"
|
|
8
|
+
import {
|
|
9
|
+
BackHandler,
|
|
10
|
+
KeyboardAvoidingView,
|
|
11
|
+
Pressable,
|
|
12
|
+
ScrollView,
|
|
13
|
+
useWindowDimensions,
|
|
14
|
+
View,
|
|
15
|
+
type LayoutChangeEvent,
|
|
16
|
+
type ViewProps,
|
|
17
|
+
} from "react-native"
|
|
18
|
+
import { Gesture, GestureDetector } from "react-native-gesture-handler"
|
|
19
|
+
import Animated, {
|
|
20
|
+
ReduceMotion,
|
|
21
|
+
useAnimatedStyle,
|
|
22
|
+
useSharedValue,
|
|
23
|
+
withTiming,
|
|
24
|
+
} from "react-native-reanimated"
|
|
25
|
+
import { scheduleOnRN } from "react-native-worklets"
|
|
26
|
+
|
|
27
|
+
import { useSurfaceFeedback } from "./feedback"
|
|
28
|
+
import { useMotion } from "./motion"
|
|
29
|
+
import {
|
|
30
|
+
getClosedOffset,
|
|
31
|
+
getDismissDuration,
|
|
32
|
+
getDragOffset,
|
|
33
|
+
getSwipeAxis,
|
|
34
|
+
shouldDismissSheet,
|
|
35
|
+
type SheetSide,
|
|
36
|
+
} from "./sheet-model"
|
|
37
|
+
import { DensityScope, useDensity } from "../ui/density"
|
|
38
|
+
|
|
39
|
+
// A finger travels this far along the sheet's axis before the swipe starts, or
|
|
40
|
+
// across it before the swipe gives way to whatever is underneath.
|
|
41
|
+
const SWIPE_SLOP = 12
|
|
42
|
+
|
|
43
|
+
type SlotProps = ViewProps & { "data-slot"?: string }
|
|
44
|
+
|
|
45
|
+
type SheetSurfaceContextValue = {
|
|
46
|
+
onOverlayPress: () => void
|
|
47
|
+
overlayStyle: React.ComponentProps<typeof Animated.View>["style"]
|
|
48
|
+
pan: ReturnType<typeof Gesture.Pan>
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const SheetSurfaceContext =
|
|
52
|
+
React.createContext<SheetSurfaceContextValue | null>(null)
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The scrim. Its opacity follows the panel, so it fades as the sheet is swiped
|
|
56
|
+
* away. SheetSurface renders one when it isn't given its own `overlay`.
|
|
57
|
+
*/
|
|
58
|
+
function SheetSurfaceOverlay({
|
|
59
|
+
className,
|
|
60
|
+
style,
|
|
61
|
+
...props
|
|
62
|
+
}: React.ComponentProps<typeof Animated.View>) {
|
|
63
|
+
const surface = React.use(SheetSurfaceContext)
|
|
64
|
+
return (
|
|
65
|
+
<Animated.View
|
|
66
|
+
data-slot="sheet-surface-overlay"
|
|
67
|
+
className={cn("bg-black/40 dark:bg-black/80 absolute inset-0", className)}
|
|
68
|
+
style={[surface?.overlayStyle, style]}
|
|
69
|
+
{...props}
|
|
70
|
+
>
|
|
71
|
+
<Pressable
|
|
72
|
+
accessible={false}
|
|
73
|
+
className={"fab:flex-1"}
|
|
74
|
+
onPress={surface?.onOverlayPress}
|
|
75
|
+
/>
|
|
76
|
+
</Animated.View>
|
|
77
|
+
)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* A ScrollView for inside a sheet. Dragging it scrolls instead of swiping the
|
|
82
|
+
* sheet away; a swipe still works from everything around it.
|
|
83
|
+
*/
|
|
84
|
+
function SheetSurfaceScrollView(
|
|
85
|
+
props: React.ComponentProps<typeof ScrollView>
|
|
86
|
+
) {
|
|
87
|
+
const pan = React.use(SheetSurfaceContext)?.pan
|
|
88
|
+
const native = React.useMemo(() => {
|
|
89
|
+
const gesture = Gesture.Native()
|
|
90
|
+
return pan ? gesture.blocksExternalGesture(pan) : gesture
|
|
91
|
+
}, [pan])
|
|
92
|
+
return (
|
|
93
|
+
<GestureDetector gesture={native}>
|
|
94
|
+
<ScrollView
|
|
95
|
+
data-slot="sheet-surface-scroll-view"
|
|
96
|
+
alwaysBounceVertical={false}
|
|
97
|
+
keyboardShouldPersistTaps="handled"
|
|
98
|
+
{...props}
|
|
99
|
+
/>
|
|
100
|
+
</GestureDetector>
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
type SheetSurfaceProps = Omit<
|
|
105
|
+
React.ComponentProps<typeof Animated.View>,
|
|
106
|
+
"children"
|
|
107
|
+
> & {
|
|
108
|
+
/** Whether the sheet is showing. It stays mounted until it has slid out. */
|
|
109
|
+
open: boolean
|
|
110
|
+
/** Asks to close, from a swipe, the scrim or Android back. */
|
|
111
|
+
onOpenChange: (open: boolean) => void
|
|
112
|
+
/** The edge it attaches to and swipes away toward. */
|
|
113
|
+
side?: SheetSide
|
|
114
|
+
/** Swipe and scrim press. Android back always closes, as Escape does on web. */
|
|
115
|
+
dismissible?: boolean
|
|
116
|
+
/** With a scrim, hiding the page from screen readers. */
|
|
117
|
+
modal?: boolean
|
|
118
|
+
/** Replaces the default scrim. Use SheetSurfaceOverlay inside it. */
|
|
119
|
+
overlay?: React.ReactNode
|
|
120
|
+
/** A named PortalHost, instead of FabricatorRoot's. */
|
|
121
|
+
hostName?: string
|
|
122
|
+
/** On the outermost portalled view. */
|
|
123
|
+
rootProps?: SlotProps
|
|
124
|
+
/** On the keyboard-avoiding view the panel sits in. */
|
|
125
|
+
viewportProps?: SlotProps
|
|
126
|
+
children?: React.ReactNode
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function SheetPanel({
|
|
130
|
+
open,
|
|
131
|
+
onOpenChange,
|
|
132
|
+
onExited,
|
|
133
|
+
side,
|
|
134
|
+
dismissible,
|
|
135
|
+
modal,
|
|
136
|
+
overlay,
|
|
137
|
+
rootProps,
|
|
138
|
+
viewportProps,
|
|
139
|
+
className,
|
|
140
|
+
style,
|
|
141
|
+
children,
|
|
142
|
+
...props
|
|
143
|
+
}: Omit<SheetSurfaceProps, "hostName" | "side" | "dismissible" | "modal"> & {
|
|
144
|
+
onExited: () => void
|
|
145
|
+
side: SheetSide
|
|
146
|
+
dismissible: boolean
|
|
147
|
+
modal: boolean
|
|
148
|
+
}) {
|
|
149
|
+
const { isReduced, enterMs, exitMs, drawerEasing } = useMotion("sheet")
|
|
150
|
+
const windowSize = useWindowDimensions()
|
|
151
|
+
const axis = getSwipeAxis(side)
|
|
152
|
+
const offset = useSharedValue(
|
|
153
|
+
getClosedOffset(side, axis === "y" ? windowSize.height : windowSize.width)
|
|
154
|
+
)
|
|
155
|
+
// Reduce Motion keeps the fade and drops the travel.
|
|
156
|
+
const fade = useSharedValue(isReduced ? 0 : 1)
|
|
157
|
+
const size = useSharedValue(0)
|
|
158
|
+
const isMeasured = React.useRef(false)
|
|
159
|
+
const isSwipeExit = React.useRef(false)
|
|
160
|
+
const hasExited = React.useRef(false)
|
|
161
|
+
const focusRef =
|
|
162
|
+
useAccessibilityFocus<React.ComponentRef<typeof Animated.View>>(open)
|
|
163
|
+
|
|
164
|
+
const requestClose = useLatestCallback(() => onOpenChange(false))
|
|
165
|
+
const handleExitEnd = useLatestCallback(() => {
|
|
166
|
+
hasExited.current = true
|
|
167
|
+
if (!open) onExited()
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
const enter = React.useCallback(() => {
|
|
171
|
+
hasExited.current = false
|
|
172
|
+
const config = {
|
|
173
|
+
duration: enterMs,
|
|
174
|
+
easing: drawerEasing,
|
|
175
|
+
reduceMotion: ReduceMotion.Never,
|
|
176
|
+
}
|
|
177
|
+
if (isReduced) {
|
|
178
|
+
offset.set(0)
|
|
179
|
+
fade.set(withTiming(1, config))
|
|
180
|
+
return
|
|
181
|
+
}
|
|
182
|
+
fade.set(1)
|
|
183
|
+
offset.set(withTiming(0, config))
|
|
184
|
+
}, [drawerEasing, enterMs, fade, isReduced, offset])
|
|
185
|
+
|
|
186
|
+
const exit = React.useCallback(
|
|
187
|
+
(duration: number) => {
|
|
188
|
+
"worklet"
|
|
189
|
+
const config = {
|
|
190
|
+
duration,
|
|
191
|
+
easing: drawerEasing,
|
|
192
|
+
reduceMotion: ReduceMotion.Never,
|
|
193
|
+
}
|
|
194
|
+
const onDone = (finished?: boolean) => {
|
|
195
|
+
"worklet"
|
|
196
|
+
if (finished) scheduleOnRN(handleExitEnd)
|
|
197
|
+
}
|
|
198
|
+
if (isReduced) {
|
|
199
|
+
fade.set(withTiming(0, config, onDone))
|
|
200
|
+
return
|
|
201
|
+
}
|
|
202
|
+
offset.set(withTiming(getClosedOffset(side, size.get()), config, onDone))
|
|
203
|
+
},
|
|
204
|
+
[drawerEasing, fade, handleExitEnd, isReduced, offset, side, size]
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
// A controlled parent can refuse a swipe: a frame later it's still open, so
|
|
208
|
+
// the sheet goes back.
|
|
209
|
+
const settleAfterSwipe = useLatestCallback(() => {
|
|
210
|
+
if (!open) return
|
|
211
|
+
isSwipeExit.current = false
|
|
212
|
+
enter()
|
|
213
|
+
})
|
|
214
|
+
const handleSwipeDismiss = useLatestCallback(() => {
|
|
215
|
+
isSwipeExit.current = true
|
|
216
|
+
onOpenChange(false)
|
|
217
|
+
requestAnimationFrame(settleAfterSwipe)
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
React.useEffect(() => {
|
|
221
|
+
if (!isMeasured.current) {
|
|
222
|
+
if (!open) onExited()
|
|
223
|
+
return
|
|
224
|
+
}
|
|
225
|
+
if (open) {
|
|
226
|
+
enter()
|
|
227
|
+
return
|
|
228
|
+
}
|
|
229
|
+
if (isSwipeExit.current) {
|
|
230
|
+
// Already on its way out. With motion off it may have landed already.
|
|
231
|
+
isSwipeExit.current = false
|
|
232
|
+
if (hasExited.current) onExited()
|
|
233
|
+
return
|
|
234
|
+
}
|
|
235
|
+
exit(exitMs)
|
|
236
|
+
}, [enter, exit, exitMs, onExited, open])
|
|
237
|
+
|
|
238
|
+
React.useEffect(() => {
|
|
239
|
+
if (!open) return
|
|
240
|
+
const subscription = BackHandler.addEventListener(
|
|
241
|
+
"hardwareBackPress",
|
|
242
|
+
() => {
|
|
243
|
+
requestClose()
|
|
244
|
+
return true
|
|
245
|
+
}
|
|
246
|
+
)
|
|
247
|
+
return () => subscription.remove()
|
|
248
|
+
}, [open, requestClose])
|
|
249
|
+
|
|
250
|
+
const handleLayout = (event: LayoutChangeEvent) => {
|
|
251
|
+
const { width, height } = event.nativeEvent.layout
|
|
252
|
+
const measured = axis === "y" ? height : width
|
|
253
|
+
size.set(measured)
|
|
254
|
+
if (isMeasured.current || measured === 0) return
|
|
255
|
+
isMeasured.current = true
|
|
256
|
+
if (!open) {
|
|
257
|
+
onExited()
|
|
258
|
+
return
|
|
259
|
+
}
|
|
260
|
+
offset.set(getClosedOffset(side, measured))
|
|
261
|
+
enter()
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const pan = React.useMemo(() => {
|
|
265
|
+
const slop: [number, number] = [-SWIPE_SLOP, SWIPE_SLOP]
|
|
266
|
+
const gesture = Gesture.Pan().enabled(dismissible)
|
|
267
|
+
const onAxis =
|
|
268
|
+
axis === "y"
|
|
269
|
+
? gesture.activeOffsetY(slop).failOffsetX(slop)
|
|
270
|
+
: gesture.activeOffsetX(slop).failOffsetY(slop)
|
|
271
|
+
return onAxis
|
|
272
|
+
.onUpdate((event) => {
|
|
273
|
+
"worklet"
|
|
274
|
+
offset.set(
|
|
275
|
+
getDragOffset(
|
|
276
|
+
side,
|
|
277
|
+
axis === "y" ? event.translationY : event.translationX
|
|
278
|
+
)
|
|
279
|
+
)
|
|
280
|
+
})
|
|
281
|
+
.onEnd((event, success) => {
|
|
282
|
+
"worklet"
|
|
283
|
+
const translation =
|
|
284
|
+
axis === "y" ? event.translationY : event.translationX
|
|
285
|
+
const velocity = axis === "y" ? event.velocityY : event.velocityX
|
|
286
|
+
if (
|
|
287
|
+
!success ||
|
|
288
|
+
!shouldDismissSheet({ side, size: size.get(), translation, velocity })
|
|
289
|
+
) {
|
|
290
|
+
// A swipe that didn't make it settles back on the sheet's own timing.
|
|
291
|
+
offset.set(
|
|
292
|
+
withTiming(0, {
|
|
293
|
+
duration: enterMs,
|
|
294
|
+
easing: drawerEasing,
|
|
295
|
+
})
|
|
296
|
+
)
|
|
297
|
+
return
|
|
298
|
+
}
|
|
299
|
+
exit(
|
|
300
|
+
getDismissDuration({
|
|
301
|
+
remaining: Math.abs(
|
|
302
|
+
getClosedOffset(side, size.get()) - offset.get()
|
|
303
|
+
),
|
|
304
|
+
velocity: Math.abs(velocity),
|
|
305
|
+
duration: exitMs,
|
|
306
|
+
})
|
|
307
|
+
)
|
|
308
|
+
scheduleOnRN(handleSwipeDismiss)
|
|
309
|
+
})
|
|
310
|
+
}, [
|
|
311
|
+
axis,
|
|
312
|
+
dismissible,
|
|
313
|
+
drawerEasing,
|
|
314
|
+
enterMs,
|
|
315
|
+
exit,
|
|
316
|
+
exitMs,
|
|
317
|
+
handleSwipeDismiss,
|
|
318
|
+
offset,
|
|
319
|
+
side,
|
|
320
|
+
size,
|
|
321
|
+
])
|
|
322
|
+
|
|
323
|
+
const panelStyle = useAnimatedStyle(() => ({
|
|
324
|
+
opacity: fade.get(),
|
|
325
|
+
transform:
|
|
326
|
+
axis === "y"
|
|
327
|
+
? [{ translateY: offset.get() }]
|
|
328
|
+
: [{ translateX: offset.get() }],
|
|
329
|
+
}))
|
|
330
|
+
const overlayStyle = useAnimatedStyle(() => {
|
|
331
|
+
const travelled =
|
|
332
|
+
size.get() > 0 ? Math.min(Math.abs(offset.get()) / size.get(), 1) : 1
|
|
333
|
+
return { opacity: fade.get() * (1 - travelled) }
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
const context = React.useMemo(
|
|
337
|
+
() => ({
|
|
338
|
+
onOverlayPress: () => {
|
|
339
|
+
if (dismissible) requestClose()
|
|
340
|
+
},
|
|
341
|
+
overlayStyle,
|
|
342
|
+
pan,
|
|
343
|
+
}),
|
|
344
|
+
[dismissible, overlayStyle, pan, requestClose]
|
|
345
|
+
)
|
|
346
|
+
|
|
347
|
+
return (
|
|
348
|
+
<SheetSurfaceContext value={context}>
|
|
349
|
+
<View
|
|
350
|
+
data-slot="sheet-surface"
|
|
351
|
+
aria-modal={modal}
|
|
352
|
+
pointerEvents={open ? "box-none" : "none"}
|
|
353
|
+
className={"fab:absolute fab:inset-0"}
|
|
354
|
+
onAccessibilityEscape={requestClose}
|
|
355
|
+
{...rootProps}
|
|
356
|
+
>
|
|
357
|
+
{modal ? (overlay ?? <SheetSurfaceOverlay />) : null}
|
|
358
|
+
<KeyboardAvoidingView
|
|
359
|
+
data-slot="sheet-surface-viewport"
|
|
360
|
+
data-side={side}
|
|
361
|
+
behavior="padding"
|
|
362
|
+
pointerEvents="box-none"
|
|
363
|
+
className={"fab:data-[side=bottom]:justify-end fab:data-[side=left]:items-start fab:data-[side=right]:items-end fab:data-[side=top]:justify-start fab:flex-1"}
|
|
364
|
+
{...viewportProps}
|
|
365
|
+
>
|
|
366
|
+
<GestureDetector gesture={pan}>
|
|
367
|
+
<Animated.View
|
|
368
|
+
ref={focusRef}
|
|
369
|
+
data-slot="sheet-surface-panel"
|
|
370
|
+
data-side={side}
|
|
371
|
+
role="dialog"
|
|
372
|
+
className={cn("md:data-[side=bottom]:max-w-xl md:data-[side=bottom]:self-center md:data-[side=top]:max-w-xl md:data-[side=top]:self-center max-h-full data-[side=bottom]:w-full data-[side=bottom]:pb-safe data-[side=left]:h-full data-[side=left]:pt-safe data-[side=left]:pb-safe data-[side=left]:pl-safe data-[side=right]:h-full data-[side=right]:pt-safe data-[side=right]:pr-safe data-[side=right]:pb-safe data-[side=top]:w-full data-[side=top]:pt-safe", className)}
|
|
373
|
+
style={[panelStyle, style]}
|
|
374
|
+
onLayout={handleLayout}
|
|
375
|
+
{...props}
|
|
376
|
+
>
|
|
377
|
+
{/* Pulled past open, the panel's colour keeps covering the edge. */}
|
|
378
|
+
<View
|
|
379
|
+
pointerEvents="none"
|
|
380
|
+
data-side={side}
|
|
381
|
+
className={"fab:bg-surface-3 fab:data-[side=bottom]:inset-x-0 fab:data-[side=bottom]:-bottom-12 fab:data-[side=bottom]:h-12 fab:data-[side=top]:inset-x-0 fab:data-[side=top]:-top-12 fab:data-[side=top]:h-12 fab:data-[side=left]:inset-y-0 fab:data-[side=left]:-left-12 fab:data-[side=left]:w-12 fab:data-[side=right]:inset-y-0 fab:data-[side=right]:-right-12 fab:data-[side=right]:w-12 fab:absolute"}
|
|
382
|
+
/>
|
|
383
|
+
{children}
|
|
384
|
+
</Animated.View>
|
|
385
|
+
</GestureDetector>
|
|
386
|
+
</KeyboardAvoidingView>
|
|
387
|
+
</View>
|
|
388
|
+
</SheetSurfaceContext>
|
|
389
|
+
)
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* A panel that slides in from one edge over a scrim: swipe to dismiss with
|
|
394
|
+
* velocity, Android back, screen reader modal, safe areas and keyboard avoidance.
|
|
395
|
+
* Portals to the root and keeps the nearest density. Portals leave the React tree,
|
|
396
|
+
* so anything inside that reads a context has to get it from inside `children`.
|
|
397
|
+
*/
|
|
398
|
+
function SheetSurface({
|
|
399
|
+
open,
|
|
400
|
+
side = "bottom",
|
|
401
|
+
dismissible = true,
|
|
402
|
+
modal = true,
|
|
403
|
+
hostName,
|
|
404
|
+
...props
|
|
405
|
+
}: SheetSurfaceProps) {
|
|
406
|
+
const density = useDensity()
|
|
407
|
+
const name = React.useId()
|
|
408
|
+
useSurfaceFeedback("panel", open)
|
|
409
|
+
const [isMounted, setIsMounted] = React.useState(open)
|
|
410
|
+
if (open && !isMounted) setIsMounted(true)
|
|
411
|
+
const handleExited = React.useCallback(() => setIsMounted(false), [])
|
|
412
|
+
if (!isMounted) return null
|
|
413
|
+
return (
|
|
414
|
+
<Portal name={name} hostName={hostName}>
|
|
415
|
+
<DensityScope key={name} density={density}>
|
|
416
|
+
<SheetPanel
|
|
417
|
+
open={open}
|
|
418
|
+
onExited={handleExited}
|
|
419
|
+
side={side}
|
|
420
|
+
dismissible={dismissible}
|
|
421
|
+
modal={modal}
|
|
422
|
+
{...props}
|
|
423
|
+
/>
|
|
424
|
+
</DensityScope>
|
|
425
|
+
</Portal>
|
|
426
|
+
)
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
export { SheetSurface, SheetSurfaceOverlay, SheetSurfaceScrollView }
|