expo-interface 0.1.1 → 0.3.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.
Files changed (78) hide show
  1. package/README.md +119 -20
  2. package/package.json +9 -9
  3. package/src/accent.tsx +21 -8
  4. package/src/alert/alert.css +3 -3
  5. package/src/alert/index.tsx +1 -2
  6. package/src/button/button.css +12 -0
  7. package/src/button/index.android.tsx +14 -3
  8. package/src/button/index.ios.tsx +13 -4
  9. package/src/button/index.tsx +8 -1
  10. package/src/button/types.ts +20 -0
  11. package/src/checkbox/index.tsx +1 -1
  12. package/src/color-picker/color-picker.css +136 -0
  13. package/src/color-picker/index.android.tsx +145 -0
  14. package/src/color-picker/index.ios.tsx +80 -0
  15. package/src/color-picker/index.tsx +112 -0
  16. package/src/color-picker/shared.ts +204 -0
  17. package/src/color-picker/sheet.tsx +432 -0
  18. package/src/color-picker/types.ts +40 -0
  19. package/src/context-menu/index.android.tsx +39 -11
  20. package/src/context-menu/index.ios.tsx +3 -1
  21. package/src/context-menu/index.tsx +24 -7
  22. package/src/fab/fab.css +72 -0
  23. package/src/fab/index.android.tsx +72 -0
  24. package/src/fab/index.ios.tsx +68 -0
  25. package/src/fab/index.tsx +37 -0
  26. package/src/fab/shared.ts +23 -0
  27. package/src/fab/types.ts +39 -0
  28. package/src/field-group/field-group.css +23 -7
  29. package/src/field-group/index.android.tsx +44 -38
  30. package/src/field-group/index.tsx +36 -5
  31. package/src/field-group/index.web.tsx +27 -11
  32. package/src/field-group/shared.tsx +49 -0
  33. package/src/field-group/types.ts +25 -0
  34. package/src/gauge/gauge.css +200 -0
  35. package/src/gauge/index.android.tsx +212 -0
  36. package/src/gauge/index.ios.tsx +40 -0
  37. package/src/gauge/index.tsx +165 -0
  38. package/src/gauge/shared.ts +79 -0
  39. package/src/gauge/types.ts +64 -0
  40. package/src/header-menu/index.tsx +76 -0
  41. package/src/host/index.tsx +32 -0
  42. package/src/index.ts +35 -6
  43. package/src/keyboard/index.tsx +81 -0
  44. package/src/keyboard/library.native.ts +17 -0
  45. package/src/keyboard/library.ts +12 -0
  46. package/src/keyboard/types.ts +20 -0
  47. package/src/list-item/index.android.tsx +23 -6
  48. package/src/list-item/index.tsx +20 -4
  49. package/src/list-item/index.web.tsx +60 -0
  50. package/src/list-item/list-item.css +65 -0
  51. package/src/list-item/types.ts +21 -0
  52. package/src/menu/index.android.tsx +22 -9
  53. package/src/menu/index.ios.tsx +26 -11
  54. package/src/menu/index.tsx +26 -10
  55. package/src/menu/list.tsx +25 -10
  56. package/src/menu/menu.css +52 -0
  57. package/src/menu/types.ts +41 -2
  58. package/src/progress/progress.css +9 -0
  59. package/src/scheme.ts +140 -0
  60. package/src/screen/header.tsx +1 -0
  61. package/src/screen/index.tsx +49 -5
  62. package/src/segmented/index.tsx +1 -1
  63. package/src/segmented/segmented.css +6 -7
  64. package/src/slider/index.tsx +1 -1
  65. package/src/stepper/index.tsx +1 -1
  66. package/src/switch/index.tsx +2 -0
  67. package/src/tab-stack/index.tsx +15 -2
  68. package/src/tabs/index.tsx +2 -1
  69. package/src/tabs/index.web.tsx +29 -4
  70. package/src/tabs/types.ts +20 -2
  71. package/src/text-field/index.android.tsx +30 -6
  72. package/src/text-field/index.ios.tsx +16 -3
  73. package/src/text-field/index.tsx +24 -4
  74. package/src/text-field/inline.tsx +87 -0
  75. package/src/text-field/shared.ts +40 -1
  76. package/src/text-field/types.ts +47 -2
  77. package/src/theme.ts +44 -5
  78. package/src/qr/index.tsx +0 -26
@@ -0,0 +1,432 @@
1
+ import type {GestureResponderEvent, LayoutChangeEvent} from 'react-native';
2
+ import type {RGBA} from './shared';
3
+
4
+ import {useCallback, useState} from 'react';
5
+ import {Pressable, StyleSheet, Text, TextInput, useColorScheme, View} from 'react-native';
6
+ import {Image} from 'expo-image';
7
+ import {fonts, fontWeights, useColor} from '../theme';
8
+ import {
9
+ checkerSvg,
10
+ grid,
11
+ gridColor,
12
+ parseColor,
13
+ spectrumColor,
14
+ spectrumPosition,
15
+ spectrumSvg,
16
+ svgDataUri,
17
+ toCss,
18
+ toHex,
19
+ useColorValue,
20
+ } from './shared';
21
+
22
+ /**
23
+ * The iOS system color picker (`UIColorPickerViewController`) redrawn with
24
+ * React Native views, shown in a bottom sheet on Android and web. A title
25
+ * row with a close button, a Grid / Spectrum / Sliders segmented control, an
26
+ * opacity slider and a footer with the preview swatch and saved colors.
27
+ * The spectrum and the checkerboard are static SVGs; the slider tracks are
28
+ * bands of solid segments, so dragging never decodes an image.
29
+ */
30
+ export interface ColorPickerSheetProps {
31
+ /** Title of the picker, the row's label on iOS. */
32
+ title: string;
33
+ /** Selected color as `#RRGGBB` or `#RRGGBBAA`. */
34
+ value: string;
35
+ /** Shows the opacity slider. */
36
+ supportsOpacity: boolean;
37
+ /** Called with the new hex whenever the user picks a color. */
38
+ onValueChange: (hex: string) => void;
39
+ /** Called from the close button. */
40
+ onClose: () => void;
41
+ /** Fixed content width (Android sizes the hosted React Native tree from its content). */
42
+ width?: number;
43
+ /** Identifier used to locate the sheet in end-to-end tests. */
44
+ testID?: string;
45
+ }
46
+
47
+ export type ColorPickerTab = 'grid' | 'spectrum' | 'sliders';
48
+
49
+ const TABS: {value: ColorPickerTab; label: string}[] = [
50
+ {value: 'grid', label: 'Grid'},
51
+ {value: 'spectrum', label: 'Spectrum'},
52
+ {value: 'sliders', label: 'Sliders'},
53
+ ];
54
+
55
+ const SPECTRUM = svgDataUri(spectrumSvg());
56
+ const CHECKER = svgDataUri(checkerSvg());
57
+ const THUMB = 28;
58
+ const TRACK = 36;
59
+ const THUMB_INSET = (TRACK - THUMB) / 2;
60
+ const SEGMENTS = 32;
61
+ const HANDLE = 28;
62
+ const clamp = (n: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, n));
63
+
64
+ /** Colors the user saved with the `+` button, shared by every picker for the session. */
65
+ let savedColors: RGBA[] = [];
66
+
67
+ const responder = (handler: (x: number, y: number) => void) => ({
68
+ onStartShouldSetResponder: () => true,
69
+ onMoveShouldSetResponder: () => true,
70
+ onResponderTerminationRequest: () => false,
71
+ onResponderGrant: (event: GestureResponderEvent) => handler(event.nativeEvent.locationX, event.nativeEvent.locationY),
72
+ onResponderMove: (event: GestureResponderEvent) => handler(event.nativeEvent.locationX, event.nativeEvent.locationY),
73
+ });
74
+
75
+ export function ColorPickerSheet({title, value, supportsOpacity, onValueChange, onClose, width, testID}: ColorPickerSheetProps) {
76
+ const [tab, setTab] = useState<ColorPickerTab>('grid');
77
+ const [color, setColor] = useColorValue(value, onValueChange, supportsOpacity);
78
+ const [saved, setSaved] = useState(savedColors);
79
+ const label = useColor('label');
80
+ const secondary = useColor('secondaryLabel');
81
+ const fill = useColor('pillBackground');
82
+ const separator = useColor('separator');
83
+ const css = toCss(color);
84
+
85
+ const save = () => {
86
+ savedColors = [...savedColors, color];
87
+ setSaved(savedColors);
88
+ };
89
+
90
+ return (
91
+ <View style={[styles.sheet, width != null ? {width} : styles.fill]} testID={testID}>
92
+ <View style={styles.header}>
93
+ <Text style={[styles.title, {color: label}]} numberOfLines={1} role="heading" accessible>{title}</Text>
94
+ <Pressable
95
+ role="button"
96
+ aria-label="Close"
97
+ onPress={onClose}
98
+ style={[styles.close, {backgroundColor: fill}]}>
99
+ <Text style={[styles.closeGlyph, {color: secondary}]}>✕</Text>
100
+ </Pressable>
101
+ </View>
102
+ <Tabs value={tab} onChange={setTab}/>
103
+ <View style={styles.section}>
104
+ {tab === 'grid' ? <Grid color={color} onChange={setColor}/> : null}
105
+ {tab === 'spectrum' ? <Spectrum color={color} onChange={setColor}/> : null}
106
+ {tab === 'sliders' ? <Sliders color={color} onChange={setColor}/> : null}
107
+ </View>
108
+ {supportsOpacity ? (
109
+ <View style={styles.section}>
110
+ <Text style={[styles.caption, {color: secondary}]}>OPACITY</Text>
111
+ <View style={styles.sliderRow}>
112
+ <Slider
113
+ label="Opacity"
114
+ value={color.a}
115
+ checkered
116
+ colorAt={t => toCss({...color, a: t})}
117
+ onChange={a => setColor({...color, a})}
118
+ />
119
+ <Field
120
+ label="Opacity percent"
121
+ value={`${Math.round(color.a * 100)}%`}
122
+ onCommit={text => setColor({...color, a: clamp(parseFloat(text) || 0, 0, 100) / 100})}
123
+ />
124
+ </View>
125
+ </View>
126
+ ) : null}
127
+ <View style={[styles.divider, {backgroundColor: separator}]}/>
128
+ <View style={styles.footer}>
129
+ <View style={styles.preview} aria-label={`Selected color ${toHex(color, supportsOpacity)}`}>
130
+ {color.a < 1 ? <Image source={{uri: CHECKER}} style={StyleSheet.absoluteFill} contentFit="cover"/> : null}
131
+ <View style={[StyleSheet.absoluteFill, {backgroundColor: css}]}/>
132
+ </View>
133
+ <View style={styles.saved}>
134
+ {saved.map((entry, index) => (
135
+ <Pressable
136
+ key={`${toHex(entry, true)}-${index}`}
137
+ role="button"
138
+ aria-label={`Saved color ${toHex(entry, true)}`}
139
+ onPress={() => setColor(entry)}
140
+ style={[styles.swatch, {backgroundColor: toCss(entry)}]}
141
+ />
142
+ ))}
143
+ <Pressable
144
+ role="button"
145
+ aria-label="Save color"
146
+ onPress={save}
147
+ style={[styles.swatch, {backgroundColor: fill}]}>
148
+ <Text style={[styles.plus, {color: secondary}]}>+</Text>
149
+ </Pressable>
150
+ </View>
151
+ </View>
152
+ </View>
153
+ );
154
+ }
155
+
156
+ function Tabs({value, onChange}: {value: ColorPickerTab; onChange: (tab: ColorPickerTab) => void}) {
157
+ const dark = useColorScheme() === 'dark';
158
+ const label = useColor('label');
159
+ const fill = useColor('pillBackground');
160
+ return (
161
+ <View style={[styles.tabs, {backgroundColor: fill}]} role="radiogroup" aria-label="Picker">
162
+ {TABS.map(tab => {
163
+ const selected = tab.value === value;
164
+ return (
165
+ <Pressable
166
+ key={tab.value}
167
+ role="radio"
168
+ aria-label={`${tab.label} tab`}
169
+ aria-checked={selected}
170
+ onPress={() => onChange(tab.value)}
171
+ style={[styles.tab, selected && [styles.tabSelected, {backgroundColor: dark ? '#636366' : '#ffffff'}]]}>
172
+ <Text style={[styles.tabLabel, {color: label}, selected && styles.tabLabelSelected]}>{tab.label}</Text>
173
+ </Pressable>
174
+ );
175
+ })}
176
+ </View>
177
+ );
178
+ }
179
+
180
+ function Grid({color, onChange}: {color: RGBA; onChange: (next: RGBA) => void}) {
181
+ const rows = Array.from({length: grid.rows}, (_, row) =>
182
+ Array.from({length: grid.columns}, (_, column) => gridColor(row, column)),
183
+ );
184
+ return (
185
+ <View style={styles.grid}>
186
+ {rows.map((cells, row) => (
187
+ <View key={row} style={styles.gridRow}>
188
+ {cells.map((cell, column) => {
189
+ const selected = cell.r === color.r && cell.g === color.g && cell.b === color.b;
190
+ return (
191
+ <Pressable
192
+ key={column}
193
+ role="button"
194
+ aria-label={`Color ${toHex({...cell, a: 1}, false)}`}
195
+ aria-selected={selected}
196
+ onPress={() => onChange({...cell, a: color.a})}
197
+ style={[styles.gridCell, {backgroundColor: toCss({...cell, a: 1})}]}>
198
+ {selected ? <View style={styles.gridSelected}/> : null}
199
+ </Pressable>
200
+ );
201
+ })}
202
+ </View>
203
+ ))}
204
+ </View>
205
+ );
206
+ }
207
+
208
+ function Spectrum({color, onChange}: {color: RGBA; onChange: (next: RGBA) => void}) {
209
+ const [size, setSize] = useState({width: 0, height: 0});
210
+ const position = spectrumPosition(color);
211
+ const pick = (x: number, y: number) => {
212
+ if (!size.width || !size.height) return;
213
+ onChange({...spectrumColor(x / size.width, y / size.height), a: color.a});
214
+ };
215
+ return (
216
+ <View
217
+ {...responder(pick)}
218
+ role="slider"
219
+ aria-label="Spectrum"
220
+ aria-valuetext={toHex(color, false)}
221
+ onLayout={(event: LayoutChangeEvent) => setSize(event.nativeEvent.layout)}
222
+ style={styles.spectrum}>
223
+ <Image source={{uri: SPECTRUM}} style={StyleSheet.absoluteFill} contentFit="fill"/>
224
+ {size.width > 0 ? (
225
+ <View
226
+ pointerEvents="none"
227
+ style={[
228
+ styles.handle,
229
+ {
230
+ left: clamp(position.x * size.width, HANDLE / 2, size.width - HANDLE / 2) - HANDLE / 2,
231
+ top: clamp(position.y * size.height, HANDLE / 2, size.height - HANDLE / 2) - HANDLE / 2,
232
+ backgroundColor: toCss({...color, a: 1}),
233
+ },
234
+ ]}
235
+ />
236
+ ) : null}
237
+ </View>
238
+ );
239
+ }
240
+
241
+ const CHANNELS = ['r', 'g', 'b'] as const;
242
+ const CHANNEL_NAMES = {r: 'Red', g: 'Green', b: 'Blue'} as const;
243
+
244
+ function Sliders({color, onChange}: {color: RGBA; onChange: (next: RGBA) => void}) {
245
+ const secondary = useColor('secondaryLabel');
246
+ return (
247
+ <View style={styles.sliders}>
248
+ {CHANNELS.map(channel => (
249
+ <View key={channel}>
250
+ <Text style={[styles.caption, {color: secondary}]}>{CHANNEL_NAMES[channel].toUpperCase()}</Text>
251
+ <View style={styles.sliderRow}>
252
+ <Slider
253
+ label={CHANNEL_NAMES[channel]}
254
+ value={color[channel] / 255}
255
+ colorAt={t => toCss({...color, a: 1, [channel]: t * 255})}
256
+ onChange={t => onChange({...color, [channel]: Math.round(t * 255)})}
257
+ />
258
+ <Field
259
+ label={`${CHANNEL_NAMES[channel]} value`}
260
+ value={String(color[channel])}
261
+ onCommit={text => onChange({...color, [channel]: clamp(Math.round(Number(text)) || 0, 0, 255)})}
262
+ />
263
+ </View>
264
+ </View>
265
+ ))}
266
+ <View style={styles.hexRow}>
267
+ <Text style={[styles.caption, styles.hexCaption, {color: secondary}]}>Display P3 Hex Color #</Text>
268
+ <Field
269
+ label="Hex color"
270
+ value={toHex(color, false).slice(1)}
271
+ wide
272
+ onCommit={text => {
273
+ if (!/^[0-9a-f]{3}$|^[0-9a-f]{6}$/i.test(text)) return;
274
+ onChange({...parseColor(text), a: color.a});
275
+ }}
276
+ />
277
+ </View>
278
+ </View>
279
+ );
280
+ }
281
+
282
+ interface SliderProps {
283
+ label: string;
284
+ /** Position `0…1`. */
285
+ value: number;
286
+ /** Color of the track at a position `0…1`. */
287
+ colorAt: (t: number) => string;
288
+ /** Draws a checkerboard under the track (for translucent colors). */
289
+ checkered?: boolean;
290
+ onChange: (value: number) => void;
291
+ }
292
+
293
+ /** A pill track banded with `SEGMENTS` solid colors and a ringed thumb, like the iOS color sliders. */
294
+ function Slider({label, value, colorAt, checkered, onChange}: SliderProps) {
295
+ const [width, setWidth] = useState(0);
296
+ // The thumb travels inside the pill, inset by the ring around it.
297
+ const travel = Math.max(0, width - THUMB - 2 * THUMB_INSET);
298
+ const pick = (x: number) => {
299
+ if (!travel) return;
300
+ onChange(clamp((x - THUMB_INSET - THUMB / 2) / travel, 0, 1));
301
+ };
302
+ return (
303
+ <View
304
+ {...responder(pick)}
305
+ role="slider"
306
+ aria-label={label}
307
+ aria-valuemin={0}
308
+ aria-valuemax={100}
309
+ aria-valuenow={Math.round(value * 100)}
310
+ onLayout={(event: LayoutChangeEvent) => setWidth(event.nativeEvent.layout.width)}
311
+ style={styles.track}>
312
+ {checkered ? <Image source={{uri: CHECKER}} style={StyleSheet.absoluteFill} contentFit="cover"/> : null}
313
+ <View style={styles.band} pointerEvents="none">
314
+ {Array.from({length: SEGMENTS}, (_, i) => (
315
+ <View key={i} style={[styles.segment, {backgroundColor: colorAt((i + 0.5) / SEGMENTS)}]}/>
316
+ ))}
317
+ </View>
318
+ {width > 0 ? (
319
+ <View
320
+ pointerEvents="none"
321
+ style={[styles.thumb, {left: THUMB_INSET + value * travel, backgroundColor: colorAt(value)}]}
322
+ />
323
+ ) : null}
324
+ </View>
325
+ );
326
+ }
327
+
328
+ interface FieldProps {
329
+ label: string;
330
+ value: string;
331
+ wide?: boolean;
332
+ /** Called with the typed text when editing ends or the return key is pressed. */
333
+ onCommit: (text: string) => void;
334
+ }
335
+
336
+ /** Rounded value box that commits when editing ends, like the iOS picker's fields. */
337
+ function Field({label, value, wide, onCommit}: FieldProps) {
338
+ const [text, setText] = useState(value);
339
+ // Follow the picked color while the field is not being edited.
340
+ const [seen, setSeen] = useState(value);
341
+ if (seen !== value) {
342
+ setSeen(value);
343
+ setText(value);
344
+ }
345
+ const fill = useColor('pillBackground');
346
+ const color = useColor('label');
347
+ const commit = useCallback(() => onCommit(text), [onCommit, text]);
348
+ return (
349
+ <TextInput
350
+ aria-label={label}
351
+ value={text}
352
+ onChangeText={setText}
353
+ onSubmitEditing={commit}
354
+ onBlur={commit}
355
+ selectTextOnFocus
356
+ style={[styles.field, wide && styles.fieldWide, {backgroundColor: fill, color}]}
357
+ />
358
+ );
359
+ }
360
+
361
+ const styles = StyleSheet.create({
362
+ sheet: {gap: 16},
363
+ fill: {alignSelf: 'stretch'},
364
+ header: {height: 44, alignItems: 'center', justifyContent: 'center'},
365
+ title: {
366
+ fontFamily: fonts?.sans,
367
+ fontSize: 17,
368
+ lineHeight: 22,
369
+ fontWeight: fontWeights.semibold,
370
+ marginHorizontal: 40,
371
+ textAlign: 'center',
372
+ },
373
+ close: {position: 'absolute', right: 0, width: 30, height: 30, borderRadius: 15, alignItems: 'center', justifyContent: 'center'},
374
+ closeGlyph: {fontFamily: fonts?.sans, fontSize: 15, lineHeight: 18, fontWeight: fontWeights.bold},
375
+ tabs: {flexDirection: 'row', padding: 2, borderRadius: 9},
376
+ tab: {flex: 1, height: 28, borderRadius: 7, alignItems: 'center', justifyContent: 'center'},
377
+ tabSelected: {
378
+ boxShadow: '0 1px 3px rgba(0, 0, 0, 0.12), 0 0 0 0.5px rgba(0, 0, 0, 0.04)',
379
+ },
380
+ tabLabel: {fontFamily: fonts?.sans, fontSize: 13, lineHeight: 18, fontWeight: fontWeights.medium},
381
+ tabLabelSelected: {fontWeight: fontWeights.semibold},
382
+ section: {gap: 8},
383
+ caption: {fontFamily: fonts?.sans, fontSize: 13, lineHeight: 18, fontWeight: fontWeights.normal, letterSpacing: 0.3},
384
+ grid: {aspectRatio: grid.columns / grid.rows, borderRadius: 10, overflow: 'hidden'},
385
+ gridRow: {flex: 1, flexDirection: 'row'},
386
+ gridCell: {flex: 1},
387
+ gridSelected: {flex: 1, borderWidth: 3, borderColor: '#ffffff', outlineWidth: 1, outlineColor: 'rgba(0, 0, 0, 0.35)', outlineOffset: -1},
388
+ spectrum: {aspectRatio: 361 / 337, borderRadius: 10, overflow: 'hidden'},
389
+ handle: {
390
+ position: 'absolute',
391
+ width: HANDLE,
392
+ height: HANDLE,
393
+ borderRadius: HANDLE / 2,
394
+ borderWidth: 3,
395
+ borderColor: '#ffffff',
396
+ boxShadow: '0 1px 4px rgba(0, 0, 0, 0.25)',
397
+ },
398
+ sliders: {gap: 12},
399
+ sliderRow: {flexDirection: 'row', alignItems: 'center', gap: 12},
400
+ track: {flex: 1, height: TRACK, borderRadius: TRACK / 2, overflow: 'hidden'},
401
+ band: {position: 'absolute', top: 0, right: 0, bottom: 0, left: 0, flexDirection: 'row'},
402
+ segment: {flex: 1},
403
+ thumb: {
404
+ position: 'absolute',
405
+ top: (TRACK - THUMB) / 2,
406
+ width: THUMB,
407
+ height: THUMB,
408
+ borderRadius: THUMB / 2,
409
+ borderWidth: 3,
410
+ borderColor: '#ffffff',
411
+ boxShadow: '0 1px 4px rgba(0, 0, 0, 0.25)',
412
+ },
413
+ field: {
414
+ width: 72,
415
+ height: TRACK,
416
+ borderRadius: 8,
417
+ paddingHorizontal: 8,
418
+ paddingVertical: 0,
419
+ fontFamily: fonts?.sans,
420
+ fontSize: 17,
421
+ textAlign: 'center',
422
+ },
423
+ fieldWide: {width: 112},
424
+ hexRow: {flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', gap: 12},
425
+ hexCaption: {flexShrink: 1},
426
+ divider: {height: StyleSheet.hairlineWidth},
427
+ footer: {flexDirection: 'row', gap: 16},
428
+ preview: {width: 72, height: 72, borderRadius: 12, overflow: 'hidden'},
429
+ saved: {flex: 1, flexDirection: 'row', flexWrap: 'wrap', gap: 12, alignContent: 'flex-start'},
430
+ swatch: {width: 30, height: 30, borderRadius: 15, alignItems: 'center', justifyContent: 'center'},
431
+ plus: {fontFamily: fonts?.sans, fontSize: 20, lineHeight: 24, fontWeight: fontWeights.medium},
432
+ });
@@ -0,0 +1,40 @@
1
+ import type {StyleProp, ViewStyle} from 'react-native';
2
+
3
+ /**
4
+ * Cross-platform color picker: a row with a label and a color well that
5
+ * opens the system color picker.
6
+ *
7
+ * Bridges the SwiftUI `ColorPicker` on iOS. Android (Jetpack Compose) and
8
+ * web (DOM) redraw the iOS row — the label and the well — and open a sheet
9
+ * that reproduces the iOS picker: Grid, Spectrum and Sliders tabs, the
10
+ * opacity slider and the preview swatch with saved colors.
11
+ * A controlled control: pair `value` with `onValueChange`.
12
+ */
13
+ export interface ColorPickerProps {
14
+ /** Label rendered at the leading edge of the row, and the title of the picker. */
15
+ label?: string;
16
+ /** Selected color as `#RRGGBB` or `#RRGGBBAA`. */
17
+ value: string;
18
+ /**
19
+ * Called with the new color whenever the user picks one, as `#RRGGBBAA`
20
+ * when `supportsOpacity` is on and `#RRGGBB` otherwise.
21
+ */
22
+ onValueChange: (value: string) => void;
23
+ /**
24
+ * Shows the opacity slider and reports the alpha channel.
25
+ * @default true
26
+ */
27
+ supportsOpacity?: boolean;
28
+ /**
29
+ * Preset colors (`#RRGGBB`) drawn as a row of round swatches before the
30
+ * well on every platform, the selected one ringed; tapping one picks it.
31
+ * The well still opens the full picker for any other color.
32
+ */
33
+ swatches?: string[];
34
+ /** Disables interaction. */
35
+ disabled?: boolean;
36
+ /** Identifier used to locate the component in end-to-end tests. */
37
+ testID?: string;
38
+ /** Style applied to the row container (web only). */
39
+ style?: StyleProp<ViewStyle>;
40
+ }
@@ -2,26 +2,54 @@ import type {ContextMenuProps} from '../menu/types';
2
2
 
3
3
  import {useState} from 'react';
4
4
  import {Box, DropdownMenu} from '@expo/ui/jetpack-compose';
5
- import {combinedClickable, testID as testIDModifier} from '@expo/ui/jetpack-compose/modifiers';
5
+ import {combinedClickable, matchParentSize, offset, size, testID as testIDModifier} from '@expo/ui/jetpack-compose/modifiers';
6
6
  import {MenuItems} from '../menu/index.android';
7
7
 
8
8
  /**
9
9
  * Android wraps `children` in a `Box` with `combinedClickable`, so a
10
- * long-press expands a Material 3 `DropdownMenu` anchored to it while a tap
11
- * goes to `onPress`. `children` must be Compose content.
10
+ * long-press expands a Material 3 `DropdownMenu` while a tap goes to
11
+ * `onPress`. `children` must be Compose content.
12
+ *
13
+ * The dropdown is anchored to an invisible box laid over the content: the
14
+ * size of the content for a long-press (the menu opens below it, as a
15
+ * Compose menu does), or a zero-size box offset to the `at` point, which is
16
+ * how the menu opens where a canvas says it was asked for.
12
17
  */
13
- export function ContextMenu({items, children, onPress, disabled, testID}: ContextMenuProps) {
18
+ export function ContextMenu({items, children, onPress, disabled, at, onDismiss, testID}: ContextMenuProps) {
14
19
  const [expanded, setExpanded] = useState(false);
20
+ const [point, setPoint] = useState(at ?? null);
21
+ // A new `at` (including one given at mount) opens the menu there; derived
22
+ // from the prop as it changes.
23
+ const [seen, setSeen] = useState<ContextMenuProps['at']>(undefined);
24
+ if (at !== seen) {
25
+ setSeen(at);
26
+ if (at && !disabled) {
27
+ setPoint(at);
28
+ setExpanded(true);
29
+ }
30
+ }
31
+ const close = () => {
32
+ setExpanded(false);
33
+ onDismiss?.();
34
+ };
35
+
15
36
  const modifiers = [
16
- ...(disabled ? [] : [combinedClickable({onClick: onPress, onLongClick: () => setExpanded(true)})]),
37
+ ...(disabled ? [] : [combinedClickable({onClick: onPress, onLongClick: () => {
38
+ setPoint(null);
39
+ setExpanded(true);
40
+ }})]),
17
41
  ...(testID ? [testIDModifier(testID)] : []),
18
42
  ];
43
+
19
44
  return (
20
- <DropdownMenu expanded={expanded} onDismissRequest={() => setExpanded(false)}>
21
- <DropdownMenu.Trigger>
22
- <Box modifiers={modifiers}>{children}</Box>
23
- </DropdownMenu.Trigger>
24
- <MenuItems items={items} onClose={() => setExpanded(false)}/>
25
- </DropdownMenu>
45
+ <Box modifiers={modifiers}>
46
+ {children}
47
+ <DropdownMenu
48
+ expanded={expanded}
49
+ onDismissRequest={close}
50
+ modifiers={point ? [offset(point.x, point.y), size(0, 0)] : [matchParentSize()]}>
51
+ <MenuItems items={items} onClose={close}/>
52
+ </DropdownMenu>
53
+ </Box>
26
54
  );
27
55
  }
@@ -7,7 +7,9 @@ import {MenuItems} from '../menu/index.ios';
7
7
  /**
8
8
  * iOS renders SwiftUI's `contextMenu`: a long-press on `children` lifts it
9
9
  * into a preview with the entries beneath. `children` must be SwiftUI
10
- * content. A plain tap is passed to `onPress` through `onTapGesture`.
10
+ * content. A plain tap is passed to `onPress` through `onTapGesture`. SwiftUI
11
+ * has no menu at a point, so `at` is ignored here (the long-press stays the
12
+ * trigger) and `onDismiss` is not reported.
11
13
  */
12
14
  export function ContextMenu({items, children, onPress, disabled, testID}: ContextMenuProps) {
13
15
  if (disabled) return <>{children}</>;
@@ -1,21 +1,24 @@
1
1
  import '../menu/menu.css';
2
2
  import type {MouseEvent, PointerEvent} from 'react';
3
3
  import type {ContextMenuProps} from '../menu/types';
4
- import {useId, useRef, useState} from 'react';
4
+ import {useEffect, useId, useRef, useState} from 'react';
5
5
  import {MenuList, menuIdent} from '../menu/list';
6
6
 
7
7
  const LONG_PRESS_MS = 500;
8
8
 
9
9
  /**
10
10
  * On web the entries live in the same native `popover="auto"` element as
11
- * `Menu`, opened with `showPopover()` on right-click (`contextmenu`) or a
12
- * touch long-press and placed at the pointer. The browser still owns the top
13
- * layer and light dismiss. The wrapper is `display: contents`, so it doesn't
14
- * affect the layout of `children`.
11
+ * `Menu`, opened with `showPopover()` on right-click (`contextmenu`), a touch
12
+ * long-press, or the `at` prop, and placed at the pointer. The browser still
13
+ * owns the top layer and light dismiss. The wrapper is `display: contents`,
14
+ * so it doesn't affect the layout of `children`; `at` is measured against
15
+ * the content's first element (its own coordinates), falling back to the
16
+ * viewport when the content has no box of its own.
15
17
  */
16
- export function ContextMenu({items, children, onPress, disabled, testID}: ContextMenuProps) {
18
+ export function ContextMenu({items, children, onPress, disabled, at, onDismiss, testID}: ContextMenuProps) {
17
19
  const ident = menuIdent(useId());
18
20
  const popover = useRef<HTMLDivElement>(null);
21
+ const wrapper = useRef<HTMLDivElement>(null);
19
22
  const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
20
23
  const [position, setPosition] = useState<{x: number; y: number} | null>(null);
21
24
 
@@ -39,8 +42,22 @@ export function ContextMenu({items, children, onPress, disabled, testID}: Contex
39
42
  timer.current = null;
40
43
  };
41
44
 
45
+ // A point the content reports (a canvas's own right click): relative to
46
+ // the content's box, which `display: contents` leaves to its first child
47
+ // (the popover itself when the content has no element of its own, in which
48
+ // case the point is taken as viewport coordinates).
49
+ useEffect(() => {
50
+ if (!at || disabled) return;
51
+ const content = wrapper.current!.firstElementChild;
52
+ const rect = content !== popover.current ? content!.getBoundingClientRect() : {left: 0, top: 0};
53
+ setPosition({x: rect.left + at.x, y: rect.top + at.y});
54
+ const element = popover.current;
55
+ if (element && !element.matches(':popover-open')) element.showPopover();
56
+ }, [at, disabled]);
57
+
42
58
  return (
43
59
  <div
60
+ ref={wrapper}
44
61
  className="ui-context-menu"
45
62
  onContextMenu={onContextMenu}
46
63
  onPointerDown={onPointerDown}
@@ -50,7 +67,7 @@ export function ContextMenu({items, children, onPress, disabled, testID}: Contex
50
67
  onClick={disabled ? undefined : onPress}
51
68
  data-testid={testID}>
52
69
  {children}
53
- <MenuList id={ident} items={items} position={position} popoverRef={popover}/>
70
+ <MenuList id={ident} items={items} position={position} popoverRef={popover} onClose={onDismiss}/>
54
71
  </div>
55
72
  );
56
73
  }
@@ -0,0 +1,72 @@
1
+ /*
2
+ * The Material floating action button geometry on the web: a 56px circle
3
+ * (40 small, 96 large) or a 56px capsule when extended, filled with the tint,
4
+ * the icon and label in the contrast color, lifted by a soft shadow. Where it
5
+ * floats is the screen's job (`Screen`'s `fab` slot fixes it to the viewport).
6
+ */
7
+ .ui-fab__anchor {
8
+ display: inline-flex;
9
+ }
10
+
11
+ .ui-fab {
12
+ -webkit-appearance: none;
13
+ appearance: none;
14
+ display: inline-flex;
15
+ flex-direction: row;
16
+ align-items: center;
17
+ justify-content: center;
18
+ gap: 12px;
19
+ box-sizing: border-box;
20
+ width: 56px;
21
+ height: 56px;
22
+ margin: 0;
23
+ padding: 0;
24
+ border: none;
25
+ border-radius: 50%;
26
+ background: var(--color-tint);
27
+ color: var(--color-on-tint);
28
+ box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
29
+ font-family: var(--font-display);
30
+ font-size: 15px;
31
+ font-weight: 600;
32
+ line-height: 1.2;
33
+ white-space: nowrap;
34
+ cursor: pointer;
35
+ user-select: none;
36
+ transition: filter 0.15s ease, opacity 0.15s ease;
37
+ }
38
+
39
+ .ui-fab--small {
40
+ width: 40px;
41
+ height: 40px;
42
+ }
43
+
44
+ .ui-fab--large {
45
+ width: 96px;
46
+ height: 96px;
47
+ }
48
+
49
+ .ui-fab--extended {
50
+ width: auto;
51
+ height: 56px;
52
+ padding: 0 20px;
53
+ border-radius: 999px;
54
+ }
55
+
56
+ .ui-fab:hover:not(:disabled) {
57
+ filter: brightness(1.08);
58
+ }
59
+
60
+ .ui-fab:active:not(:disabled) {
61
+ filter: brightness(0.92);
62
+ }
63
+
64
+ .ui-fab:focus-visible {
65
+ outline: 2px solid var(--color-label);
66
+ outline-offset: 2px;
67
+ }
68
+
69
+ .ui-fab:disabled {
70
+ opacity: 0.45;
71
+ cursor: default;
72
+ }