@cmodjs/core 8.4.2 → 8.6.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.
@@ -141,6 +141,73 @@ const elements: Readonly<Record<string, (props: Props, drawing: Drawing) => stri
141
141
  return `<img class="svg" alt="${escaped(accessibleName(props))}" src="data:image/svg+xml,${encodeURIComponent(source)}"${attribute('style', size === '' ? undefined : size)}>`
142
142
  },
143
143
  Raster: (props) => rasterHtml(typeof props['cells'] === 'string' ? props['cells'] : '', numberOr(props['columns'], 1), accessibleName(props)),
144
+ Tabs: (props, drawing) => {
145
+ const tabs = (Array.isArray(props['tabs']) ? props['tabs'] : []) as readonly Props[]
146
+ const buttons = tabs.map((tab) => {
147
+ const label = textOr(tab['label'], '')
148
+ const hotkey = typeof tab['hotkey'] === 'string' && hotkeys.test(tab['hotkey']) ? tab['hotkey'] : undefined
149
+ return `<button type="button" role="tab" class="tab" aria-selected="${tab['isSelected'] === true}"${attribute('data-key', `${textOr(props['key'], '')}tab:${textOr(tab['key'], label)}`)}${handlerAttribute('data-press', tab['onSelect'])}${attribute('data-hotkey', hotkey)}>${escaped(label)}</button>`
150
+ })
151
+ return `<div class="tabs"><div role="tablist" class="tablist">${buttons.join('')}</div><div role="tabpanel" class="tabpanel">${nodeHtml(props['children'], drawing)}</div></div>`
152
+ },
153
+ Split: (props, drawing) => `<div class="split">${(Array.isArray(props['children']) ? props['children'] : [props['children']]).map((child: unknown) => `<div class="split-part">${nodeHtml(child, drawing)}</div>`).join('')}</div>`,
154
+ Panel: (props, drawing) => `<section class="panel"><h3 class="panel-title">${escaped(textOr(props['title'], ''))}</h3><div class="panel-body">${nodeHtml(props['children'], drawing)}</div></section>`,
155
+ Toggle: (props) => {
156
+ const label = textOr(props['label'], '')
157
+ return `<button type="button" role="switch" class="switch" aria-checked="${props['checked'] === true}"${attribute('data-key', `${textOr(props['key'], '')}toggle:${label}`)}${handlerAttribute('data-press', props['onToggle'])}><span class="switch-track"></span>${escaped(label)}</button>`
158
+ },
159
+ Tooltip: (props, drawing) => `<span class="tooltip"${attribute('title', textOr(props['text'], ''))}>${nodeHtml(props['children'], drawing)}</span>`,
160
+ Help: (props) => {
161
+ const text = textOr(props['text'], '')
162
+ const isOpen = props['isOpen'] === true
163
+ return `<span class="help"><button type="button" class="help-button" aria-label="Help" aria-expanded="${isOpen}"${attribute('title', text)}${handlerAttribute('data-press', props['onToggle'])}>?</button>${isOpen ? `<span class="help-card" role="note">${escaped(text)}</span>` : ''}</span>`
164
+ },
165
+ Dialog: (props, drawing) => `<div role="dialog" class="dialog"${attribute('aria-label', textOr(props['title'], ''))}><h3 class="dialog-title">${escaped(textOr(props['title'], ''))}</h3><div class="dialog-body">${nodeHtml(props['children'], drawing)}</div><div class="actions">${nodeHtml(props['actions'], drawing)}</div></div>`,
166
+ Pagination: (props) => {
167
+ const step = (handler: unknown, label: string, hotkey: string) =>
168
+ `<button type="button" class="button"${handlerOf(handler) === undefined ? ' disabled' : `${handlerAttribute('data-press', handler)}${attribute('data-hotkey', hotkey)}`}>${label}</button>`
169
+ return `<nav class="pagination" aria-label="Pages">${step(props['onPrevious'], 'Previous', 'p')}<span class="t">${numberOr(props['page'], 1)} of ${numberOr(props['pages'], 1)}</span>${step(props['onNext'], 'Next', 'n')}</nav>`
170
+ },
171
+ Progress: (props) => {
172
+ const label = typeof props['label'] === 'string' ? props['label'] : undefined
173
+ return `<progress class="progress" max="100" value="${numberOr(props['value'], 0)}"${attribute('aria-label', label)}${attribute('style', `width:${across(numberOr(props['width'], 24))}`)}></progress>`
174
+ },
175
+ List: (props, drawing) => `<ul class="list"${attribute('aria-label', textOr(props['label'], ''))}>${nodeHtml(props['children'], drawing)}</ul>`,
176
+ ListItem: (props, drawing) => {
177
+ const dot = props['tone'] === undefined ? '' : `<span class="dot tone-${toneOf(props['tone'])}" aria-hidden="true"></span>`
178
+ const leading = typeof props['leading'] === 'string' ? `<span class="leading">${escaped(props['leading'])}</span>` : ''
179
+ const description = typeof props['description'] === 'string' ? `<span class="description">${escaped(props['description'])}</span>` : ''
180
+ const trailing = props['trailing'] === undefined ? '' : `<span class="trailing">${nodeHtml(props['trailing'], drawing)}</span>`
181
+ const inner = `${dot}${leading}<span class="item-label">${escaped(textOr(props['label'], ''))}</span>${description}${trailing}`
182
+ const isSelected = props['isSelected'] === true
183
+ const depth = typeof props['depth'] === 'number' ? props['depth'] : undefined
184
+ const isOpen = typeof props['isOpen'] === 'boolean' ? props['isOpen'] : undefined
185
+ const opener =
186
+ depth === undefined
187
+ ? ''
188
+ : isOpen === undefined || handlerOf(props['onToggle']) === undefined
189
+ ? '<span class="opener" aria-hidden="true"></span>'
190
+ : `<button type="button" class="opener" aria-label="${isOpen ? 'Close' : 'Open'}" aria-expanded="${isOpen}"${attribute('data-key', `${textOr(props['key'], '')}:toggle`)}${handlerAttribute('data-press', props['onToggle'])}></button>`
191
+ const level = depth === undefined ? '' : ` style="--depth:${depth}" aria-level="${depth + 1}"`
192
+ const row = handlerOf(props['onPress']) === undefined ? `<span class="list-item${isSelected ? ' selected' : ''}">${inner}</span>` : `<button type="button" class="list-item${isSelected ? ' selected' : ''}"${attribute('data-key', keyOf(props))}${handlerAttribute('data-press', props['onPress'])}>${inner}</button>`
193
+ return `<li class="list-row${depth === undefined ? '' : ' tree-row'}"${isSelected ? ' aria-current="true"' : ''}${level}>${opener}${row}</li>`
194
+ },
195
+ Badge: (props, drawing) => `<span class="badge tone-${toneOf(props['tone'])}">${nodeHtml(props['children'], drawing)}</span>`,
196
+ Banner: (props, drawing) => {
197
+ const title = typeof props['title'] === 'string' ? `<strong class="banner-title">${escaped(props['title'])}</strong>` : ''
198
+ const actions = props['actions'] === undefined ? '' : `<div class="actions">${nodeHtml(props['actions'], drawing)}</div>`
199
+ return `<div role="status" class="banner tone-${toneOf(props['tone'])}">${title}<div class="banner-body">${nodeHtml(props['children'], drawing)}</div>${actions}</div>`
200
+ },
201
+ }
202
+
203
+ const tones: ReadonlySet<unknown> = new Set(['neutral', 'info', 'success', 'warning', 'critical', 'off'])
204
+
205
+ function toneOf(value: unknown): string {
206
+ return tones.has(value) ? (value as string) : 'neutral'
207
+ }
208
+
209
+ function textOr(value: unknown, fallback: string): string {
210
+ return typeof value === 'string' ? value : fallback
144
211
  }
145
212
 
146
213
  function accessibleName(props: Props): string {
@@ -1,63 +1,63 @@
1
- import { Box, Button, Text } from './elements.js'
1
+ import { Box, Button, hostComponent, Text } from './elements.js'
2
2
 
3
3
  export type Tab = { readonly key: string; readonly label: string; readonly hotkey?: string }
4
4
 
5
- export function Tabs({ tabs, selected, onSelect, children }: { readonly tabs: readonly Tab[]; readonly selected: string; readonly onSelect: (key: string) => void; readonly children?: JSX.Children }): JSX.Element {
6
- const row = tabs.map(({ key, label, hotkey }) =>
7
- key === selected
8
- ? Text({ children: [...(hotkey === undefined ? [] : [Text({ color: 'claude', children: hotkey }), ': ']), Text({ bold: true, underline: true, children: label })] })
9
- : Button({ plain: true, dimColor: true, key: `tab:${key}`, ...(hotkey === undefined ? {} : { hotkey }), label, onPress: () => onSelect(key) }),
10
- )
11
- return Box({ flexDirection: 'column', gap: 1, children: [Box({ gap: 3, children: row }), children] })
12
- }
5
+ export type Tone = 'neutral' | 'info' | 'success' | 'warning' | 'critical'
13
6
 
14
- export function Split({ children }: { readonly children: readonly JSX.Element[] }): JSX.Element {
15
- const width = `${Math.floor(100 / Math.max(children.length, 1))}%`
16
- return Box({ gap: 1, children: children.map((child) => Box({ width, children: child })) })
17
- }
7
+ type Pressed = () => void
18
8
 
19
- export function Panel({ title, children }: { readonly title: string; readonly children?: JSX.Children }): JSX.Element {
20
- return Box({
21
- flexDirection: 'column',
22
- flexGrow: 1,
23
- children: [
24
- Box({ flexDirection: 'column', flexGrow: 1, minWidth: [...title].length + 6, borderStyle: 'round', borderColor: 'subtle', paddingX: 1, children }),
25
- Box({ position: 'absolute', top: 0, left: 2, children: Text({ bold: true, wrap: 'truncate-end', children: ` ${title} ` }) }),
26
- ],
27
- })
28
- }
9
+ const toneColors: Readonly<Record<Tone, string | undefined>> = { neutral: undefined, info: 'suggestion', success: 'success', warning: 'warning', critical: 'error' }
29
10
 
30
- export function Toggle({ label, checked, onChange }: { readonly label: string; readonly checked: boolean; readonly onChange: (checked: boolean) => void }): JSX.Element {
31
- return Button({ plain: true, key: `toggle:${label}`, label: `${checked ? '[x]' : '[ ]'} ${label}`, onPress: () => onChange(!checked) })
11
+ const textOf = (tone: Tone) => {
12
+ const color = toneColors[tone]
13
+ return color === undefined ? { dimColor: true } : { color }
32
14
  }
33
15
 
34
- export function Tooltip({ text, children }: { readonly text: string; readonly children?: JSX.Children }): JSX.Element {
35
- return Box({ key: `tooltip:${text}`, children: [children, card(text, false)] })
36
- }
16
+ export const Tabs = hostComponent(
17
+ 'Tabs',
18
+ ({ tabs, selected, onSelect, children }: { readonly tabs: readonly Tab[]; readonly selected: string; readonly onSelect: (key: string) => void; readonly children?: JSX.Children }) => ({
19
+ key: '',
20
+ tabs: tabs.map(({ key, label, hotkey }) => ({ key, label, ...(hotkey === undefined ? {} : { hotkey }), isSelected: key === selected, onSelect: () => onSelect(key) })),
21
+ ...(children === undefined ? {} : { children }),
22
+ }),
23
+ ({ key: prefix, tabs, children }: { readonly key: string; readonly tabs: readonly (Tab & { readonly isSelected: boolean; readonly onSelect: Pressed })[]; readonly children?: JSX.Children }) => {
24
+ const row = tabs.map(({ key, label, hotkey, isSelected, onSelect }) =>
25
+ isSelected
26
+ ? Text({ children: [...(hotkey === undefined ? [] : [Text({ color: 'claude', children: hotkey }), ': ']), Text({ bold: true, underline: true, children: label })] })
27
+ : Button({ plain: true, dimColor: true, key: `${prefix}tab:${key}`, ...(hotkey === undefined ? {} : { hotkey }), label, onPress: onSelect }),
28
+ )
29
+ return Box({ flexDirection: 'column', gap: 1, children: [Box({ gap: 3, children: row }), children] })
30
+ },
31
+ )
37
32
 
38
- export function Help({ text, isOpen, onOpenChange }: { readonly text: string; readonly isOpen: boolean; readonly onOpenChange: (isOpen: boolean) => void }): JSX.Element {
39
- return Box({ key: `help:${text}`, children: [Button({ plain: true, key: `help-button:${text}`, label: '?', onPress: () => onOpenChange(!isOpen) }), card(text, isOpen)] })
40
- }
33
+ export const Split = hostComponent(
34
+ 'Split',
35
+ ({ children }: { readonly children: readonly JSX.Element[] }) => ({ children }),
36
+ ({ children }: { readonly children: readonly JSX.Element[] }) => {
37
+ const width = `${Math.floor(100 / Math.max(children.length, 1))}%`
38
+ return Box({ gap: 1, children: children.map((child) => Box({ width, children: child })) })
39
+ },
40
+ )
41
41
 
42
- export function Dialog({ title, children, actions }: { readonly title: string; readonly children?: JSX.Children; readonly actions: JSX.Children }): JSX.Element {
43
- return Box({ flexDirection: 'column', gap: 1, paddingX: 1, children: [Text({ bold: true, children: title }), children, Box({ gap: 2, children: actions })] })
44
- }
42
+ export const Panel = hostComponent(
43
+ 'Panel',
44
+ ({ title, children }: { readonly title: string; readonly children?: JSX.Children }) => ({ title, ...(children === undefined ? {} : { children }) }),
45
+ ({ title, children }: { readonly title: string; readonly children?: JSX.Children }) =>
46
+ Box({
47
+ flexDirection: 'column',
48
+ flexGrow: 1,
49
+ children: [
50
+ Box({ flexDirection: 'column', flexGrow: 1, minWidth: [...title].length + 6, borderStyle: 'round', borderColor: 'subtle', paddingX: 1, children }),
51
+ Box({ position: 'absolute', top: 0, left: 2, children: Text({ bold: true, wrap: 'truncate-end', children: ` ${title} ` }) }),
52
+ ],
53
+ }),
54
+ )
45
55
 
46
- export function Pagination({ page, pages, onPage }: { readonly page: number; readonly pages: number; readonly onPage: (page: number) => void }): JSX.Element {
47
- return Box({
48
- gap: 2,
49
- children: [
50
- page > 1 ? Button({ plain: true, hotkey: 'p', label: 'Previous', onPress: () => onPage(page - 1) }) : Text({ dimColor: true, children: 'p: Previous' }),
51
- Text({ dimColor: true, children: `${page} of ${pages}` }),
52
- page < pages ? Button({ plain: true, hotkey: 'n', label: 'Next', onPress: () => onPage(page + 1) }) : Text({ dimColor: true, children: 'n: Next' }),
53
- ],
54
- })
55
- }
56
-
57
- export function Progress({ value, width = 24 }: { readonly value: number; readonly width?: number }): JSX.Element {
58
- const filled = Math.round((Math.min(Math.max(value, 0), 100) / 100) * width)
59
- return Text({ children: [Text({ color: 'claude', children: '█'.repeat(filled) }), Text({ color: 'subtle', children: '░'.repeat(width - filled) })] })
60
- }
56
+ export const Toggle = hostComponent(
57
+ 'Toggle',
58
+ ({ label, checked, onChange }: { readonly label: string; readonly checked: boolean; readonly onChange: (checked: boolean) => void }) => ({ key: '', label, checked, onToggle: () => onChange(!checked) }),
59
+ ({ key, label, checked, onToggle }: { readonly key: string; readonly label: string; readonly checked: boolean; readonly onToggle: Pressed }) => Button({ plain: true, key: `${key}toggle:${label}`, label: `${checked ? '[x]' : '[ ]'} ${label}`, onPress: onToggle }),
60
+ )
61
61
 
62
62
  const cardColumns = 36
63
63
 
@@ -73,3 +73,110 @@ function card(text: string, isShown: boolean): JSX.Element {
73
73
  children: Text({ color: 'inverseText', wrap: 'wrap', children: text }),
74
74
  })
75
75
  }
76
+
77
+ export const Tooltip = hostComponent(
78
+ 'Tooltip',
79
+ ({ text, children }: { readonly text: string; readonly children?: JSX.Children }) => ({ key: '', text, ...(children === undefined ? {} : { children }) }),
80
+ ({ key, text, children }: { readonly key: string; readonly text: string; readonly children?: JSX.Children }) => Box({ key: `${key}tooltip:${text}`, children: [children, card(text, false)] }),
81
+ )
82
+
83
+ export const Help = hostComponent(
84
+ 'Help',
85
+ ({ text, isOpen, onOpenChange }: { readonly text: string; readonly isOpen: boolean; readonly onOpenChange: (isOpen: boolean) => void }) => ({ key: '', text, isOpen, onToggle: () => onOpenChange(!isOpen) }),
86
+ ({ key, text, isOpen, onToggle }: { readonly key: string; readonly text: string; readonly isOpen: boolean; readonly onToggle: Pressed }) =>
87
+ Box({ key: `${key}help:${text}`, children: [Button({ plain: true, key: `${key}help-button:${text}`, label: '?', onPress: onToggle }), card(text, isOpen)] }),
88
+ )
89
+
90
+ export const Dialog = hostComponent(
91
+ 'Dialog',
92
+ ({ title, children, actions }: { readonly title: string; readonly children?: JSX.Children; readonly actions: JSX.Children }) => ({ title, actions, ...(children === undefined ? {} : { children }) }),
93
+ ({ title, children, actions }: { readonly title: string; readonly children?: JSX.Children; readonly actions: JSX.Children }) =>
94
+ Box({ flexDirection: 'column', gap: 1, paddingX: 1, children: [Text({ bold: true, children: title }), children, Box({ gap: 2, children: actions })] }),
95
+ )
96
+
97
+ export const Pagination = hostComponent(
98
+ 'Pagination',
99
+ ({ page, pages, onPage }: { readonly page: number; readonly pages: number; readonly onPage: (page: number) => void }) => ({
100
+ page,
101
+ pages,
102
+ ...(page > 1 ? { onPrevious: () => onPage(page - 1) } : {}),
103
+ ...(page < pages ? { onNext: () => onPage(page + 1) } : {}),
104
+ }),
105
+ ({ page, pages, onPrevious, onNext }: { readonly page: number; readonly pages: number; readonly onPrevious?: Pressed; readonly onNext?: Pressed }) =>
106
+ Box({
107
+ gap: 2,
108
+ children: [
109
+ onPrevious === undefined ? Text({ dimColor: true, children: 'p: Previous' }) : Button({ plain: true, hotkey: 'p', label: 'Previous', onPress: onPrevious }),
110
+ Text({ dimColor: true, children: `${page} of ${pages}` }),
111
+ onNext === undefined ? Text({ dimColor: true, children: 'n: Next' }) : Button({ plain: true, hotkey: 'n', label: 'Next', onPress: onNext }),
112
+ ],
113
+ }),
114
+ )
115
+
116
+ export const Progress = hostComponent(
117
+ 'Progress',
118
+ ({ value, width = 24, label }: { readonly value: number; readonly width?: number; readonly label?: string }) => ({ value: Math.min(Math.max(value, 0), 100), width, ...(label === undefined ? {} : { label }) }),
119
+ ({ value, width }: { readonly value: number; readonly width: number; readonly label?: string }) => {
120
+ const filled = Math.round((value / 100) * width)
121
+ return Text({ children: [Text({ color: 'claude', children: '█'.repeat(filled) }), Text({ color: 'subtle', children: '░'.repeat(width - filled) })] })
122
+ },
123
+ )
124
+
125
+ export const List = hostComponent(
126
+ 'List',
127
+ ({ label, children }: { readonly label: string; readonly children?: JSX.Children }) => ({ label, ...(children === undefined ? {} : { children }) }),
128
+ ({ children }: { readonly label: string; readonly children?: JSX.Children }) => Box({ flexDirection: 'column', children }),
129
+ )
130
+
131
+ type ListItemShared = { readonly key: string; readonly label: string; readonly description?: string; readonly leading?: string; readonly trailing?: JSX.Children; readonly tone?: Tone | 'off'; readonly isSelected?: boolean; readonly depth?: number; readonly isOpen?: boolean; readonly onPress?: Pressed }
132
+
133
+ type ListItemProps = ListItemShared & { readonly onOpenChange?: (isOpen: boolean) => void }
134
+
135
+ type ListItemData = ListItemShared & { readonly onToggle?: Pressed }
136
+
137
+ const statusOf = (tone: ListItemProps['tone']) => (tone === undefined ? [] : [tone === 'off' ? Text({ dimColor: true, children: '○ ' }) : Text({ ...textOf(tone), children: '● ' })])
138
+
139
+ function treeRow({ key, label, leading, trailing, tone, isSelected, depth = 0, isOpen, onToggle, onPress }: ListItemData): JSX.Element {
140
+ const opener = isOpen !== undefined && onToggle !== undefined ? Button({ plain: true, key: `${key}:toggle`, onPress: onToggle, children: Text({ dimColor: true, children: isOpen ? '▾' : '▸' }) }) : Text({ children: ' ' })
141
+ const shown = Text({ wrap: 'truncate-end', ...(isSelected === true ? { bold: true } : {}), children: [...statusOf(tone), ...(leading === undefined ? [] : [Text({ dimColor: true, children: `${leading} ` })]), label] })
142
+ const row = Box({ columnGap: 1, children: [opener, onPress === undefined ? shown : Button({ plain: true, key, onPress, children: shown })] })
143
+ if (trailing === undefined) return Box({ paddingLeft: depth * 2, children: row })
144
+ return Box({ width: '100%', paddingLeft: depth * 2, columnGap: 1, justifyContent: 'space-between', children: [row, Box({ flexShrink: 0, children: trailing })] })
145
+ }
146
+
147
+ export const ListItem = hostComponent(
148
+ 'ListItem',
149
+ ({ onOpenChange, ...props }: ListItemProps): ListItemData => ({ ...props, ...(onOpenChange === undefined || props.isOpen === undefined ? {} : { onToggle: () => onOpenChange(props.isOpen !== true) }) }),
150
+ (props: ListItemData) => {
151
+ const { key, label, description, leading, trailing, tone, isSelected, depth, onPress } = props
152
+ if (depth !== undefined) return treeRow(props)
153
+ const shown = [...statusOf(tone), ...(leading === undefined ? [] : [Text({ dimColor: true, children: `${leading} ` })]), label, ...(description === undefined ? [] : [Text({ dimColor: true, children: ` ${description}` })]), ...(trailing === undefined ? [] : [' ', trailing])]
154
+ if (isSelected === true || onPress === undefined) return Box({ ...(isSelected === true ? { backgroundColor: 'userMessageBackground' } : {}), paddingX: 1, children: Text({ wrap: 'truncate-end', ...(isSelected === true ? { bold: true } : {}), children: shown }) })
155
+ return Box({ paddingX: 1, children: Button({ plain: true, dimColor: true, key, onPress, children: shown }) })
156
+ },
157
+ )
158
+
159
+ export const Badge = hostComponent(
160
+ 'Badge',
161
+ ({ tone = 'neutral', children }: { readonly tone?: Tone; readonly children: JSX.Children }) => ({ tone, children }),
162
+ ({ tone, children }: { readonly tone: Tone; readonly children: JSX.Children }) => Text({ ...textOf(tone), children }),
163
+ )
164
+
165
+ const wordsOf = (children: JSX.Children): string | undefined => {
166
+ const all = [children].flat(Infinity as 1).filter((each) => each !== null && each !== undefined && typeof each !== 'boolean')
167
+ return all.every((each) => typeof each === 'string' || typeof each === 'number') ? all.join('') : undefined
168
+ }
169
+
170
+ export const Banner = hostComponent(
171
+ 'Banner',
172
+ ({ tone = 'info', title, children, actions }: { readonly tone?: Tone; readonly title?: string; readonly children?: JSX.Children; readonly actions?: JSX.Children }) => ({ tone, ...(title === undefined ? {} : { title }), ...(children === undefined ? {} : { children }), ...(actions === undefined ? {} : { actions }) }),
173
+ ({ tone, title, children, actions }: { readonly tone: Tone; readonly title?: string; readonly children?: JSX.Children; readonly actions?: JSX.Children }) =>
174
+ Box({
175
+ flexDirection: 'column',
176
+ children: [
177
+ ...(title === undefined ? [] : [Text({ ...textOf(tone), bold: true, wrap: 'wrap', children: title })]),
178
+ ...(children === undefined ? [] : [wordsOf(children) === undefined ? children : Text({ ...(title === undefined ? textOf(tone) : {}), wrap: 'wrap', children: wordsOf(children) })]),
179
+ ...(actions === undefined ? [] : [Box({ gap: 2, flexShrink: 0, children: actions })]),
180
+ ],
181
+ }),
182
+ )
@@ -44,10 +44,21 @@ type Drawing = { readonly table: CompletedTable; readonly markdown: ((props: Mar
44
44
 
45
45
  const unbuilt = Symbol('an element built outside a render')
46
46
 
47
- type Unbuilt = { readonly [unbuilt]: { readonly name: ElementName; readonly props: object } }
47
+ type Unbuilt = { readonly [unbuilt]: { readonly name: string; readonly props: object } }
48
48
 
49
49
  let drawing: Drawing | undefined
50
50
 
51
+ const lowerings = new Map<string, (props: object) => RenderElement>()
52
+
53
+ export function hostComponent<Props extends object, Canonical extends object>(name: string, canonical: (props: Props) => Canonical, lower: (props: Canonical) => JSX.Element): (props: Props) => JSX.Element {
54
+ lowerings.set(name, lower as unknown as (props: object) => RenderElement)
55
+ return (props) => elementNamed(name, canonical(props))
56
+ }
57
+
58
+ function elementNamed(name: string, props: object): RenderElement {
59
+ return drawing === undefined ? ({ [unbuilt]: { name, props } } satisfies Unbuilt as unknown as RenderElement) : build(drawing, name, props)
60
+ }
61
+
51
62
  export function drawWith<Drawn>(table: ElementTable, draw: () => Drawn, markdown?: (props: MarkdownProps) => RenderElement): Drawn {
52
63
  const outer = drawing
53
64
  const current = { table: table as CompletedTable, markdown }
@@ -60,14 +71,15 @@ export function drawWith<Drawn>(table: ElementTable, draw: () => Drawn, markdown
60
71
  }
61
72
 
62
73
  function element<Name extends keyof Terminal & ElementName>(name: Name): Terminal[Name] {
63
- const construct = (props: object) => (drawing === undefined ? ({ [unbuilt]: { name, props } } satisfies Unbuilt as unknown as RenderElement) : build(drawing, name, props))
64
- return construct
74
+ return (props: object) => elementNamed(name, props)
65
75
  }
66
76
 
67
- function build(current: Drawing, name: ElementName, props: object): RenderElement {
77
+ function build(current: Drawing, name: string, props: object): RenderElement {
78
+ const lower = lowerings.get(name)
79
+ if (lower !== undefined) return lower(props)
68
80
  const ready = 'children' in props ? { ...props, children: built(current, props.children) } : props
69
81
  if (name === 'Markdown' && current.markdown !== undefined) return current.markdown(ready as MarkdownProps)
70
- return current.table[name](ready)
82
+ return current.table[name as ElementName](ready)
71
83
  }
72
84
 
73
85
  function built(current: Drawing, value: unknown): unknown {
@@ -124,9 +136,10 @@ export function elementOf(data: unknown, keyPrefix: string, press: (handler: num
124
136
  }
125
137
  const name = fields[elementKey]
126
138
  if (name === undefined) return Object.fromEntries(Object.entries(fields).map(([key, each]) => [key, elementOf(each, keyPrefix, press)]))
127
- if (typeof name !== 'string' || !Object.hasOwn(constructors, name)) throw new Error(`a widget drew ${String(name)}, which is not an element of ui/elements.js.`)
139
+ if (typeof name !== 'string' || !(Object.hasOwn(constructors, name) || lowerings.has(name))) throw new Error(`a widget drew ${String(name)}, which is not an element of ui/elements.js or ui/components.js.`)
128
140
  const props = elementOf(fields['props'], keyPrefix, press) as Record<string, unknown>
129
- return constructors[name as keyof typeof constructors]((typeof props['key'] === 'string' ? { ...props, key: `${keyPrefix}${props['key']}` } : props) as never)
141
+ const keyed = typeof props['key'] === 'string' ? { ...props, key: `${keyPrefix}${props['key']}` } : props
142
+ return Object.hasOwn(constructors, name) ? constructors[name as keyof typeof constructors](keyed as never) : elementNamed(name, keyed)
130
143
  }
131
144
 
132
145
  const focusable: ReadonlySet<unknown> = new Set(['Button', 'Input', 'Select'])
@@ -3,45 +3,79 @@ export type Tab = {
3
3
  readonly label: string;
4
4
  readonly hotkey?: string;
5
5
  };
6
- export declare function Tabs({ tabs, selected, onSelect, children }: {
6
+ export type Tone = 'neutral' | 'info' | 'success' | 'warning' | 'critical';
7
+ type Pressed = () => void;
8
+ export declare const Tabs: (props: {
7
9
  readonly tabs: readonly Tab[];
8
10
  readonly selected: string;
9
11
  readonly onSelect: (key: string) => void;
10
12
  readonly children?: JSX.Children;
11
- }): JSX.Element;
12
- export declare function Split({ children }: {
13
+ }) => JSX.Element;
14
+ export declare const Split: (props: {
13
15
  readonly children: readonly JSX.Element[];
14
- }): JSX.Element;
15
- export declare function Panel({ title, children }: {
16
+ }) => JSX.Element;
17
+ export declare const Panel: (props: {
16
18
  readonly title: string;
17
19
  readonly children?: JSX.Children;
18
- }): JSX.Element;
19
- export declare function Toggle({ label, checked, onChange }: {
20
+ }) => JSX.Element;
21
+ export declare const Toggle: (props: {
20
22
  readonly label: string;
21
23
  readonly checked: boolean;
22
24
  readonly onChange: (checked: boolean) => void;
23
- }): JSX.Element;
24
- export declare function Tooltip({ text, children }: {
25
+ }) => JSX.Element;
26
+ export declare const Tooltip: (props: {
25
27
  readonly text: string;
26
28
  readonly children?: JSX.Children;
27
- }): JSX.Element;
28
- export declare function Help({ text, isOpen, onOpenChange }: {
29
+ }) => JSX.Element;
30
+ export declare const Help: (props: {
29
31
  readonly text: string;
30
32
  readonly isOpen: boolean;
31
33
  readonly onOpenChange: (isOpen: boolean) => void;
32
- }): JSX.Element;
33
- export declare function Dialog({ title, children, actions }: {
34
+ }) => JSX.Element;
35
+ export declare const Dialog: (props: {
34
36
  readonly title: string;
35
37
  readonly children?: JSX.Children;
36
38
  readonly actions: JSX.Children;
37
- }): JSX.Element;
38
- export declare function Pagination({ page, pages, onPage }: {
39
+ }) => JSX.Element;
40
+ export declare const Pagination: (props: {
39
41
  readonly page: number;
40
42
  readonly pages: number;
41
43
  readonly onPage: (page: number) => void;
42
- }): JSX.Element;
43
- export declare function Progress({ value, width }: {
44
+ }) => JSX.Element;
45
+ export declare const Progress: (props: {
44
46
  readonly value: number;
45
47
  readonly width?: number;
46
- }): JSX.Element;
48
+ readonly label?: string;
49
+ }) => JSX.Element;
50
+ export declare const List: (props: {
51
+ readonly label: string;
52
+ readonly children?: JSX.Children;
53
+ }) => JSX.Element;
54
+ type ListItemShared = {
55
+ readonly key: string;
56
+ readonly label: string;
57
+ readonly description?: string;
58
+ readonly leading?: string;
59
+ readonly trailing?: JSX.Children;
60
+ readonly tone?: Tone | 'off';
61
+ readonly isSelected?: boolean;
62
+ readonly depth?: number;
63
+ readonly isOpen?: boolean;
64
+ readonly onPress?: Pressed;
65
+ };
66
+ type ListItemProps = ListItemShared & {
67
+ readonly onOpenChange?: (isOpen: boolean) => void;
68
+ };
69
+ export declare const ListItem: (props: ListItemProps) => JSX.Element;
70
+ export declare const Badge: (props: {
71
+ readonly tone?: Tone;
72
+ readonly children: JSX.Children;
73
+ }) => JSX.Element;
74
+ export declare const Banner: (props: {
75
+ readonly tone?: Tone;
76
+ readonly title?: string;
77
+ readonly children?: JSX.Children;
78
+ readonly actions?: JSX.Children;
79
+ }) => JSX.Element;
80
+ export {};
47
81
  //# sourceMappingURL=components.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"components.d.ts","sourceRoot":"","sources":["../src/ui/components.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,GAAG,GAAG;IAAE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,CAAA;AAE5F,wBAAgB,IAAI,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,EAAE;IAAE,QAAQ,CAAC,IAAI,EAAE,SAAS,GAAG,EAAE,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,GAAG,CAAC,QAAQ,CAAA;CAAE,GAAG,GAAG,CAAC,OAAO,CAOlN;AAED,wBAAgB,KAAK,CAAC,EAAE,QAAQ,EAAE,EAAE;IAAE,QAAQ,CAAC,QAAQ,EAAE,SAAS,GAAG,CAAC,OAAO,EAAE,CAAA;CAAE,GAAG,GAAG,CAAC,OAAO,CAG9F;AAED,wBAAgB,KAAK,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE;IAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC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B,GAAG,CAAC,QAAQ;iBAEzF,CAAA;AAOD,eAAO,MAAM,MAAM;oBAE+C,IAAI;qBAAmB,MAAM;wBAAsB,GAAG,CAAC,QAAQ;uBAAqB,GAAG,CAAC,QAAQ;iBAUjK,CAAA"}
package/ui/components.js CHANGED
@@ -1,50 +1,32 @@
1
- import { Box, Button, Text } from './elements.js';
2
- export function Tabs({ tabs, selected, onSelect, children }) {
3
- const row = tabs.map(({ key, label, hotkey }) => key === selected
1
+ import { Box, Button, hostComponent, Text } from './elements.js';
2
+ const toneColors = { neutral: undefined, info: 'suggestion', success: 'success', warning: 'warning', critical: 'error' };
3
+ const textOf = (tone) => {
4
+ const color = toneColors[tone];
5
+ return color === undefined ? { dimColor: true } : { color };
6
+ };
7
+ export const Tabs = hostComponent('Tabs', ({ tabs, selected, onSelect, children }) => ({
8
+ key: '',
9
+ tabs: tabs.map(({ key, label, hotkey }) => ({ key, label, ...(hotkey === undefined ? {} : { hotkey }), isSelected: key === selected, onSelect: () => onSelect(key) })),
10
+ ...(children === undefined ? {} : { children }),
11
+ }), ({ key: prefix, tabs, children }) => {
12
+ const row = tabs.map(({ key, label, hotkey, isSelected, onSelect }) => isSelected
4
13
  ? Text({ children: [...(hotkey === undefined ? [] : [Text({ color: 'claude', children: hotkey }), ': ']), Text({ bold: true, underline: true, children: label })] })
5
- : Button({ plain: true, dimColor: true, key: `tab:${key}`, ...(hotkey === undefined ? {} : { hotkey }), label, onPress: () => onSelect(key) }));
14
+ : Button({ plain: true, dimColor: true, key: `${prefix}tab:${key}`, ...(hotkey === undefined ? {} : { hotkey }), label, onPress: onSelect }));
6
15
  return Box({ flexDirection: 'column', gap: 1, children: [Box({ gap: 3, children: row }), children] });
7
- }
8
- export function Split({ children }) {
16
+ });
17
+ export const Split = hostComponent('Split', ({ children }) => ({ children }), ({ children }) => {
9
18
  const width = `${Math.floor(100 / Math.max(children.length, 1))}%`;
10
19
  return Box({ gap: 1, children: children.map((child) => Box({ width, children: child })) });
11
- }
12
- export function Panel({ title, children }) {
13
- return Box({
14
- flexDirection: 'column',
15
- flexGrow: 1,
16
- children: [
17
- Box({ flexDirection: 'column', flexGrow: 1, minWidth: [...title].length + 6, borderStyle: 'round', borderColor: 'subtle', paddingX: 1, children }),
18
- Box({ position: 'absolute', top: 0, left: 2, children: Text({ bold: true, wrap: 'truncate-end', children: ` ${title} ` }) }),
19
- ],
20
- });
21
- }
22
- export function Toggle({ label, checked, onChange }) {
23
- return Button({ plain: true, key: `toggle:${label}`, label: `${checked ? '[x]' : '[ ]'} ${label}`, onPress: () => onChange(!checked) });
24
- }
25
- export function Tooltip({ text, children }) {
26
- return Box({ key: `tooltip:${text}`, children: [children, card(text, false)] });
27
- }
28
- export function Help({ text, isOpen, onOpenChange }) {
29
- return Box({ key: `help:${text}`, children: [Button({ plain: true, key: `help-button:${text}`, label: '?', onPress: () => onOpenChange(!isOpen) }), card(text, isOpen)] });
30
- }
31
- export function Dialog({ title, children, actions }) {
32
- return Box({ flexDirection: 'column', gap: 1, paddingX: 1, children: [Text({ bold: true, children: title }), children, Box({ gap: 2, children: actions })] });
33
- }
34
- export function Pagination({ page, pages, onPage }) {
35
- return Box({
36
- gap: 2,
37
- children: [
38
- page > 1 ? Button({ plain: true, hotkey: 'p', label: 'Previous', onPress: () => onPage(page - 1) }) : Text({ dimColor: true, children: 'p: Previous' }),
39
- Text({ dimColor: true, children: `${page} of ${pages}` }),
40
- page < pages ? Button({ plain: true, hotkey: 'n', label: 'Next', onPress: () => onPage(page + 1) }) : Text({ dimColor: true, children: 'n: Next' }),
41
- ],
42
- });
43
- }
44
- export function Progress({ value, width = 24 }) {
45
- const filled = Math.round((Math.min(Math.max(value, 0), 100) / 100) * width);
46
- return Text({ children: [Text({ color: 'claude', children: '█'.repeat(filled) }), Text({ color: 'subtle', children: '░'.repeat(width - filled) })] });
47
- }
20
+ });
21
+ export const Panel = hostComponent('Panel', ({ title, children }) => ({ title, ...(children === undefined ? {} : { children }) }), ({ title, children }) => Box({
22
+ flexDirection: 'column',
23
+ flexGrow: 1,
24
+ children: [
25
+ Box({ flexDirection: 'column', flexGrow: 1, minWidth: [...title].length + 6, borderStyle: 'round', borderColor: 'subtle', paddingX: 1, children }),
26
+ Box({ position: 'absolute', top: 0, left: 2, children: Text({ bold: true, wrap: 'truncate-end', children: ` ${title} ` }) }),
27
+ ],
28
+ }));
29
+ export const Toggle = hostComponent('Toggle', ({ label, checked, onChange }) => ({ key: '', label, checked, onToggle: () => onChange(!checked) }), ({ key, label, checked, onToggle }) => Button({ plain: true, key: `${key}toggle:${label}`, label: `${checked ? '[x]' : '[ ]'} ${label}`, onPress: onToggle }));
48
30
  const cardColumns = 36;
49
31
  function card(text, isShown) {
50
32
  return Box({
@@ -58,3 +40,55 @@ function card(text, isShown) {
58
40
  children: Text({ color: 'inverseText', wrap: 'wrap', children: text }),
59
41
  });
60
42
  }
43
+ export const Tooltip = hostComponent('Tooltip', ({ text, children }) => ({ key: '', text, ...(children === undefined ? {} : { children }) }), ({ key, text, children }) => Box({ key: `${key}tooltip:${text}`, children: [children, card(text, false)] }));
44
+ export const Help = hostComponent('Help', ({ text, isOpen, onOpenChange }) => ({ key: '', text, isOpen, onToggle: () => onOpenChange(!isOpen) }), ({ key, text, isOpen, onToggle }) => Box({ key: `${key}help:${text}`, children: [Button({ plain: true, key: `${key}help-button:${text}`, label: '?', onPress: onToggle }), card(text, isOpen)] }));
45
+ export const Dialog = hostComponent('Dialog', ({ title, children, actions }) => ({ title, actions, ...(children === undefined ? {} : { children }) }), ({ title, children, actions }) => Box({ flexDirection: 'column', gap: 1, paddingX: 1, children: [Text({ bold: true, children: title }), children, Box({ gap: 2, children: actions })] }));
46
+ export const Pagination = hostComponent('Pagination', ({ page, pages, onPage }) => ({
47
+ page,
48
+ pages,
49
+ ...(page > 1 ? { onPrevious: () => onPage(page - 1) } : {}),
50
+ ...(page < pages ? { onNext: () => onPage(page + 1) } : {}),
51
+ }), ({ page, pages, onPrevious, onNext }) => Box({
52
+ gap: 2,
53
+ children: [
54
+ onPrevious === undefined ? Text({ dimColor: true, children: 'p: Previous' }) : Button({ plain: true, hotkey: 'p', label: 'Previous', onPress: onPrevious }),
55
+ Text({ dimColor: true, children: `${page} of ${pages}` }),
56
+ onNext === undefined ? Text({ dimColor: true, children: 'n: Next' }) : Button({ plain: true, hotkey: 'n', label: 'Next', onPress: onNext }),
57
+ ],
58
+ }));
59
+ export const Progress = hostComponent('Progress', ({ value, width = 24, label }) => ({ value: Math.min(Math.max(value, 0), 100), width, ...(label === undefined ? {} : { label }) }), ({ value, width }) => {
60
+ const filled = Math.round((value / 100) * width);
61
+ return Text({ children: [Text({ color: 'claude', children: '█'.repeat(filled) }), Text({ color: 'subtle', children: '░'.repeat(width - filled) })] });
62
+ });
63
+ export const List = hostComponent('List', ({ label, children }) => ({ label, ...(children === undefined ? {} : { children }) }), ({ children }) => Box({ flexDirection: 'column', children }));
64
+ const statusOf = (tone) => (tone === undefined ? [] : [tone === 'off' ? Text({ dimColor: true, children: '○ ' }) : Text({ ...textOf(tone), children: '● ' })]);
65
+ function treeRow({ key, label, leading, trailing, tone, isSelected, depth = 0, isOpen, onToggle, onPress }) {
66
+ const opener = isOpen !== undefined && onToggle !== undefined ? Button({ plain: true, key: `${key}:toggle`, onPress: onToggle, children: Text({ dimColor: true, children: isOpen ? '▾' : '▸' }) }) : Text({ children: ' ' });
67
+ const shown = Text({ wrap: 'truncate-end', ...(isSelected === true ? { bold: true } : {}), children: [...statusOf(tone), ...(leading === undefined ? [] : [Text({ dimColor: true, children: `${leading} ` })]), label] });
68
+ const row = Box({ columnGap: 1, children: [opener, onPress === undefined ? shown : Button({ plain: true, key, onPress, children: shown })] });
69
+ if (trailing === undefined)
70
+ return Box({ paddingLeft: depth * 2, children: row });
71
+ return Box({ width: '100%', paddingLeft: depth * 2, columnGap: 1, justifyContent: 'space-between', children: [row, Box({ flexShrink: 0, children: trailing })] });
72
+ }
73
+ export const ListItem = hostComponent('ListItem', ({ onOpenChange, ...props }) => ({ ...props, ...(onOpenChange === undefined || props.isOpen === undefined ? {} : { onToggle: () => onOpenChange(props.isOpen !== true) }) }), (props) => {
74
+ const { key, label, description, leading, trailing, tone, isSelected, depth, onPress } = props;
75
+ if (depth !== undefined)
76
+ return treeRow(props);
77
+ const shown = [...statusOf(tone), ...(leading === undefined ? [] : [Text({ dimColor: true, children: `${leading} ` })]), label, ...(description === undefined ? [] : [Text({ dimColor: true, children: ` ${description}` })]), ...(trailing === undefined ? [] : [' ', trailing])];
78
+ if (isSelected === true || onPress === undefined)
79
+ return Box({ ...(isSelected === true ? { backgroundColor: 'userMessageBackground' } : {}), paddingX: 1, children: Text({ wrap: 'truncate-end', ...(isSelected === true ? { bold: true } : {}), children: shown }) });
80
+ return Box({ paddingX: 1, children: Button({ plain: true, dimColor: true, key, onPress, children: shown }) });
81
+ });
82
+ export const Badge = hostComponent('Badge', ({ tone = 'neutral', children }) => ({ tone, children }), ({ tone, children }) => Text({ ...textOf(tone), children }));
83
+ const wordsOf = (children) => {
84
+ const all = [children].flat(Infinity).filter((each) => each !== null && each !== undefined && typeof each !== 'boolean');
85
+ return all.every((each) => typeof each === 'string' || typeof each === 'number') ? all.join('') : undefined;
86
+ };
87
+ export const Banner = hostComponent('Banner', ({ tone = 'info', title, children, actions }) => ({ tone, ...(title === undefined ? {} : { title }), ...(children === undefined ? {} : { children }), ...(actions === undefined ? {} : { actions }) }), ({ tone, title, children, actions }) => Box({
88
+ flexDirection: 'column',
89
+ children: [
90
+ ...(title === undefined ? [] : [Text({ ...textOf(tone), bold: true, wrap: 'wrap', children: title })]),
91
+ ...(children === undefined ? [] : [wordsOf(children) === undefined ? children : Text({ ...(title === undefined ? textOf(tone) : {}), wrap: 'wrap', children: wordsOf(children) })]),
92
+ ...(actions === undefined ? [] : [Box({ gap: 2, flexShrink: 0, children: actions })]),
93
+ ],
94
+ }));
package/ui/elements.d.ts CHANGED
@@ -27,6 +27,7 @@ export type ButtonProps = Omit<ClaudeButtonProps, 'onPress'> & ({
27
27
  readonly href: string;
28
28
  readonly onPress?: never;
29
29
  });
30
+ export declare function hostComponent<Props extends object, Canonical extends object>(name: string, canonical: (props: Props) => Canonical, lower: (props: Canonical) => JSX.Element): (props: Props) => JSX.Element;
30
31
  export declare function drawWith<Drawn>(table: ElementTable, draw: () => Drawn, markdown?: (props: MarkdownProps) => RenderElement): Drawn;
31
32
  export declare function drawKeyed<Drawn>(keyPrefix: string, draw: () => Drawn): Drawn;
32
33
  export type Handler = (...args: unknown[]) => unknown;