@fluxus-ui/native 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/README.md +61 -2
- package/lib/module/Avatar.js +58 -0
- package/lib/module/Avatar.js.map +1 -0
- package/lib/module/Badge.js +72 -0
- package/lib/module/Badge.js.map +1 -0
- package/lib/module/BottomSheet.js +159 -0
- package/lib/module/BottomSheet.js.map +1 -0
- package/lib/module/Button.js +133 -0
- package/lib/module/Button.js.map +1 -0
- package/lib/module/Card.js +71 -0
- package/lib/module/Card.js.map +1 -0
- package/lib/module/Dialog.js +137 -0
- package/lib/module/Dialog.js.map +1 -0
- package/lib/module/Disclosure.js +153 -0
- package/lib/module/Disclosure.js.map +1 -0
- package/lib/module/EmptyState.js +43 -0
- package/lib/module/EmptyState.js.map +1 -0
- package/lib/module/ListItem.js +83 -0
- package/lib/module/ListItem.js.map +1 -0
- package/lib/module/Menu.js +76 -0
- package/lib/module/Menu.js.map +1 -0
- package/lib/module/Message.js +51 -0
- package/lib/module/Message.js.map +1 -0
- package/lib/module/Popover.js +153 -0
- package/lib/module/Popover.js.map +1 -0
- package/lib/module/Progress.js +54 -0
- package/lib/module/Progress.js.map +1 -0
- package/lib/module/Select.js +146 -0
- package/lib/module/Select.js.map +1 -0
- package/lib/module/Skeleton.js +39 -0
- package/lib/module/Skeleton.js.map +1 -0
- package/lib/module/Slider.js +137 -0
- package/lib/module/Slider.js.map +1 -0
- package/lib/module/Spinner.js +30 -0
- package/lib/module/Spinner.js.map +1 -0
- package/lib/module/Swap.js +87 -0
- package/lib/module/Swap.js.map +1 -0
- package/lib/module/Tabs.js +118 -0
- package/lib/module/Tabs.js.map +1 -0
- package/lib/module/Text.js +47 -0
- package/lib/module/Text.js.map +1 -0
- package/lib/module/TextField.js +190 -0
- package/lib/module/TextField.js.map +1 -0
- package/lib/module/Toast.js +108 -0
- package/lib/module/Toast.js.map +1 -0
- package/lib/module/blur.js +55 -0
- package/lib/module/blur.js.map +1 -0
- package/lib/module/controls.js +259 -0
- package/lib/module/controls.js.map +1 -0
- package/lib/module/generated/tokens.js +484 -0
- package/lib/module/generated/tokens.js.map +1 -0
- package/lib/module/icon.js +51 -0
- package/lib/module/icon.js.map +1 -0
- package/lib/module/index.js +33 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/layout.js +75 -0
- package/lib/module/layout.js.map +1 -0
- package/lib/module/motion.js +28 -0
- package/lib/module/motion.js.map +1 -0
- package/lib/module/overlay.js +99 -0
- package/lib/module/overlay.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/pending.js +177 -0
- package/lib/module/pending.js.map +1 -0
- package/lib/module/presence.js +68 -0
- package/lib/module/presence.js.map +1 -0
- package/lib/module/press.js +32 -0
- package/lib/module/press.js.map +1 -0
- package/lib/module/screen.js +45 -0
- package/lib/module/screen.js.map +1 -0
- package/lib/module/theme.js +41 -0
- package/lib/module/theme.js.map +1 -0
- package/lib/typescript/Avatar.d.ts +17 -0
- package/lib/typescript/Avatar.d.ts.map +1 -0
- package/lib/typescript/Badge.d.ts +21 -0
- package/lib/typescript/Badge.d.ts.map +1 -0
- package/lib/typescript/BottomSheet.d.ts +27 -0
- package/lib/typescript/BottomSheet.d.ts.map +1 -0
- package/lib/typescript/Button.d.ts +27 -0
- package/lib/typescript/Button.d.ts.map +1 -0
- package/lib/typescript/Card.d.ts +17 -0
- package/lib/typescript/Card.d.ts.map +1 -0
- package/lib/typescript/Dialog.d.ts +34 -0
- package/lib/typescript/Dialog.d.ts.map +1 -0
- package/lib/typescript/Disclosure.d.ts +39 -0
- package/lib/typescript/Disclosure.d.ts.map +1 -0
- package/lib/typescript/EmptyState.d.ts +12 -0
- package/lib/typescript/EmptyState.d.ts.map +1 -0
- package/lib/typescript/ListItem.d.ts +20 -0
- package/lib/typescript/ListItem.d.ts.map +1 -0
- package/lib/typescript/Menu.d.ts +23 -0
- package/lib/typescript/Menu.d.ts.map +1 -0
- package/lib/typescript/Message.d.ts +14 -0
- package/lib/typescript/Message.d.ts.map +1 -0
- package/lib/typescript/Popover.d.ts +71 -0
- package/lib/typescript/Popover.d.ts.map +1 -0
- package/lib/typescript/Progress.d.ts +12 -0
- package/lib/typescript/Progress.d.ts.map +1 -0
- package/lib/typescript/Select.d.ts +30 -0
- package/lib/typescript/Select.d.ts.map +1 -0
- package/lib/typescript/Skeleton.d.ts +13 -0
- package/lib/typescript/Skeleton.d.ts.map +1 -0
- package/lib/typescript/Slider.d.ts +21 -0
- package/lib/typescript/Slider.d.ts.map +1 -0
- package/lib/typescript/Spinner.d.ts +12 -0
- package/lib/typescript/Spinner.d.ts.map +1 -0
- package/lib/typescript/Swap.d.ts +25 -0
- package/lib/typescript/Swap.d.ts.map +1 -0
- package/lib/typescript/Tabs.d.ts +18 -0
- package/lib/typescript/Tabs.d.ts.map +1 -0
- package/lib/typescript/Text.d.ts +20 -0
- package/lib/typescript/Text.d.ts.map +1 -0
- package/lib/typescript/TextField.d.ts +36 -0
- package/lib/typescript/TextField.d.ts.map +1 -0
- package/lib/typescript/Toast.d.ts +25 -0
- package/lib/typescript/Toast.d.ts.map +1 -0
- package/lib/typescript/blur.d.ts +4 -0
- package/lib/typescript/blur.d.ts.map +1 -0
- package/lib/typescript/controls.d.ts +37 -0
- package/lib/typescript/controls.d.ts.map +1 -0
- package/lib/typescript/generated/tokens.d.ts +499 -0
- package/lib/typescript/generated/tokens.d.ts.map +1 -0
- package/lib/typescript/icon.d.ts +25 -0
- package/lib/typescript/icon.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +31 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/layout.d.ts +24 -0
- package/lib/typescript/layout.d.ts.map +1 -0
- package/lib/typescript/motion.d.ts +7 -0
- package/lib/typescript/motion.d.ts.map +1 -0
- package/lib/typescript/overlay.d.ts +33 -0
- package/lib/typescript/overlay.d.ts.map +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/pending.d.ts +25 -0
- package/lib/typescript/pending.d.ts.map +1 -0
- package/lib/typescript/presence.d.ts +37 -0
- package/lib/typescript/presence.d.ts.map +1 -0
- package/lib/typescript/press.d.ts +12 -0
- package/lib/typescript/press.d.ts.map +1 -0
- package/lib/typescript/screen.d.ts +16 -0
- package/lib/typescript/screen.d.ts.map +1 -0
- package/lib/typescript/theme.d.ts +32 -0
- package/lib/typescript/theme.d.ts.map +1 -0
- package/package.json +100 -3
- package/src/Avatar.tsx +44 -0
- package/src/Badge.tsx +45 -0
- package/src/BottomSheet.tsx +109 -0
- package/src/Button.tsx +117 -0
- package/src/Card.tsx +65 -0
- package/src/Dialog.tsx +93 -0
- package/src/Disclosure.tsx +108 -0
- package/src/EmptyState.tsx +32 -0
- package/src/ListItem.tsx +73 -0
- package/src/Menu.tsx +57 -0
- package/src/Message.tsx +43 -0
- package/src/Popover.tsx +145 -0
- package/src/Progress.tsx +33 -0
- package/src/Select.tsx +118 -0
- package/src/Skeleton.tsx +27 -0
- package/src/Slider.tsx +107 -0
- package/src/Spinner.tsx +18 -0
- package/src/Swap.tsx +74 -0
- package/src/Tabs.tsx +84 -0
- package/src/Text.tsx +44 -0
- package/src/TextField.tsx +137 -0
- package/src/Toast.tsx +94 -0
- package/src/blur.tsx +33 -0
- package/src/controls.tsx +196 -0
- package/src/generated/tokens.ts +653 -0
- package/src/icon.tsx +39 -0
- package/src/index.ts +30 -0
- package/src/layout.tsx +40 -0
- package/src/motion.ts +22 -0
- package/src/overlay.tsx +77 -0
- package/src/pending.tsx +131 -0
- package/src/presence.tsx +65 -0
- package/src/press.ts +23 -0
- package/src/screen.tsx +32 -0
- package/src/theme.tsx +49 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export { ThemeProvider, useTheme, type Theme, type ThemeProviderProps, type Mode, type Fonts } from './theme';
|
|
2
|
+
export { themes, motion, type ThemeTokens, type AppToken, type ColorToken, type SizeToken, type NumberToken, type WeightToken } from './generated/tokens';
|
|
3
|
+
export { spring, timing, type SpringPreset, type DurationToken, type EasingToken } from './motion';
|
|
4
|
+
export { Text, textStyle, type TextProps, type TextVariant, type TextTone, type TextWeight } from './Text';
|
|
5
|
+
export { Button, buttonStyle, type ButtonProps, type ButtonVariant, type ButtonSize } from './Button';
|
|
6
|
+
export { usePress } from './press';
|
|
7
|
+
export { Badge, toneColors, type BadgeProps, type Tone } from './Badge';
|
|
8
|
+
export { Card, cardStyle, type CardProps } from './Card';
|
|
9
|
+
export { Progress, type ProgressProps } from './Progress';
|
|
10
|
+
export { Message, type MessageProps } from './Message';
|
|
11
|
+
export { TextField, TextArea, SearchField, type TextFieldProps, type SearchFieldProps } from './TextField';
|
|
12
|
+
export { Stack, Row, Divider, type StackProps, type RowProps, type Space } from './layout';
|
|
13
|
+
export { Spinner, type SpinnerProps } from './Spinner';
|
|
14
|
+
export { Skeleton, type SkeletonProps } from './Skeleton';
|
|
15
|
+
export { ListItem, type ListItemProps } from './ListItem';
|
|
16
|
+
export { EmptyState, type EmptyStateProps } from './EmptyState';
|
|
17
|
+
export { Avatar, initials, type AvatarProps } from './Avatar';
|
|
18
|
+
export { Tabs, type TabsProps, type TabItem } from './Tabs';
|
|
19
|
+
export { ToastProvider, ToastLayer, useToast, announce, type ToastInput, type ToastProviderProps } from './Toast';
|
|
20
|
+
export { Appear, presence, enter, exit, shift, RISE, STAGGER, type AppearProps } from './presence';
|
|
21
|
+
export { Swap, useSwap, type SwapProps } from './Swap';
|
|
22
|
+
export { usePending, useAsyncPress, settle } from './pending';
|
|
23
|
+
export { Checkbox, Switch, RadioGroup, useControlled, type CheckboxProps, type SwitchProps, type RadioGroupProps, type RadioOption } from './controls';
|
|
24
|
+
export { Slider, type SliderProps } from './Slider';
|
|
25
|
+
export { BottomSheet, type BottomSheetProps } from './BottomSheet';
|
|
26
|
+
export { Dialog, ConfirmDialog, type DialogProps, type ConfirmDialogProps } from './Dialog';
|
|
27
|
+
export { Popover, place, type PopoverProps, type Placement, type Rect } from './Popover';
|
|
28
|
+
export { Menu, MenuItem, MenuSeparator, type MenuProps, type MenuItemProps } from './Menu';
|
|
29
|
+
export { Disclosure, Accordion, type DisclosureProps, type AccordionProps, type AccordionItem } from './Disclosure';
|
|
30
|
+
export { Select, type SelectProps, type SelectOption } from './Select';
|
package/src/layout.tsx
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { View, type ViewProps, type ViewStyle } from 'react-native';
|
|
2
|
+
import { useTheme } from './theme';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* For what takes the room left in a row. Not `flex: 1`: that has no width of its own, and in what is only as
|
|
6
|
+
* wide as what it holds (a menu, a popover) the text would break letter by letter. (SPEC §15.8)
|
|
7
|
+
*/
|
|
8
|
+
export const fill: ViewStyle = { flexGrow: 1, flexShrink: 1, flexBasis: 'auto' };
|
|
9
|
+
|
|
10
|
+
/** A step of the space scale: `space.3` is 12 points. */
|
|
11
|
+
export type Space = '1' | '2' | '3' | '4' | '5' | '6' | '8' | '10' | '12' | '16';
|
|
12
|
+
|
|
13
|
+
export interface StackProps extends ViewProps {
|
|
14
|
+
gap?: Space;
|
|
15
|
+
align?: ViewStyle['alignItems'];
|
|
16
|
+
justify?: ViewStyle['justifyContent'];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** What it holds, one under the other. */
|
|
20
|
+
export function Stack({ gap = '3', align, justify, style, ...rest }: StackProps) {
|
|
21
|
+
const theme = useTheme();
|
|
22
|
+
return <View {...rest} style={[{ gap: theme.size[`space.${gap}`], alignItems: align, justifyContent: justify }, style]} />;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface RowProps extends StackProps {
|
|
26
|
+
/** It goes on to the next line when it does not fit. */
|
|
27
|
+
wrap?: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** What it holds, side by side, in the direction of the language. */
|
|
31
|
+
export function Row({ gap = '2', align = 'center', justify, wrap, style, ...rest }: RowProps) {
|
|
32
|
+
const theme = useTheme();
|
|
33
|
+
return <View {...rest} style={[{ flexDirection: 'row', flexWrap: wrap ? 'wrap' : 'nowrap', gap: theme.size[`space.${gap}`], alignItems: align, justifyContent: justify }, style]} />;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** A rule between two things. It is not there for a screen reader. */
|
|
37
|
+
export function Divider({ style, ...rest }: ViewProps) {
|
|
38
|
+
const theme = useTheme();
|
|
39
|
+
return <View aria-hidden importantForAccessibility="no" {...rest} style={[{ height: 1, backgroundColor: theme.color['border.default'] }, style]} />;
|
|
40
|
+
}
|
package/src/motion.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The motion vocabulary of Fluxus on Reanimated: the same names, durations and
|
|
3
|
+
* springs as the web (core/src/tokens.ts). With reduced motion on in the system,
|
|
4
|
+
* a value goes to its end at once. (SPEC §15)
|
|
5
|
+
*/
|
|
6
|
+
import { Easing, ReduceMotion, withSpring, withTiming } from 'react-native-reanimated';
|
|
7
|
+
import { motion } from './generated/tokens';
|
|
8
|
+
|
|
9
|
+
export type SpringPreset = keyof typeof motion.spring;
|
|
10
|
+
export type DurationToken = keyof typeof motion.duration;
|
|
11
|
+
export type EasingToken = keyof typeof motion.easing;
|
|
12
|
+
|
|
13
|
+
export const spring = (to: number, preset: SpringPreset = 'smooth'): number => {
|
|
14
|
+
'worklet';
|
|
15
|
+
return withSpring(to, { ...motion.spring[preset], reduceMotion: ReduceMotion.System });
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const timing = (to: number, duration: DurationToken = 'normal', easing: EasingToken = 'standard'): number => {
|
|
19
|
+
'worklet';
|
|
20
|
+
const [a, b, c, d] = motion.easing[easing];
|
|
21
|
+
return withTiming(to, { duration: motion.duration[duration], easing: Easing.bezier(a, b, c, d), reduceMotion: ReduceMotion.System });
|
|
22
|
+
};
|
package/src/overlay.tsx
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What opens above everything: a window of React Native (`Modal`) that paints and moves nothing of its own,
|
|
3
|
+
* where Fluxus draws the veil and moves the panel. Back on Android, Escape in a browser and the scrub of
|
|
4
|
+
* VoiceOver close it. (SPEC §15.8)
|
|
5
|
+
*/
|
|
6
|
+
import { useEffect, useState, type ReactNode } from 'react';
|
|
7
|
+
import { AccessibilityInfo, Modal, Platform, Pressable, StyleSheet, View, type HostInstance, type ViewStyle } from 'react-native';
|
|
8
|
+
import Animated, { useAnimatedStyle, useReducedMotion, useSharedValue, type SharedValue } from 'react-native-reanimated';
|
|
9
|
+
import { motion } from './generated/tokens';
|
|
10
|
+
import { spring, timing } from './motion';
|
|
11
|
+
import { useTheme, type Theme } from './theme';
|
|
12
|
+
import { ToastLayer } from './Toast';
|
|
13
|
+
|
|
14
|
+
/** A screen reader goes to it: to the title on opening, back to what opened it on closing. */
|
|
15
|
+
export const focusOn = (target: unknown): void => {
|
|
16
|
+
if (target && Platform.OS !== 'web') AccessibilityInfo.sendAccessibilityEvent(target as HostInstance, 'focus');
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** The surface of what floats: raised, edged, and with the one shadow of the package. */
|
|
20
|
+
export const floating = (theme: Theme): ViewStyle => ({
|
|
21
|
+
borderWidth: 1,
|
|
22
|
+
borderColor: theme.color['border.default'],
|
|
23
|
+
borderRadius: theme.size['radius.card'],
|
|
24
|
+
backgroundColor: theme.color['surface.raised'],
|
|
25
|
+
boxShadow: [{ offsetX: 0, offsetY: theme.size['space.2'], blurRadius: theme.size['space.6'], color: theme.color['shadow.tint'] }],
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
/** `mounted` outlives `open` by the time it takes to go; `t` goes 0 → 1 as it comes and back as it goes. */
|
|
29
|
+
export function useOverlay(open: boolean): { mounted: boolean; t: SharedValue<number> } {
|
|
30
|
+
const reduced = useReducedMotion();
|
|
31
|
+
const [mounted, setMounted] = useState(open);
|
|
32
|
+
const t = useSharedValue(0);
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
if (open) {
|
|
35
|
+
setMounted(true);
|
|
36
|
+
t.value = spring(1, 'smooth');
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
t.value = timing(0, 'fast', 'accelerate');
|
|
40
|
+
const timer = setTimeout(() => setMounted(false), reduced ? 0 : motion.duration.fast);
|
|
41
|
+
return () => clearTimeout(timer);
|
|
42
|
+
}, [open, reduced, t]);
|
|
43
|
+
return { mounted, t };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface OverlayProps {
|
|
47
|
+
open: boolean;
|
|
48
|
+
/** Back on Android, Escape in a browser, the scrub of VoiceOver, and a press outside if `dismissible`. */
|
|
49
|
+
onClose: () => void;
|
|
50
|
+
/** It darkens what is behind. Default: true. */
|
|
51
|
+
veil?: boolean;
|
|
52
|
+
/** A press outside closes it. Default: true. */
|
|
53
|
+
dismissible?: boolean;
|
|
54
|
+
/** Once the window is on the screen. */
|
|
55
|
+
onShow?: () => void;
|
|
56
|
+
/** The panel, given `t`, in a box as big as the screen. */
|
|
57
|
+
children: (t: SharedValue<number>) => ReactNode;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function Overlay({ open, onClose, veil = true, dismissible = true, onShow, children }: OverlayProps) {
|
|
61
|
+
const theme = useTheme();
|
|
62
|
+
const { mounted, t } = useOverlay(open);
|
|
63
|
+
const fade = useAnimatedStyle(() => ({ opacity: t.value }));
|
|
64
|
+
if (!mounted) return null;
|
|
65
|
+
return (
|
|
66
|
+
<Modal transparent visible animationType="none" statusBarTranslucent navigationBarTranslucent onRequestClose={onClose} onShow={onShow}>
|
|
67
|
+
<View style={StyleSheet.absoluteFill} onAccessibilityEscape={onClose}>
|
|
68
|
+
{veil && <Animated.View pointerEvents="none" style={[StyleSheet.absoluteFill, { backgroundColor: theme.color['surface.overlay'] }, fade]} />}
|
|
69
|
+
{/* Outside the panel. A screen reader does not find it: it has the handle, the buttons and its own gesture. */}
|
|
70
|
+
<Pressable aria-hidden accessible={false} tabIndex={-1} style={StyleSheet.absoluteFill} onPress={dismissible ? onClose : undefined} />
|
|
71
|
+
{children(t)}
|
|
72
|
+
{/* A notice of the app called while this is open would be behind it. */}
|
|
73
|
+
<ToastLayer />
|
|
74
|
+
</View>
|
|
75
|
+
</Modal>
|
|
76
|
+
);
|
|
77
|
+
}
|
package/src/pending.tsx
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The wait is born from the finger, as on the web (core/src/pending.ts, SPEC §14.35): what was pressed and now
|
|
3
|
+
* waits fills slowly from where it was touched, never to the whole; when the answer comes it fills and goes,
|
|
4
|
+
* and if it failed it drains back to the finger. A wait shorter than 150 ms shows nothing; once seen, it lasts
|
|
5
|
+
* 400 ms at least. (SPEC §15.6, §15.9)
|
|
6
|
+
*/
|
|
7
|
+
import { useEffect, useRef, useState, type ReactNode } from 'react';
|
|
8
|
+
import type { GestureResponderEvent, LayoutChangeEvent } from 'react-native';
|
|
9
|
+
import Animated, { Easing, ReduceMotion, useAnimatedStyle, useReducedMotion, useSharedValue, withDelay, withTiming } from 'react-native-reanimated';
|
|
10
|
+
import { motion } from './generated/tokens';
|
|
11
|
+
|
|
12
|
+
const DELAY = 150;
|
|
13
|
+
const MIN_VISIBLE = 400;
|
|
14
|
+
/** How far it gets by itself, of the way to the farthest corner, and the weight of its colour. */
|
|
15
|
+
const REACH = 0.9;
|
|
16
|
+
const TINT = 0.14;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* `onPress` may return a promise: until it settles, what was pressed waits. If it is rejected, it failed: the
|
|
20
|
+
* third value is true until the next press. The rejection goes no further: the app has its own `catch`.
|
|
21
|
+
*/
|
|
22
|
+
export function useAsyncPress<E>(onPress: ((event: E) => unknown) | null | undefined): [waiting: boolean, run: (event: E) => void, failed: boolean] {
|
|
23
|
+
const [state, setState] = useState<'idle' | 'waiting' | 'failed'>('idle');
|
|
24
|
+
const alive = useRef(true);
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
alive.current = true;
|
|
27
|
+
return () => {
|
|
28
|
+
alive.current = false;
|
|
29
|
+
};
|
|
30
|
+
}, []);
|
|
31
|
+
const run = (event: E): void => {
|
|
32
|
+
const result = onPress?.(event) as PromiseLike<unknown> | undefined;
|
|
33
|
+
if (typeof result?.then !== 'function') return;
|
|
34
|
+
setState('waiting');
|
|
35
|
+
const end = (next: 'idle' | 'failed') => (): void => {
|
|
36
|
+
if (alive.current) setState(next);
|
|
37
|
+
};
|
|
38
|
+
result.then(end('idle'), end('failed'));
|
|
39
|
+
};
|
|
40
|
+
return [state === 'waiting', run, state === 'failed'];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** `done` runs once `result` is fulfilled if it is a promise, and at once if it is not. If it fails, it does not run. */
|
|
44
|
+
export function settle(result: unknown, done: () => void): void | Promise<void> {
|
|
45
|
+
const promise = result as PromiseLike<unknown> | undefined;
|
|
46
|
+
if (typeof promise?.then !== 'function') return done();
|
|
47
|
+
return Promise.resolve(promise).then(done, () => undefined);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface FillProps {
|
|
51
|
+
x: number;
|
|
52
|
+
y: number;
|
|
53
|
+
/** As far as the farthest corner. */
|
|
54
|
+
radius: number;
|
|
55
|
+
ink: string;
|
|
56
|
+
/** How the wait ended, once it has. */
|
|
57
|
+
ending: false | 'done' | 'failed';
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Mounted only while something waits: at rest, what can wait carries no animated view. */
|
|
61
|
+
function Fill({ x, y, radius, ink, ending }: FillProps) {
|
|
62
|
+
const reduced = useReducedMotion();
|
|
63
|
+
const grow = useSharedValue(reduced ? 1 : 0);
|
|
64
|
+
const fade = useSharedValue(1);
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
const fast = motion.duration.fast;
|
|
67
|
+
if (ending === 'failed') grow.value = withTiming(0, { duration: motion.duration.normal, reduceMotion: ReduceMotion.System });
|
|
68
|
+
else if (ending) {
|
|
69
|
+
grow.value = withTiming(1, { duration: fast, reduceMotion: ReduceMotion.System });
|
|
70
|
+
fade.value = withDelay(fast, withTiming(0, { duration: fast, reduceMotion: ReduceMotion.System }));
|
|
71
|
+
// It slows as it goes and never gets there: it does not pretend to know how long.
|
|
72
|
+
} else if (!reduced) grow.value = withTiming(REACH, { duration: 4000, easing: Easing.out(Easing.exp) });
|
|
73
|
+
}, [ending, reduced, grow, fade]);
|
|
74
|
+
const style = useAnimatedStyle(() => ({ opacity: fade.value * TINT, transform: [{ scale: grow.value }] }));
|
|
75
|
+
// Where the finger was, in the points of the element: `left` and `top`, not the direction of the language.
|
|
76
|
+
return <Animated.View pointerEvents="none" aria-hidden style={[{ position: 'absolute', left: x - radius, top: y - radius, width: 2 * radius, height: 2 * radius, borderRadius: radius, backgroundColor: ink }, style]} />;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* For what is pressed and may wait. Give it `onPressIn` and `onLayout`, put `fill` first inside it, and clip it
|
|
81
|
+
* (`overflow: 'hidden'`). `ink` is the colour of its text; `failed`, whether the wait that ends now did.
|
|
82
|
+
*/
|
|
83
|
+
export function usePending(waiting: boolean, ink: string, failed = false): { onPressIn: (e: GestureResponderEvent) => void; onLayout: (e: LayoutChangeEvent) => void; fill: ReactNode } {
|
|
84
|
+
const [phase, setPhase] = useState<'idle' | 'shown' | 'done' | 'failed'>('idle');
|
|
85
|
+
const how = useRef(failed);
|
|
86
|
+
how.current = failed;
|
|
87
|
+
const origin = useRef<{ x: number; y: number } | null>(null);
|
|
88
|
+
const size = useRef({ w: 0, h: 0 });
|
|
89
|
+
const since = useRef(0);
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
if (waiting) {
|
|
92
|
+
const timer = setTimeout(() => {
|
|
93
|
+
since.current = Date.now();
|
|
94
|
+
setPhase('shown');
|
|
95
|
+
}, DELAY);
|
|
96
|
+
return () => clearTimeout(timer);
|
|
97
|
+
}
|
|
98
|
+
if (phase !== 'shown') return;
|
|
99
|
+
let last: ReturnType<typeof setTimeout>;
|
|
100
|
+
const timer = setTimeout(
|
|
101
|
+
() => {
|
|
102
|
+
setPhase(how.current ? 'failed' : 'done');
|
|
103
|
+
last = setTimeout(() => setPhase('idle'), how.current ? motion.duration.normal : motion.duration.fast * 2);
|
|
104
|
+
},
|
|
105
|
+
Math.max(0, MIN_VISIBLE - (Date.now() - since.current)),
|
|
106
|
+
);
|
|
107
|
+
return () => {
|
|
108
|
+
clearTimeout(timer);
|
|
109
|
+
clearTimeout(last);
|
|
110
|
+
};
|
|
111
|
+
// `phase` is read, not followed: only a change of `waiting` moves it on.
|
|
112
|
+
}, [waiting]);
|
|
113
|
+
const { w, h } = size.current;
|
|
114
|
+
const at = origin.current ?? { x: w / 2, y: h / 2 };
|
|
115
|
+
return {
|
|
116
|
+
onPressIn(e) {
|
|
117
|
+
// The event gives the point in the window; the element says where it is in it.
|
|
118
|
+
origin.current = null;
|
|
119
|
+
// Pressed with a keyboard there is no point: it fills from the centre.
|
|
120
|
+
if (!e?.nativeEvent) return;
|
|
121
|
+
const { pageX, pageY } = e.nativeEvent;
|
|
122
|
+
(e.currentTarget as unknown as { measureInWindow?: (done: (x: number, y: number) => void) => void } | null)?.measureInWindow?.((x, y) => {
|
|
123
|
+
origin.current = { x: pageX - x, y: pageY - y };
|
|
124
|
+
});
|
|
125
|
+
},
|
|
126
|
+
onLayout(e) {
|
|
127
|
+
size.current = { w: e.nativeEvent.layout.width, h: e.nativeEvent.layout.height };
|
|
128
|
+
},
|
|
129
|
+
fill: phase === 'idle' ? null : <Fill x={at.x} y={at.y} radius={Math.hypot(Math.max(at.x, w - at.x), Math.max(at.y, h - at.y))} ink={ink} ending={phase === 'shown' ? false : phase} />,
|
|
130
|
+
};
|
|
131
|
+
}
|
package/src/presence.tsx
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Nothing jumps: what comes fades in as it rises, what goes fades out, and what
|
|
3
|
+
* stays glides to its new place. The values of the web (core/src/stagger.ts),
|
|
4
|
+
* on the layout animations of Reanimated. With reduced motion on in the system
|
|
5
|
+
* they are immediate. (SPEC §15.4)
|
|
6
|
+
*/
|
|
7
|
+
import type { ComponentRef, Ref } from 'react';
|
|
8
|
+
import { Platform, type ViewProps } from 'react-native';
|
|
9
|
+
import Animated, { Easing, FadeInDown, FadeOut, LinearTransition, ReduceMotion } from 'react-native-reanimated';
|
|
10
|
+
import { motion } from './generated/tokens';
|
|
11
|
+
|
|
12
|
+
const curve = (name: keyof typeof motion.easing) => {
|
|
13
|
+
const [a, b, c, d] = motion.easing[name];
|
|
14
|
+
return Easing.bezier(a, b, c, d);
|
|
15
|
+
};
|
|
16
|
+
/** How far it rises as it comes, and how long the nth of a group waits for the one before. */
|
|
17
|
+
export const RISE = 12;
|
|
18
|
+
export const STAGGER = 70;
|
|
19
|
+
|
|
20
|
+
/** For `entering`: it fades in as it rises; `index` is its place in a group that comes together. */
|
|
21
|
+
export const enter = (index = 0) =>
|
|
22
|
+
FadeInDown.duration(motion.duration.normal)
|
|
23
|
+
.delay(index * STAGGER)
|
|
24
|
+
.easing(curve('decelerate'))
|
|
25
|
+
.withInitialValues({ transform: [{ translateY: RISE }] })
|
|
26
|
+
.reduceMotion(ReduceMotion.System);
|
|
27
|
+
|
|
28
|
+
/** For `exiting`: it fades out, fast. */
|
|
29
|
+
export const exit = () => FadeOut.duration(motion.duration.fast).easing(curve('accelerate')).reduceMotion(ReduceMotion.System);
|
|
30
|
+
|
|
31
|
+
/** For `layout`: it glides to where it is now, on the smooth spring. */
|
|
32
|
+
export const shift = () => {
|
|
33
|
+
const { stiffness, damping, mass } = motion.spring.smooth;
|
|
34
|
+
return LinearTransition.springify().stiffness(stiffness).damping(damping).mass(mass).reduceMotion(ReduceMotion.System);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
interface Presence {
|
|
38
|
+
entering: ReturnType<typeof enter>;
|
|
39
|
+
exiting: ReturnType<typeof exit>;
|
|
40
|
+
layout: ReturnType<typeof shift>;
|
|
41
|
+
}
|
|
42
|
+
const MADE = new Map<number, Presence>();
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The three, for a view that comes, goes and moves. In a browser they are off: there Reanimated pins a view
|
|
46
|
+
* that has them where it first was (`position: absolute`, seen with 4.7.1), and the layout breaks.
|
|
47
|
+
*/
|
|
48
|
+
export const presence = (index = 0): Partial<Presence> => {
|
|
49
|
+
if (Platform.OS === 'web') return {};
|
|
50
|
+
// One set for each place in a group, made once: a render does not build them again.
|
|
51
|
+
let made = MADE.get(index);
|
|
52
|
+
if (!made) MADE.set(index, (made = { entering: enter(index), exiting: exit(), layout: shift() }));
|
|
53
|
+
return made;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export interface AppearProps extends ViewProps {
|
|
57
|
+
/** Its place in a group that comes together: each waits a little for the one before. */
|
|
58
|
+
index?: number;
|
|
59
|
+
ref?: Ref<ComponentRef<typeof Animated.View>>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** A view that comes, goes and moves without a jump. Its siblings glide too when each is an Appear. */
|
|
63
|
+
export function Appear({ index = 0, ...rest }: AppearProps) {
|
|
64
|
+
return <Animated.View {...presence(index)} {...rest} />;
|
|
65
|
+
}
|
package/src/press.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { GestureResponderEvent, PressableProps } from 'react-native';
|
|
2
|
+
import { useAnimatedStyle, useSharedValue } from 'react-native-reanimated';
|
|
3
|
+
import { spring } from './motion';
|
|
4
|
+
|
|
5
|
+
/** As the web (core/src/press.ts): what is pressed gives a little, on the snappy spring. */
|
|
6
|
+
const PRESSED = 0.97;
|
|
7
|
+
|
|
8
|
+
/** The style and the two handlers of something that gives when pressed; the handlers of the app still run. */
|
|
9
|
+
export function usePress({ onPressIn, onPressOut }: Pick<PressableProps, 'onPressIn' | 'onPressOut'>) {
|
|
10
|
+
const scale = useSharedValue(1);
|
|
11
|
+
const style = useAnimatedStyle(() => ({ transform: [{ scale: scale.value }] }));
|
|
12
|
+
return {
|
|
13
|
+
style,
|
|
14
|
+
onPressIn: (e: GestureResponderEvent) => {
|
|
15
|
+
scale.value = spring(PRESSED, 'snappy');
|
|
16
|
+
onPressIn?.(e);
|
|
17
|
+
},
|
|
18
|
+
onPressOut: (e: GestureResponderEvent) => {
|
|
19
|
+
scale.value = spring(1, 'snappy');
|
|
20
|
+
onPressOut?.(e);
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
}
|
package/src/screen.tsx
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@fluxus-ui/native/screen`: the ground of a screen, inside the safe area. Its own entry, so that an app that
|
|
3
|
+
* lays out its screens some other way does not need react-native-safe-area-context. (SPEC §15.5)
|
|
4
|
+
*/
|
|
5
|
+
import { ScrollView, View, type ViewProps } from 'react-native';
|
|
6
|
+
import { SafeAreaView, type Edge } from 'react-native-safe-area-context';
|
|
7
|
+
import { useTheme } from './theme';
|
|
8
|
+
|
|
9
|
+
export interface ScreenProps extends ViewProps {
|
|
10
|
+
/** It scrolls, and the keyboard does not cover the field in use. */
|
|
11
|
+
scroll?: boolean;
|
|
12
|
+
/** Which sides keep clear of the notch and the bars. Default: all. With a header or a tab bar, leave theirs out. */
|
|
13
|
+
edges?: readonly Edge[];
|
|
14
|
+
/** The gutter of the theme around what it holds, and its gap between. Default: true. */
|
|
15
|
+
padded?: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function Screen({ scroll = false, edges, padded = true, style, children, ...rest }: ScreenProps) {
|
|
19
|
+
const theme = useTheme();
|
|
20
|
+
const inner = padded ? { padding: theme.size['layout.gutter'], gap: theme.size['space.4'] } : null;
|
|
21
|
+
return (
|
|
22
|
+
<SafeAreaView edges={edges} {...rest} style={[{ flex: 1, backgroundColor: theme.color['surface.page'] }, style]}>
|
|
23
|
+
{scroll ? (
|
|
24
|
+
<ScrollView keyboardShouldPersistTaps="handled" keyboardDismissMode="interactive" automaticallyAdjustKeyboardInsets contentContainerStyle={inner}>
|
|
25
|
+
{children}
|
|
26
|
+
</ScrollView>
|
|
27
|
+
) : (
|
|
28
|
+
<View style={[{ flex: 1 }, inner]}>{children}</View>
|
|
29
|
+
)}
|
|
30
|
+
</SafeAreaView>
|
|
31
|
+
);
|
|
32
|
+
}
|
package/src/theme.tsx
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { createContext, useContext, useMemo, type ReactNode } from 'react';
|
|
2
|
+
import { useColorScheme } from 'react-native';
|
|
3
|
+
import { themes, type ThemeTokens } from './generated/tokens';
|
|
4
|
+
|
|
5
|
+
export type Mode = 'light' | 'dark';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The faces the app loaded (with expo-font, or linked), by weight. With a face for a weight, text takes
|
|
9
|
+
* that family and no `fontWeight`: Android would thicken it again. Without, the regular one and the weight.
|
|
10
|
+
*/
|
|
11
|
+
export interface Fonts {
|
|
12
|
+
regular: string;
|
|
13
|
+
medium?: string;
|
|
14
|
+
bold?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** A theme in one mode: what a component reads. `Theme<typeof theme>` has the tokens of the app too, by name. */
|
|
18
|
+
export type Theme<T extends ThemeTokens = ThemeTokens> = Omit<T, 'color'> & {
|
|
19
|
+
mode: Mode;
|
|
20
|
+
color: T['color']['light'];
|
|
21
|
+
/** Without it, the font of the system. */
|
|
22
|
+
font?: Fonts;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const Context = createContext<Theme | null>(null);
|
|
26
|
+
|
|
27
|
+
export interface ThemeProviderProps {
|
|
28
|
+
/** Default: the base theme. */
|
|
29
|
+
theme?: ThemeTokens;
|
|
30
|
+
/** Default: what the system says. */
|
|
31
|
+
mode?: Mode;
|
|
32
|
+
/** Default: the font of the system. */
|
|
33
|
+
fonts?: Fonts;
|
|
34
|
+
children?: ReactNode;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function ThemeProvider({ theme = themes.base, mode, fonts, children }: ThemeProviderProps) {
|
|
38
|
+
const system = useColorScheme();
|
|
39
|
+
const now: Mode = mode ?? (system === 'dark' ? 'dark' : 'light');
|
|
40
|
+
const value = useMemo<Theme>(() => ({ ...theme, mode: now, color: theme.color[now], font: fonts }), [theme, now, fonts?.regular, fonts?.medium, fonts?.bold]);
|
|
41
|
+
return <Context.Provider value={value}>{children}</Context.Provider>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The theme, in the mode it is in. `useTheme<typeof theme>()` gives the app its own tokens (`app.…`), checked by name. */
|
|
45
|
+
export function useTheme<T extends ThemeTokens = ThemeTokens>(): Theme<T> {
|
|
46
|
+
const theme = useContext(Context);
|
|
47
|
+
if (!theme) throw new Error('@fluxus-ui/native: no theme. Wrap the app in <ThemeProvider>.');
|
|
48
|
+
return theme as Theme<T>;
|
|
49
|
+
}
|