@trama-ui/react-native 0.0.0-stage → 0.7.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 +198 -2
- package/dist/accordion.d.ts +29 -0
- package/dist/accordion.js +137 -0
- package/dist/alert.d.ts +30 -0
- package/dist/alert.js +95 -0
- package/dist/avatar.d.ts +36 -0
- package/dist/avatar.js +91 -0
- package/dist/bars.d.ts +44 -0
- package/dist/bars.js +74 -0
- package/dist/button.d.ts +30 -0
- package/dist/button.js +90 -0
- package/dist/calendar.d.ts +71 -0
- package/dist/calendar.js +276 -0
- package/dist/card.d.ts +41 -0
- package/dist/card.js +139 -0
- package/dist/choices.d.ts +87 -0
- package/dist/choices.js +228 -0
- package/dist/collections.d.ts +131 -0
- package/dist/collections.js +230 -0
- package/dist/color-code.d.ts +37 -0
- package/dist/color-code.js +158 -0
- package/dist/compositions.d.ts +110 -0
- package/dist/compositions.js +190 -0
- package/dist/controls.d.ts +85 -0
- package/dist/controls.js +221 -0
- package/dist/drag-drop.d.ts +65 -0
- package/dist/drag-drop.js +482 -0
- package/dist/feedback.d.ts +30 -0
- package/dist/feedback.js +81 -0
- package/dist/file-upload.d.ts +67 -0
- package/dist/file-upload.js +251 -0
- package/dist/generated/icons.d.ts +1037 -0
- package/dist/generated/icons.js +3 -0
- package/dist/generated/tokens.d.ts +64 -0
- package/dist/generated/tokens.js +65 -0
- package/dist/icon.d.ts +31 -0
- package/dist/icon.js +28 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.js +43 -0
- package/dist/input.d.ts +43 -0
- package/dist/input.js +115 -0
- package/dist/interactive-layout.d.ts +26 -0
- package/dist/interactive-layout.js +117 -0
- package/dist/internal/content.d.ts +17 -0
- package/dist/internal/content.js +24 -0
- package/dist/internal/drag-geometry.d.ts +16 -0
- package/dist/internal/drag-geometry.js +25 -0
- package/dist/internal/keyboard.d.ts +20 -0
- package/dist/internal/keyboard.js +101 -0
- package/dist/internal/motion.d.ts +1 -0
- package/dist/internal/motion.js +24 -0
- package/dist/internal/number-format.d.ts +4 -0
- package/dist/internal/number-format.js +25 -0
- package/dist/internal/pointer-capture.d.ts +1 -0
- package/dist/internal/pointer-capture.js +16 -0
- package/dist/internal/state.d.ts +6 -0
- package/dist/internal/state.js +28 -0
- package/dist/internal/system-drag.d.ts +48 -0
- package/dist/internal/system-drag.js +93 -0
- package/dist/internal/table-drag.d.ts +56 -0
- package/dist/internal/table-drag.js +237 -0
- package/dist/internal/table-editing.d.ts +49 -0
- package/dist/internal/table-editing.js +278 -0
- package/dist/internal/table-editor.d.ts +11 -0
- package/dist/internal/table-editor.js +31 -0
- package/dist/internal/table-grid.d.ts +44 -0
- package/dist/internal/table-grid.js +153 -0
- package/dist/internal/table-model.d.ts +132 -0
- package/dist/internal/table-model.js +231 -0
- package/dist/internal/table-source.d.ts +9 -0
- package/dist/internal/table-source.js +80 -0
- package/dist/internal/tour-target.d.ts +23 -0
- package/dist/internal/tour-target.js +81 -0
- package/dist/keyboard.d.ts +14 -0
- package/dist/keyboard.js +27 -0
- package/dist/layout.d.ts +35 -0
- package/dist/layout.js +77 -0
- package/dist/loader.d.ts +18 -0
- package/dist/loader.js +79 -0
- package/dist/localization.d.ts +106 -0
- package/dist/localization.js +310 -0
- package/dist/media.d.ts +35 -0
- package/dist/media.js +96 -0
- package/dist/menus.d.ts +103 -0
- package/dist/menus.js +422 -0
- package/dist/navigation.d.ts +111 -0
- package/dist/navigation.js +255 -0
- package/dist/overlays.d.ts +97 -0
- package/dist/overlays.js +364 -0
- package/dist/pill.d.ts +33 -0
- package/dist/pill.js +90 -0
- package/dist/progress.d.ts +27 -0
- package/dist/progress.js +154 -0
- package/dist/responsive.d.ts +15 -0
- package/dist/responsive.js +27 -0
- package/dist/select.d.ts +74 -0
- package/dist/select.js +181 -0
- package/dist/separator.d.ts +13 -0
- package/dist/separator.js +34 -0
- package/dist/switch.d.ts +26 -0
- package/dist/switch.js +69 -0
- package/dist/table.d.ts +122 -0
- package/dist/table.js +928 -0
- package/dist/text-fields.d.ts +39 -0
- package/dist/text-fields.js +134 -0
- package/dist/theme.d.ts +47 -0
- package/dist/theme.js +71 -0
- package/dist/transient.d.ts +80 -0
- package/dist/transient.js +273 -0
- package/package.json +50 -4
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type InputHandle, type InputProps } from './input.js';
|
|
2
|
+
import type { TramaResponsive } from './responsive.js';
|
|
3
|
+
export { InputLabel as TextareaLabel, InputDescription as TextareaDescription, InputHint as TextareaHint, InputLabel as NumberFieldLabel, InputDescription as NumberFieldDescription, InputHint as NumberFieldHint, InputStart as NumberFieldStart, InputEnd as NumberFieldEnd, } from './input.js';
|
|
4
|
+
export interface TextareaProps extends InputProps {
|
|
5
|
+
rows?: TramaResponsive<number>;
|
|
6
|
+
minRows?: TramaResponsive<number>;
|
|
7
|
+
maxRows?: TramaResponsive<number>;
|
|
8
|
+
resize?: TramaResponsive<'none' | 'auto'>;
|
|
9
|
+
showCount?: boolean;
|
|
10
|
+
countMode?: 'characters' | 'remaining' | 'words';
|
|
11
|
+
}
|
|
12
|
+
export declare const Textarea: import("react").ForwardRefExoticComponent<TextareaProps & import("react").RefAttributes<InputHandle>>;
|
|
13
|
+
export interface NumberFieldProps extends Omit<InputProps, 'value' | 'defaultValue' | 'onChangeText'> {
|
|
14
|
+
value?: number | null;
|
|
15
|
+
defaultValue?: number | null;
|
|
16
|
+
onValueChange?: (value: number | null) => void;
|
|
17
|
+
min?: number;
|
|
18
|
+
max?: number;
|
|
19
|
+
step?: number;
|
|
20
|
+
locale?: string;
|
|
21
|
+
controls?: TramaResponsive<'stacked' | 'split' | 'none'>;
|
|
22
|
+
clampOnBlur?: boolean;
|
|
23
|
+
}
|
|
24
|
+
export declare const NumberField: import("react").ForwardRefExoticComponent<NumberFieldProps & import("react").RefAttributes<InputHandle>>;
|
|
25
|
+
export interface PinInputProps extends Omit<InputProps, 'maxLength' | 'minLength' | 'secureTextEntry'> {
|
|
26
|
+
length?: number;
|
|
27
|
+
type?: 'numeric' | 'alphanumeric';
|
|
28
|
+
mask?: boolean;
|
|
29
|
+
onComplete?: (value: string) => void;
|
|
30
|
+
}
|
|
31
|
+
export declare const PinInput: import("react").ForwardRefExoticComponent<PinInputProps & import("react").RefAttributes<InputHandle>>;
|
|
32
|
+
export interface TimeFieldProps extends Omit<InputProps, 'onChangeText' | 'maxLength' | 'minLength'> {
|
|
33
|
+
granularity?: 'minute' | 'second';
|
|
34
|
+
min?: string;
|
|
35
|
+
max?: string;
|
|
36
|
+
step?: number;
|
|
37
|
+
onValueChange?: (value: string) => void;
|
|
38
|
+
}
|
|
39
|
+
export declare const TimeField: import("react").ForwardRefExoticComponent<TimeFieldProps & import("react").RefAttributes<InputHandle>>;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { forwardRef, useMemo, useState } from 'react';
|
|
3
|
+
import { Text, View } from 'react-native';
|
|
4
|
+
import { Button } from './button.js';
|
|
5
|
+
import { Input, InputActions, TextEntry } from './input.js';
|
|
6
|
+
import { numberInputFormat } from './internal/number-format.js';
|
|
7
|
+
import { useControllableValue } from './internal/state.js';
|
|
8
|
+
import { useTramaMessages } from './localization.js';
|
|
9
|
+
import { useTramaAppearance, useTramaTheme } from './theme.js';
|
|
10
|
+
export { InputLabel as TextareaLabel, InputDescription as TextareaDescription, InputHint as TextareaHint, InputLabel as NumberFieldLabel, InputDescription as NumberFieldDescription, InputHint as NumberFieldHint, InputStart as NumberFieldStart, InputEnd as NumberFieldEnd, } from './input.js';
|
|
11
|
+
function rowCount(rows, fallback) {
|
|
12
|
+
return Number.isFinite(rows) && rows > 0 ? Math.min(100, Math.floor(rows)) : fallback;
|
|
13
|
+
}
|
|
14
|
+
export const Textarea = forwardRef(function Textarea({ rows, minRows, maxRows, resize, showCount, countMode = 'characters', value, defaultValue, onChangeText, style, onContentSizeChange, maxLength, ...props }, ref) {
|
|
15
|
+
const { theme, resolve } = useTramaAppearance();
|
|
16
|
+
const [current, request] = useControllableValue(value, defaultValue ?? '', onChangeText);
|
|
17
|
+
const [measured, setMeasured] = useState(0);
|
|
18
|
+
const minimum = rowCount(resolve(minRows ?? rows, 4), 4);
|
|
19
|
+
const maximum = Math.max(minimum, rowCount(resolve(maxRows, 10), 10));
|
|
20
|
+
const line = Math.ceil(theme.fontSize * theme.lineHeight);
|
|
21
|
+
const padding = theme.space * 4 + 2;
|
|
22
|
+
const minHeight = minimum * line + padding;
|
|
23
|
+
const maxHeight = maximum * line + padding;
|
|
24
|
+
const grow = resolve(resize, 'none') === 'auto';
|
|
25
|
+
const height = grow ? Math.min(maxHeight, Math.max(minHeight, measured + padding)) : minHeight;
|
|
26
|
+
const count = countMode === 'words'
|
|
27
|
+
? current.trim().split(/\s+/u).filter(Boolean).length
|
|
28
|
+
: countMode === 'remaining' && maxLength !== undefined
|
|
29
|
+
? Math.max(0, maxLength - current.length)
|
|
30
|
+
: current.length;
|
|
31
|
+
return (_jsxs(View, { style: { gap: theme.space }, children: [_jsx(TextEntry, { ...props, ref: ref, multiline: true, value: current, maxLength: maxLength, onChangeText: request, onContentSizeChange: (event) => {
|
|
32
|
+
setMeasured(event.nativeEvent.contentSize.height);
|
|
33
|
+
onContentSizeChange?.(event);
|
|
34
|
+
}, style: [{ minHeight, height }, style] }), showCount && (_jsx(Text, { style: { color: theme.colors.muted, fontFamily: theme.fontFamily, alignSelf: 'flex-end' }, children: countMode === 'characters' && maxLength !== undefined ? `${count}/${maxLength}` : count }))] }));
|
|
35
|
+
});
|
|
36
|
+
export const NumberField = forwardRef(function NumberField({ value, defaultValue, onValueChange, min, max, step = 1, locale, controls, clampOnBlur, children, onBlur, ...props }, ref) {
|
|
37
|
+
const [current, request] = useControllableValue(value, defaultValue ?? null, onValueChange);
|
|
38
|
+
const [draft, setDraft] = useState(null);
|
|
39
|
+
const messages = useTramaMessages();
|
|
40
|
+
const { resolve } = useTramaAppearance();
|
|
41
|
+
const { format, parse } = useMemo(() => numberInputFormat(locale), [locale]);
|
|
42
|
+
const lower = min !== undefined && Number.isFinite(min) ? min : -Infinity;
|
|
43
|
+
const upper = max !== undefined && Number.isFinite(max) ? Math.max(lower, max) : Infinity;
|
|
44
|
+
const increment = Number.isFinite(step) && step > 0 ? step : 1;
|
|
45
|
+
const valueText = draft ?? (current === null || !Number.isFinite(current) ? '' : format.format(current));
|
|
46
|
+
const shown = resolve(controls, 'split');
|
|
47
|
+
function adjust(amount) {
|
|
48
|
+
const parsed = parse(valueText) ?? current ?? (Number.isFinite(lower) ? lower : 0);
|
|
49
|
+
setDraft(null);
|
|
50
|
+
request(Math.min(upper, Math.max(lower, Number((parsed + amount).toPrecision(14)))));
|
|
51
|
+
}
|
|
52
|
+
const decrease = (_jsx(Button, { variant: "text", disabled: props.disabled || props.readOnly || (current !== null && current <= lower), accessibilityLabel: messages.decreaseValue, onPress: () => adjust(-increment), children: "\u2212" }));
|
|
53
|
+
const increase = (_jsx(Button, { variant: "text", disabled: props.disabled || props.readOnly || (current !== null && current >= upper), accessibilityLabel: messages.increaseValue, onPress: () => adjust(increment), children: "+" }));
|
|
54
|
+
return (_jsxs(TextEntry, { ...props, ref: ref, inputMode: props.inputMode ?? 'decimal', value: valueText, onChangeText: (text) => {
|
|
55
|
+
setDraft(text);
|
|
56
|
+
if (!text.trim())
|
|
57
|
+
request(null);
|
|
58
|
+
else {
|
|
59
|
+
const parsed = parse(text);
|
|
60
|
+
if (parsed !== undefined)
|
|
61
|
+
request(parsed);
|
|
62
|
+
}
|
|
63
|
+
}, onBlur: (event) => {
|
|
64
|
+
const parsed = parse(valueText);
|
|
65
|
+
if (parsed !== undefined && clampOnBlur) {
|
|
66
|
+
request(Math.min(upper, Math.max(lower, parsed)));
|
|
67
|
+
setDraft(null);
|
|
68
|
+
}
|
|
69
|
+
else if (!valueText.trim() || parsed !== undefined)
|
|
70
|
+
setDraft(null);
|
|
71
|
+
onBlur?.(event);
|
|
72
|
+
}, validateValue: (text) => {
|
|
73
|
+
if (!text.trim())
|
|
74
|
+
return '';
|
|
75
|
+
const parsed = parse(text);
|
|
76
|
+
if (parsed === undefined)
|
|
77
|
+
return messages.enterNumber;
|
|
78
|
+
if (parsed < lower)
|
|
79
|
+
return messages.minNumber(lower);
|
|
80
|
+
if (parsed > upper)
|
|
81
|
+
return messages.maxNumber(upper);
|
|
82
|
+
return '';
|
|
83
|
+
}, children: [children, shown !== 'none' && (_jsxs(InputActions, { style: shown === 'stacked' ? { flexDirection: 'column' } : undefined, children: [decrease, increase] }))] }));
|
|
84
|
+
});
|
|
85
|
+
export const PinInput = forwardRef(function PinInput({ length = 6, type = 'numeric', mask, value, defaultValue, onChangeText, onComplete, style, ...props }, ref) {
|
|
86
|
+
const digits = Number.isFinite(length) ? Math.max(1, Math.min(32, Math.floor(length))) : 6;
|
|
87
|
+
const sanitize = (text) => text.replace(type === 'numeric' ? /[^0-9]/gu : /[^a-z0-9]/giu, '').slice(0, digits);
|
|
88
|
+
const [current, request] = useControllableValue(value === undefined ? undefined : sanitize(value), sanitize(defaultValue ?? ''), onChangeText);
|
|
89
|
+
const theme = useTramaTheme();
|
|
90
|
+
return (_jsx(Input, { ...props, ref: ref, inputMode: props.inputMode ?? (type === 'numeric' ? 'numeric' : 'text'), textContentType: props.textContentType ?? 'oneTimeCode', autoComplete: props.autoComplete ?? 'sms-otp', value: current, minLength: digits, maxLength: digits, secureTextEntry: mask, onChangeText: (raw) => {
|
|
91
|
+
const next = sanitize(raw);
|
|
92
|
+
if (next === current)
|
|
93
|
+
return;
|
|
94
|
+
request(next);
|
|
95
|
+
if (next.length === digits)
|
|
96
|
+
onComplete?.(next);
|
|
97
|
+
}, style: [{ letterSpacing: theme.space * 2, textAlign: 'center' }, style] }));
|
|
98
|
+
});
|
|
99
|
+
function seconds(time) {
|
|
100
|
+
if (!/^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/u.test(time))
|
|
101
|
+
return undefined;
|
|
102
|
+
const [hour, minute, second = 0] = time.split(':').map(Number);
|
|
103
|
+
return hour * 3600 + minute * 60 + second;
|
|
104
|
+
}
|
|
105
|
+
export const TimeField = forwardRef(function TimeField({ granularity = 'minute', min, max, step, value, defaultValue, onValueChange, onBlur, ...props }, ref) {
|
|
106
|
+
const [current, request] = useControllableValue(value, defaultValue ?? '', onValueChange);
|
|
107
|
+
const [draft, setDraft] = useState(null);
|
|
108
|
+
const messages = useTramaMessages();
|
|
109
|
+
const precision = granularity === 'second' ? 6 : 4;
|
|
110
|
+
const length = precision + (granularity === 'second' ? 2 : 1);
|
|
111
|
+
const lower = min ? seconds(min) : undefined;
|
|
112
|
+
const upper = max ? seconds(max) : undefined;
|
|
113
|
+
function diagnose(text) {
|
|
114
|
+
if (!text)
|
|
115
|
+
return '';
|
|
116
|
+
const parsed = seconds(text);
|
|
117
|
+
if (parsed === undefined || text.length !== length)
|
|
118
|
+
return messages.enterTime;
|
|
119
|
+
if ((lower !== undefined && parsed < lower) || (upper !== undefined && parsed > upper))
|
|
120
|
+
return messages.reviewField;
|
|
121
|
+
return step && step > 0 && (parsed - (lower ?? 0)) % step !== 0 ? messages.reviewField : '';
|
|
122
|
+
}
|
|
123
|
+
return (_jsx(TextEntry, { ...props, ref: ref, value: draft ?? current, maxLength: length, inputMode: props.inputMode ?? 'numeric', placeholder: props.placeholder ?? (precision === 6 ? 'HH:MM:SS' : 'HH:MM'), onChangeText: (text) => {
|
|
124
|
+
const digits = text.replace(/[^0-9]/gu, '').slice(0, precision);
|
|
125
|
+
const next = digits.match(/.{1,2}/gu)?.join(':') ?? '';
|
|
126
|
+
setDraft(next);
|
|
127
|
+
if (!diagnose(next))
|
|
128
|
+
request(next);
|
|
129
|
+
}, onBlur: (event) => {
|
|
130
|
+
if (draft !== null && !diagnose(draft))
|
|
131
|
+
setDraft(null);
|
|
132
|
+
onBlur?.(event);
|
|
133
|
+
}, validateValue: diagnose }));
|
|
134
|
+
});
|
package/dist/theme.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import { tokens } from './generated/tokens.js';
|
|
3
|
+
import { type TramaBreakpoints, type TramaResponsive } from './responsive.js';
|
|
4
|
+
export type TramaThemeMode = 'light' | 'dark' | 'system';
|
|
5
|
+
export type TramaDirection = 'ltr' | 'rtl';
|
|
6
|
+
export type TramaColor = 'accent' | 'neutral' | 'green' | 'yellow' | 'red';
|
|
7
|
+
export type TramaSize = 'sm' | 'md' | 'lg';
|
|
8
|
+
export type TramaShape = 'rounded' | 'pill' | 'square';
|
|
9
|
+
type Palette = Record<keyof typeof tokens.palettes.light, string>;
|
|
10
|
+
export interface TramaTheme {
|
|
11
|
+
colors: Palette;
|
|
12
|
+
space: number;
|
|
13
|
+
radius: number;
|
|
14
|
+
controlHeight: number;
|
|
15
|
+
fontSize: number;
|
|
16
|
+
lineHeight: number;
|
|
17
|
+
fontFamily?: string;
|
|
18
|
+
}
|
|
19
|
+
export type TramaThemeOverrides = Partial<Omit<TramaTheme, 'colors'>> & {
|
|
20
|
+
colors?: Partial<Palette>;
|
|
21
|
+
};
|
|
22
|
+
export declare const tramaNativeThemes: Readonly<Record<'light' | 'dark', Readonly<TramaTheme>>>;
|
|
23
|
+
export interface ThemeProviderProps {
|
|
24
|
+
children?: ReactNode;
|
|
25
|
+
mode?: TramaThemeMode;
|
|
26
|
+
theme?: TramaThemeOverrides;
|
|
27
|
+
direction?: TramaDirection;
|
|
28
|
+
breakpoints?: Partial<TramaBreakpoints>;
|
|
29
|
+
/** Supply a measured native layout width to resolve nested presentation values. */
|
|
30
|
+
width?: number;
|
|
31
|
+
}
|
|
32
|
+
export declare function ThemeProvider({ children, mode, theme, direction, breakpoints, width }: ThemeProviderProps): import("react").JSX.Element;
|
|
33
|
+
export declare function useTramaTheme(): Readonly<TramaTheme>;
|
|
34
|
+
export declare function useTramaAppearance(): {
|
|
35
|
+
readonly theme: Readonly<TramaTheme>;
|
|
36
|
+
readonly direction: TramaDirection;
|
|
37
|
+
readonly resolve: <T extends string | number | boolean>(value: TramaResponsive<T> | undefined, fallback: T) => T;
|
|
38
|
+
};
|
|
39
|
+
export declare function getTone(theme: Readonly<TramaTheme>, color: TramaColor): {
|
|
40
|
+
solid: string;
|
|
41
|
+
ink: string;
|
|
42
|
+
soft: string;
|
|
43
|
+
text: string;
|
|
44
|
+
};
|
|
45
|
+
export declare const sizeStep: Record<TramaSize, number>;
|
|
46
|
+
export declare function shapeRadius(theme: Readonly<TramaTheme>, shape: TramaShape): number;
|
|
47
|
+
export {};
|
package/dist/theme.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useContext, useMemo } from 'react';
|
|
3
|
+
import { I18nManager, useColorScheme, useWindowDimensions } from 'react-native';
|
|
4
|
+
import { tokens } from './generated/tokens.js';
|
|
5
|
+
import { defaultTramaBreakpoints, resolveBreakpoints, resolveResponsive, } from './responsive.js';
|
|
6
|
+
export const tramaNativeThemes = {
|
|
7
|
+
light: createTheme(tokens.palettes.light),
|
|
8
|
+
dark: createTheme(tokens.palettes.dark),
|
|
9
|
+
};
|
|
10
|
+
function createTheme(colors) {
|
|
11
|
+
return Object.freeze({
|
|
12
|
+
colors: Object.freeze(colors),
|
|
13
|
+
space: tokens.space,
|
|
14
|
+
radius: tokens.radius,
|
|
15
|
+
controlHeight: tokens.controlHeight,
|
|
16
|
+
fontSize: tokens.fontSize,
|
|
17
|
+
lineHeight: tokens.lineHeight,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
const ThemeContext = createContext({
|
|
21
|
+
theme: tramaNativeThemes.light,
|
|
22
|
+
mode: 'light',
|
|
23
|
+
breakpoints: defaultTramaBreakpoints,
|
|
24
|
+
});
|
|
25
|
+
export function ThemeProvider({ children, mode, theme, direction, breakpoints, width }) {
|
|
26
|
+
const parent = useContext(ThemeContext);
|
|
27
|
+
const systemMode = useColorScheme();
|
|
28
|
+
const palette = mode === 'system' ? (systemMode === 'dark' ? 'dark' : 'light') : (mode ?? parent.mode);
|
|
29
|
+
const context = useMemo(() => {
|
|
30
|
+
const base = palette === parent.mode ? parent.theme : tramaNativeThemes[palette];
|
|
31
|
+
return {
|
|
32
|
+
theme: theme ? { ...base, ...theme, colors: { ...base.colors, ...theme.colors } } : base,
|
|
33
|
+
mode: palette,
|
|
34
|
+
direction: direction ?? parent.direction,
|
|
35
|
+
breakpoints: breakpoints ? resolveBreakpoints(breakpoints, parent.breakpoints) : parent.breakpoints,
|
|
36
|
+
width: width ?? parent.width,
|
|
37
|
+
};
|
|
38
|
+
}, [breakpoints, direction, palette, parent, theme, width]);
|
|
39
|
+
return _jsx(ThemeContext.Provider, { value: context, children: children });
|
|
40
|
+
}
|
|
41
|
+
export function useTramaTheme() {
|
|
42
|
+
return useContext(ThemeContext).theme;
|
|
43
|
+
}
|
|
44
|
+
export function useTramaAppearance() {
|
|
45
|
+
const context = useContext(ThemeContext);
|
|
46
|
+
const { width } = useWindowDimensions();
|
|
47
|
+
return {
|
|
48
|
+
theme: context.theme,
|
|
49
|
+
direction: context.direction ?? (I18nManager.isRTL ? 'rtl' : 'ltr'),
|
|
50
|
+
resolve: (value, fallback) => resolveResponsive(value, fallback, context.width ?? width, context.breakpoints),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
export function getTone(theme, color) {
|
|
54
|
+
const { colors } = theme;
|
|
55
|
+
switch (color) {
|
|
56
|
+
case 'neutral':
|
|
57
|
+
return { solid: colors.neutralSolid, ink: colors.onNeutral, soft: colors.subtle, text: colors.text };
|
|
58
|
+
case 'green':
|
|
59
|
+
return { solid: colors.successSolid, ink: colors.onSuccess, soft: colors.successBg, text: colors.successText };
|
|
60
|
+
case 'yellow':
|
|
61
|
+
return { solid: colors.warningSolid, ink: colors.onWarning, soft: colors.warningBg, text: colors.warningText };
|
|
62
|
+
case 'red':
|
|
63
|
+
return { solid: colors.dangerSolid, ink: colors.onDanger, soft: colors.dangerBg, text: colors.danger };
|
|
64
|
+
default:
|
|
65
|
+
return { solid: colors.accent, ink: colors.onAccent, soft: colors.subtle, text: colors.accent };
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
export const sizeStep = { sm: -1, md: 0, lg: 1 };
|
|
69
|
+
export function shapeRadius(theme, shape) {
|
|
70
|
+
return { square: 0, rounded: theme.radius, pill: 9999 }[shape];
|
|
71
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import { type PressableProps, type ViewProps } from 'react-native';
|
|
3
|
+
import { type AlertProps } from './alert.js';
|
|
4
|
+
import { type ButtonProps } from './button.js';
|
|
5
|
+
import { type TourTargetOptions } from './internal/tour-target.js';
|
|
6
|
+
import { type OverlayHandle, type OverlayStateProps, type PopoverProps } from './overlays.js';
|
|
7
|
+
import type { TramaResponsive } from './responsive.js';
|
|
8
|
+
export type HoverCardProps = PopoverProps;
|
|
9
|
+
export declare const HoverCard: import("react").ForwardRefExoticComponent<PopoverProps & import("react").RefAttributes<OverlayHandle>>;
|
|
10
|
+
export declare function HoverCardTrigger(props: ButtonProps): import("react").JSX.Element;
|
|
11
|
+
export declare function HoverCardArrow(props: ViewProps): import("react").JSX.Element;
|
|
12
|
+
export declare function HoverCardContent({ children, style, ...props }: ViewProps): import("react").JSX.Element;
|
|
13
|
+
export interface TooltipProps extends Omit<PressableProps, 'children'> {
|
|
14
|
+
content: string;
|
|
15
|
+
children?: ReactNode;
|
|
16
|
+
open?: boolean;
|
|
17
|
+
defaultOpen?: boolean;
|
|
18
|
+
onOpenChange?: (open: boolean) => void | false;
|
|
19
|
+
placement?: 'top' | 'bottom';
|
|
20
|
+
delay?: number;
|
|
21
|
+
}
|
|
22
|
+
export declare function Tooltip({ content, children, open, defaultOpen, onOpenChange, placement, delay, disabled, onHoverIn, onHoverOut, onFocus, onBlur, onLongPress, onPress, ...props }: TooltipProps): import("react").JSX.Element;
|
|
23
|
+
export type ToastDismissReason = 'close-button' | 'timeout' | 'programmatic' | 'action';
|
|
24
|
+
export interface ToastProps extends Omit<AlertProps, 'onDismiss'> {
|
|
25
|
+
duration?: number;
|
|
26
|
+
pauseOnInteraction?: boolean;
|
|
27
|
+
onDismiss?: (detail: {
|
|
28
|
+
reason: ToastDismissReason;
|
|
29
|
+
}) => void | false;
|
|
30
|
+
}
|
|
31
|
+
export declare function Toast({ duration, open, defaultOpen, onDismiss, dismissible, announcement, pauseOnInteraction, children, ...props }: ToastProps): import("react").JSX.Element;
|
|
32
|
+
export interface ToastActionProps extends ButtonProps {
|
|
33
|
+
closeOnPress?: boolean;
|
|
34
|
+
}
|
|
35
|
+
export declare function ToastAction({ closeOnPress, onPress, onFocus, onBlur, onHoverIn, onHoverOut, ...props }: ToastActionProps): import("react").JSX.Element;
|
|
36
|
+
export declare function ToastIcon(props: ViewProps): import("react").JSX.Element;
|
|
37
|
+
export interface ToastOptions extends ToastProps {
|
|
38
|
+
id?: string;
|
|
39
|
+
}
|
|
40
|
+
type ToastService = {
|
|
41
|
+
notify(options: ToastOptions): string;
|
|
42
|
+
dismiss(id: string): void;
|
|
43
|
+
clear(): void;
|
|
44
|
+
};
|
|
45
|
+
export interface ToastProviderProps extends ViewProps {
|
|
46
|
+
placement?: 'top-start' | 'top-end' | 'top-center' | 'bottom-start' | 'bottom-end' | 'bottom-center';
|
|
47
|
+
maxVisible?: number;
|
|
48
|
+
inset?: number;
|
|
49
|
+
}
|
|
50
|
+
export declare function ToastProvider({ children, placement, maxVisible, inset, style, ...props }: ToastProviderProps): import("react").JSX.Element;
|
|
51
|
+
export declare function useToast(): ToastService;
|
|
52
|
+
export type { TourTarget } from './internal/tour-target.js';
|
|
53
|
+
export type TourPlacement = 'auto' | 'top' | 'center' | 'bottom';
|
|
54
|
+
export type TourBackdrop = 'spotlight' | 'dim' | 'none';
|
|
55
|
+
export interface TourStepProps extends ViewProps, TourTargetOptions {
|
|
56
|
+
heading: string;
|
|
57
|
+
description?: string;
|
|
58
|
+
onEnter?: () => void;
|
|
59
|
+
nextLabel?: string;
|
|
60
|
+
placement?: TramaResponsive<TourPlacement>;
|
|
61
|
+
backdrop?: TramaResponsive<TourBackdrop>;
|
|
62
|
+
}
|
|
63
|
+
export declare function TourStep({ value: _value, heading: _heading, description: _description, onEnter: _onEnter, nextLabel: _nextLabel, target: _target, prepareTarget: _prepareTarget, placement: _placement, backdrop: _backdrop, ...props }: TourStepProps): import("react").JSX.Element;
|
|
64
|
+
export interface TourProps extends ViewProps, OverlayStateProps {
|
|
65
|
+
label?: string;
|
|
66
|
+
value?: string;
|
|
67
|
+
defaultValue?: string;
|
|
68
|
+
onValueChange?: (value: string) => void | false;
|
|
69
|
+
onFinish?: () => void;
|
|
70
|
+
onSkip?: () => void;
|
|
71
|
+
trigger?: ReactNode;
|
|
72
|
+
skippable?: boolean;
|
|
73
|
+
placement?: TramaResponsive<TourPlacement>;
|
|
74
|
+
backdrop?: TramaResponsive<TourBackdrop>;
|
|
75
|
+
backdropColor?: string;
|
|
76
|
+
targetGap?: number;
|
|
77
|
+
onTargetUnavailable?: (step: string) => void;
|
|
78
|
+
renderIndicator?: (position: number, total: number) => ReactNode;
|
|
79
|
+
}
|
|
80
|
+
export declare const Tour: import("react").ForwardRefExoticComponent<TourProps & import("react").RefAttributes<OverlayHandle>>;
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Children, createContext, forwardRef, isValidElement, useCallback, useContext, useEffect, useImperativeHandle, useMemo, useRef, useState, } from 'react';
|
|
3
|
+
import { AppState, Pressable, Text, useWindowDimensions, View, } from 'react-native';
|
|
4
|
+
import { Alert } from './alert.js';
|
|
5
|
+
import { Button } from './button.js';
|
|
6
|
+
import { splitContent, TextContent } from './internal/content.js';
|
|
7
|
+
import { useReducedMotion } from './internal/motion.js';
|
|
8
|
+
import { useControllableValue } from './internal/state.js';
|
|
9
|
+
import { TourMask, useTourTarget } from './internal/tour-target.js';
|
|
10
|
+
import { Cluster } from './layout.js';
|
|
11
|
+
import { useTramaMessages } from './localization.js';
|
|
12
|
+
import { Dialog, DialogBackdropLayer, DialogFooter, DialogTrigger, Popover, PopoverArrow, PopoverTrigger, } from './overlays.js';
|
|
13
|
+
import { useTramaAppearance, useTramaTheme } from './theme.js';
|
|
14
|
+
export const HoverCard = forwardRef(function HoverCard({ children, ...props }, ref) {
|
|
15
|
+
const { body, named } = splitContent(children, { trigger: HoverCardTrigger, arrow: HoverCardArrow });
|
|
16
|
+
const trigger = named.trigger?.[0];
|
|
17
|
+
const arrow = named.arrow?.[0];
|
|
18
|
+
return (_jsxs(Popover, { ...props, ref: ref, children: [isValidElement(trigger) && (_jsx(PopoverTrigger, { variant: "text", ...trigger.props, disabled: props.disabled || trigger.props.disabled })), isValidElement(arrow) && _jsx(PopoverArrow, { ...arrow.props }), body] }));
|
|
19
|
+
});
|
|
20
|
+
export function HoverCardTrigger(props) {
|
|
21
|
+
return _jsx(Button, { variant: "text", ...props });
|
|
22
|
+
}
|
|
23
|
+
export function HoverCardArrow(props) {
|
|
24
|
+
return _jsx(PopoverArrow, { ...props });
|
|
25
|
+
}
|
|
26
|
+
export function HoverCardContent({ children, style, ...props }) {
|
|
27
|
+
const theme = useTramaTheme();
|
|
28
|
+
return (_jsx(View, { ...props, style: [{ gap: theme.space * 2 }, style], children: _jsx(TextContent, { style: { color: theme.colors.text }, children: children }) }));
|
|
29
|
+
}
|
|
30
|
+
export function Tooltip({ content, children, open, defaultOpen, onOpenChange, placement = 'top', delay = 300, disabled, onHoverIn, onHoverOut, onFocus, onBlur, onLongPress, onPress, ...props }) {
|
|
31
|
+
const { theme, direction } = useTramaAppearance();
|
|
32
|
+
const screen = useWindowDimensions();
|
|
33
|
+
const anchor = useRef(null);
|
|
34
|
+
const [visible, setOpen] = useControllableValue(open, defaultOpen ?? false);
|
|
35
|
+
const [offset, setOffset] = useState(0);
|
|
36
|
+
const [below, setBelow] = useState(placement === 'bottom');
|
|
37
|
+
const boxWidth = Math.min(220, Math.max(0, screen.width - theme.space * 4));
|
|
38
|
+
const measure = useCallback(() => {
|
|
39
|
+
anchor.current?.measureInWindow((left, top, width, height) => {
|
|
40
|
+
const desired = direction === 'rtl' ? left + width - boxWidth : left;
|
|
41
|
+
const fitted = Math.min(screen.width - boxWidth - theme.space * 2, Math.max(theme.space * 2, desired));
|
|
42
|
+
setOffset(fitted - desired);
|
|
43
|
+
setBelow(placement === 'bottom' ? screen.height - top - height > 92 || top < 92 : top < 92);
|
|
44
|
+
});
|
|
45
|
+
}, [boxWidth, direction, placement, screen.height, screen.width, theme.space]);
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
if (visible)
|
|
48
|
+
measure();
|
|
49
|
+
}, [measure, visible]);
|
|
50
|
+
const timer = useRef(null);
|
|
51
|
+
useEffect(() => () => {
|
|
52
|
+
if (timer.current !== null)
|
|
53
|
+
clearTimeout(timer.current);
|
|
54
|
+
}, []);
|
|
55
|
+
function show(next) {
|
|
56
|
+
if (timer.current !== null) {
|
|
57
|
+
clearTimeout(timer.current);
|
|
58
|
+
timer.current = null;
|
|
59
|
+
}
|
|
60
|
+
if (!disabled && next !== visible && onOpenChange?.(next) !== false)
|
|
61
|
+
setOpen(next);
|
|
62
|
+
}
|
|
63
|
+
return (_jsxs(View, { ref: anchor, collapsable: false, onLayout: () => {
|
|
64
|
+
if (visible)
|
|
65
|
+
measure();
|
|
66
|
+
}, style: { alignSelf: 'flex-start', direction, zIndex: visible && !disabled ? 5 : 0 }, children: [_jsx(Pressable, { ...props, disabled: disabled, accessibilityRole: props.accessibilityRole ?? 'button', accessibilityHint: props.accessibilityHint ?? content, onPress: (event) => {
|
|
67
|
+
onPress?.(event);
|
|
68
|
+
show(onPress ? false : !visible);
|
|
69
|
+
}, onHoverIn: (event) => {
|
|
70
|
+
onHoverIn?.(event);
|
|
71
|
+
if (timer.current !== null)
|
|
72
|
+
clearTimeout(timer.current);
|
|
73
|
+
timer.current = setTimeout(() => show(true), Math.max(0, delay));
|
|
74
|
+
}, onHoverOut: (event) => {
|
|
75
|
+
onHoverOut?.(event);
|
|
76
|
+
show(false);
|
|
77
|
+
}, onFocus: (event) => {
|
|
78
|
+
onFocus?.(event);
|
|
79
|
+
show(true);
|
|
80
|
+
}, onBlur: (event) => {
|
|
81
|
+
onBlur?.(event);
|
|
82
|
+
show(false);
|
|
83
|
+
}, onLongPress: (event) => {
|
|
84
|
+
onLongPress?.(event);
|
|
85
|
+
show(true);
|
|
86
|
+
}, children: _jsx(TextContent, { style: { color: theme.colors.text }, children: children }) }), visible && !disabled && (_jsx(View, { accessibilityElementsHidden: true, importantForAccessibility: "no-hide-descendants", pointerEvents: "none", style: {
|
|
87
|
+
position: 'absolute',
|
|
88
|
+
start: 0,
|
|
89
|
+
width: boxWidth,
|
|
90
|
+
top: below ? '100%' : undefined,
|
|
91
|
+
bottom: below ? undefined : '100%',
|
|
92
|
+
marginTop: below ? theme.space : undefined,
|
|
93
|
+
marginBottom: below ? undefined : theme.space,
|
|
94
|
+
transform: [{ translateX: offset }],
|
|
95
|
+
}, children: _jsx(Text, { nativeID: props.nativeID ? props.nativeID + '-description' : undefined, style: {
|
|
96
|
+
alignSelf: 'flex-start',
|
|
97
|
+
color: theme.colors.surface,
|
|
98
|
+
backgroundColor: theme.colors.text,
|
|
99
|
+
borderRadius: theme.radius / 2,
|
|
100
|
+
padding: theme.space * 2,
|
|
101
|
+
fontSize: theme.fontSize - 2,
|
|
102
|
+
}, children: content }) }))] }));
|
|
103
|
+
}
|
|
104
|
+
const ToastContext = createContext(null);
|
|
105
|
+
export function Toast({ duration = 5000, open, defaultOpen, onDismiss, dismissible = true, announcement = 'polite', pauseOnInteraction = true, children, ...props }) {
|
|
106
|
+
const [visible, change] = useControllableValue(open, defaultOpen ?? true);
|
|
107
|
+
const { named, body } = splitContent(children, { icon: ToastIcon, action: ToastAction });
|
|
108
|
+
const [appActive, setAppActive] = useState(!AppState.currentState || AppState.currentState === 'active');
|
|
109
|
+
const [interacting, setInteracting] = useState(false);
|
|
110
|
+
const remaining = useRef(duration);
|
|
111
|
+
const dismiss = useRef(close);
|
|
112
|
+
dismiss.current = close;
|
|
113
|
+
useEffect(() => {
|
|
114
|
+
const subscription = AppState.addEventListener('change', (state) => setAppActive(state === 'active'));
|
|
115
|
+
return () => subscription.remove();
|
|
116
|
+
}, []);
|
|
117
|
+
useEffect(() => {
|
|
118
|
+
remaining.current = duration;
|
|
119
|
+
}, [duration, visible]);
|
|
120
|
+
useEffect(() => {
|
|
121
|
+
if (!visible || !appActive || (interacting && pauseOnInteraction) || duration <= 0 || !Number.isFinite(duration))
|
|
122
|
+
return;
|
|
123
|
+
const started = Date.now();
|
|
124
|
+
const timer = setTimeout(() => dismiss.current('timeout'), Math.max(0, remaining.current));
|
|
125
|
+
return () => {
|
|
126
|
+
clearTimeout(timer);
|
|
127
|
+
remaining.current -= Date.now() - started;
|
|
128
|
+
};
|
|
129
|
+
}, [visible, appActive, interacting, pauseOnInteraction, duration]);
|
|
130
|
+
function close(reason) {
|
|
131
|
+
if (onDismiss?.({ reason }) !== false)
|
|
132
|
+
change(false);
|
|
133
|
+
}
|
|
134
|
+
return (_jsx(ToastContext.Provider, { value: { close, pause: setInteracting }, children: _jsxs(Alert, { ...props, open: visible, dismissible: dismissible, announcement: announcement, icon: named.icon ?? props.icon, onDismiss: () => {
|
|
135
|
+
close('close-button');
|
|
136
|
+
return false;
|
|
137
|
+
}, children: [body, named.action && (_jsx(Cluster, { gap: "xs", onTouchStart: () => setInteracting(true), onTouchCancel: () => setInteracting(false), onTouchEnd: () => setInteracting(false), children: named.action }))] }) }));
|
|
138
|
+
}
|
|
139
|
+
export function ToastAction({ closeOnPress = true, onPress, onFocus, onBlur, onHoverIn, onHoverOut, ...props }) {
|
|
140
|
+
const state = useContext(ToastContext);
|
|
141
|
+
return (_jsx(Button, { variant: "text", size: "sm", ...props, onPress: (event) => {
|
|
142
|
+
onPress?.(event);
|
|
143
|
+
if (closeOnPress)
|
|
144
|
+
state?.close('action');
|
|
145
|
+
}, onFocus: (event) => {
|
|
146
|
+
state?.pause(true);
|
|
147
|
+
onFocus?.(event);
|
|
148
|
+
}, onBlur: (event) => {
|
|
149
|
+
state?.pause(false);
|
|
150
|
+
onBlur?.(event);
|
|
151
|
+
}, onHoverIn: (event) => {
|
|
152
|
+
state?.pause(true);
|
|
153
|
+
onHoverIn?.(event);
|
|
154
|
+
}, onHoverOut: (event) => {
|
|
155
|
+
state?.pause(false);
|
|
156
|
+
onHoverOut?.(event);
|
|
157
|
+
} }));
|
|
158
|
+
}
|
|
159
|
+
export function ToastIcon(props) {
|
|
160
|
+
return _jsx(View, { ...props });
|
|
161
|
+
}
|
|
162
|
+
const ToastServiceContext = createContext(null);
|
|
163
|
+
export function ToastProvider({ children, placement = 'bottom-end', maxVisible = 3, inset, style, ...props }) {
|
|
164
|
+
const { theme, direction } = useTramaAppearance();
|
|
165
|
+
const [queue, setQueue] = useState([]);
|
|
166
|
+
const generation = useRef(0);
|
|
167
|
+
const service = useMemo(() => ({
|
|
168
|
+
notify(options) {
|
|
169
|
+
const id = options.id ?? 'toast-' + ++generation.current;
|
|
170
|
+
setQueue((items) => items.some((entry) => entry.id === id)
|
|
171
|
+
? items.map((entry) => (entry.id === id ? { ...options, id } : entry))
|
|
172
|
+
: [...items, { ...options, id }]);
|
|
173
|
+
return id;
|
|
174
|
+
},
|
|
175
|
+
dismiss(id) {
|
|
176
|
+
setQueue((items) => items.filter((entry) => entry.id !== id));
|
|
177
|
+
},
|
|
178
|
+
clear() {
|
|
179
|
+
setQueue([]);
|
|
180
|
+
},
|
|
181
|
+
}), []);
|
|
182
|
+
const offset = inset ?? theme.space * 3;
|
|
183
|
+
const top = placement.startsWith('top');
|
|
184
|
+
const middle = placement.endsWith('center');
|
|
185
|
+
return (_jsx(ToastServiceContext.Provider, { value: service, children: _jsxs(View, { ...props, style: [{ direction, flex: 1 }, style], children: [children, queue.length > 0 && (_jsx(View, { pointerEvents: "box-none", style: {
|
|
186
|
+
position: 'absolute',
|
|
187
|
+
zIndex: 50,
|
|
188
|
+
top: top ? offset : undefined,
|
|
189
|
+
bottom: top ? undefined : offset,
|
|
190
|
+
start: placement.endsWith('start') || middle ? offset : undefined,
|
|
191
|
+
end: placement.endsWith('end') || middle ? offset : undefined,
|
|
192
|
+
width: middle ? undefined : '90%',
|
|
193
|
+
alignSelf: middle ? 'center' : undefined,
|
|
194
|
+
maxWidth: 420,
|
|
195
|
+
gap: theme.space * 2,
|
|
196
|
+
}, children: queue.slice(0, Math.max(1, maxVisible)).map(({ id, onDismiss, ...options }) => (_jsx(Toast, { ...options, open: true, onDismiss: (detail) => {
|
|
197
|
+
if (onDismiss?.(detail) !== false)
|
|
198
|
+
service.dismiss(id);
|
|
199
|
+
} }, id))) }))] }) }));
|
|
200
|
+
}
|
|
201
|
+
export function useToast() {
|
|
202
|
+
const service = useContext(ToastServiceContext);
|
|
203
|
+
if (!service)
|
|
204
|
+
throw new Error('useToast requires a ToastProvider.');
|
|
205
|
+
return service;
|
|
206
|
+
}
|
|
207
|
+
export function TourStep({ value: _value, heading: _heading, description: _description, onEnter: _onEnter, nextLabel: _nextLabel, target: _target, prepareTarget: _prepareTarget, placement: _placement, backdrop: _backdrop, ...props }) {
|
|
208
|
+
return _jsx(View, { ...props });
|
|
209
|
+
}
|
|
210
|
+
export const Tour = forwardRef(function Tour({ children, label, value, defaultValue, onValueChange, onFinish, onSkip, open, defaultOpen, onOpenChange, trigger, skippable = true, placement, backdrop, backdropColor, targetGap, onTargetUnavailable, renderIndicator, style, ...props }, ref) {
|
|
211
|
+
const messages = useTramaMessages();
|
|
212
|
+
const { theme, resolve } = useTramaAppearance();
|
|
213
|
+
const { height } = useWindowDimensions();
|
|
214
|
+
const reduced = useReducedMotion();
|
|
215
|
+
const steps = Children.toArray(children).flatMap((step) => isValidElement(step) && step.type === TourStep ? [step.props] : []);
|
|
216
|
+
const [current, setStep] = useControllableValue(value, defaultValue ?? steps[0]?.value ?? '');
|
|
217
|
+
const [visible, setVisible] = useControllableValue(open, defaultOpen ?? false);
|
|
218
|
+
const position = Math.max(0, steps.findIndex((step) => step.value === current));
|
|
219
|
+
const step = steps[position];
|
|
220
|
+
const last = position >= steps.length - 1;
|
|
221
|
+
const enter = useRef(step?.onEnter);
|
|
222
|
+
enter.current = step?.onEnter;
|
|
223
|
+
useEffect(() => {
|
|
224
|
+
if (visible && step?.value)
|
|
225
|
+
enter.current?.();
|
|
226
|
+
}, [visible, step?.value]);
|
|
227
|
+
const treatment = resolve(step?.backdrop ?? backdrop, 'spotlight');
|
|
228
|
+
const target = useTourTarget(step, visible && treatment === 'spotlight', onTargetUnavailable);
|
|
229
|
+
const gap = targetGap ?? theme.space * 2;
|
|
230
|
+
const positionPreference = resolve(step?.placement ?? placement, 'auto');
|
|
231
|
+
const above = target ? Math.max(0, target.y - gap - theme.space * 4) : 0;
|
|
232
|
+
const below = target ? Math.max(0, height - target.y - target.height - gap - theme.space * 4) : 0;
|
|
233
|
+
const dock = positionPreference !== 'auto'
|
|
234
|
+
? positionPreference
|
|
235
|
+
: !target || Math.max(above, below) < 144
|
|
236
|
+
? 'center'
|
|
237
|
+
: above >= below
|
|
238
|
+
? 'top'
|
|
239
|
+
: 'bottom';
|
|
240
|
+
const available = dock === 'top' ? above : below;
|
|
241
|
+
function move(next) {
|
|
242
|
+
const candidate = steps[next];
|
|
243
|
+
if (candidate && candidate.value !== current && onValueChange?.(candidate.value) !== false)
|
|
244
|
+
setStep(candidate.value);
|
|
245
|
+
}
|
|
246
|
+
const surface = useRef(null);
|
|
247
|
+
useImperativeHandle(ref, () => ({
|
|
248
|
+
show: () => surface.current?.show(),
|
|
249
|
+
hide: () => surface.current?.hide(),
|
|
250
|
+
toggle: () => surface.current?.toggle(),
|
|
251
|
+
}));
|
|
252
|
+
if (!step)
|
|
253
|
+
return null;
|
|
254
|
+
return (_jsxs(Dialog, { ...props, ref: surface, label: label ?? step.heading, heading: step.heading, description: step.description, open: visible, closeOnOutside: false, closeOnEscape: skippable, backdrop: treatment === 'none' ? 'none' : 'dim', backdropColor: backdropColor, placement: dock, motion: reduced ? 'none' : 'fade', style: [target && dock !== 'center' && available >= 144 && { maxHeight: available }, style], onOpenChange: (next, detail) => {
|
|
255
|
+
if (onOpenChange?.(next, detail) !== false) {
|
|
256
|
+
setVisible(next);
|
|
257
|
+
if (!next && detail.reason === 'escape')
|
|
258
|
+
onSkip?.();
|
|
259
|
+
}
|
|
260
|
+
}, children: [trigger && _jsx(DialogTrigger, { children: trigger }), target && (_jsx(DialogBackdropLayer, { children: _jsx(TourMask, { target: target, gap: gap, color: backdropColor ?? 'rgba(0,0,0,0.55)', testID: props.testID ? props.testID + '-spotlight' : undefined }) })), _jsxs(View, { style: { gap: theme.space * 2 }, children: [renderIndicator?.(position + 1, steps.length) ?? (_jsx(Text, { accessibilityLiveRegion: "polite", style: { color: theme.colors.muted }, children: messages.pageOf(position + 1, steps.length) })), _jsx(TextContent, { style: { color: theme.colors.text }, children: step.children })] }), _jsxs(DialogFooter, { children: [skippable && (_jsx(Button, { variant: "text", onPress: () => {
|
|
261
|
+
if (onOpenChange?.(false, { reason: 'close-button' }) !== false) {
|
|
262
|
+
setVisible(false);
|
|
263
|
+
onSkip?.();
|
|
264
|
+
}
|
|
265
|
+
}, children: messages.skipTour })), position > 0 && (_jsx(Button, { variant: "outlined", onPress: () => move(position - 1), children: messages.back })), _jsx(Button, { onPress: () => {
|
|
266
|
+
if (!last)
|
|
267
|
+
move(position + 1);
|
|
268
|
+
else if (onOpenChange?.(false, { reason: 'close-button' }) !== false) {
|
|
269
|
+
setVisible(false);
|
|
270
|
+
onFinish?.();
|
|
271
|
+
}
|
|
272
|
+
}, children: step.nextLabel ?? (last ? messages.finishTour : messages.next) })] })] }));
|
|
273
|
+
});
|