@xaui/native 0.9.1-alpha.0 → 0.9.1-alpha.10

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,3 +1,9 @@
1
+ import {
2
+ ThemeContext,
3
+ alpha,
4
+ mix
5
+ } from "./chunk-X2K2FX3W.js";
6
+
1
7
  // src/provider/xaui-provider.tsx
2
8
  import { useColorScheme } from "react-native";
3
9
 
@@ -18,68 +24,6 @@ function stableHash(value) {
18
24
  return hash.toString(36);
19
25
  }
20
26
 
21
- // src/utils/colors.ts
22
- var srgbToLinear = (c) => c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
23
- var linearToSrgb = (c) => c <= 31308e-7 ? 12.92 * c : 1.055 * c ** (1 / 2.4) - 0.055;
24
- var clamp01 = (n) => Math.min(1, Math.max(0, n));
25
- var HEX = /^#?(?:[0-9a-f]{3}|[0-9a-f]{6})$/i;
26
- function hexToRgb(hex) {
27
- if (!HEX.test(hex)) {
28
- throw new Error(
29
- `XAUI: "${hex}" is not a hex colour. Tokens that feed mix() and alpha() must be #rgb or #rrggbb \u2014 named colours and rgb()/rgba() values cannot be blended.`
30
- );
31
- }
32
- const raw = hex.replace("#", "");
33
- const full = raw.length === 3 ? raw.split("").map((c) => c + c).join("") : raw;
34
- return [
35
- parseInt(full.slice(0, 2), 16) / 255,
36
- parseInt(full.slice(2, 4), 16) / 255,
37
- parseInt(full.slice(4, 6), 16) / 255
38
- ];
39
- }
40
- function rgbToHex([r, g, b]) {
41
- const to = (n) => Math.round(clamp01(n) * 255).toString(16).padStart(2, "0");
42
- return `#${to(r)}${to(g)}${to(b)}`;
43
- }
44
- function rgbToOklab([r, g, b]) {
45
- const R = srgbToLinear(r);
46
- const G = srgbToLinear(g);
47
- const B = srgbToLinear(b);
48
- const l = Math.cbrt(0.4122214708 * R + 0.5363325363 * G + 0.0514459929 * B);
49
- const m = Math.cbrt(0.2119034982 * R + 0.6806995451 * G + 0.1073969566 * B);
50
- const s = Math.cbrt(0.0883024619 * R + 0.2817188376 * G + 0.6299787005 * B);
51
- return [
52
- 0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s,
53
- 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s,
54
- 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s
55
- ];
56
- }
57
- function oklabToRgb([L, a, b]) {
58
- const l = (L + 0.3963377774 * a + 0.2158037573 * b) ** 3;
59
- const m = (L - 0.1055613458 * a - 0.0638541728 * b) ** 3;
60
- const s = (L - 0.0894841775 * a - 1.291485548 * b) ** 3;
61
- return [
62
- clamp01(linearToSrgb(4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s)),
63
- clamp01(linearToSrgb(-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s)),
64
- clamp01(linearToSrgb(-0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s))
65
- ];
66
- }
67
- function mix(base, other, amount) {
68
- const from = rgbToOklab(hexToRgb(base));
69
- const to = rgbToOklab(hexToRgb(other));
70
- return rgbToHex(
71
- oklabToRgb([
72
- from[0] + (to[0] - from[0]) * amount,
73
- from[1] + (to[1] - from[1]) * amount,
74
- from[2] + (to[2] - from[2]) * amount
75
- ])
76
- );
77
- }
78
- function alpha(hex, amount) {
79
- const [r, g, b] = hexToRgb(hex).map((v) => Math.round(v * 255));
80
- return `rgba(${r}, ${g}, ${b}, ${amount})`;
81
- }
82
-
83
27
  // src/theme/derive-colors.ts
84
28
  function deriveColors(s) {
85
29
  return {
@@ -442,11 +386,8 @@ function createTheme(config = {}) {
442
386
  }
443
387
  var defaultTheme = createTheme();
444
388
 
445
- // src/theme/theme-context.ts
446
- import { createContext } from "react";
447
- var ThemeContext = createContext(null);
448
-
449
389
  // src/provider/xaui-provider.tsx
390
+ import { jsx } from "react/jsx-runtime";
450
391
  function XAUIProvider({
451
392
  children,
452
393
  theme = defaultTheme,
@@ -454,7 +395,7 @@ function XAUIProvider({
454
395
  }) {
455
396
  const scheme = useColorScheme();
456
397
  const resolved = colorMode === "system" ? scheme === "dark" ? "dark" : "light" : colorMode;
457
- return /* @__PURE__ */ React.createElement(ThemeContext.Provider, { value: theme[resolved] }, children);
398
+ return /* @__PURE__ */ jsx(ThemeContext.Provider, { value: theme[resolved], children });
458
399
  }
459
400
 
460
401
  // src/theme/palette.ts
@@ -756,23 +697,6 @@ var primitives = {
756
697
  eclipse: "#18181b"
757
698
  };
758
699
 
759
- // src/theme/theme-hooks.ts
760
- import { useContext } from "react";
761
- function useXAUITheme() {
762
- const theme = useContext(ThemeContext);
763
- if (theme === null) {
764
- throw new Error("XAUI: useXAUITheme must be used within <XAUIProvider>.");
765
- }
766
- return theme;
767
- }
768
- function useColorMode() {
769
- return useXAUITheme().mode;
770
- }
771
- function useThemeColor(token) {
772
- const { colors } = useXAUITheme();
773
- return Array.isArray(token) ? token.map((key) => colors[key]) : colors[token];
774
- }
775
-
776
700
  export {
777
701
  deriveColors,
778
702
  buildRadius,
@@ -781,11 +705,7 @@ export {
781
705
  sourceKeys,
782
706
  createTheme,
783
707
  defaultTheme,
784
- ThemeContext,
785
708
  XAUIProvider,
786
709
  palette,
787
- primitives,
788
- useXAUITheme,
789
- useColorMode,
790
- useThemeColor
710
+ primitives
791
711
  };
@@ -0,0 +1,353 @@
1
+ import {
2
+ Icon,
3
+ PressableFeedback,
4
+ childrenToString,
5
+ createRecipe,
6
+ createSlotContext
7
+ } from "./chunk-66OVPWF4.js";
8
+ import {
9
+ useXAUITheme
10
+ } from "./chunk-X2K2FX3W.js";
11
+
12
+ // src/components/button/button.context.ts
13
+ var [ButtonProvider, useButton] = createSlotContext("Button");
14
+
15
+ // src/components/button/button-icon.tsx
16
+ import { jsx } from "react/jsx-runtime";
17
+ function ButtonIcon({ size, color, ...rest }) {
18
+ const { icon } = useButton();
19
+ return /* @__PURE__ */ jsx(Icon, { size: size ?? icon.size, color: color ?? icon.color, ...rest });
20
+ }
21
+ ButtonIcon.displayName = "XAUI.Button.Icon";
22
+
23
+ // src/components/button/button-label.tsx
24
+ import { forwardRef } from "react";
25
+ import { Text } from "react-native";
26
+ import { jsx as jsx2 } from "react/jsx-runtime";
27
+ var ButtonLabel = forwardRef(function ButtonLabel2({ children, style, numberOfLines = 1, ...rest }, ref) {
28
+ const { labelStyle } = useButton();
29
+ return /* @__PURE__ */ jsx2(
30
+ Text,
31
+ {
32
+ ref,
33
+ numberOfLines,
34
+ style: [labelStyle, style],
35
+ ...rest,
36
+ children
37
+ }
38
+ );
39
+ });
40
+ ButtonLabel.displayName = "XAUI.Button.Label";
41
+
42
+ // src/components/button/button-spinner.tsx
43
+ import { useEffect } from "react";
44
+ import { View } from "react-native";
45
+ import Animated, {
46
+ Easing,
47
+ cancelAnimation,
48
+ useAnimatedStyle,
49
+ useSharedValue,
50
+ withRepeat,
51
+ withTiming
52
+ } from "react-native-reanimated";
53
+ import { jsx as jsx3 } from "react/jsx-runtime";
54
+ var ROTATION_DURATION = 800;
55
+ function ButtonSpinner({ style, animation = true }) {
56
+ const { spinnerStyle } = useButton();
57
+ if (!animation) return /* @__PURE__ */ jsx3(View, { style: [spinnerStyle, style] });
58
+ return /* @__PURE__ */ jsx3(SpinningRing, { style: [spinnerStyle, style] });
59
+ }
60
+ ButtonSpinner.displayName = "XAUI.Button.Spinner";
61
+ function SpinningRing({ style }) {
62
+ const angle = useSharedValue(0);
63
+ useEffect(() => {
64
+ angle.value = withRepeat(
65
+ withTiming(360, { duration: ROTATION_DURATION, easing: Easing.linear }),
66
+ -1,
67
+ false
68
+ );
69
+ return () => cancelAnimation(angle);
70
+ }, [angle]);
71
+ const animatedStyle = useAnimatedStyle(
72
+ () => ({ transform: [{ rotate: `${angle.value}deg` }] }),
73
+ [angle]
74
+ );
75
+ return /* @__PURE__ */ jsx3(Animated.View, { style: [style, animatedStyle] });
76
+ }
77
+
78
+ // src/components/button/button.tsx
79
+ import { forwardRef as forwardRef2, useMemo } from "react";
80
+ import { StyleSheet } from "react-native";
81
+
82
+ // src/hooks/use-press-state.ts
83
+ import { useCallback, useRef, useState } from "react";
84
+ function usePressState(handlers = {}) {
85
+ const [isPressed, setIsPressed] = useState(false);
86
+ const latest = useRef(handlers);
87
+ latest.current = handlers;
88
+ const onPressIn = useCallback((event) => {
89
+ setIsPressed(true);
90
+ latest.current.onPressIn?.(event);
91
+ }, []);
92
+ const onPressOut = useCallback((event) => {
93
+ setIsPressed(false);
94
+ latest.current.onPressOut?.(event);
95
+ }, []);
96
+ const press = useRef({ onPressIn, onPressOut }).current;
97
+ return [isPressed, press];
98
+ }
99
+
100
+ // src/utils/warn-dev.ts
101
+ function warnDev(message) {
102
+ if (typeof __DEV__ !== "undefined" && !__DEV__) return;
103
+ console.warn(`XAUI: ${message}`);
104
+ }
105
+
106
+ // src/components/button/button.recipe.ts
107
+ var SLOTS = ["root", "label", "icon", "spinner"];
108
+ var VARIANT_TOKENS = {
109
+ primary: { bg: "accent", bgPressed: "accentPressed", fg: "accentForeground" },
110
+ secondary: { bg: "default", bgPressed: "defaultPressed", fg: "defaultForeground" },
111
+ tertiary: {
112
+ border: "border",
113
+ bgPressed: "defaultSoftPressed",
114
+ fg: "foreground"
115
+ },
116
+ ghost: { bgPressed: "defaultSoftPressed", fg: "foreground" },
117
+ success: { bg: "success", bgPressed: "successPressed", fg: "successForeground" },
118
+ "success-soft": {
119
+ bg: "successSoft",
120
+ bgPressed: "successSoftPressed",
121
+ fg: "successSoftForeground"
122
+ },
123
+ warning: { bg: "warning", bgPressed: "warningPressed", fg: "warningForeground" },
124
+ "warning-soft": {
125
+ bg: "warningSoft",
126
+ bgPressed: "warningSoftPressed",
127
+ fg: "warningSoftForeground"
128
+ },
129
+ danger: { bg: "danger", bgPressed: "dangerPressed", fg: "dangerForeground" },
130
+ "danger-soft": {
131
+ bg: "dangerSoft",
132
+ bgPressed: "dangerSoftPressed",
133
+ fg: "dangerSoftForeground"
134
+ }
135
+ };
136
+ function ring(theme, size) {
137
+ return {
138
+ width: size,
139
+ height: size,
140
+ borderRadius: size / 2,
141
+ borderWidth: theme.borderWidth.default * 2,
142
+ borderTopColor: "transparent"
143
+ };
144
+ }
145
+ function sizeAxis(step) {
146
+ const { size, padding, gap, glyph, radius } = step;
147
+ return (theme) => ({
148
+ root: {
149
+ height: theme.controlHeights[size],
150
+ paddingHorizontal: theme.spacing(padding),
151
+ gap: theme.spacing(gap),
152
+ borderRadius: theme.radius[radius]
153
+ },
154
+ label: {
155
+ fontSize: theme.fontSizes[size],
156
+ lineHeight: theme.lineHeights[size]
157
+ },
158
+ icon: { fontSize: theme.fontSizes[glyph] },
159
+ spinner: ring(theme, theme.fontSizes[glyph])
160
+ });
161
+ }
162
+ var buttonRecipe = createRecipe({
163
+ slots: SLOTS,
164
+ base: (theme) => ({
165
+ root: {
166
+ flexDirection: "row",
167
+ alignItems: "center",
168
+ justifyContent: "center",
169
+ borderWidth: 0,
170
+ // iOS's squircle. Free on Android, and it is what makes a large radius read as a
171
+ // shape rather than as two arcs meeting a straight edge.
172
+ borderCurve: "continuous"
173
+ },
174
+ label: {
175
+ fontFamily: theme.fontFamilies.body,
176
+ fontWeight: theme.fontWeights.medium
177
+ }
178
+ }),
179
+ variantTokens: VARIANT_TOKENS,
180
+ /**
181
+ * Where the variant's colours land, for every variant at once. The border width follows
182
+ * the *presence* of the border role rather than a per-variant flag — `tertiary` is the
183
+ * only variant that names one, and that fact is already in the table above.
184
+ */
185
+ paint: (theme, colors) => ({
186
+ root: {
187
+ backgroundColor: colors.bg,
188
+ borderColor: colors.border,
189
+ borderWidth: colors.border ? theme.borderWidth.default : 0
190
+ },
191
+ label: { color: colors.fg },
192
+ icon: { color: colors.fg },
193
+ spinner: { borderColor: colors.fg }
194
+ }),
195
+ /**
196
+ * Declaration order is application order: `radius` overrides the radius `size` set, and
197
+ * `isIconOnly` overrides its padding. Both are axes rather than a static sheet merged
198
+ * at the call site, so they stay inside the cache key and a press still allocates
199
+ * nothing.
200
+ */
201
+ variants: {
202
+ size: {
203
+ xs: sizeAxis({ size: "xs", padding: 3, gap: 1, glyph: "sm", radius: "3xl" }),
204
+ sm: sizeAxis({
205
+ size: "sm",
206
+ padding: 3.5,
207
+ gap: 1.5,
208
+ glyph: "md",
209
+ radius: "3xl"
210
+ }),
211
+ md: sizeAxis({ size: "md", padding: 4, gap: 2, glyph: "lg", radius: "3xl" }),
212
+ lg: sizeAxis({ size: "lg", padding: 5, gap: 2.5, glyph: "xl", radius: "4xl" })
213
+ },
214
+ radius: {
215
+ xs: (t) => ({ root: { borderRadius: t.radius.xs } }),
216
+ sm: (t) => ({ root: { borderRadius: t.radius.sm } }),
217
+ md: (t) => ({ root: { borderRadius: t.radius.md } }),
218
+ lg: (t) => ({ root: { borderRadius: t.radius.lg } }),
219
+ xl: (t) => ({ root: { borderRadius: t.radius.xl } }),
220
+ "2xl": (t) => ({ root: { borderRadius: t.radius["2xl"] } }),
221
+ "3xl": (t) => ({ root: { borderRadius: t.radius["3xl"] } }),
222
+ "4xl": (t) => ({ root: { borderRadius: t.radius["4xl"] } }),
223
+ field: (t) => ({ root: { borderRadius: t.radius.field } }),
224
+ full: (t) => ({ root: { borderRadius: t.radius.full } })
225
+ },
226
+ // A square on a fixed height. No width is computed, and none needs to be.
227
+ isIconOnly: {
228
+ true: () => ({ root: { paddingHorizontal: 0, aspectRatio: 1 } })
229
+ }
230
+ },
231
+ /**
232
+ * The pressed colour lives here rather than in a `PressableFeedback.Highlight`, because
233
+ * a control picks one treatment or the other — both, and a pressed button darkens
234
+ * twice. This one is the variant's own `…Pressed` token, so the press reads as the
235
+ * button's colour going down rather than as a neutral film over it, and a tinted button
236
+ * presses through the same OKLab formula as a token one.
237
+ */
238
+ states: {
239
+ pressed: (_theme, colors) => ({ root: { backgroundColor: colors.bgPressed } }),
240
+ disabled: (theme) => ({ root: { opacity: theme.opacity.disabled } })
241
+ },
242
+ defaultVariants: { variant: "primary", size: "md" }
243
+ });
244
+
245
+ // src/components/button/button.utils.ts
246
+ import { Children, isValidElement } from "react";
247
+ function containsElementOfType(children, type) {
248
+ return Children.toArray(children).some(
249
+ (child) => isValidElement(child) && child.type === type
250
+ );
251
+ }
252
+
253
+ // src/components/button/button.tsx
254
+ import { Fragment, jsx as jsx4, jsxs } from "react/jsx-runtime";
255
+ var ButtonRoot = forwardRef2(function Button({
256
+ children,
257
+ variant,
258
+ size,
259
+ radius,
260
+ color,
261
+ isDisabled = false,
262
+ isLoading = false,
263
+ isIconOnly = false,
264
+ asChild = false,
265
+ // `scale` and not `scale-highlight`: the recipe's `pressed` state already paints the
266
+ // variant's own pressed colour, and a neutral wash on top would darken it twice.
267
+ feedbackVariant = "scale",
268
+ accessibilityRole = "button",
269
+ accessibilityState,
270
+ style,
271
+ onPressIn,
272
+ onPressOut,
273
+ ...rest
274
+ }, ref) {
275
+ const theme = useXAUITheme();
276
+ const [isPressed, press] = usePressState({ onPressIn, onPressOut });
277
+ const selection = {
278
+ variant,
279
+ size,
280
+ radius,
281
+ isIconOnly: isIconOnly ? "true" : void 0
282
+ };
283
+ const states = { pressed: isPressed, disabled: isDisabled || isLoading };
284
+ const styles = buttonRecipe.resolve({ theme, selection, states });
285
+ const tint = color ? buttonRecipe.tint({ theme, color, selection, states }) : void 0;
286
+ const context = useMemo(() => {
287
+ const icon = StyleSheet.flatten([styles.icon, tint?.icon]);
288
+ return {
289
+ labelStyle: tint ? [styles.label, tint.label] : styles.label,
290
+ spinnerStyle: tint ? [styles.spinner, tint.spinner] : styles.spinner,
291
+ icon: {
292
+ size: icon.fontSize,
293
+ // `ColorValue` also covers the platform's opaque colours, which `Icon` cannot
294
+ // hand to a third-party component expecting a string.
295
+ color: typeof icon.color === "string" ? icon.color : void 0
296
+ },
297
+ isDisabled,
298
+ isLoading
299
+ };
300
+ }, [styles, tint, isDisabled, isLoading]);
301
+ const rootStyle = [
302
+ styles.root,
303
+ tint?.root,
304
+ typeof style === "function" ? style({ pressed: isPressed }) : style
305
+ ];
306
+ const text = childrenToString(children);
307
+ const showSpinner = isLoading && !containsElementOfType(children, ButtonSpinner);
308
+ if (isIconOnly && !rest.accessibilityLabel && !rest["aria-label"]) {
309
+ warnDev(
310
+ "Button: an icon-only button needs an `accessibilityLabel` \u2014 there is no text for a screen reader to read, so it announces as an unlabelled button."
311
+ );
312
+ }
313
+ return /* @__PURE__ */ jsx4(ButtonProvider, { value: context, children: /* @__PURE__ */ jsx4(
314
+ PressableFeedback,
315
+ {
316
+ ref,
317
+ isPressed,
318
+ isDisabled: isDisabled || isLoading,
319
+ asChild,
320
+ feedbackVariant,
321
+ accessibilityRole,
322
+ accessibilityState: {
323
+ disabled: isDisabled,
324
+ busy: isLoading,
325
+ ...accessibilityState
326
+ },
327
+ ...rest,
328
+ style: rootStyle,
329
+ onPressIn: press.onPressIn,
330
+ onPressOut: press.onPressOut,
331
+ children: asChild ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
332
+ showSpinner ? /* @__PURE__ */ jsx4(ButtonSpinner, {}) : null,
333
+ text !== null ? /* @__PURE__ */ jsx4(ButtonLabel, { children: text }) : children
334
+ ] })
335
+ }
336
+ ) });
337
+ });
338
+ ButtonRoot.displayName = "XAUI.Button.Root";
339
+
340
+ // src/components/button/index.ts
341
+ var Button2 = Object.assign(ButtonRoot, {
342
+ Label: ButtonLabel,
343
+ Icon: ButtonIcon,
344
+ Spinner: ButtonSpinner
345
+ });
346
+
347
+ export {
348
+ useButton,
349
+ usePressState,
350
+ warnDev,
351
+ buttonRecipe,
352
+ Button2 as Button
353
+ };