@farcaster/snap 2.4.0 → 2.5.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 (58) hide show
  1. package/dist/button-orientation-utils.d.ts +3 -0
  2. package/dist/button-orientation-utils.js +25 -0
  3. package/dist/constants.d.ts +1 -0
  4. package/dist/constants.js +1 -0
  5. package/dist/react/components/action-button.js +5 -5
  6. package/dist/react/components/cell-grid.js +6 -2
  7. package/dist/react/components/item-group.js +2 -1
  8. package/dist/react/components/item-layout-context.d.ts +2 -0
  9. package/dist/react/components/item-layout-context.js +7 -0
  10. package/dist/react/components/item.js +40 -3
  11. package/dist/react/components/stack.js +46 -37
  12. package/dist/react/components/toggle-group.js +6 -4
  13. package/dist/react-native/components/item-layout-context.d.ts +2 -0
  14. package/dist/react-native/components/item-layout-context.js +6 -0
  15. package/dist/react-native/components/snap-action-button.js +15 -2
  16. package/dist/react-native/components/snap-cell-grid.js +30 -4
  17. package/dist/react-native/components/snap-item-group.js +16 -6
  18. package/dist/react-native/components/snap-item.js +56 -13
  19. package/dist/react-native/components/snap-stack.js +32 -33
  20. package/dist/react-native/components/snap-toggle-group.js +5 -4
  21. package/dist/stack-horizontal-utils.d.ts +7 -5
  22. package/dist/stack-horizontal-utils.js +24 -14
  23. package/dist/ui/catalog.d.ts +60 -1
  24. package/dist/ui/catalog.js +3 -3
  25. package/dist/ui/cell-grid.d.ts +4 -0
  26. package/dist/ui/cell-grid.js +3 -4
  27. package/dist/ui/index.d.ts +2 -2
  28. package/dist/ui/index.js +1 -1
  29. package/dist/ui/item.d.ts +112 -1
  30. package/dist/ui/item.js +28 -2
  31. package/dist/ui/stack.d.ts +1 -0
  32. package/dist/ui/stack.js +3 -1
  33. package/dist/validator.js +19 -1
  34. package/llms.txt +3 -1
  35. package/package.json +1 -1
  36. package/src/button-orientation-utils.ts +36 -0
  37. package/src/constants.ts +1 -0
  38. package/src/react/components/action-button.tsx +5 -4
  39. package/src/react/components/cell-grid.tsx +6 -2
  40. package/src/react/components/item-group.tsx +19 -17
  41. package/src/react/components/item-layout-context.tsx +12 -0
  42. package/src/react/components/item.tsx +97 -4
  43. package/src/react/components/stack.tsx +51 -40
  44. package/src/react/components/toggle-group.tsx +6 -4
  45. package/src/react-native/components/item-layout-context.tsx +10 -0
  46. package/src/react-native/components/snap-action-button.tsx +15 -2
  47. package/src/react-native/components/snap-cell-grid.tsx +36 -4
  48. package/src/react-native/components/snap-item-group.tsx +31 -17
  49. package/src/react-native/components/snap-item.tsx +92 -14
  50. package/src/react-native/components/snap-stack.tsx +37 -36
  51. package/src/react-native/components/snap-toggle-group.tsx +5 -4
  52. package/src/stack-horizontal-utils.ts +32 -13
  53. package/src/ui/catalog.ts +5 -4
  54. package/src/ui/cell-grid.ts +5 -5
  55. package/src/ui/index.ts +2 -2
  56. package/src/ui/item.ts +35 -5
  57. package/src/ui/stack.ts +3 -1
  58. package/src/validator.ts +29 -1
@@ -2,8 +2,14 @@ import type { ComponentRenderProps } from "@json-render/react-native";
2
2
  import { Children, Fragment, type ReactNode } from "react";
3
3
  import { StyleSheet, View } from "react-native";
4
4
  import { useSnapTheme } from "../theme";
5
+ import { SnapItemGroupBorderProvider } from "./item-layout-context";
5
6
 
6
- const GAP_MAP: Record<string, number> = { none: 0, sm: 4, md: 8, lg: 12 };
7
+ const GAP_MAP: Record<string, number> = {
8
+ none: 0,
9
+ sm: 4,
10
+ md: 8,
11
+ lg: 12,
12
+ };
7
13
 
8
14
  export function SnapItemGroup({
9
15
  element: { props },
@@ -16,22 +22,30 @@ export function SnapItemGroup({
16
22
  const items = Children.toArray(children);
17
23
 
18
24
  return (
19
- <View
20
- style={[
21
- styles.group,
22
- border && { borderWidth: 1, borderColor: colors.border, borderRadius: 12 },
23
- { gap },
24
- ]}
25
- >
26
- {items.map((child, i) => (
27
- <Fragment key={i}>
28
- {separator && i > 0 && (
29
- <View style={{ height: 1, backgroundColor: colors.border + "80" }} />
30
- )}
31
- {child}
32
- </Fragment>
33
- ))}
34
- </View>
25
+ <SnapItemGroupBorderProvider value={border}>
26
+ <View
27
+ style={[
28
+ styles.group,
29
+ border && {
30
+ borderWidth: 1,
31
+ borderColor: colors.border,
32
+ borderRadius: 12,
33
+ },
34
+ { gap },
35
+ ]}
36
+ >
37
+ {items.map((child, i) => (
38
+ <Fragment key={i}>
39
+ {separator && i > 0 && (
40
+ <View
41
+ style={{ height: 1, backgroundColor: colors.border + "80" }}
42
+ />
43
+ )}
44
+ {child}
45
+ </Fragment>
46
+ ))}
47
+ </View>
48
+ </SnapItemGroupBorderProvider>
35
49
  );
36
50
  }
37
51
 
@@ -1,33 +1,98 @@
1
1
  import type { ComponentRenderProps } from "@json-render/react-native";
2
+ import { Image } from "expo-image";
2
3
  import type { ReactNode } from "react";
3
4
  import { StyleSheet, Text, View } from "react-native";
4
5
  import { useSnapStackDirection } from "../stack-direction-context";
5
6
  import { useSnapTheme } from "../theme";
7
+ import { useSnapPalette } from "../use-snap-palette";
8
+ import { useSnapItemGroupHasBorder } from "./item-layout-context";
9
+ import { ICON_MAP } from "./snap-icon";
10
+
11
+ type ItemMediaConfig =
12
+ | {
13
+ variant: "icon";
14
+ name: string;
15
+ color?: string;
16
+ }
17
+ | {
18
+ variant: "image";
19
+ url: string;
20
+ alt?: string;
21
+ round?: boolean;
22
+ };
23
+
24
+ function parseItemMedia(value: unknown): ItemMediaConfig | undefined {
25
+ if (!value || typeof value !== "object") return undefined;
26
+
27
+ const media = value as Record<string, unknown>;
28
+ if (media.variant === "icon" && typeof media.name === "string") {
29
+ return {
30
+ variant: "icon",
31
+ name: media.name,
32
+ color: typeof media.color === "string" ? media.color : undefined,
33
+ };
34
+ }
35
+
36
+ if (media.variant === "image" && typeof media.url === "string") {
37
+ return {
38
+ variant: "image",
39
+ url: media.url,
40
+ alt: typeof media.alt === "string" ? media.alt : undefined,
41
+ round: typeof media.round === "boolean" ? media.round : undefined,
42
+ };
43
+ }
44
+
45
+ return undefined;
46
+ }
6
47
 
7
48
  export function SnapItem({
8
49
  element: { props },
9
50
  children,
10
51
  }: ComponentRenderProps<Record<string, unknown>> & { children?: ReactNode }) {
11
52
  const { colors } = useSnapTheme();
53
+ const { accentHex, hex } = useSnapPalette();
12
54
  const title = String(props.title ?? "");
13
- const description = props.description
14
- ? String(props.description)
15
- : undefined;
55
+ const description = props.description ? String(props.description) : undefined;
56
+ const media = parseItemMedia(props.media);
57
+ const inBorderedGroup = useSnapItemGroupHasBorder();
16
58
  /** Match web `Item className="flex-1"`: row peers must share width or title/description collapse. */
17
59
  const rowPeer = useSnapStackDirection() === "horizontal";
60
+ const MediaIcon =
61
+ media?.variant === "icon" ? ICON_MAP[media.name] : undefined;
62
+ const mediaColor =
63
+ media?.variant === "icon" && media.color && media.color !== "accent"
64
+ ? hex(media.color)
65
+ : accentHex;
18
66
 
19
- const containerVariant = { paddingVertical: 6, paddingHorizontal: 10 };
67
+ const containerVariant = {
68
+ paddingVertical: 6,
69
+ paddingHorizontal: inBorderedGroup ? 8 : 0,
70
+ columnGap: 8,
71
+ };
20
72
 
21
73
  return (
22
74
  <View
23
- style={[
24
- styles.container,
25
- containerVariant,
26
- rowPeer && styles.rowPeer,
27
- ]}
75
+ style={[styles.container, containerVariant, rowPeer && styles.rowPeer]}
28
76
  >
77
+ {media?.variant === "icon" && MediaIcon ? (
78
+ <View style={styles.iconMedia}>
79
+ <MediaIcon size={20} color={mediaColor} />
80
+ </View>
81
+ ) : null}
82
+ {media?.variant === "image" ? (
83
+ <View style={[styles.imageMedia, media.round && styles.roundImage]}>
84
+ <Image
85
+ source={{ uri: media.url }}
86
+ style={StyleSheet.absoluteFill}
87
+ contentFit="cover"
88
+ accessibilityLabel={media.alt || undefined}
89
+ />
90
+ </View>
91
+ ) : null}
29
92
  <View style={styles.content}>
30
- {title ? <Text style={[styles.title, { color: colors.text }]}>{title}</Text> : null}
93
+ {title ? (
94
+ <Text style={[styles.title, { color: colors.text }]}>{title}</Text>
95
+ ) : null}
31
96
  {description ? (
32
97
  <Text style={[styles.description, { color: colors.textSecondary }]}>
33
98
  {description}
@@ -55,19 +120,32 @@ const styles = StyleSheet.create({
55
120
  content: {
56
121
  flex: 1,
57
122
  },
123
+ iconMedia: {
124
+ alignItems: "center",
125
+ justifyContent: "center",
126
+ },
127
+ imageMedia: {
128
+ width: 40,
129
+ height: 40,
130
+ borderRadius: 6,
131
+ overflow: "hidden",
132
+ backgroundColor: "#f3f4f6",
133
+ },
134
+ roundImage: {
135
+ borderRadius: 9999,
136
+ },
58
137
  title: {
59
138
  fontSize: 15,
60
139
  lineHeight: 20,
61
140
  fontWeight: "500",
62
141
  },
63
142
  description: {
64
- fontSize: 13,
65
- lineHeight: 18,
66
- marginTop: 1,
143
+ fontSize: 12,
144
+ lineHeight: 16,
67
145
  },
68
146
  actions: {
69
147
  marginLeft: "auto",
70
- paddingLeft: 12,
148
+ paddingLeft: 8,
71
149
  flexDirection: "row",
72
150
  alignItems: "center",
73
151
  flexShrink: 0,
@@ -2,9 +2,10 @@ import type { ComponentRenderProps } from "@json-render/react-native";
2
2
  import { Children, type ReactNode } from "react";
3
3
  import { StyleSheet, View } from "react-native";
4
4
  import {
5
+ childrenShouldUseHorizontalButtonLayout,
6
+ childrenAreAllButtons,
5
7
  countRenderableChildren,
6
8
  defaultHorizontalGapSize,
7
- horizontalChildrenAreAllButtons,
8
9
  } from "../../stack-horizontal-utils.js";
9
10
  import {
10
11
  SnapStackDirectionProvider,
@@ -13,7 +14,7 @@ import {
13
14
 
14
15
  const VGAP: Record<string, number> = {
15
16
  none: 0,
16
- sm: 8,
17
+ sm: 4,
17
18
  md: 16,
18
19
  lg: 24,
19
20
  };
@@ -33,7 +34,7 @@ const JUSTIFY: Record<string, "flex-start" | "center" | "flex-end" | "space-betw
33
34
  around: "space-around",
34
35
  };
35
36
 
36
- /** Equal-width cells for explicit `columns` and all-button horizontal rows. */
37
+ /** Equal-width cells for explicit `equalWidth` / `columns` props. */
37
38
  function wrapEqualColumnCells(children: ReactNode): ReactNode {
38
39
  const cells = Children.toArray(children).filter(
39
40
  (c) => c != null && c !== false,
@@ -50,17 +51,21 @@ export function SnapStack({
50
51
  children,
51
52
  }: ComponentRenderProps<Record<string, unknown>> & { children?: ReactNode }) {
52
53
  const parentDirection = useSnapStackDirection();
53
- const direction = String(props.direction ?? "vertical");
54
+ const buttonContentUsesHorizontal =
55
+ childrenShouldUseHorizontalButtonLayout(children);
56
+ const direction =
57
+ buttonContentUsesHorizontal === undefined
58
+ ? String(props.direction ?? "vertical")
59
+ : buttonContentUsesHorizontal
60
+ ? "horizontal"
61
+ : "vertical";
54
62
  const rawGap = props.gap;
55
63
  const isHorizontal = direction === "horizontal";
56
64
  const gapMap = isHorizontal ? HGAP : VGAP;
57
- const buttonRowGrid =
58
- isHorizontal && horizontalChildrenAreAllButtons(children);
59
- const buttonRowCount = buttonRowGrid
60
- ? countRenderableChildren(children)
61
- : 0;
65
+ const allChildrenAreButtons = childrenAreAllButtons(children);
62
66
 
63
67
  const columnsRaw = props.columns;
68
+ const equalWidth = props.equalWidth === true;
64
69
  const columns =
65
70
  typeof columnsRaw === "number" &&
66
71
  columnsRaw >= 2 &&
@@ -69,49 +74,44 @@ export function SnapStack({
69
74
  ? columnsRaw
70
75
  : undefined;
71
76
 
72
- // Horizontal default depends on column count: 2→lg, 3→md, 4+→sm. Vertical stays md.
73
- // Count comes from explicit `columns`, then button-row inference, else direct children
74
- // count (any horizontal stack is N columns wide regardless of child types).
75
- const horizontalColumnCount = isHorizontal
76
- ? (columns ??
77
- (buttonRowGrid ? buttonRowCount : undefined) ??
78
- countRenderableChildren(children))
77
+ const equalWidthColumnCount =
78
+ columns ?? (equalWidth ? countRenderableChildren(children) : undefined);
79
+ const explicitEqualWidth =
80
+ isHorizontal &&
81
+ equalWidthColumnCount !== undefined &&
82
+ equalWidthColumnCount >= 1 &&
83
+ equalWidthColumnCount <= 6;
84
+
85
+ // Button-only stacks always default to sm; mixed horizontal stacks scale by child count.
86
+ // Vertical non-button stacks default to md.
87
+ const horizontalChildCount = isHorizontal
88
+ ? (explicitEqualWidth
89
+ ? equalWidthColumnCount
90
+ : countRenderableChildren(children))
79
91
  : undefined;
80
92
  const gap =
81
93
  typeof rawGap === "number"
82
94
  ? rawGap
83
95
  : typeof rawGap === "string" && rawGap in gapMap
84
96
  ? gapMap[rawGap]!
85
- : isHorizontal
86
- ? gapMap[defaultHorizontalGapSize(horizontalColumnCount)]!
97
+ : allChildrenAreButtons
98
+ ? gapMap.sm!
99
+ : isHorizontal
100
+ ? gapMap[defaultHorizontalGapSize(horizontalChildCount)]!
87
101
  : VGAP.md!;
88
- const explicitColumnGrid =
89
- isHorizontal && columns !== undefined && !buttonRowGrid;
90
-
91
102
  const justify =
92
103
  props.justify &&
93
- (!isHorizontal || (!buttonRowGrid && !explicitColumnGrid))
104
+ (!isHorizontal || !explicitEqualWidth)
94
105
  ? JUSTIFY[String(props.justify)]
95
106
  : undefined;
96
107
 
97
108
  const isRowChild = parentDirection === "horizontal";
98
109
 
99
110
  const packedHorizontal =
100
- isHorizontal &&
101
- ((buttonRowGrid &&
102
- buttonRowCount >= 1 &&
103
- buttonRowCount <= 6) ||
104
- explicitColumnGrid);
111
+ isHorizontal && explicitEqualWidth;
105
112
 
106
113
  let horizontalBody: ReactNode = children;
107
- if (
108
- isHorizontal &&
109
- buttonRowGrid &&
110
- buttonRowCount >= 1 &&
111
- buttonRowCount <= 6
112
- ) {
113
- horizontalBody = wrapEqualColumnCells(children);
114
- } else if (isHorizontal && explicitColumnGrid && columns !== undefined) {
114
+ if (isHorizontal && explicitEqualWidth) {
115
115
  horizontalBody = wrapEqualColumnCells(children);
116
116
  }
117
117
 
@@ -163,7 +163,7 @@ const styles = StyleSheet.create({
163
163
  width: "100%",
164
164
  minWidth: 0,
165
165
  },
166
- /** Single row for packed equal-width cells (button grids & explicit columns). */
166
+ /** Single row for packed equal-width cells from explicit equal-width layout. */
167
167
  horizontalPacked: {
168
168
  flexDirection: "row",
169
169
  flexWrap: "nowrap",
@@ -176,5 +176,6 @@ const styles = StyleSheet.create({
176
176
  flexShrink: 1,
177
177
  flexBasis: 0,
178
178
  minWidth: 0,
179
+ alignSelf: "stretch",
179
180
  },
180
181
  });
@@ -1,6 +1,7 @@
1
1
  import type { ComponentRenderProps } from "@json-render/react-native";
2
2
  import { useStateStore } from "@json-render/react-native";
3
3
  import { Pressable, StyleSheet, Text, View } from "react-native";
4
+ import { shouldUseHorizontalButtonContent } from "../../button-orientation-utils.js";
4
5
  import { useSnapTheme } from "../theme";
5
6
 
6
7
  export function SnapToggleGroup({
@@ -12,7 +13,6 @@ export function SnapToggleGroup({
12
13
  const path = `/inputs/${name}`;
13
14
  const label = props.label ? String(props.label) : undefined;
14
15
  const isMultiple = Boolean(props.multiple);
15
- const orientation = String(props.orientation ?? "horizontal");
16
16
  const options = Array.isArray(props.options)
17
17
  ? (props.options as string[])
18
18
  : [];
@@ -38,7 +38,7 @@ export function SnapToggleGroup({
38
38
  return [];
39
39
  })();
40
40
 
41
- const isVertical = orientation === "vertical";
41
+ const isVertical = !shouldUseHorizontalButtonContent(options);
42
42
 
43
43
  const handlePress = (opt: string) => {
44
44
  if (isMultiple) {
@@ -65,7 +65,7 @@ export function SnapToggleGroup({
65
65
  const isSelected = selected.includes(opt);
66
66
  return (
67
67
  <Pressable
68
- key={index}
68
+ key={`${opt}-${index}`}
69
69
  style={({ pressed }) => [
70
70
  styles.option,
71
71
  {
@@ -117,7 +117,8 @@ const styles = StyleSheet.create({
117
117
  justifyContent: "center",
118
118
  },
119
119
  optionHorizontal: {
120
- flex: 1,
120
+ flexGrow: 1,
121
+ flexShrink: 1,
121
122
  },
122
123
  optionText: {
123
124
  fontSize: 13,
@@ -1,5 +1,7 @@
1
1
  import { Children, isValidElement, type ReactNode } from "react";
2
2
 
3
+ import { shouldUseHorizontalButtonContent } from "./button-orientation-utils.js";
4
+
3
5
  /**
4
6
  * True when every rendered child comes from a catalog `button` element.
5
7
  * json-render passes `{ element: { type, props, ... } }` into each catalog component.
@@ -10,32 +12,49 @@ function isRenderableChild(c: ReactNode): boolean {
10
12
  return true;
11
13
  }
12
14
 
13
- export function horizontalChildrenAreAllButtons(children: ReactNode): boolean {
15
+ export function childrenAreAllButtons(children: ReactNode): boolean {
16
+ return getButtonChildLabels(children) !== undefined;
17
+ }
18
+
19
+ export function getButtonChildLabels(children: ReactNode): string[] | undefined {
14
20
  const items = Children.toArray(children).filter(isRenderableChild);
15
- if (items.length === 0) return false;
21
+ if (items.length === 0) return undefined;
22
+ const labels: string[] = [];
16
23
  for (const child of items) {
17
- if (!isValidElement(child)) return false;
18
- const typ = (child.props as { element?: { type?: unknown } }).element?.type;
19
- if (typ !== "button") return false;
24
+ if (!isValidElement(child)) return undefined;
25
+ const element = (
26
+ child.props as {
27
+ element?: { type?: unknown; props?: Record<string, unknown> };
28
+ }
29
+ ).element;
30
+ if (element?.type !== "button") return undefined;
31
+ labels.push(String(element.props?.label ?? ""));
20
32
  }
21
- return true;
33
+ return labels;
34
+ }
35
+
36
+ export function childrenShouldUseHorizontalButtonLayout(
37
+ children: ReactNode,
38
+ ): boolean | undefined {
39
+ const labels = getButtonChildLabels(children);
40
+ return labels ? shouldUseHorizontalButtonContent(labels) : undefined;
22
41
  }
23
42
 
24
- /** Direct snap catalog children under a stack (used for all-button grid column count). */
43
+ /** Direct snap catalog children under a stack (used for horizontal gap defaults). */
25
44
  export function countRenderableChildren(children: ReactNode): number {
26
45
  return Children.toArray(children).filter(isRenderableChild).length;
27
46
  }
28
47
 
29
48
  /**
30
- * Default horizontal stack gap as a t-shirt size, chosen by column count:
31
- * 2 cols → lg, 3 cols → md, 4+ cols → sm. Unknown count falls back to md.
49
+ * Default horizontal stack gap as a t-shirt size, chosen by direct child count:
50
+ * 2 children → lg, 3 children → md, 4+ children → sm. Unknown count falls back to md.
32
51
  * Tighter gaps for denser layouts; authors can always override via the `gap` prop.
33
52
  */
34
53
  export function defaultHorizontalGapSize(
35
- columnCount: number | undefined,
54
+ childCount: number | undefined,
36
55
  ): "sm" | "md" | "lg" {
37
- if (columnCount === undefined) return "md";
38
- if (columnCount <= 2) return "lg";
39
- if (columnCount === 3) return "md";
56
+ if (childCount === undefined) return "md";
57
+ if (childCount <= 2) return "lg";
58
+ if (childCount === 3) return "md";
40
59
  return "sm";
41
60
  }
package/src/ui/catalog.ts CHANGED
@@ -48,7 +48,7 @@ export const snapJsonRenderCatalog = defineCatalog(snapJsonRenderSchema, {
48
48
  toggle_group: {
49
49
  props: toggleGroupProps,
50
50
  description:
51
- "Single or multi-select choice group; `name` becomes POST inputs key. mode: single (default) | multiple. Optional label.",
51
+ "Single or multi-select choice group; `name` becomes POST inputs key. The @farcaster/snap React/React Native components choose row/column orientation from option label length, ignoring snap-sent orientation hints.",
52
52
  },
53
53
  input: {
54
54
  props: inputProps,
@@ -58,7 +58,7 @@ export const snapJsonRenderCatalog = defineCatalog(snapJsonRenderSchema, {
58
58
  item: {
59
59
  props: itemProps,
60
60
  description:
61
- "Content row with title and optional description. Children render in the actions slot (right side) — badge, button, and icon elements are all valid. The item itself is not interactive, so avoid navigation-style icons (`chevron-right`, `arrow-right`, `external-link`) that imply the row navigates.",
61
+ "Content row matching shadcn Item: optional media renders on the left, title and optional description render in the content area, and children render in the actions slot (right side). The item itself is not interactive, so avoid navigation-style icons (`chevron-right`, `arrow-right`, `external-link`) that imply the row navigates.",
62
62
  },
63
63
  item_group: {
64
64
  props: itemGroupProps,
@@ -92,7 +92,7 @@ export const snapJsonRenderCatalog = defineCatalog(snapJsonRenderSchema, {
92
92
  stack: {
93
93
  props: stackProps,
94
94
  description:
95
- "Layout container — direction: vertical (default) | horizontal. Children are element ids in order. Horizontal stacks use a single flex row so peers stay side-by-side and shrink with min-width 0. Nested stacks participate as flexible row peers. All-button horizontal stacks use an equal N-column grid where N is the number of buttons (1–6). Optional `columns` (`2`–`6`) forces an explicit equal grid for mixed children.",
95
+ "Layout container — direction: vertical (default) | horizontal. Children are element ids in order. Horizontal stacks use a single flex row so peers stay side-by-side and shrink with min-width 0. Nested stacks participate as flexible row peers. For all-button stacks, the @farcaster/snap React/React Native components choose row/column orientation from button label length, ignoring snap-sent direction hints; horizontal button rows use content-proportional widths while filling the container unless `equalWidth: true` is provided to force equal-width cells. Vertical button rows default to a tighter gap when `gap` is omitted.",
96
96
  },
97
97
  text: {
98
98
  props: textProps,
@@ -121,7 +121,8 @@ export const snapJsonRenderCatalog = defineCatalog(snapJsonRenderSchema, {
121
121
  params: z.object({ target: z.string() }),
122
122
  },
123
123
  open_snap: {
124
- description: "Open a snap URL inline. The client renders the target as a snap rather than opening a browser.",
124
+ description:
125
+ "Open a snap URL inline. The client renders the target as a snap rather than opening a browser.",
125
126
  params: z.object({ target: z.string() }),
126
127
  },
127
128
  open_mini_app: {
@@ -6,6 +6,7 @@ import {
6
6
  GRID_MIN_ROWS,
7
7
  GRID_MAX_ROWS,
8
8
  GRID_GAP_VALUES,
9
+ GRID_CELL_ASPECT_RATIO_VALUES,
9
10
  } from "../constants.js";
10
11
 
11
12
  /** Palette name or `#rrggbb`; input is trimmed so palette and hex rules match runtime resolvers. */
@@ -13,11 +14,9 @@ const cellGridCellColorSchema = z.preprocess(
13
14
  (v) => (typeof v === "string" ? v.trim() : v),
14
15
  z.union([
15
16
  z.enum(PALETTE_COLOR_VALUES),
16
- z
17
- .string()
18
- .refine(isSnapHexColorString, {
19
- message: "cell_grid cell hex color must be #rrggbb",
20
- }),
17
+ z.string().refine(isSnapHexColorString, {
18
+ message: "cell_grid cell hex color must be #rrggbb",
19
+ }),
21
20
  ]),
22
21
  );
23
22
 
@@ -36,6 +35,7 @@ export const cellGridProps = z
36
35
  rows: z.number().int().min(GRID_MIN_ROWS).max(GRID_MAX_ROWS),
37
36
  cells: z.array(cellGridCellSchema),
38
37
  gap: z.enum(GRID_GAP_VALUES).optional(),
38
+ cellAspectRatio: z.enum(GRID_CELL_ASPECT_RATIO_VALUES).optional(),
39
39
  rowHeight: z.number().int().min(8).max(64).optional(),
40
40
  select: z.enum(["off", "single", "multiple"]).optional(),
41
41
  })
package/src/ui/index.ts CHANGED
@@ -16,8 +16,8 @@ export type { ToggleGroupProps } from "./toggle-group.js";
16
16
  export { inputProps } from "./input.js";
17
17
  export type { InputProps } from "./input.js";
18
18
 
19
- export { itemProps } from "./item.js";
20
- export type { ItemProps } from "./item.js";
19
+ export { itemProps, itemMediaProps } from "./item.js";
20
+ export type { ItemMediaProps, ItemProps } from "./item.js";
21
21
 
22
22
  export { itemGroupProps } from "./item-group.js";
23
23
  export type { ItemGroupProps } from "./item-group.js";
package/src/ui/item.ts CHANGED
@@ -1,13 +1,43 @@
1
1
  import { z } from "zod";
2
+ import { PROGRESS_COLOR_VALUES } from "../colors.js";
3
+ import { ICON_NAMES } from "./icon.js";
2
4
 
3
5
  export const ITEM_VARIANTS = ["default"] as const;
6
+ export const ITEM_MEDIA_VARIANTS = ["icon", "image"] as const;
4
7
  export const ITEM_MAX_TITLE_CHARS = 100;
5
8
  export const ITEM_MAX_DESCRIPTION_CHARS = 160;
9
+ export const ITEM_MAX_MEDIA_ALT_CHARS = 120;
6
10
 
7
- export const itemProps = z.object({
8
- title: z.string().min(1).max(ITEM_MAX_TITLE_CHARS),
9
- description: z.string().max(ITEM_MAX_DESCRIPTION_CHARS).optional(),
10
- variant: z.enum(ITEM_VARIANTS).optional(),
11
- });
11
+ const itemIconMediaProps = z
12
+ .object({
13
+ variant: z.literal("icon"),
14
+ name: z.enum(ICON_NAMES),
15
+ color: z.enum(PROGRESS_COLOR_VALUES).optional(),
16
+ })
17
+ .strict();
18
+
19
+ const itemImageMediaProps = z
20
+ .object({
21
+ variant: z.literal("image"),
22
+ url: z.string(),
23
+ alt: z.string().max(ITEM_MAX_MEDIA_ALT_CHARS).optional(),
24
+ round: z.boolean().optional(),
25
+ })
26
+ .strict();
27
+
28
+ export const itemMediaProps = z.discriminatedUnion("variant", [
29
+ itemIconMediaProps,
30
+ itemImageMediaProps,
31
+ ]);
32
+
33
+ export const itemProps = z
34
+ .object({
35
+ title: z.string().min(1).max(ITEM_MAX_TITLE_CHARS),
36
+ description: z.string().max(ITEM_MAX_DESCRIPTION_CHARS).optional(),
37
+ variant: z.enum(ITEM_VARIANTS).optional(),
38
+ media: itemMediaProps.optional(),
39
+ })
40
+ .strict();
12
41
 
13
42
  export type ItemProps = z.infer<typeof itemProps>;
43
+ export type ItemMediaProps = z.infer<typeof itemMediaProps>;
package/src/ui/stack.ts CHANGED
@@ -8,7 +8,9 @@ export const stackProps = z.object({
8
8
  direction: z.enum(STACK_DIRECTIONS).optional(),
9
9
  gap: z.enum(STACK_GAPS).optional(),
10
10
  justify: z.enum(STACK_JUSTIFY).optional(),
11
- /** Horizontal stacks only: fixed column grid (`2`–`6`). Prefer omitting this when children are stacks — they flex as row peers automatically. */
11
+ /** Horizontal stacks only: make direct children equal width. */
12
+ equalWidth: z.boolean().optional(),
13
+ /** Horizontal stacks only: legacy fixed equal-width column count (`2`–`6`). Prefer `equalWidth`. */
12
14
  columns: z.union([
13
15
  z.literal(2),
14
16
  z.literal(3),
package/src/validator.ts CHANGED
@@ -29,7 +29,12 @@ const URL_TARGET_ACTIONS = new Set([
29
29
  */
30
30
  function isLoopback(url: URL): boolean {
31
31
  const host = url.hostname;
32
- return host === "localhost" || host === "127.0.0.1" || host === "::1" || host === "[::1]";
32
+ return (
33
+ host === "localhost" ||
34
+ host === "127.0.0.1" ||
35
+ host === "::1" ||
36
+ host === "[::1]"
37
+ );
33
38
  }
34
39
 
35
40
  /**
@@ -84,6 +89,11 @@ type ElementShape = {
84
89
  on?: Record<string, { action?: string; params?: Record<string, unknown> }>;
85
90
  };
86
91
 
92
+ type ItemMediaShape = {
93
+ variant?: string;
94
+ url?: unknown;
95
+ };
96
+
87
97
  // ─── Structural validation ────────────────────────────
88
98
 
89
99
  /**
@@ -169,6 +179,24 @@ function validateUrls(elements: Record<string, unknown>): z.core.$ZodIssue[] {
169
179
  }
170
180
  }
171
181
 
182
+ if (
183
+ el.type === "item" &&
184
+ el.props?.media &&
185
+ typeof el.props.media === "object"
186
+ ) {
187
+ const media = el.props.media as ItemMediaShape;
188
+ if (media.variant === "image" && typeof media.url === "string") {
189
+ const error = validateUrl(media.url);
190
+ if (error) {
191
+ issues.push({
192
+ code: "custom",
193
+ message: error,
194
+ path: ["ui", "elements", id, "props", "media", "url"],
195
+ });
196
+ }
197
+ }
198
+ }
199
+
172
200
  // Validate action target URLs
173
201
  if (el.on) {
174
202
  for (const [event, binding] of Object.entries(el.on)) {