@asaddevworld/sheet-stack 0.0.0-stage → 0.1.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 +20 -0
- package/README.md +148 -2
- package/lib/module/Backdrop.js +30 -0
- package/lib/module/Backdrop.js.map +1 -0
- package/lib/module/Sheet.js +303 -0
- package/lib/module/Sheet.js.map +1 -0
- package/lib/module/SheetHost.js +94 -0
- package/lib/module/SheetHost.js.map +1 -0
- package/lib/module/SheetProvider.js +76 -0
- package/lib/module/SheetProvider.js.map +1 -0
- package/lib/module/SheetScrollView.js +51 -0
- package/lib/module/SheetScrollView.js.map +1 -0
- package/lib/module/context.js +33 -0
- package/lib/module/context.js.map +1 -0
- package/lib/module/defaults.js +34 -0
- package/lib/module/defaults.js.map +1 -0
- package/lib/module/index.js +7 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/types.js +4 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/utils/snap.js +93 -0
- package/lib/module/utils/snap.js.map +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/Backdrop.d.ts +9 -0
- package/lib/typescript/src/Backdrop.d.ts.map +1 -0
- package/lib/typescript/src/Sheet.d.ts +18 -0
- package/lib/typescript/src/Sheet.d.ts.map +1 -0
- package/lib/typescript/src/SheetHost.d.ts +2 -0
- package/lib/typescript/src/SheetHost.d.ts.map +1 -0
- package/lib/typescript/src/SheetProvider.d.ts +10 -0
- package/lib/typescript/src/SheetProvider.d.ts.map +1 -0
- package/lib/typescript/src/SheetScrollView.d.ts +7 -0
- package/lib/typescript/src/SheetScrollView.d.ts.map +1 -0
- package/lib/typescript/src/context.d.ts +20 -0
- package/lib/typescript/src/context.d.ts.map +1 -0
- package/lib/typescript/src/defaults.d.ts +13 -0
- package/lib/typescript/src/defaults.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +6 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/types.d.ts +66 -0
- package/lib/typescript/src/types.d.ts.map +1 -0
- package/lib/typescript/src/utils/snap.d.ts +28 -0
- package/lib/typescript/src/utils/snap.d.ts.map +1 -0
- package/package.json +163 -4
- package/src/Backdrop.tsx +32 -0
- package/src/Sheet.tsx +404 -0
- package/src/SheetHost.tsx +132 -0
- package/src/SheetProvider.tsx +76 -0
- package/src/SheetScrollView.tsx +50 -0
- package/src/context.tsx +52 -0
- package/src/defaults.ts +38 -0
- package/src/index.tsx +13 -0
- package/src/types.ts +78 -0
- package/src/utils/snap.ts +101 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { useCallback, useContext, useEffect, useMemo, useState } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
BackHandler,
|
|
4
|
+
Keyboard,
|
|
5
|
+
Platform,
|
|
6
|
+
StyleSheet,
|
|
7
|
+
View,
|
|
8
|
+
type KeyboardEvent,
|
|
9
|
+
type LayoutChangeEvent,
|
|
10
|
+
} from 'react-native';
|
|
11
|
+
import {
|
|
12
|
+
Easing,
|
|
13
|
+
ReduceMotion,
|
|
14
|
+
useSharedValue,
|
|
15
|
+
withTiming,
|
|
16
|
+
} from 'react-native-reanimated';
|
|
17
|
+
import { useSafeAreaInsets } from 'react-native-safe-area-context';
|
|
18
|
+
import { Backdrop } from './Backdrop';
|
|
19
|
+
import { ActionsContext, RemoveContext, StackContext } from './context';
|
|
20
|
+
import { Sheet } from './Sheet';
|
|
21
|
+
import type { SheetId } from './types';
|
|
22
|
+
|
|
23
|
+
const TOP_GAP = 8;
|
|
24
|
+
|
|
25
|
+
export function SheetHost() {
|
|
26
|
+
const entries = useContext(StackContext);
|
|
27
|
+
const actions = useContext(ActionsContext);
|
|
28
|
+
const remove = useContext(RemoveContext);
|
|
29
|
+
const insets = useSafeAreaInsets();
|
|
30
|
+
const [containerHeight, setContainerHeight] = useState(0);
|
|
31
|
+
const keyboardHeight = useSharedValue(0);
|
|
32
|
+
const backdropOpacity = useSharedValue(0);
|
|
33
|
+
|
|
34
|
+
const open = useMemo(
|
|
35
|
+
() => entries.filter((e) => e.status === 'open'),
|
|
36
|
+
[entries]
|
|
37
|
+
);
|
|
38
|
+
const top = open[open.length - 1];
|
|
39
|
+
|
|
40
|
+
const dismiss = useCallback(
|
|
41
|
+
(id: SheetId) => {
|
|
42
|
+
actions?.dismiss(id);
|
|
43
|
+
},
|
|
44
|
+
[actions]
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
const showEvent =
|
|
49
|
+
Platform.OS === 'ios' ? 'keyboardWillShow' : 'keyboardDidShow';
|
|
50
|
+
const hideEvent =
|
|
51
|
+
Platform.OS === 'ios' ? 'keyboardWillHide' : 'keyboardDidHide';
|
|
52
|
+
const animate = (to: number, e: KeyboardEvent | undefined) => {
|
|
53
|
+
keyboardHeight.value = withTiming(to, {
|
|
54
|
+
duration: e?.duration && e.duration > 0 ? e.duration : 220,
|
|
55
|
+
easing: Easing.out(Easing.cubic),
|
|
56
|
+
reduceMotion: ReduceMotion.System,
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
const show = Keyboard.addListener(showEvent, (e) =>
|
|
60
|
+
animate(e.endCoordinates.height, e)
|
|
61
|
+
);
|
|
62
|
+
const hide = Keyboard.addListener(hideEvent, (e) => animate(0, e));
|
|
63
|
+
return () => {
|
|
64
|
+
show.remove();
|
|
65
|
+
hide.remove();
|
|
66
|
+
};
|
|
67
|
+
}, [keyboardHeight]);
|
|
68
|
+
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
backdropOpacity.value = withTiming(top ? top.options.backdropOpacity : 0, {
|
|
71
|
+
duration: 240,
|
|
72
|
+
reduceMotion: ReduceMotion.System,
|
|
73
|
+
});
|
|
74
|
+
}, [top, backdropOpacity]);
|
|
75
|
+
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
if (!top) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const sub = BackHandler.addEventListener('hardwareBackPress', () => {
|
|
81
|
+
if (top.options.dismissible) {
|
|
82
|
+
dismiss(top.id);
|
|
83
|
+
}
|
|
84
|
+
return true;
|
|
85
|
+
});
|
|
86
|
+
return () => sub.remove();
|
|
87
|
+
}, [top, dismiss]);
|
|
88
|
+
|
|
89
|
+
const onLayout = useCallback((e: LayoutChangeEvent) => {
|
|
90
|
+
setContainerHeight(Math.round(e.nativeEvent.layout.height));
|
|
91
|
+
}, []);
|
|
92
|
+
|
|
93
|
+
const onBackdropPress = useCallback(() => {
|
|
94
|
+
if (top?.options.dismissible) {
|
|
95
|
+
dismiss(top.id);
|
|
96
|
+
}
|
|
97
|
+
}, [top, dismiss]);
|
|
98
|
+
|
|
99
|
+
return (
|
|
100
|
+
<View
|
|
101
|
+
style={StyleSheet.absoluteFill}
|
|
102
|
+
pointerEvents={entries.length > 0 ? 'box-none' : 'none'}
|
|
103
|
+
onLayout={onLayout}
|
|
104
|
+
>
|
|
105
|
+
<Backdrop
|
|
106
|
+
opacity={backdropOpacity}
|
|
107
|
+
active={top !== undefined}
|
|
108
|
+
onPress={onBackdropPress}
|
|
109
|
+
/>
|
|
110
|
+
{containerHeight > 0 &&
|
|
111
|
+
entries.map((entry, i) => {
|
|
112
|
+
const openIndex = open.indexOf(entry);
|
|
113
|
+
const depth = openIndex === -1 ? 0 : open.length - 1 - openIndex;
|
|
114
|
+
return (
|
|
115
|
+
<Sheet
|
|
116
|
+
key={entry.id}
|
|
117
|
+
entry={entry}
|
|
118
|
+
index={openIndex === -1 ? i : openIndex}
|
|
119
|
+
depth={depth}
|
|
120
|
+
isTop={entry === top}
|
|
121
|
+
containerHeight={containerHeight}
|
|
122
|
+
topInset={insets.top + TOP_GAP}
|
|
123
|
+
bottomInset={insets.bottom}
|
|
124
|
+
keyboardHeight={keyboardHeight}
|
|
125
|
+
onClosed={remove}
|
|
126
|
+
requestDismiss={dismiss}
|
|
127
|
+
/>
|
|
128
|
+
);
|
|
129
|
+
})}
|
|
130
|
+
</View>
|
|
131
|
+
);
|
|
132
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { useCallback, useMemo, useRef, useState, type ReactNode } from 'react';
|
|
2
|
+
import { ActionsContext, RemoveContext, StackContext } from './context';
|
|
3
|
+
import { resolveOptions } from './defaults';
|
|
4
|
+
import { SheetHost } from './SheetHost';
|
|
5
|
+
import type { SheetActions, SheetEntry, SheetId } from './types';
|
|
6
|
+
|
|
7
|
+
interface Props {
|
|
8
|
+
children: ReactNode;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Put this once near the root of the app, inside `GestureHandlerRootView` and `SafeAreaProvider`.
|
|
13
|
+
*/
|
|
14
|
+
export function SheetProvider({ children }: Props) {
|
|
15
|
+
const [entries, setEntries] = useState<SheetEntry[]>([]);
|
|
16
|
+
const counter = useRef(0);
|
|
17
|
+
|
|
18
|
+
const actions = useMemo<SheetActions>(
|
|
19
|
+
() => ({
|
|
20
|
+
push: (options) => {
|
|
21
|
+
counter.current += 1;
|
|
22
|
+
const id = `sheet-${counter.current}`;
|
|
23
|
+
setEntries((prev) => [
|
|
24
|
+
...prev,
|
|
25
|
+
{ id, options: resolveOptions(options), status: 'open' },
|
|
26
|
+
]);
|
|
27
|
+
return id;
|
|
28
|
+
},
|
|
29
|
+
dismiss: (id) => {
|
|
30
|
+
setEntries((prev) =>
|
|
31
|
+
prev.map((e) => (e.id === id ? { ...e, status: 'closing' } : e))
|
|
32
|
+
);
|
|
33
|
+
},
|
|
34
|
+
pop: () => {
|
|
35
|
+
setEntries((prev) => {
|
|
36
|
+
const openEntries = prev.filter((e) => e.status === 'open');
|
|
37
|
+
const top = openEntries[openEntries.length - 1];
|
|
38
|
+
if (!top) {
|
|
39
|
+
return prev;
|
|
40
|
+
}
|
|
41
|
+
return prev.map((e) =>
|
|
42
|
+
e.id === top.id ? { ...e, status: 'closing' } : e
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
},
|
|
46
|
+
popAll: () => {
|
|
47
|
+
setEntries((prev) => prev.map((e) => ({ ...e, status: 'closing' })));
|
|
48
|
+
},
|
|
49
|
+
update: (id, patch) => {
|
|
50
|
+
setEntries((prev) =>
|
|
51
|
+
prev.map((e) =>
|
|
52
|
+
e.id === id
|
|
53
|
+
? { ...e, options: resolveOptions({ ...e.options, ...patch }) }
|
|
54
|
+
: e
|
|
55
|
+
)
|
|
56
|
+
);
|
|
57
|
+
},
|
|
58
|
+
}),
|
|
59
|
+
[]
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
const remove = useCallback((id: SheetId) => {
|
|
63
|
+
setEntries((prev) => prev.filter((e) => e.id !== id));
|
|
64
|
+
}, []);
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<ActionsContext.Provider value={actions}>
|
|
68
|
+
<RemoveContext.Provider value={remove}>
|
|
69
|
+
<StackContext.Provider value={entries}>
|
|
70
|
+
{children}
|
|
71
|
+
<SheetHost />
|
|
72
|
+
</StackContext.Provider>
|
|
73
|
+
</RemoveContext.Provider>
|
|
74
|
+
</ActionsContext.Provider>
|
|
75
|
+
);
|
|
76
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { useContext, useEffect } from 'react';
|
|
2
|
+
import type { ScrollViewProps } from 'react-native';
|
|
3
|
+
import { GestureDetector } from 'react-native-gesture-handler';
|
|
4
|
+
import Animated, {
|
|
5
|
+
useAnimatedProps,
|
|
6
|
+
useAnimatedScrollHandler,
|
|
7
|
+
} from 'react-native-reanimated';
|
|
8
|
+
import { SheetInstanceContext } from './context';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A ScrollView that hands the drag to the sheet when its content is at the top,
|
|
12
|
+
* so a downward drag collapses or dismisses the sheet instead of bouncing.
|
|
13
|
+
*/
|
|
14
|
+
export function SheetScrollView(props: ScrollViewProps) {
|
|
15
|
+
const instance = useContext(SheetInstanceContext);
|
|
16
|
+
if (!instance) {
|
|
17
|
+
throw new Error('SheetScrollView must be used inside a sheet.');
|
|
18
|
+
}
|
|
19
|
+
const { hasScroll, scrollY, y, topY, scrollGesture } = instance;
|
|
20
|
+
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
hasScroll.value = true;
|
|
23
|
+
return () => {
|
|
24
|
+
hasScroll.value = false;
|
|
25
|
+
};
|
|
26
|
+
}, [hasScroll]);
|
|
27
|
+
|
|
28
|
+
const onScroll = useAnimatedScrollHandler({
|
|
29
|
+
onScroll: (e) => {
|
|
30
|
+
scrollY.value = e.contentOffset.y;
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const animatedProps = useAnimatedProps(() => ({
|
|
35
|
+
scrollEnabled: y.value <= topY.value + 0.5,
|
|
36
|
+
}));
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<GestureDetector gesture={scrollGesture}>
|
|
40
|
+
<Animated.ScrollView
|
|
41
|
+
bounces={false}
|
|
42
|
+
overScrollMode="never"
|
|
43
|
+
{...props}
|
|
44
|
+
onScroll={onScroll}
|
|
45
|
+
scrollEventThrottle={16}
|
|
46
|
+
animatedProps={animatedProps}
|
|
47
|
+
/>
|
|
48
|
+
</GestureDetector>
|
|
49
|
+
);
|
|
50
|
+
}
|
package/src/context.tsx
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { createContext, useContext, useMemo } from 'react';
|
|
2
|
+
import type { NativeGesture } from 'react-native-gesture-handler';
|
|
3
|
+
import type { SharedValue } from 'react-native-reanimated';
|
|
4
|
+
import type {
|
|
5
|
+
SheetActions,
|
|
6
|
+
SheetEntry,
|
|
7
|
+
SheetId,
|
|
8
|
+
SheetRenderContext,
|
|
9
|
+
SheetsApi,
|
|
10
|
+
} from './types';
|
|
11
|
+
|
|
12
|
+
export const ActionsContext = createContext<SheetActions | null>(null);
|
|
13
|
+
export const StackContext = createContext<SheetEntry[]>([]);
|
|
14
|
+
export const RemoveContext = createContext<(id: SheetId) => void>(() => {});
|
|
15
|
+
|
|
16
|
+
/** Shared values one sheet exposes to the scroll view inside it. */
|
|
17
|
+
export interface SheetInstance extends SheetRenderContext {
|
|
18
|
+
y: SharedValue<number>;
|
|
19
|
+
topY: SharedValue<number>;
|
|
20
|
+
scrollY: SharedValue<number>;
|
|
21
|
+
hasScroll: SharedValue<boolean>;
|
|
22
|
+
scrollGesture: NativeGesture;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const SheetInstanceContext = createContext<SheetInstance | null>(null);
|
|
26
|
+
|
|
27
|
+
/** Open and close sheets from anywhere under `SheetProvider`. */
|
|
28
|
+
export function useSheets(): SheetsApi {
|
|
29
|
+
const actions = useContext(ActionsContext);
|
|
30
|
+
const entries = useContext(StackContext);
|
|
31
|
+
if (!actions) {
|
|
32
|
+
throw new Error('useSheets must be used inside <SheetProvider>.');
|
|
33
|
+
}
|
|
34
|
+
return useMemo(
|
|
35
|
+
() => ({
|
|
36
|
+
...actions,
|
|
37
|
+
stack: entries.filter((e) => e.status === 'open').map((e) => e.id),
|
|
38
|
+
}),
|
|
39
|
+
[actions, entries]
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Information about the sheet the calling component is rendered in. */
|
|
44
|
+
export function useSheet(): SheetRenderContext {
|
|
45
|
+
const instance = useContext(SheetInstanceContext);
|
|
46
|
+
if (!instance) {
|
|
47
|
+
throw new Error(
|
|
48
|
+
'useSheet must be used inside a sheet rendered by SheetProvider.'
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
return instance;
|
|
52
|
+
}
|
package/src/defaults.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ResolvedSheetOptions, SheetOptions } from './types';
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_OPTIONS = {
|
|
4
|
+
snapPoints: ['auto'],
|
|
5
|
+
initialSnap: 0,
|
|
6
|
+
dismissible: true,
|
|
7
|
+
backdropOpacity: 0.4,
|
|
8
|
+
handle: true,
|
|
9
|
+
keyboardBehavior: 'padding',
|
|
10
|
+
stackScale: 0.94,
|
|
11
|
+
stackOffset: 12,
|
|
12
|
+
} satisfies Omit<ResolvedSheetOptions, 'render'>;
|
|
13
|
+
|
|
14
|
+
export function resolveOptions(options: SheetOptions): ResolvedSheetOptions {
|
|
15
|
+
const merged: ResolvedSheetOptions = {
|
|
16
|
+
...DEFAULT_OPTIONS,
|
|
17
|
+
...stripUndefined(options),
|
|
18
|
+
};
|
|
19
|
+
if (merged.snapPoints.length === 0) {
|
|
20
|
+
merged.snapPoints = DEFAULT_OPTIONS.snapPoints;
|
|
21
|
+
}
|
|
22
|
+
merged.initialSnap = Math.min(
|
|
23
|
+
Math.max(0, merged.initialSnap),
|
|
24
|
+
merged.snapPoints.length - 1
|
|
25
|
+
);
|
|
26
|
+
merged.backdropOpacity = Math.min(Math.max(0, merged.backdropOpacity), 1);
|
|
27
|
+
return merged;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function stripUndefined<T extends object>(obj: T): T {
|
|
31
|
+
const out = {} as T;
|
|
32
|
+
for (const key of Object.keys(obj) as (keyof T)[]) {
|
|
33
|
+
if (obj[key] !== undefined) {
|
|
34
|
+
out[key] = obj[key];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return out;
|
|
38
|
+
}
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { SheetProvider } from './SheetProvider';
|
|
2
|
+
export { SheetScrollView } from './SheetScrollView';
|
|
3
|
+
export { useSheet, useSheets } from './context';
|
|
4
|
+
export { DEFAULT_OPTIONS } from './defaults';
|
|
5
|
+
export type {
|
|
6
|
+
KeyboardBehavior,
|
|
7
|
+
SheetActions,
|
|
8
|
+
SheetId,
|
|
9
|
+
SheetOptions,
|
|
10
|
+
SheetRenderContext,
|
|
11
|
+
SheetsApi,
|
|
12
|
+
SnapPoint,
|
|
13
|
+
} from './types';
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type { StyleProp, ViewStyle } from 'react-native';
|
|
3
|
+
|
|
4
|
+
export type SheetId = string;
|
|
5
|
+
|
|
6
|
+
/** A height the sheet can rest at: pixels, a percentage of the container, or 'auto' to fit content. */
|
|
7
|
+
export type SnapPoint = number | `${number}%` | 'auto';
|
|
8
|
+
|
|
9
|
+
export type KeyboardBehavior = 'none' | 'padding';
|
|
10
|
+
|
|
11
|
+
export interface SheetRenderContext {
|
|
12
|
+
/** Id returned by `push`. */
|
|
13
|
+
id: SheetId;
|
|
14
|
+
/** Position in the open stack, 0 is the first sheet opened. */
|
|
15
|
+
index: number;
|
|
16
|
+
/** True when this sheet is the one on top. */
|
|
17
|
+
isTop: boolean;
|
|
18
|
+
/** Close this sheet. */
|
|
19
|
+
dismiss: () => void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface SheetOptions {
|
|
23
|
+
/** Content of the sheet. */
|
|
24
|
+
render: (ctx: SheetRenderContext) => ReactNode;
|
|
25
|
+
/** Heights the sheet can rest at. Default `['auto']`. */
|
|
26
|
+
snapPoints?: SnapPoint[];
|
|
27
|
+
/** Index into `snapPoints` to open at. Default 0. */
|
|
28
|
+
initialSnap?: number;
|
|
29
|
+
/** Drag down, backdrop tap and the Android back button close the sheet. Default true. */
|
|
30
|
+
dismissible?: boolean;
|
|
31
|
+
/** Dim of the backdrop behind the top sheet, 0 to 1. Default 0.4. */
|
|
32
|
+
backdropOpacity?: number;
|
|
33
|
+
/** Show the grab handle. Default true. */
|
|
34
|
+
handle?: boolean;
|
|
35
|
+
/** Move the sheet up with the keyboard. Default 'padding'. */
|
|
36
|
+
keyboardBehavior?: KeyboardBehavior;
|
|
37
|
+
/** Called after the close animation finishes. */
|
|
38
|
+
onDismiss?: () => void;
|
|
39
|
+
/** Called when the sheet settles at a snap point. */
|
|
40
|
+
onSnap?: (index: number) => void;
|
|
41
|
+
/** Style of the sheet container (background, radius). */
|
|
42
|
+
style?: StyleProp<ViewStyle>;
|
|
43
|
+
/** Scale applied to the sheet beneath when a new one opens. Default 0.94. */
|
|
44
|
+
stackScale?: number;
|
|
45
|
+
/** Pixels the sheet beneath moves up when a new one opens. Default 12. */
|
|
46
|
+
stackOffset?: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export type ResolvedSheetOptions = Required<
|
|
50
|
+
Omit<SheetOptions, 'onDismiss' | 'onSnap' | 'style'>
|
|
51
|
+
> &
|
|
52
|
+
Pick<SheetOptions, 'onDismiss' | 'onSnap' | 'style'>;
|
|
53
|
+
|
|
54
|
+
export type SheetStatus = 'open' | 'closing';
|
|
55
|
+
|
|
56
|
+
export interface SheetEntry {
|
|
57
|
+
id: SheetId;
|
|
58
|
+
options: ResolvedSheetOptions;
|
|
59
|
+
status: SheetStatus;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface SheetActions {
|
|
63
|
+
/** Open a sheet on top of the stack. Returns its id. */
|
|
64
|
+
push: (options: SheetOptions) => SheetId;
|
|
65
|
+
/** Close the top sheet. */
|
|
66
|
+
pop: () => void;
|
|
67
|
+
/** Close a specific sheet. */
|
|
68
|
+
dismiss: (id: SheetId) => void;
|
|
69
|
+
/** Close every sheet. */
|
|
70
|
+
popAll: () => void;
|
|
71
|
+
/** Change the options of an open sheet. */
|
|
72
|
+
update: (id: SheetId, patch: Partial<SheetOptions>) => void;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface SheetsApi extends SheetActions {
|
|
76
|
+
/** Ids of the open sheets, bottom to top. Reactive. */
|
|
77
|
+
stack: SheetId[];
|
|
78
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { SnapPoint } from '../types';
|
|
2
|
+
|
|
3
|
+
export const MIN_SHEET_HEIGHT = 80;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Turn snap points into pixel heights, ascending, clamped, without duplicates.
|
|
7
|
+
* `autoHeight` is the measured content height used for 'auto'.
|
|
8
|
+
*/
|
|
9
|
+
export function resolveSnapHeights(
|
|
10
|
+
points: SnapPoint[],
|
|
11
|
+
containerHeight: number,
|
|
12
|
+
maxHeight: number,
|
|
13
|
+
autoHeight: number
|
|
14
|
+
): number[] {
|
|
15
|
+
const heights: number[] = [];
|
|
16
|
+
for (const point of points) {
|
|
17
|
+
let h: number;
|
|
18
|
+
if (point === 'auto') {
|
|
19
|
+
h = autoHeight;
|
|
20
|
+
} else if (typeof point === 'number') {
|
|
21
|
+
h = point;
|
|
22
|
+
} else {
|
|
23
|
+
h = (parseFloat(point) / 100) * containerHeight;
|
|
24
|
+
}
|
|
25
|
+
if (!Number.isFinite(h)) {
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
heights.push(
|
|
29
|
+
Math.round(Math.min(Math.max(h, MIN_SHEET_HEIGHT), maxHeight))
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
const unique = Array.from(new Set(heights)).sort((a, b) => a - b);
|
|
33
|
+
if (unique.length === 0) {
|
|
34
|
+
return [Math.round(Math.min(MIN_SHEET_HEIGHT, maxHeight))];
|
|
35
|
+
}
|
|
36
|
+
return unique;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** iOS-style resistance: the further you pull, the less it moves. */
|
|
40
|
+
export function rubberBand(
|
|
41
|
+
distance: number,
|
|
42
|
+
dimension: number,
|
|
43
|
+
coefficient = 0.55
|
|
44
|
+
): number {
|
|
45
|
+
'worklet';
|
|
46
|
+
if (distance <= 0 || dimension <= 0) {
|
|
47
|
+
return 0;
|
|
48
|
+
}
|
|
49
|
+
return (1 - 1 / ((distance * coefficient) / dimension + 1)) * dimension;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface SnapTarget {
|
|
53
|
+
y: number;
|
|
54
|
+
/** Index into snapYs, or -1 for dismiss. */
|
|
55
|
+
index: number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const FLING_VELOCITY = 1200;
|
|
59
|
+
const PROJECTION = 0.15;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Decide where the sheet should settle after a release.
|
|
63
|
+
* `snapYs` are translateY values in the same order as the resolved heights (so the first
|
|
64
|
+
* entry is the lowest position). `closedY` is the off-screen position.
|
|
65
|
+
*/
|
|
66
|
+
export function pickSnapTarget(params: {
|
|
67
|
+
y: number;
|
|
68
|
+
velocityY: number;
|
|
69
|
+
snapYs: number[];
|
|
70
|
+
closedY: number;
|
|
71
|
+
dismissible: boolean;
|
|
72
|
+
}): SnapTarget {
|
|
73
|
+
'worklet';
|
|
74
|
+
const { y, velocityY, snapYs, closedY, dismissible } = params;
|
|
75
|
+
const lowestY = snapYs[0] ?? closedY;
|
|
76
|
+
const projected = y + velocityY * PROJECTION;
|
|
77
|
+
|
|
78
|
+
if (dismissible) {
|
|
79
|
+
if (velocityY > FLING_VELOCITY) {
|
|
80
|
+
return { y: closedY, index: -1 };
|
|
81
|
+
}
|
|
82
|
+
if (projected > lowestY + (closedY - lowestY) * 0.5) {
|
|
83
|
+
return { y: closedY, index: -1 };
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
let best = 0;
|
|
88
|
+
let bestDistance = Number.POSITIVE_INFINITY;
|
|
89
|
+
for (let i = 0; i < snapYs.length; i++) {
|
|
90
|
+
const candidate = snapYs[i];
|
|
91
|
+
if (candidate === undefined) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
const distance = Math.abs(candidate - projected);
|
|
95
|
+
if (distance < bestDistance) {
|
|
96
|
+
bestDistance = distance;
|
|
97
|
+
best = i;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return { y: snapYs[best] ?? lowestY, index: best };
|
|
101
|
+
}
|