@vvedantb/native-ui 0.25.2 → 0.26.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.
@@ -1,4 +1,4 @@
1
- import { useState, type ReactNode } from "react";
1
+ import type { ReactNode } from "react";
2
2
  import { Modal, Pressable, StyleSheet, Text, View, useWindowDimensions } from "react-native";
3
3
  import { useSafeAreaInsets } from "react-native-safe-area-context";
4
4
  import { cn } from "../cn";
@@ -10,6 +10,7 @@ import { useReduceMotion } from "../useReduceMotion";
10
10
  import { useScheme } from "../useScheme";
11
11
  import { chatTextSize } from "./chat-text";
12
12
  import { MESSAGE_MENU, messageContextMenuLayout, type MessageMenu, type MessageMenuRect } from "./message-menu-layout";
13
+ import { usePressed } from "../usePressed";
13
14
 
14
15
  /**
15
16
  * The iMessage hold menu for a chat message: the chat fades under a frosted
@@ -193,7 +194,7 @@ function MenuCardRow({
193
194
  }) {
194
195
  const scheme = useScheme();
195
196
  // A state, not a style function: with a `className`, NativeWind drops a style function.
196
- const [pressed, setPressed] = useState(false);
197
+ const press = usePressed();
197
198
  const disabled = action.disabled === true;
198
199
  const Icon = action.icon;
199
200
  const color = action.destructive ? palette.danger[scheme] : tokens.color.foreground[scheme];
@@ -205,14 +206,14 @@ function MenuCardRow({
205
206
  accessibilityState={{ disabled }}
206
207
  disabled={disabled}
207
208
  onPress={onPress}
208
- onPressIn={() => setPressed(true)}
209
- onPressOut={() => setPressed(false)}
209
+ onPressIn={press.onPressIn}
210
+ onPressOut={press.onPressOut}
210
211
  className="flex-row items-center gap-3 px-4"
211
212
  style={[
212
213
  {
213
214
  minHeight: rowHeight,
214
215
  opacity: disabled ? 0.5 : 1,
215
- backgroundColor: pressed && !disabled ? palette.default[scheme] : "transparent",
216
+ backgroundColor: press.pressed && !disabled ? palette.default[scheme] : "transparent",
216
217
  },
217
218
  !first && { borderTopWidth: StyleSheet.hairlineWidth, borderTopColor: tokens.color.separator[scheme] },
218
219
  ]}
@@ -24,6 +24,7 @@ import { useOptionalConversation } from "./conversation-context";
24
24
  import { MessageContextMenu } from "./message-context-menu";
25
25
  import type { MessageMenu, MessageMenuRect } from "./message-menu-layout";
26
26
  import { ChatTextColorContext, ResponseText } from "./response-text";
27
+ import { usePressed } from "../usePressed";
27
28
 
28
29
  export type MessageRole = "user" | "assistant" | "system";
29
30
 
@@ -138,7 +139,7 @@ export function Message({
138
139
  }: MessageProps) {
139
140
  const user = from === "user";
140
141
  const [sheetOpen, setSheetOpen] = useState(false);
141
- const [pressing, setPressing] = useState(false);
142
+ const press = usePressed();
142
143
  // The open context menu: where the message was and what it offers, read at the hold.
143
144
  const [held, setHeld] = useState<{ anchor: MessageMenuRect; menu: MessageMenu } | null>(null);
144
145
  // Measured at the hold: the menu draws the message again where it is on screen.
@@ -183,9 +184,9 @@ export function Message({
183
184
  className={merged}
184
185
  // While held it dims a little: the feedback before the menu opens. A state, not a
185
186
  // style function: with a `className`, NativeWind drops a style function, layout and all.
186
- style={[layout, pressing ? HELD_STYLE : null]}
187
- onPressIn={() => setPressing(true)}
188
- onPressOut={() => setPressing(false)}
187
+ style={[layout, press.pressed ? HELD_STYLE : null]}
188
+ onPressIn={press.onPressIn}
189
+ onPressOut={press.onPressOut}
189
190
  onLongPress={longPress}
190
191
  delayLongPress={HOLD_DELAY_MS}
191
192
  // TalkBack's actions menu reaches the same sheet as a long press.
@@ -8,6 +8,7 @@ import { withAlpha } from "../palette";
8
8
  import { tokens } from "../tokens";
9
9
  import { Input as FieldInput } from "../primitives/input";
10
10
  import { COMPOSER_INPUT_ID, KeyboardAvoidingView, KeyboardLiftContext } from "../primitives/keyboard-parts";
11
+ import { usePasteParts, type PasteParts } from "../primitives/paste-parts";
11
12
  import { usePressFeedback } from "../motion";
12
13
  import { useControllableState } from "../primitives/use-controllable-state";
13
14
  import { useLargerText } from "../theme";
@@ -254,6 +255,12 @@ export type PromptInputProps = {
254
255
  * while the picker is open.
255
256
  */
256
257
  menu?: ReactNode;
258
+ /**
259
+ * Images pasted into the field (long-press Paste, or the keyboard's image
260
+ * insert), as `file://` URIs. Needs `PasteInputProvider` from
261
+ * `@vvedantb/native-ui/paste-input`; without it a field takes text only.
262
+ */
263
+ onPasteImages?: (uris: string[]) => void;
257
264
  /**
258
265
  * `stacked` (default): the field over a footer of tools and send, the web
259
266
  * `PromptInput`. `inline`: one row, as the web `Composer layout="inline"`
@@ -324,6 +331,7 @@ export function PromptInput({
324
331
  toolbar,
325
332
  footer,
326
333
  menu,
334
+ onPasteImages,
327
335
  layout = "stacked",
328
336
  shape = "rounded",
329
337
  leading,
@@ -337,6 +345,9 @@ export function PromptInput({
337
345
  const largerText = useLargerText();
338
346
  const insets = useSafeAreaInsets();
339
347
  const lifted = useContext(KeyboardLiftContext);
348
+ const paste = usePasteParts();
349
+ // The field inside the paste target when an app takes pasted images; it then fills the target.
350
+ const pasteTarget = paste && onPasteImages ? paste.Target : null;
340
351
  const [text, setText] = useControllableState({ value, defaultValue, onChange: onChangeText });
341
352
  const message = text.trim();
342
353
  const canSubmit = message.length > 0 && !disabled && !isGenerating(status);
@@ -393,6 +404,57 @@ export function PromptInput({
393
404
  {leading}
394
405
  </View>
395
406
  ) : null}
407
+ <PasteTarget target={pasteTarget} onPasteImages={onPasteImages} style={PASTE_TARGET_INLINE}>
408
+ <FieldInput
409
+ testID={testID ? `${testID}-input` : undefined}
410
+ nativeID={COMPOSER_INPUT_ID}
411
+ multiline
412
+ numberOfLines={ONE_ROW}
413
+ value={text}
414
+ onChangeText={changeText}
415
+ placeholder={placeholder}
416
+ placeholderTextColor={pill ? withAlpha(tokens.color.muted[scheme], PILL.placeholderAlpha) : undefined}
417
+ accessibilityLabel={accessibilityLabel}
418
+ editable={!disabled}
419
+ textAlignVertical="center"
420
+ scrollEnabled={scrollEnabled}
421
+ onContentSizeChange={(e) => setContentHeight(e.nativeEvent.contentSize.height)}
422
+ className={cn(
423
+ // In the row the field grows across; in a paste target, the target does and the field fills it.
424
+ pasteTarget ? "border-0 text-sm font-instrument-sans" : "flex-1 border-0 text-sm font-instrument-sans",
425
+ pill ? "min-h-9 px-1 py-2" : "min-h-8 px-2 py-1",
426
+ largerText && "min-h-11 py-2 text-xl font-instrument-sans-medium",
427
+ )}
428
+ style={[
429
+ fieldTextStyle(scheme),
430
+ fieldText,
431
+ largerText ? typeStyle("medium") : null,
432
+ {
433
+ backgroundColor: "transparent",
434
+ maxHeight,
435
+ ...growStyle,
436
+ },
437
+ ]}
438
+ />
439
+ </PasteTarget>
440
+ <View className="shrink-0 flex-row items-center gap-1" style={pill ? { minHeight: PILL.lineHeight } : null}>
441
+ {toolbar}
442
+ <PromptInputSubmit
443
+ status={status}
444
+ onSubmit={submit}
445
+ onStop={onStop}
446
+ canSubmit={canSubmit}
447
+ size={pill ? "md" : undefined}
448
+ />
449
+ </View>
450
+ </View>
451
+ </View>
452
+ ) : (
453
+ <View className="border" style={fieldStyle(scheme)}>
454
+ {replyTo ? <View className="px-2 pt-2">{replyTo}</View> : null}
455
+ {header ? <View className="flex-row flex-wrap gap-1 px-2 pt-2">{header}</View> : null}
456
+ {/* The `Textarea` field without its `numberOfLines` (default 4), which on Android sets the line count: this one starts at one line and grows. */}
457
+ <PasteTarget target={pasteTarget} onPasteImages={onPasteImages}>
396
458
  <FieldInput
397
459
  testID={testID ? `${testID}-input` : undefined}
398
460
  nativeID={COMPOSER_INPUT_ID}
@@ -401,16 +463,14 @@ export function PromptInput({
401
463
  value={text}
402
464
  onChangeText={changeText}
403
465
  placeholder={placeholder}
404
- placeholderTextColor={pill ? withAlpha(tokens.color.muted[scheme], PILL.placeholderAlpha) : undefined}
405
466
  accessibilityLabel={accessibilityLabel}
406
467
  editable={!disabled}
407
- textAlignVertical="center"
468
+ textAlignVertical="top"
408
469
  scrollEnabled={scrollEnabled}
409
470
  onContentSizeChange={(e) => setContentHeight(e.nativeEvent.contentSize.height)}
410
471
  className={cn(
411
- "flex-1 border-0 text-sm font-instrument-sans",
412
- pill ? "min-h-9 px-1 py-2" : "min-h-8 px-2 py-1",
413
- largerText && "min-h-11 py-2 text-xl font-instrument-sans-medium",
472
+ "min-h-11 border-0 px-3.5 py-2.5 text-base font-instrument-sans",
473
+ largerText && "min-h-14 px-4 py-3 text-xl font-instrument-sans-medium",
414
474
  )}
415
475
  style={[
416
476
  fieldTextStyle(scheme),
@@ -423,51 +483,7 @@ export function PromptInput({
423
483
  },
424
484
  ]}
425
485
  />
426
- <View className="shrink-0 flex-row items-center gap-1" style={pill ? { minHeight: PILL.lineHeight } : null}>
427
- {toolbar}
428
- <PromptInputSubmit
429
- status={status}
430
- onSubmit={submit}
431
- onStop={onStop}
432
- canSubmit={canSubmit}
433
- size={pill ? "md" : undefined}
434
- />
435
- </View>
436
- </View>
437
- </View>
438
- ) : (
439
- <View className="border" style={fieldStyle(scheme)}>
440
- {replyTo ? <View className="px-2 pt-2">{replyTo}</View> : null}
441
- {header ? <View className="flex-row flex-wrap gap-1 px-2 pt-2">{header}</View> : null}
442
- {/* The `Textarea` field without its `numberOfLines` (default 4), which on Android sets the line count: this one starts at one line and grows. */}
443
- <FieldInput
444
- testID={testID ? `${testID}-input` : undefined}
445
- nativeID={COMPOSER_INPUT_ID}
446
- multiline
447
- numberOfLines={ONE_ROW}
448
- value={text}
449
- onChangeText={changeText}
450
- placeholder={placeholder}
451
- accessibilityLabel={accessibilityLabel}
452
- editable={!disabled}
453
- textAlignVertical="top"
454
- scrollEnabled={scrollEnabled}
455
- onContentSizeChange={(e) => setContentHeight(e.nativeEvent.contentSize.height)}
456
- className={cn(
457
- "min-h-11 border-0 px-3.5 py-2.5 text-base font-instrument-sans",
458
- largerText && "min-h-14 px-4 py-3 text-xl font-instrument-sans-medium",
459
- )}
460
- style={[
461
- fieldTextStyle(scheme),
462
- fieldText,
463
- largerText ? typeStyle("medium") : null,
464
- {
465
- backgroundColor: "transparent",
466
- maxHeight,
467
- ...growStyle,
468
- },
469
- ]}
470
- />
486
+ </PasteTarget>
471
487
  <View className="flex-row items-center justify-between gap-1 px-2 pb-2">
472
488
  <View className="min-w-0 shrink flex-row items-center gap-1">{toolbar}</View>
473
489
  <PromptInputSubmit status={status} onSubmit={submit} onStop={onStop} canSubmit={canSubmit} />
@@ -488,3 +504,26 @@ export function PromptInput({
488
504
  </KeyboardAvoidingView>
489
505
  );
490
506
  }
507
+
508
+ /** In the inline row the paste target takes the field's place: it grows across, and the field fills it. */
509
+ const PASTE_TARGET_INLINE = { flex: 1, minWidth: 0 };
510
+
511
+ /** The field inside the app's paste target (`PasteInputProvider`), or the field alone. */
512
+ function PasteTarget({
513
+ target: Target,
514
+ onPasteImages,
515
+ style,
516
+ children,
517
+ }: {
518
+ target: PasteParts["Target"] | null;
519
+ onPasteImages: ((uris: string[]) => void) | undefined;
520
+ style?: { flex: number; minWidth: number };
521
+ children: ReactNode;
522
+ }) {
523
+ if (!Target || !onPasteImages) return children;
524
+ return (
525
+ <Target onPasteImages={onPasteImages} style={style}>
526
+ {children}
527
+ </Target>
528
+ );
529
+ }
@@ -15,6 +15,7 @@ import {
15
15
  type CollapsibleTriggerProps,
16
16
  } from "./collapsible";
17
17
  import { ResponseText } from "./response-text";
18
+ import { usePressed } from "../usePressed";
18
19
 
19
20
  /** The sources a reply used, folded (default closed). */
20
21
  export function Sources({ className, ...props }: CollapsibleProps) {
@@ -75,6 +76,7 @@ const ROW_HEIGHT = 36;
75
76
  * Pressed, it takes the web hover fill (`bg-surface-secondary`).
76
77
  */
77
78
  export function Source({ href, title, onPress, testID }: SourceProps) {
79
+ const press = usePressed();
78
80
  const scheme = useScheme();
79
81
  const largerText = useLargerText();
80
82
  const label = title ?? href;
@@ -85,11 +87,13 @@ export function Source({ href, title, onPress, testID }: SourceProps) {
85
87
  accessibilityLabel={label}
86
88
  hitSlop={touchSlop(MIN_TOUCH_TARGET, ROW_HEIGHT)}
87
89
  onPress={() => (onPress ? onPress(href) : void Linking.openURL(href))}
90
+ onPressIn={press.onPressIn}
91
+ onPressOut={press.onPressOut}
88
92
  className="flex-row items-center justify-between gap-2 px-3 py-2"
89
- style={({ pressed }) => ({
93
+ style={{
90
94
  borderRadius: tokens.radius.md,
91
- backgroundColor: pressed ? tokens.color.surfaceSecondary[scheme] : "transparent",
92
- })}
95
+ backgroundColor: press.pressed ? tokens.color.surfaceSecondary[scheme] : "transparent",
96
+ }}
93
97
  >
94
98
  <ResponseText size="small" tone="muted" selectable={false} numberOfLines={1} className="shrink">
95
99
  {label}
@@ -7,6 +7,7 @@ import { useLargerText } from "../theme";
7
7
  import { tokens } from "../tokens";
8
8
  import { useScheme } from "../useScheme";
9
9
  import { chatTextSize } from "./chat-text";
10
+ import { usePressed } from "../usePressed";
10
11
 
11
12
  export type SuggestionsProps = {
12
13
  /** `horizontal` (default) scrolls one row of chips; `vertical` stacks them full width. */
@@ -61,6 +62,7 @@ const CHIP_HEIGHT = 32;
61
62
  * it takes the web hover (`bg-surface-tertiary text-foreground`).
62
63
  */
63
64
  export function Suggestion({ suggestion, onPress, children, disabled, testID }: SuggestionProps) {
65
+ const press = usePressed();
64
66
  const scheme = useScheme();
65
67
  const largerText = useLargerText();
66
68
  const size = chatTextSize("small", largerText);
@@ -74,11 +76,13 @@ export function Suggestion({ suggestion, onPress, children, disabled, testID }:
74
76
  hitSlop={touchSlop(MIN_TOUCH_TARGET, CHIP_HEIGHT)}
75
77
  onPress={() => onPress?.(suggestion)}
76
78
  className={cn("justify-center border", largerText ? "min-h-12 px-4 py-2" : "min-h-8 px-3 py-1.5", disabled && "opacity-45")}
77
- style={({ pressed }) => ({
79
+ onPressIn={press.onPressIn}
80
+ onPressOut={press.onPressOut}
81
+ style={{
78
82
  borderRadius: tokens.radius.pill,
79
83
  borderColor: tokens.color.border[scheme],
80
- backgroundColor: pressed ? palette.surfaceTertiary[scheme] : tokens.color.surfaceSecondary[scheme],
81
- })}
84
+ backgroundColor: press.pressed ? palette.surfaceTertiary[scheme] : tokens.color.surfaceSecondary[scheme],
85
+ }}
82
86
  >
83
87
  {({ pressed }) => (
84
88
  <Text
@@ -0,0 +1,4 @@
1
+ // `@vvedantb/native-ui/paste-input`. Imports `expo-paste-input` (an optional
2
+ // peer with native code): the only entry that does, so an app without it keeps
3
+ // text-only paste and bundles as before.
4
+ export { PasteInputProvider } from "../primitives/paste-input";
@@ -0,0 +1,29 @@
1
+ import { TextInputWrapper, type PasteEventPayload } from "expo-paste-input";
2
+ import type { ReactNode } from "react";
3
+ import { PastePartsContext, type PasteParts } from "./paste-parts";
4
+
5
+ // The image paste on `expo-paste-input`: on Android an `OnReceiveContentListener`
6
+ // on the wrapped field (Paste, and the keyboard's image insert), on iOS the
7
+ // field's `paste(_:)` and keyboard stickers. Text paste stays the field's own.
8
+
9
+ const Target: PasteParts["Target"] = ({ onPasteImages, style, children }) => (
10
+ <TextInputWrapper
11
+ style={style}
12
+ onPaste={(payload: PasteEventPayload) => {
13
+ if (payload.type === "images" && payload.uris.length > 0) onPasteImages(payload.uris);
14
+ }}
15
+ >
16
+ {children}
17
+ </TextInputWrapper>
18
+ );
19
+
20
+ const PARTS: PasteParts = { Target };
21
+
22
+ /**
23
+ * Image paste for the kit's text fields (`PromptInput` `onPasteImages`). Wrap
24
+ * the app once, under `ThemeProvider`. Needs `expo-paste-input` and a native
25
+ * build (not Expo Go); on the web it does nothing.
26
+ */
27
+ export function PasteInputProvider({ children }: { children: ReactNode }) {
28
+ return <PastePartsContext.Provider value={PARTS}>{children}</PastePartsContext.Provider>;
29
+ }
@@ -0,0 +1,27 @@
1
+ import { createContext, useContext, type ComponentType, type ReactNode } from "react";
2
+ import type { StyleProp, ViewStyle } from "react-native";
3
+
4
+ /**
5
+ * The `expo-paste-input` build of a text field's image paste, from
6
+ * `@vvedantb/native-ui/paste-input`. React Native's `TextInput` takes no
7
+ * pasted image: the long-press Paste and the keyboard's image insert (Gboard's
8
+ * clipboard, stickers) do nothing. `Target` wraps a field so they hand the
9
+ * images over as local file URIs. Without it (the peer is optional, and needs
10
+ * a native build) a field keeps text paste only. A context, so only that entry
11
+ * imports the peer and an app without it bundles as before.
12
+ */
13
+ export type PasteParts = {
14
+ Target: ComponentType<{
15
+ /** The pasted images, as `file://` URIs in the app's cache. */
16
+ onPasteImages: (uris: string[]) => void;
17
+ style?: StyleProp<ViewStyle>;
18
+ children: ReactNode;
19
+ }>;
20
+ };
21
+
22
+ export const PastePartsContext = createContext<PasteParts | null>(null);
23
+
24
+ /** The image paste parts from `PasteInputProvider`, or null without it. */
25
+ export function usePasteParts(): PasteParts | null {
26
+ return useContext(PastePartsContext);
27
+ }
@@ -0,0 +1,13 @@
1
+ import { useState } from "react";
2
+
3
+ /**
4
+ * Whether a `Pressable` is held down, as state, for a pressed look. With a
5
+ * `className`, NativeWind drops a `Pressable` style function (`({ pressed })
6
+ * => ...`), and with it the radius, border and fill it sets; so a pressable
7
+ * with classes reads `pressed` from here and passes a plain style. Spread
8
+ * `onPressIn` and `onPressOut` on the `Pressable`.
9
+ */
10
+ export function usePressed() {
11
+ const [pressed, setPressed] = useState(false);
12
+ return { pressed, onPressIn: () => setPressed(true), onPressOut: () => setPressed(false) };
13
+ }