@farcaster/snap 2.4.0 → 2.5.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.
Files changed (59) 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 +12 -3
  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 +36 -5
  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 +70 -1
  24. package/dist/ui/catalog.js +4 -4
  25. package/dist/ui/cell-grid.d.ts +14 -0
  26. package/dist/ui/cell-grid.js +7 -7
  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 +13 -4
  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 +43 -6
  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/README.md +1 -1
  54. package/src/ui/catalog.ts +6 -5
  55. package/src/ui/cell-grid.ts +8 -7
  56. package/src/ui/index.ts +2 -2
  57. package/src/ui/item.ts +35 -5
  58. package/src/ui/stack.ts +3 -1
  59. package/src/validator.ts +29 -1
@@ -18,6 +18,7 @@ export function SnapCellGrid({
18
18
  const rows = Number(props.rows ?? 2);
19
19
  const cells = Array.isArray(props.cells) ? props.cells : [];
20
20
  const rowHeight = typeof props.rowHeight === "number" ? props.rowHeight : 28;
21
+ const squareCells = props.cellAspectRatio === "square";
21
22
  const gap = String(props.gap ?? "sm");
22
23
  const gapMap: Record<string, number> = { none: 0, sm: 1, md: 2, lg: 4 };
23
24
  const gapPx = gapMap[gap] ?? 1;
@@ -34,11 +35,12 @@ export function SnapCellGrid({
34
35
 
35
36
  const cellMap = new Map<
36
37
  string,
37
- { color?: string; content?: string; value?: string }
38
+ { color?: string; textColor?: string; content?: string; value?: string }
38
39
  >();
39
40
  for (const c of cells) {
40
41
  cellMap.set(`${Number(c.row)},${Number(c.col)}`, {
41
42
  color: c.color as string | undefined,
43
+ textColor: c.textColor as string | undefined,
42
44
  content: c.content != null ? String(c.content) : undefined,
43
45
  value: typeof c.value === "string" ? c.value : undefined,
44
46
  });
@@ -91,7 +93,11 @@ export function SnapCellGrid({
91
93
  const selected = interactive && isSelected(r, c);
92
94
  const bgHex = cell?.color ? hex(cell.color) : null;
93
95
  const bg = bgHex ?? emptyCellBg;
94
- const textColor = bgHex ? readableTextOnHex(bgHex) : colors.text;
96
+ const textColor = cell?.textColor
97
+ ? hex(cell.textColor)
98
+ : bgHex
99
+ ? readableTextOnHex(bgHex)
100
+ : colors.text;
95
101
 
96
102
  const cellContent = cell?.content ? (
97
103
  <Text style={[styles.cellText, { color: textColor }]}>
@@ -101,18 +107,35 @@ export function SnapCellGrid({
101
107
 
102
108
  // Two-tone ring: outer View with contrasting border, inner View with inverse border
103
109
  const cellView = selected ? (
104
- <View style={[styles.cell, { height: rowHeight, borderWidth: 1, borderColor: ringOuter, borderRadius: 4 }]}>
110
+ <View
111
+ style={[
112
+ styles.cell,
113
+ squareCells ? styles.squareCell : { height: rowHeight },
114
+ { borderWidth: 1, borderColor: ringOuter, borderRadius: 4 },
115
+ ]}
116
+ >
105
117
  <View
106
118
  style={[
107
119
  styles.innerCell,
108
- { backgroundColor: bg, borderWidth: 1, borderColor: ringInner, borderRadius: 3 },
120
+ {
121
+ backgroundColor: bg,
122
+ borderWidth: 1,
123
+ borderColor: ringInner,
124
+ borderRadius: 3,
125
+ },
109
126
  ]}
110
127
  >
111
128
  {cellContent}
112
129
  </View>
113
130
  </View>
114
131
  ) : (
115
- <View style={[styles.cell, { height: rowHeight, backgroundColor: bg }]}>
132
+ <View
133
+ style={[
134
+ styles.cell,
135
+ squareCells ? styles.squareCell : { height: rowHeight },
136
+ { backgroundColor: bg },
137
+ ]}
138
+ >
116
139
  {cellContent}
117
140
  </View>
118
141
  );
@@ -141,7 +164,17 @@ export function SnapCellGrid({
141
164
  }
142
165
 
143
166
  return (
144
- <View style={[styles.wrap, { gap: gapPx, backgroundColor: colors.muted, padding: 4, borderRadius: 8 }]}>
167
+ <View
168
+ style={[
169
+ styles.wrap,
170
+ {
171
+ gap: gapPx,
172
+ backgroundColor: colors.muted,
173
+ padding: 4,
174
+ borderRadius: 8,
175
+ },
176
+ ]}
177
+ >
145
178
  {rowEls}
146
179
  </View>
147
180
  );
@@ -156,6 +189,10 @@ const styles = StyleSheet.create({
156
189
  alignItems: "center",
157
190
  justifyContent: "center",
158
191
  },
192
+ squareCell: {
193
+ aspectRatio: 1,
194
+ width: "100%",
195
+ },
159
196
  innerCell: {
160
197
  width: "100%",
161
198
  height: "100%",
@@ -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/README.md CHANGED
@@ -41,7 +41,7 @@ Some elements accept a `color` prop:
41
41
 
42
42
  - `progress` — color name or `"accent"`
43
43
  - `bar_chart` — color name or `"accent"` at chart level; color name only per-bar
44
- - `grid` cells — `#rrggbb` hex
44
+ - `grid` cell fill/text colors — `#rrggbb` hex
45
45
 
46
46
  When `color` is `"accent"` (or omitted), the element uses the accent color. When it is a specific color name or hex value, that color is **explicit** and independent of `page.theme.accent`.
47
47
 
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,
@@ -107,7 +107,7 @@ export const snapJsonRenderCatalog = defineCatalog(snapJsonRenderSchema, {
107
107
  cell_grid: {
108
108
  props: cellGridProps,
109
109
  description:
110
- "Cell grid — sparse colored cells on a rows×cols grid. Cell color is a palette name or literal #rrggbb hex (hex ignores page accent). Two interaction modes: leave select 'off' and bind on.press to fire an action per cell press (inputs[name] is the pressed 'row,col' before the action runs); or set select 'single'/'multiple' for press-to-select with a visual ring (no auto-fire — pair with a separate submit button). on.press is ignored when select is on.",
110
+ "Cell grid — sparse colored cells on a rows×cols grid. Cell color and textColor are palette names or literal #rrggbb hex values (hex ignores page accent); textColor overrides the default auto-contrast text color. Two interaction modes: leave select 'off' and bind on.press to fire an action per cell press (inputs[name] is the pressed 'row,col' before the action runs); or set select 'single'/'multiple' for press-to-select with a visual ring (no auto-fire — pair with a separate submit button). on.press is ignored when select is on.",
111
111
  },
112
112
  },
113
113
  actions: {
@@ -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,25 +6,25 @@ 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. */
12
- const cellGridCellColorSchema = z.preprocess(
13
+ const cellGridCellColorSchema = (field: "color" | "textColor") => 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 ${field} hex must be #rrggbb`,
19
+ }),
21
20
  ]),
22
21
  );
23
22
 
24
23
  const cellGridCellSchema = z.object({
25
24
  row: z.number().int().nonnegative(),
26
25
  col: z.number().int().nonnegative(),
27
- color: cellGridCellColorSchema.optional(),
26
+ color: cellGridCellColorSchema("color").optional(),
27
+ textColor: cellGridCellColorSchema("textColor").optional(),
28
28
  content: z.string().optional(),
29
29
  value: z.string().min(1).max(30).optional(),
30
30
  });
@@ -36,6 +36,7 @@ export const cellGridProps = z
36
36
  rows: z.number().int().min(GRID_MIN_ROWS).max(GRID_MAX_ROWS),
37
37
  cells: z.array(cellGridCellSchema),
38
38
  gap: z.enum(GRID_GAP_VALUES).optional(),
39
+ cellAspectRatio: z.enum(GRID_CELL_ASPECT_RATIO_VALUES).optional(),
39
40
  rowHeight: z.number().int().min(8).max(64).optional(),
40
41
  select: z.enum(["off", "single", "multiple"]).optional(),
41
42
  })