@tastic/hud 0.25.1 → 0.27.1

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.
@@ -0,0 +1,315 @@
1
+ // src/fonts.ts
2
+ import { Platform } from "react-native";
3
+ var MONO_FONT = Platform.select({ ios: "Menlo", android: "monospace", default: "monospace" });
4
+
5
+ // src/PopoverBody.tsx
6
+ import { StyleSheet, View } from "react-native";
7
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
8
+ var CARET_SIZE = 7;
9
+ var CARET_RING_OFFSET = 2;
10
+ var CARET_DIP = 1;
11
+ function PopoverBody({ visible, children, align = "center", verticalAlign = "below", caretColor, caretBorderColor, caretBorderWidth = 1, triggerSize = 0 }) {
12
+ if (!visible) return null;
13
+ const above = verticalAlign === "above";
14
+ const alignStyle = align === "left" ? styles.contentLeft : align === "right" ? styles.contentRight : styles.contentCenter;
15
+ const verticalStyle = above ? styles.contentAbove : styles.contentBelow;
16
+ const ringSize = CARET_SIZE + caretBorderWidth;
17
+ const caretOffset = (halfFootprint) => align === "right" ? { right: triggerSize / 2 - halfFootprint } : { left: triggerSize / 2 - halfFootprint };
18
+ const caretFill = above ? "borderTopColor" : "borderBottomColor";
19
+ const caretWidthProp = above ? "borderTopWidth" : "borderBottomWidth";
20
+ const caretEdge = (size) => above ? { bottom: -size + CARET_DIP } : { top: -size + CARET_DIP };
21
+ return /* @__PURE__ */ jsxs(View, { style: [styles.content, alignStyle, verticalStyle], children: [
22
+ children,
23
+ caretColor && /* @__PURE__ */ jsxs(Fragment, { children: [
24
+ /* @__PURE__ */ jsx(View, { style: [styles.caret, caretOffset(ringSize), caretEdge(ringSize), { [caretFill]: caretBorderColor ?? caretColor, [caretWidthProp]: ringSize, borderLeftWidth: ringSize, borderRightWidth: ringSize }] }),
25
+ /* @__PURE__ */ jsx(View, { style: [styles.caret, caretOffset(CARET_SIZE), caretEdge(CARET_SIZE - CARET_RING_OFFSET), { [caretFill]: caretColor, [caretWidthProp]: CARET_SIZE, borderLeftWidth: CARET_SIZE, borderRightWidth: CARET_SIZE }] })
26
+ ] })
27
+ ] });
28
+ }
29
+ var styles = StyleSheet.create({
30
+ caret: {
31
+ borderLeftColor: "transparent",
32
+ borderRightColor: "transparent",
33
+ height: 0,
34
+ position: "absolute",
35
+ width: 0,
36
+ // Explicit, not left to DOM order - the box it dips into is itself a position:'relative' view,
37
+ // which React Native Web always gives an implicit zIndex:0 (see PopoverBody's stacking-context
38
+ // notes elsewhere in this codebase), so painting on top needs a real zIndex to beat that
39
+ // reliably rather than relying on being the later sibling.
40
+ zIndex: 1
41
+ },
42
+ content: {
43
+ position: "absolute",
44
+ zIndex: 50
45
+ },
46
+ contentAbove: {
47
+ bottom: "100%",
48
+ marginBottom: 8
49
+ },
50
+ contentBelow: {
51
+ marginTop: 8,
52
+ top: "100%"
53
+ },
54
+ // Stretched to the anchor's own (trigger) width, with alignItems centering the actual (usually
55
+ // much wider) popover box inside via plain flexbox - not `left:'50%'` plus a percentage
56
+ // `transform`, which depends on native resolving the box's own auto width before applying the
57
+ // transform and has proven unreliable there, visibly shifting the box off-center.
58
+ contentCenter: {
59
+ alignItems: "center",
60
+ left: 0,
61
+ right: 0
62
+ },
63
+ contentLeft: {
64
+ left: 0
65
+ },
66
+ contentRight: {
67
+ right: 0
68
+ }
69
+ });
70
+
71
+ // src/useAutoAlign.ts
72
+ import { useEffect, useRef, useState } from "react";
73
+ import { useWindowDimensions } from "react-native";
74
+ var EDGE_MARGIN = 12;
75
+ function rotateRect(x, y, width, height, rotation, windowWidth, windowHeight) {
76
+ if (rotation === 0) return { x, y, width, height };
77
+ const cx = windowWidth / 2;
78
+ const cy = windowHeight / 2;
79
+ const dx = x + width / 2 - cx;
80
+ const dy = y + height / 2 - cy;
81
+ const [newDx, newDy, newWidth, newHeight] = rotation === 180 ? [-dx, -dy, width, height] : rotation === 90 ? [-dy, dx, height, width] : [dy, -dx, height, width];
82
+ return { x: cx + newDx - newWidth / 2, y: cy + newDy - newHeight / 2, width: newWidth, height: newHeight };
83
+ }
84
+ function useAutoAlign(open, contentWidth, contentHeight, rotation = 0, bounds) {
85
+ const triggerRef = useRef(null);
86
+ const { width: windowWidth, height: windowHeight } = useWindowDimensions();
87
+ const [align, setAlign] = useState("center");
88
+ const [verticalAlign, setVerticalAlign] = useState("below");
89
+ const [maxHeight, setMaxHeight] = useState(contentHeight);
90
+ const [measured, setMeasured] = useState(false);
91
+ const [prevOpen, setPrevOpen] = useState(open);
92
+ if (open !== prevOpen) {
93
+ setPrevOpen(open);
94
+ if (open) setMeasured(false);
95
+ }
96
+ useEffect(() => {
97
+ if (!open) return;
98
+ const place = (limits) => (rawX, rawY, rawWidth, rawHeight) => {
99
+ const { x, y, width: triggerWidth, height: triggerHeight } = rotateRect(rawX, rawY, rawWidth, rawHeight, rotation, windowWidth, windowHeight);
100
+ const centerX = x + triggerWidth / 2;
101
+ const overflowsRight = centerX + contentWidth / 2 > limits.right - EDGE_MARGIN;
102
+ const overflowsLeft = centerX - contentWidth / 2 < limits.left + EDGE_MARGIN;
103
+ if (overflowsRight && !overflowsLeft) setAlign("right");
104
+ else if (overflowsLeft && !overflowsRight) setAlign("left");
105
+ else setAlign("center");
106
+ const roomBelow = limits.bottom - (y + triggerHeight) - EDGE_MARGIN;
107
+ const roomAbove = y - limits.top - EDGE_MARGIN;
108
+ const above = contentHeight > roomBelow && roomAbove > roomBelow;
109
+ setVerticalAlign(above ? "above" : "below");
110
+ setMaxHeight(Math.max(above ? roomAbove : roomBelow, 0));
111
+ setMeasured(true);
112
+ };
113
+ const screen = { left: 0, top: 0, right: windowWidth, bottom: windowHeight };
114
+ const boundsView = bounds?.current;
115
+ if (!boundsView) {
116
+ triggerRef.current?.measureInWindow(place(screen));
117
+ return;
118
+ }
119
+ boundsView.measureInWindow((rawX, rawY, rawWidth, rawHeight) => {
120
+ const b = rotateRect(rawX, rawY, rawWidth, rawHeight, rotation, windowWidth, windowHeight);
121
+ triggerRef.current?.measureInWindow(place({ left: Math.max(b.x, 0), top: Math.max(b.y, 0), right: Math.min(b.x + b.width, windowWidth), bottom: Math.min(b.y + b.height, windowHeight) }));
122
+ });
123
+ }, [open, contentWidth, contentHeight, windowWidth, windowHeight, rotation, bounds]);
124
+ return { align, maxHeight, measured, triggerRef, verticalAlign };
125
+ }
126
+
127
+ // src/InlineColorPicker.tsx
128
+ import { defaultColors, getContrastColor } from "@rific/auto-paper";
129
+ import { TouchableRipple } from "@rific/feedback-press";
130
+ import { clamp, useRotation } from "@tastic/core";
131
+ import { ScrollView, StyleSheet as StyleSheet2, useWindowDimensions as useWindowDimensions2, View as View3 } from "react-native";
132
+ import { Icon, Text } from "react-native-paper";
133
+ import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
134
+ var EMOJI_PATTERN = /\p{Extended_Pictographic}/u;
135
+ var DEFAULT_SIZE = 48;
136
+ var SWATCH_SIZE = 28;
137
+ var SWATCH_RING_WIDTH = 2;
138
+ var SWATCHES_PADDING = 8;
139
+ var SWATCHES_GAP = 6;
140
+ var SWATCHES_BORDER_WIDTH = 2;
141
+ var MIN_COLUMNS = 4;
142
+ var MAX_COLUMNS = 5;
143
+ var SCREEN_MARGIN = 40;
144
+ function widthForColumns(columns) {
145
+ return SWATCHES_BORDER_WIDTH * 2 + SWATCHES_PADDING * 2 + SWATCH_SIZE * columns + SWATCHES_GAP * (columns - 1);
146
+ }
147
+ function getInlineColorPickerContentSize(swatchCount, windowWidth, columns) {
148
+ const autoColumns = clamp(Math.floor((windowWidth - 2 * SCREEN_MARGIN + SWATCHES_GAP) / (SWATCH_SIZE + SWATCHES_GAP)), MIN_COLUMNS, MAX_COLUMNS);
149
+ const resolvedColumns = columns ?? autoColumns;
150
+ const width = widthForColumns(resolvedColumns);
151
+ const rows = Math.ceil(swatchCount / resolvedColumns);
152
+ const height = SWATCHES_BORDER_WIDTH * 2 + SWATCHES_PADDING * 2 + SWATCH_SIZE * rows + SWATCHES_GAP * (rows - 1);
153
+ return { width, height };
154
+ }
155
+ function SwatchDrawing({ size, children }) {
156
+ return /* @__PURE__ */ jsx2(View3, { style: [styles2.drawing, { borderRadius: size / 2, height: size, width: size }], children });
157
+ }
158
+ function InlineColorPicker({ id, host, value, onChange, previewValue, swatches = defaultColors, takenValue, allowSwapTaken, dark, align: forcedAlign, alignOverride, rotation: rotationOverride, icon = "palette", tag, labelFontFamily = MONO_FONT, autoDismiss = true, columns, size = DEFAULT_SIZE, renderSwatch, bounds }) {
159
+ const ambientRotation = useRotation();
160
+ const rotation = rotationOverride ?? ambientRotation;
161
+ const menuBg = dark ? "#000000" : "#FFFFFF";
162
+ const { width: windowWidth } = useWindowDimensions2();
163
+ const { width: swatchesWidth, height: swatchesHeight } = getInlineColorPickerContentSize(swatches.length, windowWidth, columns);
164
+ const open = host.openId === id;
165
+ const auto = useAutoAlign(open, swatchesWidth, swatchesHeight, rotation, bounds);
166
+ const placement = alignOverride ?? auto;
167
+ const { maxHeight, measured, triggerRef, verticalAlign } = placement;
168
+ const align = forcedAlign ?? placement.align;
169
+ const displayFor = previewValue ?? ((hex) => hex);
170
+ const displayValue = displayFor(value);
171
+ const contrastColor = getContrastColor(displayValue);
172
+ const tagFontSize = size * (tag && EMOJI_PATTERN.test(tag) ? 0.6 : 0.4);
173
+ const triggerDrawing = renderSwatch?.(displayValue, size, "trigger") ?? null;
174
+ return /* @__PURE__ */ jsxs2(View3, { style: [styles2.anchor, open && styles2.anchorOpen], children: [
175
+ /* @__PURE__ */ jsx2(TouchableRipple, { onPress: () => host.toggle(id), borderless: true, style: [styles2.trigger, triggerDrawing === null && { backgroundColor: displayValue }, { borderRadius: size / 2, height: size, width: size }], children: /* @__PURE__ */ jsxs2(View3, { ref: triggerRef, collapsable: false, style: styles2.triggerMeasure, children: [
176
+ triggerDrawing !== null && /* @__PURE__ */ jsx2(SwatchDrawing, { size, children: triggerDrawing }),
177
+ tag ? /* @__PURE__ */ jsx2(Text, { style: [styles2.tagLabel, { color: contrastColor, fontFamily: labelFontFamily, fontSize: tagFontSize }], numberOfLines: 1, adjustsFontSizeToFit: true, children: tag }) : /* @__PURE__ */ jsx2(Icon, { source: icon, size: size * 0.6, color: contrastColor })
178
+ ] }) }),
179
+ /* @__PURE__ */ jsx2(PopoverBody, { visible: open && measured, align, verticalAlign, caretColor: menuBg, caretBorderColor: displayValue, caretBorderWidth: SWATCHES_BORDER_WIDTH, triggerSize: size, children: /* @__PURE__ */ jsx2(ScrollView, { style: [styles2.swatches, { backgroundColor: menuBg, borderColor: displayValue, width: swatchesWidth, maxHeight }], contentContainerStyle: styles2.swatchesContent, showsVerticalScrollIndicator: false, children: swatches.map((swatch) => {
180
+ const selected = swatch.value.toLowerCase() === value.toLowerCase();
181
+ const taken = !selected && !!takenValue && swatch.value.toLowerCase() === takenValue.toLowerCase();
182
+ const swappable = taken && allowSwapTaken;
183
+ const swatchDisplay = displayFor(swatch.value);
184
+ const contrast = getContrastColor(swatchDisplay);
185
+ const drawnSize = selected ? SWATCH_SIZE - 2 * SWATCH_RING_WIDTH : SWATCH_SIZE;
186
+ const drawing = renderSwatch?.(swatchDisplay, drawnSize, "swatch") ?? null;
187
+ const mark = selected ? /* @__PURE__ */ jsx2(Icon, { source: "check", size: 16, color: contrast }) : swappable ? /* @__PURE__ */ jsx2(Icon, { source: "swap-horizontal", size: 16, color: contrast }) : taken ? /* @__PURE__ */ jsx2(Icon, { source: "close", size: 16, color: contrast }) : /* @__PURE__ */ jsx2(View3, {});
188
+ return /* @__PURE__ */ jsx2(
189
+ TouchableRipple,
190
+ {
191
+ disabled: taken && !swappable,
192
+ onPress: () => {
193
+ onChange(swatch.value);
194
+ if (autoDismiss) host.close();
195
+ },
196
+ borderless: true,
197
+ style: [styles2.swatch, drawing === null && { backgroundColor: swatchDisplay }, selected && styles2.swatchSelected, taken && !swappable && styles2.swatchTaken],
198
+ children: drawing !== null ? /* @__PURE__ */ jsxs2(View3, { style: styles2.swatchFace, children: [
199
+ /* @__PURE__ */ jsx2(SwatchDrawing, { size: drawnSize, children: drawing }),
200
+ mark
201
+ ] }) : mark
202
+ },
203
+ swatch.value
204
+ );
205
+ }) }) })
206
+ ] });
207
+ }
208
+ var styles2 = StyleSheet2.create({
209
+ anchor: {
210
+ position: "relative"
211
+ },
212
+ // See SectionedDropdown's identical comment - React Native Web gives every position:'relative' view
213
+ // its own stacking context, so the elevation has to live on the anchor itself, not just the
214
+ // popover content nested inside it, to correctly paint above this anchor's own later siblings.
215
+ anchorOpen: {
216
+ zIndex: 100
217
+ },
218
+ // A renderSwatch drawing (see SwatchDrawing).
219
+ drawing: {
220
+ left: 0,
221
+ overflow: "hidden",
222
+ pointerEvents: "none",
223
+ position: "absolute",
224
+ top: 0
225
+ },
226
+ swatch: {
227
+ alignItems: "center",
228
+ borderRadius: SWATCH_SIZE / 2,
229
+ height: SWATCH_SIZE,
230
+ justifyContent: "center",
231
+ width: SWATCH_SIZE
232
+ },
233
+ // A drawn swatch (see SwatchDrawing) and its mark: the whole circle inside the selected one's ring.
234
+ swatchFace: {
235
+ alignItems: "center",
236
+ bottom: 0,
237
+ justifyContent: "center",
238
+ left: 0,
239
+ position: "absolute",
240
+ right: 0,
241
+ top: 0
242
+ },
243
+ swatchSelected: {
244
+ borderColor: "#ffffff",
245
+ borderWidth: SWATCH_RING_WIDTH
246
+ },
247
+ swatchTaken: {
248
+ opacity: 0.35
249
+ },
250
+ swatches: {
251
+ borderRadius: 12,
252
+ borderWidth: SWATCHES_BORDER_WIDTH,
253
+ elevation: 8,
254
+ shadowColor: "#000000",
255
+ shadowOffset: { height: 2, width: 0 },
256
+ shadowOpacity: 0.3,
257
+ shadowRadius: 8
258
+ // width is set inline above - border-box sizing means it has to include the border too
259
+ // (2*SWATCHES_BORDER_WIDTH), not just padding+content, or exactly enough room goes missing that
260
+ // the last column silently wraps to a new row. Anything wider than the exact sum just shows as
261
+ // dead space on the right edge of each row instead.
262
+ },
263
+ // Separate from `swatches` itself - see SectionedDropdown's identical menu/menuContent split.
264
+ // The scroll frame's own bounds (including maxHeight) live on the ScrollView; the actual grid
265
+ // wrapping belongs to its contentContainerStyle, which sizes to the unclamped content instead.
266
+ swatchesContent: {
267
+ flexDirection: "row",
268
+ flexWrap: "wrap",
269
+ gap: SWATCHES_GAP,
270
+ padding: SWATCHES_PADDING
271
+ },
272
+ tagLabel: {
273
+ fontWeight: "700",
274
+ letterSpacing: 0.5
275
+ },
276
+ // borderRadius/height/width come from the `size` prop instead (see render-site override) - no
277
+ // fixed default here, since this style object is shared by every caller regardless of size.
278
+ trigger: {
279
+ alignItems: "center",
280
+ alignSelf: "flex-start",
281
+ justifyContent: "center"
282
+ },
283
+ // Wraps just the icon/tag, inside the TouchableRipple, purely so useAutoAlign has a plain View
284
+ // to attach its measurement ref to - TouchableRipple itself doesn't forward a ref to a
285
+ // measurable native node. Padded horizontally so a multi-character tag doesn't butt right up
286
+ // against the trigger's own circular edge (an icon alone doesn't need it, but this style is
287
+ // shared by both).
288
+ triggerMeasure: {
289
+ alignItems: "center",
290
+ height: "100%",
291
+ justifyContent: "center",
292
+ paddingHorizontal: 4,
293
+ width: "100%"
294
+ }
295
+ });
296
+
297
+ // src/usePopoverHost.ts
298
+ import { useCallback, useState as useState2 } from "react";
299
+ function usePopoverHost() {
300
+ const [openId, setOpenId] = useState2(null);
301
+ const toggle = useCallback((id) => {
302
+ setOpenId((prev) => prev === id ? null : id);
303
+ }, []);
304
+ const close = useCallback(() => setOpenId(null), []);
305
+ return { openId, toggle, close };
306
+ }
307
+
308
+ export {
309
+ MONO_FONT,
310
+ PopoverBody,
311
+ useAutoAlign,
312
+ getInlineColorPickerContentSize,
313
+ InlineColorPicker,
314
+ usePopoverHost
315
+ };