@plexui/react-native 0.2.21 → 0.2.23
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.
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { useEffect, useRef, useState } from "react";
|
|
3
|
-
import { Animated, Platform, StyleSheet, View } from "react-native";
|
|
3
|
+
import { Animated, Easing, Platform, StyleSheet, View } from "react-native";
|
|
4
4
|
import { isRTL } from "../../internal/direction";
|
|
5
5
|
import { curves } from "../../internal/motion";
|
|
6
6
|
import { useTheme } from "../../theme/ThemeProvider";
|
|
@@ -46,17 +46,73 @@ const SCRUB_THRESHOLD = 8;
|
|
|
46
46
|
* page and back as it moves), in a 48dp target, with no capsule at all. For a screen reader it is adjustable: swipe
|
|
47
47
|
* up or down for the next or previous page.
|
|
48
48
|
*/
|
|
49
|
-
export function PageControl({ count, value: valueProp, defaultValue = 0, onChange, background = "automatic", indicator = "dots", tone = "default", direction = "horizontal", size = "md", valueText = defaultValueText, "aria-label": ariaLabel = "Pages", testID, style, }) {
|
|
49
|
+
export function PageControl({ count, value: valueProp, defaultValue = 0, position: positionProp, onChange, background = "automatic", indicator = "dots", tone = "default", direction = "horizontal", size = "md", valueText = defaultValueText, "aria-label": ariaLabel = "Pages", testID, style, }) {
|
|
50
50
|
const theme = useTheme();
|
|
51
51
|
const [own, setOwn] = useState(defaultValue);
|
|
52
52
|
const current = clampIndex(valueProp ?? own, count);
|
|
53
53
|
const [press, setPress] = useState(null);
|
|
54
54
|
// Where the dots stand inside the target, which can be wider than them (x is physical, from the left)
|
|
55
55
|
const [capsule, setCapsule] = useState({ x: 0, width: 0 });
|
|
56
|
+
// The fractional page the marks follow: the app's own `position`, or one that moves to `value` by itself
|
|
57
|
+
const [ownPosition] = useState(() => new Animated.Value(typeof positionProp === "number" ? positionProp : current));
|
|
58
|
+
const driven = positionProp != null && typeof positionProp === "object";
|
|
59
|
+
const scrubbed = useRef(false);
|
|
60
|
+
const shown = useRef(null);
|
|
61
|
+
// The page nearest the position: the dots' window from seven pages follows it, so the marks it passes stay drawn
|
|
62
|
+
const [near, setNear] = useState(current);
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
if (typeof positionProp === "number") {
|
|
65
|
+
ownPosition.setValue(positionProp);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const source = driven ? positionProp : ownPosition;
|
|
69
|
+
const follow = (value) => {
|
|
70
|
+
// The own position keeps up with an app's, so it moves on from there when the app's goes
|
|
71
|
+
if (driven)
|
|
72
|
+
ownPosition.setValue(value);
|
|
73
|
+
const page = clampIndex(Math.round(value), count);
|
|
74
|
+
setNear((last) => (last === page ? last : page));
|
|
75
|
+
};
|
|
76
|
+
const id = source.addListener(({ value }) => follow(value));
|
|
77
|
+
// A listener hears only what comes next: a new source's value now sets the window at once
|
|
78
|
+
const read = () => source.__getValue?.();
|
|
79
|
+
let live = true;
|
|
80
|
+
void Promise.resolve().then(() => {
|
|
81
|
+
// Read as it runs, so a change in between is not undone
|
|
82
|
+
const now = read();
|
|
83
|
+
if (live && typeof now === "number")
|
|
84
|
+
follow(now);
|
|
85
|
+
});
|
|
86
|
+
return () => {
|
|
87
|
+
live = false;
|
|
88
|
+
source.removeListener(id);
|
|
89
|
+
};
|
|
90
|
+
}, [positionProp, driven, ownPosition, count]);
|
|
91
|
+
useEffect(() => {
|
|
92
|
+
if (positionProp != null)
|
|
93
|
+
return;
|
|
94
|
+
const still = shown.current == null || theme.reducedMotion || scrubbed.current;
|
|
95
|
+
shown.current = current;
|
|
96
|
+
scrubbed.current = false;
|
|
97
|
+
if (still) {
|
|
98
|
+
ownPosition.setValue(current);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const animation = Animated.timing(ownPosition, { toValue: current, ...(theme.isAndroid ? ANDROID_SLIDE : IOS_SLIDE), useNativeDriver: false });
|
|
102
|
+
animation.start();
|
|
103
|
+
return () => animation.stop();
|
|
104
|
+
}, [current, positionProp, ownPosition, theme.reducedMotion, theme.isAndroid]);
|
|
105
|
+
// An overscroll past the first or the last page stands on it, so its mark keeps the whole pill
|
|
106
|
+
const position = (driven ? positionProp : ownPosition).interpolate({
|
|
107
|
+
inputRange: [0, Math.max(1, count - 1)],
|
|
108
|
+
outputRange: [0, Math.max(0, count - 1)],
|
|
109
|
+
extrapolate: "clamp",
|
|
110
|
+
});
|
|
56
111
|
const go = (index, scrubbing = false) => {
|
|
57
112
|
const next = clampIndex(index, count);
|
|
58
113
|
if (next === current)
|
|
59
114
|
return;
|
|
115
|
+
scrubbed.current = scrubbing;
|
|
60
116
|
if (valueProp === undefined)
|
|
61
117
|
setOwn(next);
|
|
62
118
|
onChange?.(next, { scrubbing });
|
|
@@ -69,7 +125,7 @@ export function PageControl({ count, value: valueProp, defaultValue = 0, onChang
|
|
|
69
125
|
const padding = small ? 6 : theme.length("--page-control-padding-inline", 12);
|
|
70
126
|
const pitch = dot + gap;
|
|
71
127
|
// Android draws every page's dot at full size; the iOS kit shrinks the ends from seven pages
|
|
72
|
-
const sizes = android ? Array.from({ length: Math.max(0, count) }, () => "full") : pageControlDots(count,
|
|
128
|
+
const sizes = android ? Array.from({ length: Math.max(0, count) }, () => "full") : pageControlDots(count, clampIndex(typeof positionProp === "number" ? Math.round(positionProp) : near, count));
|
|
73
129
|
// The small and smaller ends keep their share of a full dot (6 and 4 of 8 on iOS)
|
|
74
130
|
const dotSize = (kind) => kind === "full"
|
|
75
131
|
? dot
|
|
@@ -77,8 +133,9 @@ export function PageControl({ count, value: valueProp, defaultValue = 0, onChang
|
|
|
77
133
|
? Math.max(2, dot - (kind === "small" ? 1 : 2))
|
|
78
134
|
: (dot / 8) * (kind === "small" ? theme.length("--page-control-dot-size-small", 6) : theme.length("--page-control-dot-size-smaller", 4));
|
|
79
135
|
const pill = indicator === "pill";
|
|
80
|
-
// A pill: the current page two and a half dots long (
|
|
81
|
-
const
|
|
136
|
+
// A pill: the current page two and a half dots long (14 at `sm`), along the direction
|
|
137
|
+
const pillLength = small ? 14 : dot * 2.5;
|
|
138
|
+
const dotWidth = (kind, index) => (pill && index === current ? pillLength : dotSize(kind));
|
|
82
139
|
// The center of the current dot, for a tap on either side of it
|
|
83
140
|
let x = padding;
|
|
84
141
|
let currentCenter = padding;
|
|
@@ -93,8 +150,9 @@ export function PageControl({ count, value: valueProp, defaultValue = 0, onChang
|
|
|
93
150
|
const on = onColor ? "#FFFFFF" : android ? theme.material.primary : theme.color("--page-control-dot-color-current", theme.colors.text);
|
|
94
151
|
// Android's page dots: #BBBBBB, #555555 in a dark theme
|
|
95
152
|
const off = onColor ? "rgba(255, 255, 255, 0.4)" : android ? (theme.scheme === "dark" ? "#555555" : "#BBBBBB") : theme.color("--page-control-dot-color", theme.colors.labelTertiary);
|
|
96
|
-
// Android's current dot is drawn over the gray ones (it stretches between pages)
|
|
97
|
-
|
|
153
|
+
// Android's current dot is drawn over the gray ones (it stretches between pages) unless the pages' scroll drives the
|
|
154
|
+
// dots, when their color crosses over as on iOS; a pill is a dot of its own
|
|
155
|
+
const worm = android && !pill && positionProp == null;
|
|
98
156
|
// Android draws no capsule, not even for `prominent`
|
|
99
157
|
const capsuleHeight = small ? 16 : theme.length("--page-control-height", 24);
|
|
100
158
|
const target = android ? 48 : theme.length("--page-control-target-size", 44);
|
|
@@ -152,13 +210,27 @@ export function PageControl({ count, value: valueProp, defaultValue = 0, onChang
|
|
|
152
210
|
? { flexDirection: "column", width: capsuleHeight, paddingVertical: padding, gap, borderRadius: capsuleHeight / 2 }
|
|
153
211
|
: { height: capsuleHeight, paddingHorizontal: padding, gap, borderRadius: capsuleHeight / 2 },
|
|
154
212
|
showBackground ? { backgroundColor: onColor ? "rgba(255, 255, 255, 0.2)" : theme.color("--page-control-background") } : null,
|
|
155
|
-
], children: [sizes.map((size, index) =>
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
213
|
+
], children: [sizes.map((size, index) => {
|
|
214
|
+
if (size === "hidden")
|
|
215
|
+
return null;
|
|
216
|
+
const across = dotSize(size);
|
|
217
|
+
if (worm)
|
|
218
|
+
return _jsx(View, { style: { width: vertical ? across : dotWidth(size, index), height: vertical ? dotWidth(size, index) : across, borderRadius: dot / 2, backgroundColor: off } }, index);
|
|
219
|
+
// How much of the current page this mark is: 1 on its page, 0 a page away, a line between
|
|
220
|
+
const weight = position.interpolate(pageControlWeight(index));
|
|
221
|
+
const along = pill ? weight.interpolate({ inputRange: [0, 1], outputRange: [across, pillLength] }) : across;
|
|
222
|
+
return (_jsx(Animated.View, { testID: testID ? `${testID}-mark-${index}` : undefined, style: { width: vertical ? across : along, height: vertical ? along : across, borderRadius: dot / 2, backgroundColor: off }, children: _jsx(Animated.View, { style: [StyleSheet.absoluteFill, { borderRadius: dot / 2, backgroundColor: on, opacity: weight }] }) }, index));
|
|
223
|
+
}), worm ? _jsx(AndroidCurrent, { start: currentCenter - dot / 2, color: on, reduced: theme.reducedMotion, dot: dot, vertical: vertical }) : null] }) }));
|
|
161
224
|
}
|
|
225
|
+
/**
|
|
226
|
+
* The interpolation of a mark's share of the current page from the pages' position: 1 on its own page, 0 from a page
|
|
227
|
+
* away, a line between. Two neighbours share one page between them, so a pill's length moves from one to the other and
|
|
228
|
+
* the row keeps its length.
|
|
229
|
+
*/
|
|
230
|
+
export const pageControlWeight = (index) => ({ inputRange: [index - 1, index, index + 1], outputRange: [0, 1, 0], extrapolate: "clamp" });
|
|
231
|
+
/** A new `value` with no `position`: iOS's default UIView curve, Material's standard easing and its medium duration. */
|
|
232
|
+
const IOS_SLIDE = { duration: 250, easing: curves.easeInOut };
|
|
233
|
+
const ANDROID_SLIDE = { duration: 300, easing: Easing.bezier(0.2, 0, 0, 1) };
|
|
162
234
|
/** Android's page dots: 5dp, 11dp from one to the next. */
|
|
163
235
|
const ANDROID_DOT = 5;
|
|
164
236
|
const ANDROID_GAP = 6;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useState } from "react";
|
|
2
3
|
import { Animated, Platform, StyleSheet, View } from "react-native";
|
|
3
4
|
import { useTheme, useThemeOverrides } from "../theme/ThemeProvider";
|
|
4
5
|
import { blurModule, liquidGlassModule } from "./optional";
|
|
@@ -53,9 +54,35 @@ const WEB_LIQUID = {
|
|
|
53
54
|
},
|
|
54
55
|
lens: { token: "--mobile-glass-web-lens-filter", value: "blur(3px) brightness(1.12) saturate(200%)" },
|
|
55
56
|
};
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
/**
|
|
58
|
+
* The lens: the glass's edge only, fading in over 14 from its sides and 9 from its top and bottom (a small glass keeps
|
|
59
|
+
* a half and a third of its radius), along the glass's own shape. A ring whose hole is the glass's outline drawn
|
|
60
|
+
* smaller, blurred so its inner edge fades; bands across the box would leave a square in a round glass. The outline is
|
|
61
|
+
* the corner radius as the browser draws it, so no larger than half the glass's shorter side (a capsule's 999 is its
|
|
62
|
+
* height's half). A round glass (a bar button, a progress ring) keeps a thin rim of it: a ring as deep as a bar's
|
|
63
|
+
* reads on a small circle as a halo around a darker middle.
|
|
64
|
+
*/
|
|
65
|
+
const lensMask = (width, height, radius) => {
|
|
66
|
+
const r = Math.max(0, Math.min(radius, width / 2, height / 2));
|
|
67
|
+
const round = r > 0 && Math.abs(width - height) <= 1 && r >= Math.min(width, height) / 2 - 0.5;
|
|
68
|
+
const inline = round ? r / 6 : Math.min(14, r > 0 ? r / 2 : 14);
|
|
69
|
+
const block = round ? r / 8 : Math.min(9, r > 0 ? r / 3 : 9);
|
|
70
|
+
const x = inline / 2;
|
|
71
|
+
const y = block / 2;
|
|
72
|
+
const w = Math.max(0, width - 2 * x);
|
|
73
|
+
const h = Math.max(0, height - 2 * y);
|
|
74
|
+
const rx = Math.max(0, Math.min(r - x, w / 2));
|
|
75
|
+
const ry = Math.max(0, Math.min(r - y, h / 2));
|
|
76
|
+
const f = (n) => +n.toFixed(2);
|
|
77
|
+
const hole = `M${f(x + rx)} ${f(y)}H${f(x + w - rx)}A${f(rx)} ${f(ry)} 0 0 1 ${f(x + w)} ${f(y + ry)}V${f(y + h - ry)}` +
|
|
78
|
+
`A${f(rx)} ${f(ry)} 0 0 1 ${f(x + w - rx)} ${f(y + h)}H${f(x + rx)}A${f(rx)} ${f(ry)} 0 0 1 ${f(x)} ${f(y + h - ry)}` +
|
|
79
|
+
`V${f(y + ry)}A${f(rx)} ${f(ry)} 0 0 1 ${f(x + rx)} ${f(y)}Z`;
|
|
80
|
+
// The outer edge stands 20 past the box, so the blur fades only the hole's edge; the glass's radius clips the rest
|
|
81
|
+
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${f(width)}" height="${f(height)}">` +
|
|
82
|
+
`<filter id="e" x="-1" y="-1" width="3" height="3"><feGaussianBlur stdDeviation="${f(inline / 4)} ${f(block / 4)}"/></filter>` +
|
|
83
|
+
`<path filter="url(#e)" fill-rule="evenodd" d="M-20 -20H${f(width + 20)}V${f(height + 20)}H-20Z${hole}"/></svg>`;
|
|
84
|
+
return `url("data:image/svg+xml,${encodeURIComponent(svg)}")`;
|
|
85
|
+
};
|
|
59
86
|
/** The filter and the shadow of the glass surfaces of the phone layer, by their fill (mobile-materials.css, touch.css). */
|
|
60
87
|
const SURFACES = {
|
|
61
88
|
"--menu-mobile-background": { filter: "--menu-mobile-backdrop-filter", shadow: "--menu-shadow" },
|
|
@@ -76,6 +103,8 @@ const intensityOf = (radius) => Math.max(0, Math.min(100, Math.round(radius * 5)
|
|
|
76
103
|
*/
|
|
77
104
|
export function Glass({ radius = 0, elevation = "regular", tint, interactive, fillToken, filterToken, shadowToken, blur, style, children, ...rest }) {
|
|
78
105
|
const theme = useTheme();
|
|
106
|
+
// The web lens's mask follows the glass's shape, so it needs the glass's size
|
|
107
|
+
const [size, setSize] = useState(null);
|
|
79
108
|
const reduceTransparency = useReduceTransparency();
|
|
80
109
|
const overrides = useThemeOverrides();
|
|
81
110
|
const shape = { borderRadius: radius, borderCurve: "continuous" };
|
|
@@ -117,17 +146,24 @@ export function Glass({ radius = 0, elevation = "regular", tint, interactive, fi
|
|
|
117
146
|
const sheen = liquid && tint == null ? own(WEB_LIQUID.sheen) : undefined;
|
|
118
147
|
const lensToken = theme.token(WEB_LIQUID.lens.token);
|
|
119
148
|
const lensFilter = typeof lensToken === "string" ? lensToken : WEB_LIQUID.lens.value;
|
|
120
|
-
const mask =
|
|
149
|
+
const mask = size != null && size.width > 0 && size.height > 0 ? lensMask(size.width, size.height, radius) : undefined;
|
|
121
150
|
// An Animated.View, so a panel can move its own glass (a transform above the blur would stop it)
|
|
122
151
|
return (_jsxs(Animated.View, { ...rest, style: [
|
|
123
152
|
shape,
|
|
124
153
|
{ backgroundColor: fill, boxShadow: liquidShadow },
|
|
125
154
|
{ backdropFilter: filter, WebkitBackdropFilter: filter, backgroundImage: sheen },
|
|
126
155
|
style,
|
|
127
|
-
], children: [liquid && lensFilter !== "none" ? (_jsx(View, { pointerEvents: "none", "aria-hidden": true,
|
|
156
|
+
], children: [liquid && lensFilter !== "none" ? (_jsx(View, { pointerEvents: "none", "aria-hidden": true, onLayout: (event) => {
|
|
157
|
+
const { width, height } = event.nativeEvent.layout;
|
|
158
|
+
if (size?.width !== width || size?.height !== height)
|
|
159
|
+
setSize({ width, height });
|
|
160
|
+
}, style: [
|
|
128
161
|
StyleSheet.absoluteFill,
|
|
129
162
|
shape,
|
|
130
|
-
|
|
163
|
+
// Until the glass has a size, no lens: a lens unmasked would blur the whole glass
|
|
164
|
+
mask == null
|
|
165
|
+
? null
|
|
166
|
+
: { backdropFilter: lensFilter, WebkitBackdropFilter: lensFilter, maskImage: mask, WebkitMaskImage: mask, maskSize: "100% 100%", WebkitMaskSize: "100% 100%" },
|
|
131
167
|
] })) : null, children] }));
|
|
132
168
|
}
|
|
133
169
|
const radiusPx = Number(/blur\(\s*([\d.]+)px\s*\)/.exec(filter)?.[1] ?? 12);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type StyleProp, type ViewStyle } from "react-native";
|
|
1
|
+
import { Animated, type StyleProp, type ViewStyle } from "react-native";
|
|
2
2
|
export type PageControlDotSize = "full" | "small" | "smaller" | "hidden";
|
|
3
3
|
export type PageControlChangeDetails = {
|
|
4
4
|
/** The change came from scrubbing: show the page without the scrolling animation, as the HIG asks. */
|
|
@@ -11,6 +11,16 @@ export type PageControlProps = {
|
|
|
11
11
|
value?: number;
|
|
12
12
|
/** @default 0 */
|
|
13
13
|
defaultValue?: number;
|
|
14
|
+
/**
|
|
15
|
+
* Where the pages stand as they scroll: the page index with its fraction (the scroll offset over the page's length),
|
|
16
|
+
* a number or an `Animated` value, so a pager's scroll drives the control on every frame without a render. The
|
|
17
|
+
* pill flows from one page's mark to the next and the dots' color crosses over with it, under the finger and then
|
|
18
|
+
* with the pager's own settle. Without it the control moves to a new `value` over a short standard transition (at
|
|
19
|
+
* once while scrubbing and with reduced motion). The current page for a screen reader, a tap and the keys stays
|
|
20
|
+
* `value`. An `Animated` value drives the marks' length, so it moves on the JavaScript driver
|
|
21
|
+
* (`Animated.event(…, { useNativeDriver: false })`), as the package's own animations do.
|
|
22
|
+
*/
|
|
23
|
+
position?: number | Animated.Value | Animated.AnimatedInterpolation<number>;
|
|
14
24
|
/** Called with the index of the next page. */
|
|
15
25
|
onChange?: (index: number, details: PageControlChangeDetails) => void;
|
|
16
26
|
/**
|
|
@@ -62,4 +72,14 @@ export declare const pageControlDots: (count: number, current: number) => PageCo
|
|
|
62
72
|
* page and back as it moves), in a 48dp target, with no capsule at all. For a screen reader it is adjustable: swipe
|
|
63
73
|
* up or down for the next or previous page.
|
|
64
74
|
*/
|
|
65
|
-
export declare function PageControl({ count, value: valueProp, defaultValue, onChange, background, indicator, tone, direction, size, valueText, "aria-label": ariaLabel, testID, style, }: PageControlProps): import("react").JSX.Element;
|
|
75
|
+
export declare function PageControl({ count, value: valueProp, defaultValue, position: positionProp, onChange, background, indicator, tone, direction, size, valueText, "aria-label": ariaLabel, testID, style, }: PageControlProps): import("react").JSX.Element;
|
|
76
|
+
/**
|
|
77
|
+
* The interpolation of a mark's share of the current page from the pages' position: 1 on its own page, 0 from a page
|
|
78
|
+
* away, a line between. Two neighbours share one page between them, so a pill's length moves from one to the other and
|
|
79
|
+
* the row keeps its length.
|
|
80
|
+
*/
|
|
81
|
+
export declare const pageControlWeight: (index: number) => {
|
|
82
|
+
inputRange: number[];
|
|
83
|
+
outputRange: number[];
|
|
84
|
+
extrapolate: "clamp";
|
|
85
|
+
};
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useEffect, useRef, useState } from "react"
|
|
2
|
-
import { Animated, Platform, StyleSheet, View, type AccessibilityActionEvent, type GestureResponderEvent, type StyleProp, type ViewStyle } from "react-native"
|
|
2
|
+
import { Animated, Easing, Platform, StyleSheet, View, type AccessibilityActionEvent, type GestureResponderEvent, type StyleProp, type ViewStyle } from "react-native"
|
|
3
3
|
import { isRTL } from "../../internal/direction"
|
|
4
4
|
import { curves } from "../../internal/motion"
|
|
5
5
|
import { useTheme } from "../../theme/ThemeProvider"
|
|
@@ -18,6 +18,16 @@ export type PageControlProps = {
|
|
|
18
18
|
value?: number
|
|
19
19
|
/** @default 0 */
|
|
20
20
|
defaultValue?: number
|
|
21
|
+
/**
|
|
22
|
+
* Where the pages stand as they scroll: the page index with its fraction (the scroll offset over the page's length),
|
|
23
|
+
* a number or an `Animated` value, so a pager's scroll drives the control on every frame without a render. The
|
|
24
|
+
* pill flows from one page's mark to the next and the dots' color crosses over with it, under the finger and then
|
|
25
|
+
* with the pager's own settle. Without it the control moves to a new `value` over a short standard transition (at
|
|
26
|
+
* once while scrubbing and with reduced motion). The current page for a screen reader, a tap and the keys stays
|
|
27
|
+
* `value`. An `Animated` value drives the marks' length, so it moves on the JavaScript driver
|
|
28
|
+
* (`Animated.event(…, { useNativeDriver: false })`), as the package's own animations do.
|
|
29
|
+
*/
|
|
30
|
+
position?: number | Animated.Value | Animated.AnimatedInterpolation<number>
|
|
21
31
|
/** Called with the index of the next page. */
|
|
22
32
|
onChange?: (index: number, details: PageControlChangeDetails) => void
|
|
23
33
|
/**
|
|
@@ -105,6 +115,7 @@ export function PageControl({
|
|
|
105
115
|
count,
|
|
106
116
|
value: valueProp,
|
|
107
117
|
defaultValue = 0,
|
|
118
|
+
position: positionProp,
|
|
108
119
|
onChange,
|
|
109
120
|
background = "automatic",
|
|
110
121
|
indicator = "dots",
|
|
@@ -122,9 +133,62 @@ export function PageControl({
|
|
|
122
133
|
const [press, setPress] = useState<Press | null>(null)
|
|
123
134
|
// Where the dots stand inside the target, which can be wider than them (x is physical, from the left)
|
|
124
135
|
const [capsule, setCapsule] = useState({ x: 0, width: 0 })
|
|
136
|
+
// The fractional page the marks follow: the app's own `position`, or one that moves to `value` by itself
|
|
137
|
+
const [ownPosition] = useState(() => new Animated.Value(typeof positionProp === "number" ? positionProp : current))
|
|
138
|
+
const driven = positionProp != null && typeof positionProp === "object"
|
|
139
|
+
const scrubbed = useRef(false)
|
|
140
|
+
const shown = useRef<number | null>(null)
|
|
141
|
+
// The page nearest the position: the dots' window from seven pages follows it, so the marks it passes stay drawn
|
|
142
|
+
const [near, setNear] = useState(current)
|
|
143
|
+
useEffect(() => {
|
|
144
|
+
if (typeof positionProp === "number") {
|
|
145
|
+
ownPosition.setValue(positionProp)
|
|
146
|
+
return
|
|
147
|
+
}
|
|
148
|
+
const source = driven ? (positionProp as Animated.Value) : ownPosition
|
|
149
|
+
const follow = (value: number) => {
|
|
150
|
+
// The own position keeps up with an app's, so it moves on from there when the app's goes
|
|
151
|
+
if (driven) ownPosition.setValue(value)
|
|
152
|
+
const page = clampIndex(Math.round(value), count)
|
|
153
|
+
setNear((last) => (last === page ? last : page))
|
|
154
|
+
}
|
|
155
|
+
const id = source.addListener(({ value }) => follow(value))
|
|
156
|
+
// A listener hears only what comes next: a new source's value now sets the window at once
|
|
157
|
+
const read = () => (source as unknown as { __getValue?: () => number }).__getValue?.()
|
|
158
|
+
let live = true
|
|
159
|
+
void Promise.resolve().then(() => {
|
|
160
|
+
// Read as it runs, so a change in between is not undone
|
|
161
|
+
const now = read()
|
|
162
|
+
if (live && typeof now === "number") follow(now)
|
|
163
|
+
})
|
|
164
|
+
return () => {
|
|
165
|
+
live = false
|
|
166
|
+
source.removeListener(id)
|
|
167
|
+
}
|
|
168
|
+
}, [positionProp, driven, ownPosition, count])
|
|
169
|
+
useEffect(() => {
|
|
170
|
+
if (positionProp != null) return
|
|
171
|
+
const still = shown.current == null || theme.reducedMotion || scrubbed.current
|
|
172
|
+
shown.current = current
|
|
173
|
+
scrubbed.current = false
|
|
174
|
+
if (still) {
|
|
175
|
+
ownPosition.setValue(current)
|
|
176
|
+
return
|
|
177
|
+
}
|
|
178
|
+
const animation = Animated.timing(ownPosition, { toValue: current, ...(theme.isAndroid ? ANDROID_SLIDE : IOS_SLIDE), useNativeDriver: false })
|
|
179
|
+
animation.start()
|
|
180
|
+
return () => animation.stop()
|
|
181
|
+
}, [current, positionProp, ownPosition, theme.reducedMotion, theme.isAndroid])
|
|
182
|
+
// An overscroll past the first or the last page stands on it, so its mark keeps the whole pill
|
|
183
|
+
const position = (driven ? (positionProp as Animated.Value) : ownPosition).interpolate({
|
|
184
|
+
inputRange: [0, Math.max(1, count - 1)],
|
|
185
|
+
outputRange: [0, Math.max(0, count - 1)],
|
|
186
|
+
extrapolate: "clamp",
|
|
187
|
+
})
|
|
125
188
|
const go = (index: number, scrubbing = false) => {
|
|
126
189
|
const next = clampIndex(index, count)
|
|
127
190
|
if (next === current) return
|
|
191
|
+
scrubbed.current = scrubbing
|
|
128
192
|
if (valueProp === undefined) setOwn(next)
|
|
129
193
|
onChange?.(next, { scrubbing })
|
|
130
194
|
}
|
|
@@ -136,7 +200,7 @@ export function PageControl({
|
|
|
136
200
|
const padding = small ? 6 : theme.length("--page-control-padding-inline", 12)
|
|
137
201
|
const pitch = dot + gap
|
|
138
202
|
// Android draws every page's dot at full size; the iOS kit shrinks the ends from seven pages
|
|
139
|
-
const sizes = android ? Array.from({ length: Math.max(0, count) }, (): PageControlDotSize => "full") : pageControlDots(count,
|
|
203
|
+
const sizes = android ? Array.from({ length: Math.max(0, count) }, (): PageControlDotSize => "full") : pageControlDots(count, clampIndex(typeof positionProp === "number" ? Math.round(positionProp) : near, count))
|
|
140
204
|
// The small and smaller ends keep their share of a full dot (6 and 4 of 8 on iOS)
|
|
141
205
|
const dotSize = (kind: PageControlDotSize) =>
|
|
142
206
|
kind === "full"
|
|
@@ -145,8 +209,9 @@ export function PageControl({
|
|
|
145
209
|
? Math.max(2, dot - (kind === "small" ? 1 : 2))
|
|
146
210
|
: (dot / 8) * (kind === "small" ? theme.length("--page-control-dot-size-small", 6) : theme.length("--page-control-dot-size-smaller", 4))
|
|
147
211
|
const pill = indicator === "pill"
|
|
148
|
-
// A pill: the current page two and a half dots long (
|
|
149
|
-
const
|
|
212
|
+
// A pill: the current page two and a half dots long (14 at `sm`), along the direction
|
|
213
|
+
const pillLength = small ? 14 : dot * 2.5
|
|
214
|
+
const dotWidth = (kind: PageControlDotSize, index: number) => (pill && index === current ? pillLength : dotSize(kind))
|
|
150
215
|
// The center of the current dot, for a tap on either side of it
|
|
151
216
|
let x = padding
|
|
152
217
|
let currentCenter = padding
|
|
@@ -159,8 +224,9 @@ export function PageControl({
|
|
|
159
224
|
const on = onColor ? "#FFFFFF" : android ? theme.material.primary : theme.color("--page-control-dot-color-current", theme.colors.text)
|
|
160
225
|
// Android's page dots: #BBBBBB, #555555 in a dark theme
|
|
161
226
|
const off = onColor ? "rgba(255, 255, 255, 0.4)" : android ? (theme.scheme === "dark" ? "#555555" : "#BBBBBB") : theme.color("--page-control-dot-color", theme.colors.labelTertiary)
|
|
162
|
-
// Android's current dot is drawn over the gray ones (it stretches between pages)
|
|
163
|
-
|
|
227
|
+
// Android's current dot is drawn over the gray ones (it stretches between pages) unless the pages' scroll drives the
|
|
228
|
+
// dots, when their color crosses over as on iOS; a pill is a dot of its own
|
|
229
|
+
const worm = android && !pill && positionProp == null
|
|
164
230
|
// Android draws no capsule, not even for `prominent`
|
|
165
231
|
const capsuleHeight = small ? 16 : theme.length("--page-control-height", 24)
|
|
166
232
|
const target = android ? 48 : theme.length("--page-control-target-size", 44)
|
|
@@ -242,25 +308,40 @@ export function PageControl({
|
|
|
242
308
|
showBackground ? { backgroundColor: onColor ? "rgba(255, 255, 255, 0.2)" : theme.color("--page-control-background") } : null,
|
|
243
309
|
]}
|
|
244
310
|
>
|
|
245
|
-
{sizes.map((size, index) =>
|
|
246
|
-
size === "hidden"
|
|
247
|
-
|
|
311
|
+
{sizes.map((size, index) => {
|
|
312
|
+
if (size === "hidden") return null
|
|
313
|
+
const across = dotSize(size)
|
|
314
|
+
if (worm) return <View key={index} style={{ width: vertical ? across : dotWidth(size, index), height: vertical ? dotWidth(size, index) : across, borderRadius: dot / 2, backgroundColor: off }} />
|
|
315
|
+
// How much of the current page this mark is: 1 on its page, 0 a page away, a line between
|
|
316
|
+
const weight = position.interpolate(pageControlWeight(index))
|
|
317
|
+
const along = pill ? weight.interpolate({ inputRange: [0, 1], outputRange: [across, pillLength] }) : across
|
|
318
|
+
return (
|
|
319
|
+
<Animated.View
|
|
248
320
|
key={index}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
),
|
|
257
|
-
)}
|
|
321
|
+
testID={testID ? `${testID}-mark-${index}` : undefined}
|
|
322
|
+
style={{ width: vertical ? across : along, height: vertical ? along : across, borderRadius: dot / 2, backgroundColor: off }}
|
|
323
|
+
>
|
|
324
|
+
<Animated.View style={[StyleSheet.absoluteFill, { borderRadius: dot / 2, backgroundColor: on, opacity: weight }]} />
|
|
325
|
+
</Animated.View>
|
|
326
|
+
)
|
|
327
|
+
})}
|
|
258
328
|
{worm ? <AndroidCurrent start={currentCenter - dot / 2} color={on} reduced={theme.reducedMotion} dot={dot} vertical={vertical} /> : null}
|
|
259
329
|
</View>
|
|
260
330
|
</View>
|
|
261
331
|
)
|
|
262
332
|
}
|
|
263
333
|
|
|
334
|
+
/**
|
|
335
|
+
* The interpolation of a mark's share of the current page from the pages' position: 1 on its own page, 0 from a page
|
|
336
|
+
* away, a line between. Two neighbours share one page between them, so a pill's length moves from one to the other and
|
|
337
|
+
* the row keeps its length.
|
|
338
|
+
*/
|
|
339
|
+
export const pageControlWeight = (index: number) => ({ inputRange: [index - 1, index, index + 1], outputRange: [0, 1, 0], extrapolate: "clamp" as const })
|
|
340
|
+
|
|
341
|
+
/** A new `value` with no `position`: iOS's default UIView curve, Material's standard easing and its medium duration. */
|
|
342
|
+
const IOS_SLIDE = { duration: 250, easing: curves.easeInOut }
|
|
343
|
+
const ANDROID_SLIDE = { duration: 300, easing: Easing.bezier(0.2, 0, 0, 1) }
|
|
344
|
+
|
|
264
345
|
/** Android's page dots: 5dp, 11dp from one to the next. */
|
|
265
346
|
const ANDROID_DOT = 5
|
|
266
347
|
const ANDROID_GAP = 6
|
package/src/internal/Glass.tsx
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { useState } from "react"
|
|
1
2
|
import { Animated, Platform, StyleSheet, View, type StyleProp, type ViewProps, type ViewStyle } from "react-native"
|
|
2
3
|
import { useTheme, useThemeOverrides } from "../theme/ThemeProvider"
|
|
3
4
|
import { blurModule, liquidGlassModule } from "./optional"
|
|
@@ -83,10 +84,37 @@ const WEB_LIQUID = {
|
|
|
83
84
|
lens: { token: "--mobile-glass-web-lens-filter", value: "blur(3px) brightness(1.12) saturate(200%)" },
|
|
84
85
|
}
|
|
85
86
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
87
|
+
/**
|
|
88
|
+
* The lens: the glass's edge only, fading in over 14 from its sides and 9 from its top and bottom (a small glass keeps
|
|
89
|
+
* a half and a third of its radius), along the glass's own shape. A ring whose hole is the glass's outline drawn
|
|
90
|
+
* smaller, blurred so its inner edge fades; bands across the box would leave a square in a round glass. The outline is
|
|
91
|
+
* the corner radius as the browser draws it, so no larger than half the glass's shorter side (a capsule's 999 is its
|
|
92
|
+
* height's half). A round glass (a bar button, a progress ring) keeps a thin rim of it: a ring as deep as a bar's
|
|
93
|
+
* reads on a small circle as a halo around a darker middle.
|
|
94
|
+
*/
|
|
95
|
+
const lensMask = (width: number, height: number, radius: number) => {
|
|
96
|
+
const r = Math.max(0, Math.min(radius, width / 2, height / 2))
|
|
97
|
+
const round = r > 0 && Math.abs(width - height) <= 1 && r >= Math.min(width, height) / 2 - 0.5
|
|
98
|
+
const inline = round ? r / 6 : Math.min(14, r > 0 ? r / 2 : 14)
|
|
99
|
+
const block = round ? r / 8 : Math.min(9, r > 0 ? r / 3 : 9)
|
|
100
|
+
const x = inline / 2
|
|
101
|
+
const y = block / 2
|
|
102
|
+
const w = Math.max(0, width - 2 * x)
|
|
103
|
+
const h = Math.max(0, height - 2 * y)
|
|
104
|
+
const rx = Math.max(0, Math.min(r - x, w / 2))
|
|
105
|
+
const ry = Math.max(0, Math.min(r - y, h / 2))
|
|
106
|
+
const f = (n: number) => +n.toFixed(2)
|
|
107
|
+
const hole =
|
|
108
|
+
`M${f(x + rx)} ${f(y)}H${f(x + w - rx)}A${f(rx)} ${f(ry)} 0 0 1 ${f(x + w)} ${f(y + ry)}V${f(y + h - ry)}` +
|
|
109
|
+
`A${f(rx)} ${f(ry)} 0 0 1 ${f(x + w - rx)} ${f(y + h)}H${f(x + rx)}A${f(rx)} ${f(ry)} 0 0 1 ${f(x)} ${f(y + h - ry)}` +
|
|
110
|
+
`V${f(y + ry)}A${f(rx)} ${f(ry)} 0 0 1 ${f(x + rx)} ${f(y)}Z`
|
|
111
|
+
// The outer edge stands 20 past the box, so the blur fades only the hole's edge; the glass's radius clips the rest
|
|
112
|
+
const svg =
|
|
113
|
+
`<svg xmlns="http://www.w3.org/2000/svg" width="${f(width)}" height="${f(height)}">` +
|
|
114
|
+
`<filter id="e" x="-1" y="-1" width="3" height="3"><feGaussianBlur stdDeviation="${f(inline / 4)} ${f(block / 4)}"/></filter>` +
|
|
115
|
+
`<path filter="url(#e)" fill-rule="evenodd" d="M-20 -20H${f(width + 20)}V${f(height + 20)}H-20Z${hole}"/></svg>`
|
|
116
|
+
return `url("data:image/svg+xml,${encodeURIComponent(svg)}")`
|
|
117
|
+
}
|
|
90
118
|
|
|
91
119
|
/** The filter and the shadow of the glass surfaces of the phone layer, by their fill (mobile-materials.css, touch.css). */
|
|
92
120
|
const SURFACES: Record<string, { filter: string; shadow: string }> = {
|
|
@@ -122,6 +150,8 @@ export function Glass({
|
|
|
122
150
|
...rest
|
|
123
151
|
}: GlassProps) {
|
|
124
152
|
const theme = useTheme()
|
|
153
|
+
// The web lens's mask follows the glass's shape, so it needs the glass's size
|
|
154
|
+
const [size, setSize] = useState<{ width: number; height: number } | null>(null)
|
|
125
155
|
const reduceTransparency = useReduceTransparency()
|
|
126
156
|
const overrides = useThemeOverrides()
|
|
127
157
|
const shape: ViewStyle = { borderRadius: radius, borderCurve: "continuous" }
|
|
@@ -174,7 +204,7 @@ export function Glass({
|
|
|
174
204
|
const sheen = liquid && tint == null ? own(WEB_LIQUID.sheen) : undefined
|
|
175
205
|
const lensToken = theme.token(WEB_LIQUID.lens.token)
|
|
176
206
|
const lensFilter = typeof lensToken === "string" ? lensToken : WEB_LIQUID.lens.value
|
|
177
|
-
const mask =
|
|
207
|
+
const mask = size != null && size.width > 0 && size.height > 0 ? lensMask(size.width, size.height, radius) : undefined
|
|
178
208
|
// An Animated.View, so a panel can move its own glass (a transform above the blur would stop it)
|
|
179
209
|
return (
|
|
180
210
|
<Animated.View
|
|
@@ -190,10 +220,17 @@ export function Glass({
|
|
|
190
220
|
<View
|
|
191
221
|
pointerEvents="none"
|
|
192
222
|
aria-hidden
|
|
223
|
+
onLayout={(event) => {
|
|
224
|
+
const { width, height } = event.nativeEvent.layout
|
|
225
|
+
if (size?.width !== width || size?.height !== height) setSize({ width, height })
|
|
226
|
+
}}
|
|
193
227
|
style={[
|
|
194
228
|
StyleSheet.absoluteFill,
|
|
195
229
|
shape,
|
|
196
|
-
|
|
230
|
+
// Until the glass has a size, no lens: a lens unmasked would blur the whole glass
|
|
231
|
+
mask == null
|
|
232
|
+
? null
|
|
233
|
+
: ({ backdropFilter: lensFilter, WebkitBackdropFilter: lensFilter, maskImage: mask, WebkitMaskImage: mask, maskSize: "100% 100%", WebkitMaskSize: "100% 100%" } as ViewStyle),
|
|
197
234
|
]}
|
|
198
235
|
/>
|
|
199
236
|
) : null}
|