mobile-design 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 +21 -0
- package/LICENSE-Apache-2.0.txt +55 -0
- package/NOTICE +11 -0
- package/README.md +96 -2
- package/assets/fonts/MaterialSymbolsRounded.ttf +0 -0
- package/assets/fonts/MaterialSymbolsRounded.woff2 +0 -0
- package/assets/fonts/roboto/OFL.txt +93 -0
- package/assets/fonts/roboto/Roboto_400Regular.ttf +0 -0
- package/assets/fonts/roboto/Roboto_400Regular.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_400Regular_Italic.ttf +0 -0
- package/assets/fonts/roboto/Roboto_400Regular_Italic.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_500Medium.ttf +0 -0
- package/assets/fonts/roboto/Roboto_500Medium.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_500Medium_Italic.ttf +0 -0
- package/assets/fonts/roboto/Roboto_500Medium_Italic.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_600SemiBold.ttf +0 -0
- package/assets/fonts/roboto/Roboto_600SemiBold.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_600SemiBold_Italic.ttf +0 -0
- package/assets/fonts/roboto/Roboto_600SemiBold_Italic.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_700Bold.ttf +0 -0
- package/assets/fonts/roboto/Roboto_700Bold.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_700Bold_Italic.ttf +0 -0
- package/assets/fonts/roboto/Roboto_700Bold_Italic.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_800ExtraBold.ttf +0 -0
- package/assets/fonts/roboto/Roboto_800ExtraBold.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_800ExtraBold_Italic.ttf +0 -0
- package/assets/fonts/roboto/Roboto_800ExtraBold_Italic.woff2 +0 -0
- package/assets/logo.svg +13 -0
- package/package.json +62 -3
- package/src/catalog.ts +683 -0
- package/src/color-scheme.ts +68 -0
- package/src/color.ts +35 -0
- package/src/components/action-surface.tsx +83 -0
- package/src/components/appbar-layout.ts +85 -0
- package/src/components/appbar-scroll-behavior.tsx +386 -0
- package/src/components/appbar.tsx +474 -0
- package/src/components/avatar.tsx +135 -0
- package/src/components/bottom-sheet-text-field.tsx +13 -0
- package/src/components/bottom-sheet-text-input.tsx +2 -0
- package/src/components/bottom-sheet-text-input.web.tsx +2 -0
- package/src/components/button-group.tsx +996 -0
- package/src/components/button.tsx +504 -0
- package/src/components/card.tsx +284 -0
- package/src/components/carousel-full-screen.tsx +374 -0
- package/src/components/carousel-layout.ts +418 -0
- package/src/components/carousel-scroll-controller.tsx +303 -0
- package/src/components/carousel-vertical-layout.ts +92 -0
- package/src/components/carousel-vertical.tsx +505 -0
- package/src/components/carousel.tsx +678 -0
- package/src/components/choice-motion.tsx +108 -0
- package/src/components/choice-types.ts +52 -0
- package/src/components/choice.tsx +400 -0
- package/src/components/choice.web.ts +1 -0
- package/src/components/circular-progress-status.tsx +218 -0
- package/src/components/container-transform.tsx +304 -0
- package/src/components/content.tsx +1450 -0
- package/src/components/date-picker-calendar.tsx +933 -0
- package/src/components/date-picker-months.tsx +102 -0
- package/src/components/date-picker-utils.ts +96 -0
- package/src/components/date-picker.tsx +641 -0
- package/src/components/dialog.tsx +340 -0
- package/src/components/docked-date-picker.tsx +525 -0
- package/src/components/drawer.tsx +248 -0
- package/src/components/fab.tsx +1221 -0
- package/src/components/focus-order.ts +40 -0
- package/src/components/full-screen-dialog.tsx +234 -0
- package/src/components/icon-button.tsx +72 -0
- package/src/components/list-focus.tsx +182 -0
- package/src/components/list-reorder.tsx +349 -0
- package/src/components/loading-indicator.tsx +378 -0
- package/src/components/menu.tsx +1091 -0
- package/src/components/navigation-bar.tsx +390 -0
- package/src/components/navigation-focus.ts +45 -0
- package/src/components/navigation-rail-layout.ts +53 -0
- package/src/components/navigation.tsx +682 -0
- package/src/components/popup.tsx +624 -0
- package/src/components/press-scope.ts +17 -0
- package/src/components/progress-indicator-paths.ts +254 -0
- package/src/components/progress-indicator.tsx +570 -0
- package/src/components/progress.tsx +39 -0
- package/src/components/pull-to-refresh.tsx +379 -0
- package/src/components/rich-tooltip.tsx +183 -0
- package/src/components/search-icon-button.tsx +8 -0
- package/src/components/search-trigger.tsx +28 -0
- package/src/components/search-view.tsx +790 -0
- package/src/components/searchbar.tsx +190 -0
- package/src/components/segmented-buttons.tsx +456 -0
- package/src/components/selection-controls.tsx +760 -0
- package/src/components/sheets.tsx +1027 -0
- package/src/components/slider.tsx +1290 -0
- package/src/components/snackbar.tsx +430 -0
- package/src/components/split-button.tsx +525 -0
- package/src/components/swipe-list-item.tsx +793 -0
- package/src/components/switch.tsx +319 -0
- package/src/components/tabs-pager.tsx +241 -0
- package/src/components/tabs-pager.web.tsx +110 -0
- package/src/components/tabs.tsx +759 -0
- package/src/components/text-field.tsx +673 -0
- package/src/components/time-picker.tsx +1248 -0
- package/src/components/toolbar.tsx +590 -0
- package/src/components/tooltip.tsx +273 -0
- package/src/components/web-choice.tsx +572 -0
- package/src/core.tsx +169 -0
- package/src/font-assets.ts +28 -0
- package/src/font-assets.web.ts +25 -0
- package/src/index.ts +139 -0
- package/src/material-symbol-codepoints.ts +67 -0
- package/src/material-symbol-paths.ts +340 -0
- package/src/material-symbols.tsx +155 -0
- package/src/native-modal.tsx +50 -0
- package/src/outside-interaction.tsx +39 -0
- package/src/paper-icon-aliases.ts +147 -0
- package/src/paper-internals.d.ts +15 -0
- package/src/paper-motion-bridge.tsx +11 -0
- package/src/paper-motion.tsx +24 -0
- package/src/primitives.tsx +13 -0
- package/src/ripple.tsx +7 -0
- package/src/ripple.web.tsx +108 -0
- package/src/scroll-indicators.ts +12 -0
- package/src/typography.ts +9 -0
- package/src/web-accessibility.ts +19 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { Theme } from 'react-native-paper';
|
|
2
|
+
import materialColors from '@mobile-design/tokens/colors';
|
|
3
|
+
|
|
4
|
+
/** Flat generated tokens are shared by native Paper and the application CSS. */
|
|
5
|
+
type MobileDesignColorScheme = typeof materialColors.appColorTokens.light;
|
|
6
|
+
|
|
7
|
+
/** Convert only once at the theme boundary; legacy CSS roles stay outside Paper. */
|
|
8
|
+
export function createMobileDesignColors(colorTokens: MobileDesignColorScheme) {
|
|
9
|
+
return {
|
|
10
|
+
primary: colorTokens.primary,
|
|
11
|
+
onPrimary: colorTokens.onPrimary,
|
|
12
|
+
primaryContainer: colorTokens.primaryContainer,
|
|
13
|
+
onPrimaryContainer: colorTokens.onPrimaryContainer,
|
|
14
|
+
secondary: colorTokens.secondary,
|
|
15
|
+
onSecondary: colorTokens.onSecondary,
|
|
16
|
+
secondaryContainer: colorTokens.secondaryContainer,
|
|
17
|
+
onSecondaryContainer: colorTokens.onSecondaryContainer,
|
|
18
|
+
tertiary: colorTokens.tertiary,
|
|
19
|
+
onTertiary: colorTokens.onTertiary,
|
|
20
|
+
tertiaryContainer: colorTokens.tertiaryContainer,
|
|
21
|
+
onTertiaryContainer: colorTokens.onTertiaryContainer,
|
|
22
|
+
error: colorTokens.error,
|
|
23
|
+
onError: colorTokens.onError,
|
|
24
|
+
errorContainer: colorTokens.errorContainer,
|
|
25
|
+
onErrorContainer: colorTokens.onErrorContainer,
|
|
26
|
+
background: colorTokens.background,
|
|
27
|
+
onBackground: colorTokens.onBackground,
|
|
28
|
+
surface: colorTokens.surface,
|
|
29
|
+
onSurface: colorTokens.onSurface,
|
|
30
|
+
surfaceVariant: colorTokens.surfaceVariant,
|
|
31
|
+
onSurfaceVariant: colorTokens.onSurfaceVariant,
|
|
32
|
+
outline: colorTokens.outline,
|
|
33
|
+
outlineVariant: colorTokens.outlineVariant,
|
|
34
|
+
inverseSurface: colorTokens.inverseSurface,
|
|
35
|
+
inverseOnSurface: colorTokens.inverseOnSurface,
|
|
36
|
+
inversePrimary: colorTokens.inversePrimary,
|
|
37
|
+
shadow: colorTokens.shadow,
|
|
38
|
+
scrim: colorTokens.scrim,
|
|
39
|
+
surfaceDim: colorTokens.surfaceDim,
|
|
40
|
+
surfaceBright: colorTokens.surfaceBright,
|
|
41
|
+
surfaceContainerLowest: colorTokens.surfaceContainerLowest,
|
|
42
|
+
surfaceContainerLow: colorTokens.surfaceContainerLow,
|
|
43
|
+
surfaceContainer: colorTokens.surfaceContainer,
|
|
44
|
+
surfaceContainerHigh: colorTokens.surfaceContainerHigh,
|
|
45
|
+
surfaceContainerHighest: colorTokens.surfaceContainerHighest,
|
|
46
|
+
primaryFixed: colorTokens.primaryFixed,
|
|
47
|
+
primaryFixedDim: colorTokens.primaryFixedDim,
|
|
48
|
+
onPrimaryFixed: colorTokens.onPrimaryFixed,
|
|
49
|
+
onPrimaryFixedVariant: colorTokens.onPrimaryFixedVariant,
|
|
50
|
+
secondaryFixed: colorTokens.secondaryFixed,
|
|
51
|
+
secondaryFixedDim: colorTokens.secondaryFixedDim,
|
|
52
|
+
onSecondaryFixed: colorTokens.onSecondaryFixed,
|
|
53
|
+
onSecondaryFixedVariant: colorTokens.onSecondaryFixedVariant,
|
|
54
|
+
tertiaryFixed: colorTokens.tertiaryFixed,
|
|
55
|
+
tertiaryFixedDim: colorTokens.tertiaryFixedDim,
|
|
56
|
+
onTertiaryFixed: colorTokens.onTertiaryFixed,
|
|
57
|
+
onTertiaryFixedVariant: colorTokens.onTertiaryFixedVariant,
|
|
58
|
+
stateLayerPressed: colorTokens.stateLayerPressed,
|
|
59
|
+
elevation: {
|
|
60
|
+
level0: colorTokens.elevationLevel0,
|
|
61
|
+
level1: colorTokens.elevationLevel1,
|
|
62
|
+
level2: colorTokens.elevationLevel2,
|
|
63
|
+
level3: colorTokens.elevationLevel3,
|
|
64
|
+
level4: colorTokens.elevationLevel4,
|
|
65
|
+
level5: colorTokens.elevationLevel5,
|
|
66
|
+
},
|
|
67
|
+
} satisfies Theme['colors'];
|
|
68
|
+
}
|
package/src/color.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { ColorValue } from 'react-native';
|
|
2
|
+
|
|
3
|
+
export function withAlpha(color: string, alpha: number) {
|
|
4
|
+
const rgbMatch = color.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/);
|
|
5
|
+
|
|
6
|
+
if (rgbMatch) {
|
|
7
|
+
return `rgba(${rgbMatch[1]}, ${rgbMatch[2]}, ${rgbMatch[3]}, ${alpha})`;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const hexMatch = color.match(/^#?([a-f\d]{3}|[a-f\d]{6})$/i);
|
|
11
|
+
|
|
12
|
+
if (!hexMatch) {
|
|
13
|
+
return color;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const hexValue =
|
|
17
|
+
hexMatch[1].length === 3
|
|
18
|
+
? hexMatch[1]
|
|
19
|
+
.split('')
|
|
20
|
+
.map((character) => `${character}${character}`)
|
|
21
|
+
.join('')
|
|
22
|
+
: hexMatch[1];
|
|
23
|
+
const red = Number.parseInt(hexValue.slice(0, 2), 16);
|
|
24
|
+
const green = Number.parseInt(hexValue.slice(2, 4), 16);
|
|
25
|
+
const blue = Number.parseInt(hexValue.slice(4, 6), 16);
|
|
26
|
+
|
|
27
|
+
return `rgba(${red}, ${green}, ${blue}, ${alpha})`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Adapt numeric ARGB colors at the vector-icon boundary without casting dynamic colors. */
|
|
31
|
+
export function toIconColor(color: ColorValue | undefined) {
|
|
32
|
+
if (color == null) return undefined;
|
|
33
|
+
if (typeof color !== 'number') return color;
|
|
34
|
+
return `rgba(${(color >>> 16) & 255}, ${(color >>> 8) & 255}, ${color & 255}, ${((color >>> 24) & 255) / 255})`;
|
|
35
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Shadow layers adapted from React Native Paper6.0.0-alpha.0 Surface/elevation
|
|
3
|
+
* (MIT, Copyright 2017 Callstack). MD3 elevation tokens, not a new shadow scale.
|
|
4
|
+
* Unlike Paper's iOS Surface, both native layers receive Reanimated geometry.
|
|
5
|
+
*/
|
|
6
|
+
import type { ComponentProps, ReactNode } from 'react';
|
|
7
|
+
import { Platform, StyleSheet, type StyleProp, type ViewProps, type ViewStyle } from 'react-native';
|
|
8
|
+
import { useTheme, type Theme } from 'react-native-paper';
|
|
9
|
+
import Animated, { type AnimatedStyle } from 'react-native-reanimated';
|
|
10
|
+
import { withAlpha } from '../color';
|
|
11
|
+
|
|
12
|
+
type Elevation = 0 | 1 | 2 | 3 | 4 | 5;
|
|
13
|
+
type Props = Omit<ViewProps, 'style'> & {
|
|
14
|
+
children?: ReactNode;
|
|
15
|
+
elevation?: Elevation;
|
|
16
|
+
mode?: 'flat' | 'elevated';
|
|
17
|
+
style?: ComponentProps<typeof Animated.View>['style'];
|
|
18
|
+
geometry?: AnimatedStyle<ViewStyle>;
|
|
19
|
+
};
|
|
20
|
+
const androidLevels = [0, 1, 3, 6, 8, 12];
|
|
21
|
+
const layers = [
|
|
22
|
+
{ alpha: 0.15, y: [0, 1, 2, 4, 6, 8], blur: [0, 3, 6, 8, 10, 12] },
|
|
23
|
+
{ alpha: 0.3, y: [0, 1, 1, 1, 2, 4], blur: [0, 2, 2, 3, 3, 4] },
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
/** A Paper-context surface whose shadow and fill share the same UI-thread shape. */
|
|
27
|
+
export function AnimatedActionSurface({ elevation = 0, mode = 'elevated', style, geometry, children, ...props }: Props) {
|
|
28
|
+
const theme = useTheme<Theme>();
|
|
29
|
+
const flat = StyleSheet.flatten(style as StyleProp<ViewStyle>) ?? {};
|
|
30
|
+
const level = mode === 'flat' ? 0 : elevation;
|
|
31
|
+
const backgroundColor = flat.backgroundColor ?? theme.colors.elevation[`level${level}`];
|
|
32
|
+
if (Platform.OS === 'android')
|
|
33
|
+
return (
|
|
34
|
+
<Animated.View {...props} style={[{ backgroundColor, elevation: androidLevels[level] }, style, geometry]}>
|
|
35
|
+
{children}
|
|
36
|
+
</Animated.View>
|
|
37
|
+
);
|
|
38
|
+
if (Platform.OS === 'web')
|
|
39
|
+
return (
|
|
40
|
+
<Animated.View
|
|
41
|
+
{...props}
|
|
42
|
+
style={[
|
|
43
|
+
{
|
|
44
|
+
backgroundColor,
|
|
45
|
+
boxShadow:
|
|
46
|
+
level && typeof theme.colors.shadow === 'string'
|
|
47
|
+
? layers.map((layer) => ({
|
|
48
|
+
offsetX: 0,
|
|
49
|
+
offsetY: layer.y[level],
|
|
50
|
+
blurRadius: layer.blur[level],
|
|
51
|
+
color: withAlpha(theme.colors.shadow as string, layer.alpha),
|
|
52
|
+
}))
|
|
53
|
+
: undefined,
|
|
54
|
+
},
|
|
55
|
+
style,
|
|
56
|
+
geometry,
|
|
57
|
+
]}
|
|
58
|
+
>
|
|
59
|
+
{children}
|
|
60
|
+
</Animated.View>
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const corners = Object.fromEntries(Object.entries(flat).filter(([key]) => key.startsWith('border') && key.endsWith('Radius')));
|
|
64
|
+
const shadow = (index: number): ViewStyle => ({
|
|
65
|
+
shadowColor: theme.colors.shadow,
|
|
66
|
+
shadowOpacity: level ? layers[index].alpha : 0,
|
|
67
|
+
shadowOffset: { width: 0, height: layers[index].y[level] },
|
|
68
|
+
// RN's iOS shadowRadius is Gaussian sigma, CSS blur-radius is2×sigma.
|
|
69
|
+
shadowRadius: layers[index].blur[level] * 0.5,
|
|
70
|
+
});
|
|
71
|
+
return (
|
|
72
|
+
<Animated.View {...props} style={[{ backgroundColor, ...shadow(0) }, style, geometry]}>
|
|
73
|
+
<Animated.View
|
|
74
|
+
style={[
|
|
75
|
+
{ alignSelf: 'stretch', backgroundColor, ...corners, ...(flat.flex || flat.height ? { flex: 1 } : {}), ...shadow(1) },
|
|
76
|
+
geometry,
|
|
77
|
+
]}
|
|
78
|
+
>
|
|
79
|
+
{children}
|
|
80
|
+
</Animated.View>
|
|
81
|
+
</Animated.View>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Adapted from AndroidX AppBar.kt, Copyright 2021 The Android Open Source Project.
|
|
3
|
+
// https://github.com/androidx/androidx/blob/d153d87ed76372ece0c99dac837ff07ab845d495/compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/AppBar.kt
|
|
4
|
+
// See ../../LICENSE-Apache-2.0.txt and ../../NOTICE.
|
|
5
|
+
export function appBarFinite(value: number, fallback = 0) {
|
|
6
|
+
'worklet';
|
|
7
|
+
return Number.isFinite(value) ? value : fallback;
|
|
8
|
+
}
|
|
9
|
+
export function appBarFraction(value: number) {
|
|
10
|
+
'worklet';
|
|
11
|
+
return Math.max(0, Math.min(1, appBarFinite(value)));
|
|
12
|
+
}
|
|
13
|
+
/** Solve the x coordinate before evaluating y; an easing is not a polynomial in x. */
|
|
14
|
+
function bezierAt(x: number, x1: number, y1: number, x2: number, y2: number) {
|
|
15
|
+
'worklet';
|
|
16
|
+
const fraction = appBarFraction(x);
|
|
17
|
+
if (fraction === 0 || fraction === 1) return fraction;
|
|
18
|
+
let low = 0,
|
|
19
|
+
high = 1,
|
|
20
|
+
t = fraction;
|
|
21
|
+
for (let index = 0; index < 18; index++) {
|
|
22
|
+
const inverse = 1 - t;
|
|
23
|
+
const at = 3 * inverse * inverse * t * x1 + 3 * inverse * t * t * x2 + t * t * t;
|
|
24
|
+
if (Math.abs(at - fraction) < 0.000001) break;
|
|
25
|
+
if (at < fraction) low = t;
|
|
26
|
+
else high = t;
|
|
27
|
+
t = (low + high) / 2;
|
|
28
|
+
}
|
|
29
|
+
const inverse = 1 - t;
|
|
30
|
+
return 3 * inverse * inverse * t * y1 + 3 * inverse * t * t * y2 + t * t * t;
|
|
31
|
+
}
|
|
32
|
+
/** TopTitleAlphaEasing from AndroidX; the expanded title keeps alpha1−fraction. */
|
|
33
|
+
export function appBarCompactTitleAlpha(fraction: number) {
|
|
34
|
+
'worklet';
|
|
35
|
+
return bezierAt(fraction, 0.8, 0, 0.8, 0.15);
|
|
36
|
+
}
|
|
37
|
+
/** FastOutLinearInEasing used by TopAppBarColors.containerColor. */
|
|
38
|
+
export function appBarContainerFraction(fraction: number) {
|
|
39
|
+
'worklet';
|
|
40
|
+
return bezierAt(fraction, 0.4, 0, 1, 1);
|
|
41
|
+
}
|
|
42
|
+
/** At312dp stop filling the entire interval; beyond624dp use half the interval. */
|
|
43
|
+
export function appBarSearchWidth(available: number) {
|
|
44
|
+
return Math.min(Math.max(0, appBarFinite(available)), Math.max(312, appBarFinite(available) * 0.5));
|
|
45
|
+
}
|
|
46
|
+
/** Center in the whole safe width, then move only as far as real actions require. */
|
|
47
|
+
export function appBarTitleFrame(
|
|
48
|
+
width: number,
|
|
49
|
+
leftInset: number,
|
|
50
|
+
rightInset: number,
|
|
51
|
+
leading: number,
|
|
52
|
+
trailing: number,
|
|
53
|
+
hasLeading: boolean,
|
|
54
|
+
rtl: boolean,
|
|
55
|
+
centered: boolean,
|
|
56
|
+
measuredWidth: number
|
|
57
|
+
) {
|
|
58
|
+
const start = hasLeading ? 8 + leading : 16;
|
|
59
|
+
const end = 8 + trailing;
|
|
60
|
+
const left = leftInset + (rtl ? end : start);
|
|
61
|
+
const right = Math.max(left, width - rightInset - (rtl ? start : end));
|
|
62
|
+
const available = Math.max(0, right - left);
|
|
63
|
+
if (!centered || measuredWidth <= 0)
|
|
64
|
+
return {
|
|
65
|
+
left,
|
|
66
|
+
width: available,
|
|
67
|
+
translateX: 0,
|
|
68
|
+
};
|
|
69
|
+
const textWidth = Math.min(available, measuredWidth);
|
|
70
|
+
const center = (leftInset + width - rightInset) / 2;
|
|
71
|
+
const movement = Math.max(0, (available - textWidth) / 2);
|
|
72
|
+
// Keep the measure constraint at the entire available width. A previously
|
|
73
|
+
// truncated measurement must not cap intrinsic text when the window grows.
|
|
74
|
+
return {
|
|
75
|
+
left,
|
|
76
|
+
width: available,
|
|
77
|
+
translateX: Math.max(-movement, Math.min(movement, center - (left + right) / 2)),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/** Bottom24/28 is baseline padding, reduced when the measured row has no room. */
|
|
81
|
+
export function appBarExpandedRow(minHeight: number, contentHeight: number, lastBaseline: number, baselinePadding: number) {
|
|
82
|
+
const height = Math.max(0, minHeight, appBarFinite(contentHeight));
|
|
83
|
+
const belowBaseline = Math.max(0, contentHeight - appBarFinite(lastBaseline, contentHeight));
|
|
84
|
+
return { height, bottomPadding: Math.max(0, Math.min(height - contentHeight, baselinePadding - belowBaseline)) };
|
|
85
|
+
}
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Adapted from AndroidX AppBar.kt, Copyright 2021 The Android Open Source Project.
|
|
3
|
+
// https://github.com/androidx/androidx/blob/d153d87ed76372ece0c99dac837ff07ab845d495/compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/AppBar.kt
|
|
4
|
+
// See ../../LICENSE-Apache-2.0.txt and ../../NOTICE.
|
|
5
|
+
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef } from 'react';
|
|
6
|
+
import { Platform } from 'react-native';
|
|
7
|
+
import { useTheme, type Theme } from 'react-native-paper';
|
|
8
|
+
import { Gesture } from 'react-native-gesture-handler';
|
|
9
|
+
import {
|
|
10
|
+
cancelAnimation,
|
|
11
|
+
ReduceMotion,
|
|
12
|
+
useAnimatedScrollHandler,
|
|
13
|
+
useDerivedValue,
|
|
14
|
+
useSharedValue,
|
|
15
|
+
withDecay,
|
|
16
|
+
withSpring,
|
|
17
|
+
type SharedValue,
|
|
18
|
+
} from 'react-native-reanimated';
|
|
19
|
+
import { scheduleOnRN, scheduleOnUI } from 'react-native-worklets';
|
|
20
|
+
import { appBarFinite, appBarFraction } from './appbar-layout';
|
|
21
|
+
export type AppBarScrollMode = 'pinned' | 'exit-until-collapsed' | 'enter-always';
|
|
22
|
+
type Metrics = {
|
|
23
|
+
expanded: number;
|
|
24
|
+
collapsed: number;
|
|
25
|
+
top: number;
|
|
26
|
+
};
|
|
27
|
+
export type AppBarScrollBehavior = {
|
|
28
|
+
onScroll: ReturnType<typeof useAnimatedScrollHandler>;
|
|
29
|
+
collapseProgress: SharedValue<number>;
|
|
30
|
+
heightOffset: SharedValue<number>;
|
|
31
|
+
/** Resets the bar; the caller still owns the ScrollView and its scrollTo. */
|
|
32
|
+
reset: () => void;
|
|
33
|
+
/** @internal Measured by TopAppBar, not configured by the consuming screen. */
|
|
34
|
+
_metrics: SharedValue<Metrics>;
|
|
35
|
+
/** @internal */ _mode: SharedValue<AppBarScrollMode>;
|
|
36
|
+
/** @internal */ _contentOffset: SharedValue<number>;
|
|
37
|
+
/** @internal */ _setLayout: (expanded: number, collapsed: number, top: number) => void;
|
|
38
|
+
/** @internal Mounted on the bar only; pinned bars have no pan recognizer. */
|
|
39
|
+
_dragGesture?: ReturnType<typeof Gesture.Pan>;
|
|
40
|
+
};
|
|
41
|
+
function limit(metrics: Metrics, mode: AppBarScrollMode) {
|
|
42
|
+
'worklet';
|
|
43
|
+
return mode === 'pinned'
|
|
44
|
+
? 0
|
|
45
|
+
: mode === 'enter-always'
|
|
46
|
+
? metrics.expanded + metrics.top
|
|
47
|
+
: Math.max(0, metrics.expanded - metrics.collapsed);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* ScrollView adapter for MD3 pinned/exit-until-collapsed/enter-always behavior.
|
|
51
|
+
* Native scrolling supplies momentum; its frames never visit the RN runtime.
|
|
52
|
+
* Unlike Compose NestedScrollConnection it cannot consume a child's pre-scroll.
|
|
53
|
+
*/
|
|
54
|
+
export function useAppBarScrollBehavior({
|
|
55
|
+
behavior = 'exit-until-collapsed',
|
|
56
|
+
snap = true,
|
|
57
|
+
}: {
|
|
58
|
+
behavior?: AppBarScrollMode;
|
|
59
|
+
snap?: boolean;
|
|
60
|
+
} = {}): AppBarScrollBehavior {
|
|
61
|
+
const theme = useTheme<Theme>();
|
|
62
|
+
const reduced = theme.animation.scale === 0;
|
|
63
|
+
const springStiffness = theme.motion.spring.default.effects.stiffness;
|
|
64
|
+
const springDamping = 2 * theme.motion.spring.default.effects.damping * Math.sqrt(springStiffness);
|
|
65
|
+
const mounted = useRef(true);
|
|
66
|
+
const metrics = useSharedValue<Metrics>({
|
|
67
|
+
expanded: 64,
|
|
68
|
+
collapsed: 64,
|
|
69
|
+
top: 0,
|
|
70
|
+
});
|
|
71
|
+
const mode = useSharedValue(behavior),
|
|
72
|
+
snapEnabled = useSharedValue(snap),
|
|
73
|
+
reducedUI = useSharedValue(reduced);
|
|
74
|
+
const contentOffset = useSharedValue(0),
|
|
75
|
+
heightOffset = useSharedValue(0),
|
|
76
|
+
previousY = useSharedValue(0);
|
|
77
|
+
const alive = useSharedValue(true),
|
|
78
|
+
settling = useSharedValue(false),
|
|
79
|
+
snapTarget = useSharedValue(0),
|
|
80
|
+
revision = useSharedValue(0);
|
|
81
|
+
const dragging = useSharedValue(false),
|
|
82
|
+
dragTranslation = useSharedValue(0),
|
|
83
|
+
barInertia = useSharedValue(false);
|
|
84
|
+
const collapseProgress = useDerivedValue(() => {
|
|
85
|
+
const current = metrics.get(),
|
|
86
|
+
range = current.expanded - current.collapsed;
|
|
87
|
+
return range > 0 ? appBarFraction(-heightOffset.get() / range) : 0;
|
|
88
|
+
});
|
|
89
|
+
const stop = useCallback(() => {
|
|
90
|
+
'worklet';
|
|
91
|
+
revision.set(revision.get() + 1);
|
|
92
|
+
cancelAnimation(heightOffset);
|
|
93
|
+
settling.set(false);
|
|
94
|
+
barInertia.set(false);
|
|
95
|
+
}, [barInertia, heightOffset, revision, settling]);
|
|
96
|
+
const settle = useCallback(() => {
|
|
97
|
+
'worklet';
|
|
98
|
+
if (!alive.get() || !snapEnabled.get()) return;
|
|
99
|
+
const range = limit(metrics.get(), mode.get());
|
|
100
|
+
if (range <= 0) return;
|
|
101
|
+
const current = Math.max(-range, Math.min(0, appBarFinite(heightOffset.get())));
|
|
102
|
+
if (current === 0 || current === -range) return;
|
|
103
|
+
stop();
|
|
104
|
+
const target = current <= -range / 2 ? -range : 0;
|
|
105
|
+
snapTarget.set(target);
|
|
106
|
+
if (reducedUI.get()) {
|
|
107
|
+
heightOffset.set(target);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
settling.set(true);
|
|
111
|
+
const token = revision.get();
|
|
112
|
+
// TopAppBarDefaults.snapAnimationSpec = MotionScheme.DefaultEffects.
|
|
113
|
+
heightOffset.set(
|
|
114
|
+
withSpring(
|
|
115
|
+
target,
|
|
116
|
+
{
|
|
117
|
+
stiffness: springStiffness,
|
|
118
|
+
damping: springDamping,
|
|
119
|
+
mass: 1,
|
|
120
|
+
overshootClamping: true,
|
|
121
|
+
reduceMotion: ReduceMotion.Never,
|
|
122
|
+
},
|
|
123
|
+
(finished) => {
|
|
124
|
+
if (finished && alive.get() && revision.get() === token) settling.set(false);
|
|
125
|
+
}
|
|
126
|
+
)
|
|
127
|
+
);
|
|
128
|
+
}, [alive, heightOffset, metrics, mode, reducedUI, revision, settling, snapEnabled, snapTarget, springDamping, springStiffness, stop]);
|
|
129
|
+
// RN Web does not emit ScrollView's drag/momentum end events. Wait for
|
|
130
|
+
// the browser's scroll stream to become idle, then use the same snap spring.
|
|
131
|
+
const webIdle = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
|
|
132
|
+
const clearWebIdle = useCallback(() => {
|
|
133
|
+
if (Platform.OS === 'web') clearTimeout(webIdle.current);
|
|
134
|
+
webIdle.current = undefined;
|
|
135
|
+
}, []);
|
|
136
|
+
const queueWebSettle = useCallback(() => {
|
|
137
|
+
clearWebIdle();
|
|
138
|
+
webIdle.current = setTimeout(() => {
|
|
139
|
+
webIdle.current = undefined;
|
|
140
|
+
if (mounted.current && !dragging.get() && !barInertia.get()) scheduleOnUI(settle);
|
|
141
|
+
}, 120);
|
|
142
|
+
}, [barInertia, clearWebIdle, dragging, settle]);
|
|
143
|
+
const releaseBar = useCallback(
|
|
144
|
+
(velocity: number) => {
|
|
145
|
+
'worklet';
|
|
146
|
+
if (!alive.get()) return;
|
|
147
|
+
const range = limit(metrics.get(), mode.get());
|
|
148
|
+
const current = Math.max(-range, Math.min(0, appBarFinite(heightOffset.get())));
|
|
149
|
+
if (range <= 0 || current === 0 || current === -range) return;
|
|
150
|
+
if (reducedUI.get() || Math.abs(appBarFinite(velocity)) <= 1) {
|
|
151
|
+
settle();
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
stop();
|
|
155
|
+
settling.set(true);
|
|
156
|
+
barInertia.set(true);
|
|
157
|
+
snapTarget.set(current <= -range / 2 ? -range : 0);
|
|
158
|
+
const token = revision.get();
|
|
159
|
+
// AndroidX settles a direct bar fling with decay, then DefaultEffects.
|
|
160
|
+
// RN uses Reanimated's native decay here; its friction is an adaptation,
|
|
161
|
+
// not a claim of Android's platform-specific spline physics on iOS.
|
|
162
|
+
heightOffset.set(
|
|
163
|
+
withDecay({ velocity, clamp: [-range, 0], reduceMotion: ReduceMotion.Never }, (finished) => {
|
|
164
|
+
if (!finished || !alive.get() || revision.get() !== token) return;
|
|
165
|
+
settling.set(false);
|
|
166
|
+
barInertia.set(false);
|
|
167
|
+
settle();
|
|
168
|
+
})
|
|
169
|
+
);
|
|
170
|
+
},
|
|
171
|
+
[alive, barInertia, heightOffset, metrics, mode, reducedUI, revision, settle, settling, snapTarget, stop]
|
|
172
|
+
);
|
|
173
|
+
const dragGesture = useMemo(
|
|
174
|
+
() =>
|
|
175
|
+
behavior === 'pinned'
|
|
176
|
+
? undefined
|
|
177
|
+
: Gesture.Pan()
|
|
178
|
+
// Android's MD3 draggable observes touch slop before taking a vertical
|
|
179
|
+
// drag. A tap/hold never activates this recognizer; horizontal input fails.
|
|
180
|
+
.activeOffsetY([-8, 8])
|
|
181
|
+
.failOffsetX([-8, 8])
|
|
182
|
+
.maxPointers(1)
|
|
183
|
+
.cancelsTouchesInView(true)
|
|
184
|
+
.onStart((event) => {
|
|
185
|
+
if (!alive.get() || mode.get() === 'pinned') return;
|
|
186
|
+
stop();
|
|
187
|
+
dragging.set(true);
|
|
188
|
+
const translation = appBarFinite(event.translationY);
|
|
189
|
+
dragTranslation.set(translation);
|
|
190
|
+
const overslop = Math.sign(translation) * Math.max(0, Math.abs(translation) - 8);
|
|
191
|
+
const range = limit(metrics.get(), mode.get());
|
|
192
|
+
heightOffset.set(Math.max(-range, Math.min(0, heightOffset.get() + overslop)));
|
|
193
|
+
})
|
|
194
|
+
.onUpdate((event) => {
|
|
195
|
+
if (!alive.get() || !dragging.get() || mode.get() === 'pinned' || !Number.isFinite(event.translationY)) return;
|
|
196
|
+
const translation = event.translationY;
|
|
197
|
+
const delta = translation - dragTranslation.get();
|
|
198
|
+
dragTranslation.set(translation);
|
|
199
|
+
const range = limit(metrics.get(), mode.get());
|
|
200
|
+
heightOffset.set(Math.max(-range, Math.min(0, heightOffset.get() + delta)));
|
|
201
|
+
})
|
|
202
|
+
.onEnd((event, success) => {
|
|
203
|
+
if (!dragging.get()) return;
|
|
204
|
+
dragging.set(false);
|
|
205
|
+
releaseBar(success ? appBarFinite(event.velocityY) : 0);
|
|
206
|
+
})
|
|
207
|
+
.onFinalize(() => {
|
|
208
|
+
if (!dragging.get()) return;
|
|
209
|
+
dragging.set(false);
|
|
210
|
+
releaseBar(0);
|
|
211
|
+
}),
|
|
212
|
+
[alive, behavior, dragTranslation, dragging, heightOffset, metrics, mode, releaseBar, stop]
|
|
213
|
+
);
|
|
214
|
+
const onScroll = useAnimatedScrollHandler({
|
|
215
|
+
onBeginDrag: (event) => {
|
|
216
|
+
if (!alive.get()) return;
|
|
217
|
+
dragging.set(false);
|
|
218
|
+
stop();
|
|
219
|
+
previousY.set(Math.max(0, appBarFinite(event.contentOffset.y, previousY.get())));
|
|
220
|
+
},
|
|
221
|
+
onScroll: (event) => {
|
|
222
|
+
if (!alive.get() || !Number.isFinite(event.contentOffset.y)) return;
|
|
223
|
+
const y = Math.max(0, event.contentOffset.y),
|
|
224
|
+
previous = previousY.get(),
|
|
225
|
+
delta = y - previous;
|
|
226
|
+
previousY.set(y);
|
|
227
|
+
contentOffset.set(y);
|
|
228
|
+
if (Platform.OS === 'web') scheduleOnRN(queueWebSettle);
|
|
229
|
+
// A second native momentum stream must not fight a drag/fling of
|
|
230
|
+
// the bar. A new content drag above explicitly takes ownership.
|
|
231
|
+
if (dragging.get() || barInertia.get()) return;
|
|
232
|
+
const currentMode = mode.get(),
|
|
233
|
+
range = limit(metrics.get(), currentMode);
|
|
234
|
+
if (range <= 0) {
|
|
235
|
+
if (heightOffset.get() !== 0) {
|
|
236
|
+
stop();
|
|
237
|
+
heightOffset.set(0);
|
|
238
|
+
}
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
if (delta === 0 && y > 0) return;
|
|
242
|
+
stop();
|
|
243
|
+
const consumed = currentMode === 'exit-until-collapsed' && delta < 0 ? Math.min(range, y) - Math.min(range, previous) : delta;
|
|
244
|
+
const next = y === 0 ? 0 : heightOffset.get() - consumed;
|
|
245
|
+
heightOffset.set(Math.max(-range, Math.min(0, appBarFinite(next))));
|
|
246
|
+
},
|
|
247
|
+
onEndDrag: (event) => {
|
|
248
|
+
if (dragging.get() || barInertia.get()) return;
|
|
249
|
+
// iOS may report a nonzero release velocity without starting any
|
|
250
|
+
// momentum sequence. Wait only when it projects an actual travel;
|
|
251
|
+
// on platforms without that projection, MomentumBegin cancels the
|
|
252
|
+
// provisional settle before native momentum owns subsequent frames.
|
|
253
|
+
const target = event.targetContentOffset?.y;
|
|
254
|
+
if (!Number.isFinite(target) || Math.abs(target! - event.contentOffset.y) < 0.5) settle();
|
|
255
|
+
},
|
|
256
|
+
onMomentumBegin: () => {
|
|
257
|
+
if (alive.get() && !dragging.get() && !barInertia.get()) stop();
|
|
258
|
+
},
|
|
259
|
+
onMomentumEnd: () => {
|
|
260
|
+
if (!dragging.get() && !barInertia.get()) settle();
|
|
261
|
+
},
|
|
262
|
+
});
|
|
263
|
+
const updateLayout = useCallback(
|
|
264
|
+
(expanded: number, collapsed: number, top: number) => {
|
|
265
|
+
'worklet';
|
|
266
|
+
if (!alive.get()) return;
|
|
267
|
+
const old = metrics.get();
|
|
268
|
+
const next = {
|
|
269
|
+
expanded: Math.max(64, appBarFinite(expanded, 64)),
|
|
270
|
+
collapsed: Math.max(64, appBarFinite(collapsed, 64)),
|
|
271
|
+
top: Math.max(0, appBarFinite(top)),
|
|
272
|
+
};
|
|
273
|
+
next.expanded = Math.max(next.expanded, next.collapsed);
|
|
274
|
+
if (old.expanded === next.expanded && old.collapsed === next.collapsed && old.top === next.top) return;
|
|
275
|
+
const oldCollapse = Math.max(0, old.expanded - old.collapsed),
|
|
276
|
+
newCollapse = Math.max(0, next.expanded - next.collapsed);
|
|
277
|
+
const offset = Math.max(0, -appBarFinite(heightOffset.get()));
|
|
278
|
+
const rebased =
|
|
279
|
+
offset <= oldCollapse && oldCollapse > 0
|
|
280
|
+
? (offset / oldCollapse) * newCollapse
|
|
281
|
+
: mode.get() === 'enter-always' && offset > oldCollapse
|
|
282
|
+
? newCollapse + ((offset - oldCollapse) / Math.max(1, old.collapsed + old.top)) * (next.collapsed + next.top)
|
|
283
|
+
: Math.min(offset, newCollapse);
|
|
284
|
+
stop();
|
|
285
|
+
dragging.set(false);
|
|
286
|
+
metrics.set(next);
|
|
287
|
+
heightOffset.set(-Math.min(limit(next, mode.get()), rebased));
|
|
288
|
+
},
|
|
289
|
+
[alive, dragging, heightOffset, metrics, mode, stop]
|
|
290
|
+
);
|
|
291
|
+
const setLayout = useCallback(
|
|
292
|
+
(expanded: number, collapsed: number, top: number) => {
|
|
293
|
+
if (mounted.current) scheduleOnUI(updateLayout, expanded, collapsed, top);
|
|
294
|
+
},
|
|
295
|
+
[updateLayout]
|
|
296
|
+
);
|
|
297
|
+
const resetUI = useCallback(() => {
|
|
298
|
+
'worklet';
|
|
299
|
+
if (!alive.get()) return;
|
|
300
|
+
stop();
|
|
301
|
+
dragging.set(false);
|
|
302
|
+
heightOffset.set(0);
|
|
303
|
+
previousY.set(0);
|
|
304
|
+
contentOffset.set(0);
|
|
305
|
+
}, [alive, contentOffset, dragging, heightOffset, previousY, stop]);
|
|
306
|
+
const reset = useCallback(() => {
|
|
307
|
+
clearWebIdle();
|
|
308
|
+
if (mounted.current) scheduleOnUI(resetUI);
|
|
309
|
+
}, [clearWebIdle, resetUI]);
|
|
310
|
+
useLayoutEffect(() => {
|
|
311
|
+
clearWebIdle();
|
|
312
|
+
scheduleOnUI(
|
|
313
|
+
(nextMode: AppBarScrollMode, nextSnap: boolean, nextReduced: boolean) => {
|
|
314
|
+
'worklet';
|
|
315
|
+
if (!alive.get()) return;
|
|
316
|
+
const wasSettling = settling.get(),
|
|
317
|
+
wasInertia = barInertia.get(),
|
|
318
|
+
target = snapTarget.get();
|
|
319
|
+
const changedMode = nextMode !== mode.get();
|
|
320
|
+
stop();
|
|
321
|
+
if (changedMode) dragging.set(false);
|
|
322
|
+
mode.set(nextMode);
|
|
323
|
+
snapEnabled.set(nextSnap);
|
|
324
|
+
reducedUI.set(nextReduced);
|
|
325
|
+
const range = limit(metrics.get(), nextMode);
|
|
326
|
+
const reducedTarget = wasInertia ? (heightOffset.get() <= -range / 2 ? -range : 0) : target;
|
|
327
|
+
const next = changedMode
|
|
328
|
+
? Math.max(-range, Math.min(0, heightOffset.get()))
|
|
329
|
+
: nextReduced && wasSettling
|
|
330
|
+
? reducedTarget
|
|
331
|
+
: heightOffset.get();
|
|
332
|
+
heightOffset.set(Math.max(-limit(metrics.get(), nextMode), Math.min(0, next)));
|
|
333
|
+
},
|
|
334
|
+
behavior,
|
|
335
|
+
snap,
|
|
336
|
+
reduced
|
|
337
|
+
);
|
|
338
|
+
}, [
|
|
339
|
+
alive,
|
|
340
|
+
clearWebIdle,
|
|
341
|
+
barInertia,
|
|
342
|
+
behavior,
|
|
343
|
+
dragging,
|
|
344
|
+
heightOffset,
|
|
345
|
+
metrics,
|
|
346
|
+
mode,
|
|
347
|
+
reduced,
|
|
348
|
+
reducedUI,
|
|
349
|
+
settling,
|
|
350
|
+
snap,
|
|
351
|
+
snapEnabled,
|
|
352
|
+
snapTarget,
|
|
353
|
+
stop,
|
|
354
|
+
]);
|
|
355
|
+
useEffect(() => {
|
|
356
|
+
mounted.current = true;
|
|
357
|
+
scheduleOnUI(() => {
|
|
358
|
+
'worklet';
|
|
359
|
+
alive.set(true);
|
|
360
|
+
});
|
|
361
|
+
return () => {
|
|
362
|
+
mounted.current = false;
|
|
363
|
+
clearWebIdle();
|
|
364
|
+
scheduleOnUI(() => {
|
|
365
|
+
'worklet';
|
|
366
|
+
alive.set(false);
|
|
367
|
+
dragging.set(false);
|
|
368
|
+
stop();
|
|
369
|
+
});
|
|
370
|
+
};
|
|
371
|
+
}, [alive, clearWebIdle, dragging, stop]);
|
|
372
|
+
return useMemo(
|
|
373
|
+
() => ({
|
|
374
|
+
onScroll,
|
|
375
|
+
collapseProgress,
|
|
376
|
+
heightOffset,
|
|
377
|
+
reset,
|
|
378
|
+
_metrics: metrics,
|
|
379
|
+
_mode: mode,
|
|
380
|
+
_contentOffset: contentOffset,
|
|
381
|
+
_setLayout: setLayout,
|
|
382
|
+
_dragGesture: dragGesture,
|
|
383
|
+
}),
|
|
384
|
+
[collapseProgress, contentOffset, dragGesture, heightOffset, metrics, mode, onScroll, reset, setLayout]
|
|
385
|
+
);
|
|
386
|
+
}
|