storybook-nitroui 0.2.1 → 0.2.2

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.
package/README.md CHANGED
@@ -375,7 +375,8 @@ Each is marked with a `// TODO:` in the source.
375
375
  - The story root lays itself out only on its own commits, not when the canvas view moves. The canvas fills the screen, and the space the bar, sheets, sidebar and panel cover is passed to the story through a store that its environment decorator reads.
376
376
  - NitroUI has no incoming URL API, so there are no deep links.
377
377
  - `TextInput` has no return-key event, return key type or `keyboardDismissMode`, so the sheets in `storybook-nitroui-containers` dismiss the keyboard natively (taps and drags outside the input, Return, collapsing and closing), and a "Done" button calls `Keyboard.dismiss()`.
378
- - `TextInput` has no font, text color or placeholder color props, so the search field and text controls keep the system text style.
378
+ - `TextInput` has no font, text color or placeholder color props, so the search field and text controls keep the system text style. The object control's raw JSON mode uses the native `CodeEditor` from `storybook-nitroui-controls` for a monospace font.
379
+ - A `borderWidth` around a `TextInput` that measures its own height breaks the layout, so text fields draw their 1 px border as padding on a border-colored wrapper.
379
380
  - Text has no letter spacing, so section headers space their uppercase letters with thin spaces.
380
381
  - On iOS, `List` places rows of mixed heights a few points off. Every tree row has the same fixed height, so the gap before a section is a full row instead of web's 14 px.
381
382
  - A `View` laid out at 0 by 0 inside a sheet that is open in its first update does not recover. `BottomSheet` mounts closed and opens right after its first layout.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "storybook-nitroui",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Storybook framework for NitroUI apps: on-device preview and manager, Node-side indexing and docgen.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -73,8 +73,8 @@
73
73
  "nitro-ui-navigation": "0.3.0",
74
74
  "typescript": ">=6",
75
75
  "nitro-mmkv": "0.3.0",
76
- "storybook-nitroui-containers": "^0.2.1",
77
- "storybook-nitroui-controls": "^0.2.1",
76
+ "storybook-nitroui-containers": "^0.2.2",
77
+ "storybook-nitroui-controls": "^0.2.2",
78
78
  "nitro-networking": "0.3.0",
79
79
  "nitro-ui-dev": "0.3.0"
80
80
  },
@@ -94,8 +94,8 @@
94
94
  "nitro-ui-renderer": "0.3.0",
95
95
  "react": "19.3.0",
96
96
  "storybook": "11.0.1",
97
- "storybook-nitroui-containers": "0.2.1",
98
- "storybook-nitroui-controls": "0.2.1",
97
+ "storybook-nitroui-containers": "0.2.2",
98
+ "storybook-nitroui-controls": "0.2.2",
99
99
  "typescript": "npm:@typescript/typescript6@^6.0.2"
100
100
  },
101
101
  "peerDependenciesMeta": {
@@ -7,6 +7,9 @@ export function straightenPunctuation(text: string) {
7
7
  return text.replace(/[“”„‟″]/g, '"').replace(/[‘’‚‛′]/g, "'").replace(/[–—]/g, '--')
8
8
  }
9
9
 
10
+ /** The 1 px "border": every edge is pinned, so a caller's `paddingVertical` and the like cannot widen it. */
11
+ export const framePadding = { paddingTop: 1, paddingBottom: 1, paddingLeft: 1, paddingRight: 1 } as const
12
+
10
13
  /**
11
14
  * A text field that keeps its own text while focused (web `ArgControl` boxes the value the same way)
12
15
  * and takes external changes, such as a reset, when it is not.
@@ -34,10 +37,11 @@ export function Field({ text, onText, placeholder, multiline = false, plain = fa
34
37
  }, [text])
35
38
  // Web's form inputs: input background, a 1 px input border, 4 px corners.
36
39
  // TODO: a border (borderWidth) around a TextInput that measures its own height breaks NitroUI's
37
- // layout of the whole tree, so the border is a 1 px padding on a border-colored wrapper.
40
+ // layout of the whole tree, so the border is a 1 px padding on a border-colored wrapper. `layout`
41
+ // places the field; it cannot change that padding, which would turn the border into a thick band.
38
42
  return (
39
43
  <View
40
- layout={{ padding: 1, ...layout }}
44
+ layout={{ ...layout, ...framePadding }}
41
45
  style={{ background: error ? theme.error : theme.inputBorder, cornerRadius: theme.radius, opacity: disabled ? 0.5 : 1 }}
42
46
  >
43
47
  <View style={{ background: error ? (((theme.error & 0xffffff00) | 0x1a) >>> 0) : theme.inputBg, cornerRadius: theme.radius - 1 }}>
@@ -1,10 +1,11 @@
1
- import { useState } from 'react'
1
+ import { useReducer, useState } from 'react'
2
2
  import { Text, View } from 'nitro-ui-renderer'
3
- import { Switch } from 'storybook-nitroui-controls'
3
+ import { CodeEditor, Switch } from 'storybook-nitroui-controls'
4
4
  import type { ControlProps } from '../controls.tsx'
5
+ import { Icon, type IconData } from '../icons/Icon.tsx'
5
6
  import { TextButton, useTap } from '../primitives.tsx'
6
- import { useTheme } from '../theme.ts'
7
- import { Field } from './Field.tsx'
7
+ import { useTheme, type Theme } from '../theme.ts'
8
+ import { Field, framePadding } from './Field.tsx'
8
9
  import { useDebouncedCommit } from './useDebouncedCommit.ts'
9
10
 
10
11
  // The object/array control: an editable tree (expand and collapse, edit leaves in place, add and
@@ -60,6 +61,8 @@ export function ObjectControl(props: ControlProps) {
60
61
  const theme = useTheme()
61
62
  const [raw, setRaw] = useState(false)
62
63
  const [error, setError] = useState<string | undefined>(undefined)
64
+ // The editor measures its own text; a commit after each edit lets NitroUI lay out its new height.
65
+ const [, relayout] = useReducer((count: number) => count + 1, 0)
63
66
  const commitRaw = useDebouncedCommit(props.onChange, 250)
64
67
  const readOnly = (props.argType.table as { readonly?: boolean } | undefined)?.readonly === true
65
68
  if (props.value !== undefined && !isPlainContainer(props.value)) {
@@ -67,7 +70,7 @@ export function ObjectControl(props: ControlProps) {
67
70
  return <Text fontSize={13} color={theme.secondaryLabel}>{stringify(props.value)}</Text>
68
71
  }
69
72
  return (
70
- <View layout={{ gap: 6 }}>
73
+ <View layout={{ gap: 8 }}>
71
74
  <View layout={{ flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center' }}>
72
75
  <Text fontSize={13} color={theme.secondaryLabel}>{summary(props.value)}</Text>
73
76
  {readOnly ? null : (
@@ -80,21 +83,32 @@ export function ObjectControl(props: ControlProps) {
80
83
  </View>
81
84
  {raw ? (
82
85
  <View>
83
- <Field
84
- text={stringify(props.value)}
85
- multiline
86
- plain
87
- error={error !== undefined}
88
- onText={(text) => {
89
- try {
90
- const parsed: unknown = text.trim() === '' ? undefined : JSON.parse(text)
91
- setError(undefined)
92
- commitRaw(parsed)
93
- } catch (cause) {
94
- setError((cause as Error).message)
95
- }
96
- }}
97
- />
86
+ {/* Web's raw mode is an auto-growing monospace textarea; this grows up to a cap and then scrolls.
87
+ TODO: NitroUI's TextInput has no font props, so raw JSON uses the native CodeEditor. */}
88
+ <View
89
+ layout={framePadding}
90
+ style={{ background: error === undefined ? theme.inputBorder : theme.error, cornerRadius: theme.radius }}
91
+ >
92
+ <View style={{ background: error === undefined ? theme.inputBg : (((theme.error & 0xffffff00) | 0x1a) >>> 0), cornerRadius: theme.radius - 1 }}>
93
+ <CodeEditor
94
+ text={stringify(props.value)}
95
+ textColor={theme.inputTextColor}
96
+ onTextChange={(text) => {
97
+ relayout()
98
+ try {
99
+ // Empty text is a parse error, as on web, not a cleared arg: clearing the editor to
100
+ // retype it would otherwise unset the arg and close the editor mid-edit.
101
+ const parsed: unknown = JSON.parse(text)
102
+ setError(undefined)
103
+ commitRaw(parsed)
104
+ } catch (cause) {
105
+ setError((cause as Error).message)
106
+ }
107
+ }}
108
+ layout={{ minHeight: 88, maxHeight: 360 }}
109
+ />
110
+ </View>
111
+ </View>
98
112
  {error === undefined ? null : <Text fontSize={12} color={theme.error} layout={{ marginTop: 4 }}>{error}</Text>}
99
113
  </View>
100
114
  ) : (
@@ -104,6 +118,40 @@ export function ObjectControl(props: ControlProps) {
104
118
  )
105
119
  }
106
120
 
121
+ /** Rows of one level: a fixed gap between them, so neighbouring fields never touch. */
122
+ const rowGap = 6
123
+ /** Width of one nesting step, with a guide line down its left edge. */
124
+ const indent = 14
125
+
126
+ // Circle-minus and circle-plus in the 14 px grid of Storybook's icon set.
127
+ const circle = { d: 'M7 14A7 7 0 107 0a7 7 0 000 14zm0-1A6 6 0 107 1a6 6 0 000 12z', fillRule: 'evenodd' as const }
128
+ const RemoveIcon: IconData = { viewBox: [0, 0, 14, 14], paths: [{ d: 'M4 6.5h6a.5.5 0 010 1H4a.5.5 0 010-1z' }, circle] }
129
+ const AddIcon: IconData = {
130
+ viewBox: [0, 0, 14, 14],
131
+ paths: [{ d: 'M7.5 4v2.5H10a.5.5 0 010 1H7.5V10a.5.5 0 01-1 0V7.5H4a.5.5 0 010-1h2.5V4a.5.5 0 011 0z' }, circle],
132
+ }
133
+
134
+ /** A borderless icon button for removing an entry, sized for touch. */
135
+ function RemoveButton({ onPress, theme }: { onPress: () => void; theme: Theme }) {
136
+ const tap = useTap(onPress)
137
+ return (
138
+ <View gestureRecognizers={tap} layout={{ width: 32, height: 32, alignItems: 'center', justifyContent: 'center', flexShrink: 0 }}>
139
+ <Icon icon={RemoveIcon} size={16} color={theme.error} />
140
+ </View>
141
+ )
142
+ }
143
+
144
+ /** "+ Add item" / "+ Add key": a borderless link-style row under the entries. */
145
+ function AddButton({ title, onPress, theme }: { title: string; onPress: () => void; theme: Theme }) {
146
+ const tap = useTap(onPress)
147
+ return (
148
+ <View gestureRecognizers={tap} layout={{ flexDirection: 'row', alignItems: 'center', gap: 6, alignSelf: 'flex-start', minHeight: 32, paddingRight: 8 }}>
149
+ <Icon icon={AddIcon} size={16} color={theme.secondary} />
150
+ <Text fontSize={13} fontWeight="bold" color={theme.secondary}>{title}</Text>
151
+ </View>
152
+ )
153
+ }
154
+
107
155
  function TreeNode({ path, name, value, root, readOnly, onChange, depth, onRemove }: {
108
156
  path: Path
109
157
  name: string | undefined
@@ -115,16 +163,16 @@ function TreeNode({ path, name, value, root, readOnly, onChange, depth, onRemove
115
163
  onRemove?: () => void
116
164
  }) {
117
165
  const theme = useTheme()
118
- const [open, setOpen] = useState(depth < 2)
166
+ const [open, setOpen] = useState(depth < 3)
119
167
  const toggle = useTap(() => setOpen((value) => !value))
120
168
  const container = isPlainContainer(value)
121
169
  const label = name === undefined ? null : (
122
- <Text fontSize={14} fontWeight="medium" color={theme.label} layout={{ minWidth: 60 }}>{name}</Text>
170
+ <Text fontSize={14} fontWeight="medium" color={theme.label} numberOfLines={1} layout={{ minWidth: 56, maxWidth: 120, flexShrink: 0 }}>{name}</Text>
123
171
  )
124
- const remove = readOnly || onRemove === undefined ? null : <TextButton title="−" destructive onPress={onRemove} />
172
+ const remove = readOnly || onRemove === undefined ? null : <RemoveButton onPress={onRemove} theme={theme} />
125
173
  if (!container) {
126
174
  return (
127
- <View layout={{ flexDirection: 'row', alignItems: 'center', gap: 8, paddingLeft: depth * 12, minHeight: 36 }}>
175
+ <View layout={{ flexDirection: 'row', alignItems: 'center', gap: 8, minHeight: 34 }}>
128
176
  {label}
129
177
  <View layout={{ flex: 1 }}>
130
178
  <Leaf value={value} readOnly={readOnly} onChange={(next) => onChange(setAt(root, path, next))} />
@@ -144,20 +192,23 @@ function TreeNode({ path, name, value, root, readOnly, onChange, depth, onRemove
144
192
  onChange(setAt(root, path, { ...value, [key]: '' }))
145
193
  }
146
194
  }
195
+ const nested = name !== undefined
147
196
  return (
148
- <View layout={{ paddingLeft: depth === 0 ? 0 : depth * 12 }}>
149
- {name === undefined ? null : (
150
- <View layout={{ flexDirection: 'row', alignItems: 'center', gap: 8, minHeight: 36 }}>
151
- <View gestureRecognizers={toggle} layout={{ flexDirection: 'row', alignItems: 'center', gap: 6, flex: 1 }}>
152
- <Text fontSize={12} color={theme.secondaryLabel}>{open ? '▾' : '▸'}</Text>
197
+ <View layout={{ gap: rowGap }}>
198
+ {nested ? (
199
+ <View layout={{ flexDirection: 'row', alignItems: 'center', gap: 8, minHeight: 34 }}>
200
+ <View gestureRecognizers={toggle} layout={{ flexDirection: 'row', alignItems: 'center', gap: 6, flex: 1, minHeight: 34 }}>
201
+ <Text fontSize={12} color={theme.secondaryLabel} layout={{ width: 10 }}>{open ? '▾' : '▸'}</Text>
153
202
  {label}
154
203
  <Text fontSize={13} color={theme.secondaryLabel}>{summary(value)}</Text>
155
204
  </View>
156
205
  {remove}
157
206
  </View>
158
- )}
159
- {open || name === undefined ? (
160
- <View>
207
+ ) : null}
208
+ {open || !nested ? (
209
+ // Nested levels sit one step in, along a thin guide line under the disclosure triangle.
210
+ <View layout={{ gap: rowGap, paddingLeft: nested ? indent : 0, marginLeft: nested ? 4 : 0 }}>
211
+ {nested ? <View layout={{ position: 'absolute', left: 0, top: 0, bottom: 0, width: 1 }} style={{ background: theme.appBorderColor }} /> : null}
161
212
  {entries.map(([key, child]) => (
162
213
  <TreeNode
163
214
  key={String(key)}
@@ -167,11 +218,11 @@ function TreeNode({ path, name, value, root, readOnly, onChange, depth, onRemove
167
218
  root={root}
168
219
  readOnly={readOnly}
169
220
  onChange={onChange}
170
- depth={name === undefined ? 0 : 1}
221
+ depth={depth + 1}
171
222
  onRemove={() => onChange(removeAt(root, [...path, key]))}
172
223
  />
173
224
  ))}
174
- {readOnly ? null : <TextButton title={Array.isArray(value) ? '+ Add item' : '+ Add key'} onPress={add} layout={{ alignSelf: 'flex-start' }} />}
225
+ {readOnly ? null : <AddButton title={Array.isArray(value) ? 'Add item' : 'Add key'} onPress={add} theme={theme} />}
175
226
  </View>
176
227
  ) : null}
177
228
  </View>
@@ -188,7 +239,7 @@ function Leaf({ value, readOnly, onChange }: { value: unknown; readOnly: boolean
188
239
  const commit = useDebouncedCommit(onChange, 250)
189
240
  const [invalid, setInvalid] = useState(false)
190
241
  if (typeof value === 'boolean') return <Switch value={value} disabled={readOnly} onValueChange={onChange} layout={{ alignSelf: 'flex-start' }} />
191
- if (typeof value === 'string') return <Field text={value} disabled={readOnly} onText={commit} layout={{ paddingVertical: 6 }} />
242
+ if (typeof value === 'string') return <Field text={value} disabled={readOnly} onText={commit} />
192
243
  if (typeof value === 'number') {
193
244
  return (
194
245
  <Field
@@ -201,7 +252,6 @@ function Leaf({ value, readOnly, onChange }: { value: unknown; readOnly: boolean
201
252
  setInvalid(text.trim() === '' || Number.isNaN(number))
202
253
  if (text.trim() !== '' && !Number.isNaN(number)) commit(number)
203
254
  }}
204
- layout={{ paddingVertical: 6 }}
205
255
  />
206
256
  )
207
257
  }