agent-facets 0.1.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.
- package/CHANGELOG.md +30 -0
- package/bunfig.toml +2 -0
- package/dist/facet +0 -0
- package/dist/facet-ink-test +0 -0
- package/package.json +41 -0
- package/src/__tests__/cli.test.ts +152 -0
- package/src/__tests__/create-build.test.ts +207 -0
- package/src/commands/build.ts +45 -0
- package/src/commands/create/index.ts +30 -0
- package/src/commands/create/types.ts +9 -0
- package/src/commands/create/wizard.tsx +24 -0
- package/src/commands/create-scaffold.ts +180 -0
- package/src/commands.ts +31 -0
- package/src/help.ts +36 -0
- package/src/index.ts +9 -0
- package/src/run.ts +55 -0
- package/src/suggest.ts +35 -0
- package/src/tui/components/asset-inline-input.tsx +79 -0
- package/src/tui/components/asset-item.tsx +48 -0
- package/src/tui/components/asset-section.tsx +145 -0
- package/src/tui/components/button.tsx +92 -0
- package/src/tui/components/editable-field.tsx +172 -0
- package/src/tui/components/exit-toast.tsx +20 -0
- package/src/tui/components/stage-row.tsx +33 -0
- package/src/tui/components/version-selector.tsx +79 -0
- package/src/tui/context/focus-mode-context.ts +36 -0
- package/src/tui/context/focus-order-context.ts +62 -0
- package/src/tui/context/form-state-context.ts +229 -0
- package/src/tui/gradient.ts +1 -0
- package/src/tui/hooks/use-exit-keys.ts +75 -0
- package/src/tui/hooks/use-navigation-keys.ts +34 -0
- package/src/tui/layouts/wizard-layout.tsx +41 -0
- package/src/tui/theme.ts +1 -0
- package/src/tui/views/build/build-view.tsx +153 -0
- package/src/tui/views/create/confirm-view.tsx +74 -0
- package/src/tui/views/create/create-view.tsx +154 -0
- package/src/tui/views/create/wizard.tsx +68 -0
- package/src/version.ts +3 -0
- package/tsconfig.json +4 -0
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { Box, Text, useInput } from 'ink'
|
|
2
|
+
import Gradient from 'ink-gradient'
|
|
3
|
+
import type { ReactNode } from 'react'
|
|
4
|
+
import { useEffect, useState } from 'react'
|
|
5
|
+
import { useFocusOrder } from '../context/focus-order-context.ts'
|
|
6
|
+
import { GRADIENT_STOPS, getAnimatedGradient } from '../gradient.ts'
|
|
7
|
+
import { THEME } from '../theme.ts'
|
|
8
|
+
|
|
9
|
+
const ANIMATION_INTERVAL_MS = 75
|
|
10
|
+
|
|
11
|
+
export function Button({
|
|
12
|
+
id,
|
|
13
|
+
label,
|
|
14
|
+
hint,
|
|
15
|
+
onPress,
|
|
16
|
+
disabled,
|
|
17
|
+
color,
|
|
18
|
+
gradient: showGradient,
|
|
19
|
+
animateGradient,
|
|
20
|
+
}: {
|
|
21
|
+
id: string
|
|
22
|
+
label: string
|
|
23
|
+
hint?: ReactNode
|
|
24
|
+
onPress: () => void
|
|
25
|
+
disabled?: boolean
|
|
26
|
+
color?: string
|
|
27
|
+
autoFocus?: boolean
|
|
28
|
+
gradient?: boolean
|
|
29
|
+
animateGradient?: boolean
|
|
30
|
+
}) {
|
|
31
|
+
const { focusedId } = useFocusOrder()
|
|
32
|
+
const isFocused = focusedId === id && !disabled
|
|
33
|
+
const [offset, setOffset] = useState(0)
|
|
34
|
+
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
if (!animateGradient) return
|
|
37
|
+
const interval = setInterval(() => {
|
|
38
|
+
setOffset((prev) => (prev + 1) % GRADIENT_STOPS.length)
|
|
39
|
+
}, ANIMATION_INTERVAL_MS)
|
|
40
|
+
return () => clearInterval(interval)
|
|
41
|
+
}, [animateGradient])
|
|
42
|
+
|
|
43
|
+
useInput(
|
|
44
|
+
(_input, key) => {
|
|
45
|
+
if (key.return) {
|
|
46
|
+
onPress()
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
{ isActive: isFocused },
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
const prefix = isFocused ? '▸ ' : ' '
|
|
53
|
+
|
|
54
|
+
const focusHint = isFocused && hint ? <Text> {hint}</Text> : null
|
|
55
|
+
|
|
56
|
+
if (disabled) {
|
|
57
|
+
return (
|
|
58
|
+
<Box gap={0}>
|
|
59
|
+
<Text color="gray" dimColor>
|
|
60
|
+
{prefix}
|
|
61
|
+
{label}
|
|
62
|
+
</Text>
|
|
63
|
+
</Box>
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (showGradient) {
|
|
68
|
+
const colors = animateGradient ? getAnimatedGradient(offset) : [...THEME.gradient]
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
<Box gap={0}>
|
|
72
|
+
<Gradient colors={colors}>
|
|
73
|
+
<Text bold>
|
|
74
|
+
{prefix}
|
|
75
|
+
{label}
|
|
76
|
+
</Text>
|
|
77
|
+
</Gradient>
|
|
78
|
+
{focusHint}
|
|
79
|
+
</Box>
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return (
|
|
84
|
+
<Box gap={0}>
|
|
85
|
+
<Text color={isFocused ? (color ?? THEME.primary) : undefined} bold={isFocused}>
|
|
86
|
+
{prefix}
|
|
87
|
+
{label}
|
|
88
|
+
</Text>
|
|
89
|
+
{focusHint}
|
|
90
|
+
</Box>
|
|
91
|
+
)
|
|
92
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { Box, Text, useInput } from 'ink'
|
|
2
|
+
import TextInput from 'ink-text-input'
|
|
3
|
+
import { useCallback, useEffect, useState } from 'react'
|
|
4
|
+
import { useFocusMode } from '../context/focus-mode-context.ts'
|
|
5
|
+
import { useFocusOrder } from '../context/focus-order-context.ts'
|
|
6
|
+
import type { RequiredFieldKey } from '../context/form-state-context.ts'
|
|
7
|
+
import { useFormState } from '../context/form-state-context.ts'
|
|
8
|
+
import { THEME } from '../theme.ts'
|
|
9
|
+
|
|
10
|
+
export function EditableField({
|
|
11
|
+
field,
|
|
12
|
+
label,
|
|
13
|
+
placeholder,
|
|
14
|
+
hint,
|
|
15
|
+
defaultValue,
|
|
16
|
+
dimmed,
|
|
17
|
+
validate,
|
|
18
|
+
onConfirm,
|
|
19
|
+
}: {
|
|
20
|
+
field: RequiredFieldKey
|
|
21
|
+
label: string
|
|
22
|
+
placeholder?: string
|
|
23
|
+
hint?: string
|
|
24
|
+
defaultValue?: string
|
|
25
|
+
dimmed?: boolean
|
|
26
|
+
validate?: (value: string) => string | undefined
|
|
27
|
+
onConfirm?: () => void
|
|
28
|
+
}) {
|
|
29
|
+
const id = `field-${field}`
|
|
30
|
+
const { focusedId } = useFocusOrder()
|
|
31
|
+
const { mode, setMode } = useFocusMode()
|
|
32
|
+
const { form, setFieldValue, setFieldStatus } = useFormState()
|
|
33
|
+
const { value, status } = form.fields[field]
|
|
34
|
+
const isFocused = focusedId === id
|
|
35
|
+
const editing = status === 'editing'
|
|
36
|
+
const [error, setError] = useState('')
|
|
37
|
+
const [didAutofill, setDidAutofill] = useState(false)
|
|
38
|
+
const [previousValue, setPreviousValue] = useState<string | undefined>(undefined)
|
|
39
|
+
|
|
40
|
+
const startEditing = useCallback(() => {
|
|
41
|
+
setPreviousValue(value || undefined)
|
|
42
|
+
setFieldStatus(field, 'editing')
|
|
43
|
+
// Revising an existing value: Escape reverts without triggering exit modal
|
|
44
|
+
// Initial entry: Escape passes through to the exit hook
|
|
45
|
+
setMode(value ? 'field-revision' : 'field-initial-entry')
|
|
46
|
+
}, [field, value, setFieldStatus, setMode])
|
|
47
|
+
|
|
48
|
+
const stopEditing = useCallback(() => {
|
|
49
|
+
setPreviousValue(undefined)
|
|
50
|
+
setFieldStatus(field, value ? 'confirmed' : 'empty')
|
|
51
|
+
setMode('form-navigation')
|
|
52
|
+
}, [field, value, setFieldStatus, setMode])
|
|
53
|
+
|
|
54
|
+
const cancelEditing = useCallback(() => {
|
|
55
|
+
if (previousValue) {
|
|
56
|
+
setFieldValue(field, previousValue)
|
|
57
|
+
setPreviousValue(undefined)
|
|
58
|
+
setFieldStatus(field, 'confirmed')
|
|
59
|
+
setMode('form-navigation')
|
|
60
|
+
setError('')
|
|
61
|
+
}
|
|
62
|
+
}, [previousValue, field, setFieldValue, setFieldStatus, setMode])
|
|
63
|
+
|
|
64
|
+
// Handle Enter to edit, or 'c' to clear and edit
|
|
65
|
+
useInput(
|
|
66
|
+
(input, key) => {
|
|
67
|
+
if (!editing) {
|
|
68
|
+
if (key.return) {
|
|
69
|
+
startEditing()
|
|
70
|
+
}
|
|
71
|
+
if (input === 'c' && value) {
|
|
72
|
+
setFieldValue(field, '')
|
|
73
|
+
startEditing()
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
{ isActive: isFocused && !editing && mode === 'form-navigation' },
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
// Handle Enter to confirm / Escape to revert (if previously had value)
|
|
81
|
+
useInput(
|
|
82
|
+
(_input, key) => {
|
|
83
|
+
if (key.return) {
|
|
84
|
+
if (validate) {
|
|
85
|
+
const err = validate(value)
|
|
86
|
+
if (err) {
|
|
87
|
+
setError(err)
|
|
88
|
+
return
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (!value) {
|
|
92
|
+
setError(`${label} is required`)
|
|
93
|
+
return
|
|
94
|
+
}
|
|
95
|
+
setError('')
|
|
96
|
+
stopEditing()
|
|
97
|
+
onConfirm?.()
|
|
98
|
+
}
|
|
99
|
+
if (key.escape && previousValue) {
|
|
100
|
+
cancelEditing()
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
{ isActive: isFocused && editing },
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
// Auto-start editing when focused on an empty required field
|
|
107
|
+
useEffect(() => {
|
|
108
|
+
if (isFocused && !value && !editing && mode === 'form-navigation') {
|
|
109
|
+
startEditing()
|
|
110
|
+
}
|
|
111
|
+
}, [isFocused, value, editing, mode, startEditing])
|
|
112
|
+
|
|
113
|
+
// Autofill defaultValue when editing starts on an empty field
|
|
114
|
+
useEffect(() => {
|
|
115
|
+
if (editing && !value && defaultValue && !didAutofill) {
|
|
116
|
+
setFieldValue(field, defaultValue)
|
|
117
|
+
setDidAutofill(true)
|
|
118
|
+
}
|
|
119
|
+
}, [editing, value, defaultValue, didAutofill, field, setFieldValue])
|
|
120
|
+
|
|
121
|
+
const isEditing = editing && isFocused
|
|
122
|
+
|
|
123
|
+
return (
|
|
124
|
+
<Box flexDirection="column" gap={0}>
|
|
125
|
+
<Box gap={1}>
|
|
126
|
+
<Text color={isFocused ? THEME.primary : undefined} bold={isFocused} dimColor={dimmed && !isFocused}>
|
|
127
|
+
{label}:
|
|
128
|
+
</Text>
|
|
129
|
+
{isEditing && error ? (
|
|
130
|
+
<Text color={THEME.warning}>{error}</Text>
|
|
131
|
+
) : isEditing ? (
|
|
132
|
+
<>
|
|
133
|
+
{hint && <Text color={THEME.hint}>({hint})</Text>}
|
|
134
|
+
{previousValue && (
|
|
135
|
+
<Text color={THEME.hint}>
|
|
136
|
+
<Text color={THEME.keyword}>Escape</Text> to revert to{' '}
|
|
137
|
+
<Text color={THEME.keyword}>"{previousValue}"</Text>
|
|
138
|
+
</Text>
|
|
139
|
+
)}
|
|
140
|
+
</>
|
|
141
|
+
) : !isEditing && value ? (
|
|
142
|
+
<Text color={dimmed && !isFocused ? undefined : THEME.success} dimColor={dimmed && !isFocused}>
|
|
143
|
+
✓
|
|
144
|
+
</Text>
|
|
145
|
+
) : !isEditing ? (
|
|
146
|
+
<Text dimColor>(not set)</Text>
|
|
147
|
+
) : null}
|
|
148
|
+
</Box>
|
|
149
|
+
<Box marginLeft={2}>
|
|
150
|
+
{isEditing ? (
|
|
151
|
+
<Box gap={1}>
|
|
152
|
+
<Text color={THEME.tertiary}>{'> '}</Text>
|
|
153
|
+
<TextInput value={value} onChange={(v) => setFieldValue(field, v)} placeholder={placeholder} focus />
|
|
154
|
+
<Text color={THEME.hint}>
|
|
155
|
+
· <Text color={THEME.keyword}>Enter</Text> to save
|
|
156
|
+
</Text>
|
|
157
|
+
</Box>
|
|
158
|
+
) : (
|
|
159
|
+
<Box gap={1}>
|
|
160
|
+
<Text dimColor={dimmed && !isFocused}>{value || ' '}</Text>
|
|
161
|
+
{isFocused && value && (
|
|
162
|
+
<Text color={THEME.hint}>
|
|
163
|
+
· <Text color={THEME.keyword}>Enter</Text> to edit · <Text color={THEME.keyword}>c</Text> to clear and
|
|
164
|
+
edit
|
|
165
|
+
</Text>
|
|
166
|
+
)}
|
|
167
|
+
</Box>
|
|
168
|
+
)}
|
|
169
|
+
</Box>
|
|
170
|
+
</Box>
|
|
171
|
+
)
|
|
172
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Box, Text } from 'ink'
|
|
2
|
+
import { useFocusMode } from '../context/focus-mode-context.ts'
|
|
3
|
+
import { THEME } from '../theme.ts'
|
|
4
|
+
|
|
5
|
+
export function ExitFooter() {
|
|
6
|
+
const { mode, exitSecondsLeft } = useFocusMode()
|
|
7
|
+
const visible = mode === 'exit-modal' && exitSecondsLeft > 0
|
|
8
|
+
|
|
9
|
+
return (
|
|
10
|
+
<Box marginTop={1}>
|
|
11
|
+
{visible ? (
|
|
12
|
+
<Text color={THEME.warning} bold>
|
|
13
|
+
Press Escape again to exit ({exitSecondsLeft}s) — any other key to cancel
|
|
14
|
+
</Text>
|
|
15
|
+
) : (
|
|
16
|
+
<Text> </Text>
|
|
17
|
+
)}
|
|
18
|
+
</Box>
|
|
19
|
+
)
|
|
20
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Box, Text } from 'ink'
|
|
2
|
+
import Spinner from 'ink-spinner'
|
|
3
|
+
import type { ReactNode } from 'react'
|
|
4
|
+
import { THEME } from '../theme.ts'
|
|
5
|
+
|
|
6
|
+
export type StageStatus = 'pending' | 'running' | 'done' | 'failed'
|
|
7
|
+
|
|
8
|
+
export interface Stage {
|
|
9
|
+
label: string
|
|
10
|
+
status: StageStatus
|
|
11
|
+
detail?: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const ICONS: Record<StageStatus, ReactNode> = {
|
|
15
|
+
pending: <Text color={THEME.hint}>○</Text>,
|
|
16
|
+
running: (
|
|
17
|
+
<Text color={THEME.secondary}>
|
|
18
|
+
<Spinner type="dots" />
|
|
19
|
+
</Text>
|
|
20
|
+
),
|
|
21
|
+
done: <Text color={THEME.success}>●</Text>,
|
|
22
|
+
failed: <Text color={THEME.warning}>✕</Text>,
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function StageRow({ stage }: { stage: Stage }) {
|
|
26
|
+
return (
|
|
27
|
+
<Box gap={1}>
|
|
28
|
+
{ICONS[stage.status]}
|
|
29
|
+
<Text dimColor={stage.status === 'pending'}>{stage.label}</Text>
|
|
30
|
+
{stage.detail && <Text color={THEME.hint}> — {stage.detail}</Text>}
|
|
31
|
+
</Box>
|
|
32
|
+
)
|
|
33
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Box, Text, useInput } from 'ink'
|
|
2
|
+
import { useState } from 'react'
|
|
3
|
+
|
|
4
|
+
export function VersionSelector({
|
|
5
|
+
value,
|
|
6
|
+
onChange,
|
|
7
|
+
onSubmit,
|
|
8
|
+
active,
|
|
9
|
+
}: {
|
|
10
|
+
value: string
|
|
11
|
+
onChange: (v: string) => void
|
|
12
|
+
onSubmit: () => void
|
|
13
|
+
active: boolean
|
|
14
|
+
}) {
|
|
15
|
+
const parts = value.split('.').map(Number)
|
|
16
|
+
const [cursor, setCursor] = useState(0) // 0=major, 1=minor, 2=patch
|
|
17
|
+
|
|
18
|
+
useInput(
|
|
19
|
+
(input, key) => {
|
|
20
|
+
if (!active) return
|
|
21
|
+
|
|
22
|
+
if (key.return) {
|
|
23
|
+
onSubmit()
|
|
24
|
+
return
|
|
25
|
+
}
|
|
26
|
+
// Tab and left/right all move between segments
|
|
27
|
+
if (key.leftArrow || (key.shift && key.tab)) {
|
|
28
|
+
setCursor((c) => Math.max(0, c - 1))
|
|
29
|
+
return
|
|
30
|
+
}
|
|
31
|
+
if (key.rightArrow || key.tab) {
|
|
32
|
+
setCursor((c) => Math.min(2, c + 1))
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
if (key.upArrow) {
|
|
36
|
+
const next = [...parts]
|
|
37
|
+
next[cursor] = (next[cursor] ?? 0) + 1
|
|
38
|
+
onChange(next.join('.'))
|
|
39
|
+
return
|
|
40
|
+
}
|
|
41
|
+
if (key.downArrow) {
|
|
42
|
+
const next = [...parts]
|
|
43
|
+
next[cursor] = Math.max(0, (next[cursor] ?? 0) - 1)
|
|
44
|
+
onChange(next.join('.'))
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
// Allow typing a full version directly
|
|
48
|
+
if (key.backspace) {
|
|
49
|
+
onChange(value.slice(0, -1))
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
if (/[0-9.]/.test(input)) {
|
|
53
|
+
onChange(value + input)
|
|
54
|
+
return
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
{ isActive: active },
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
const segments = value.split('.')
|
|
61
|
+
return (
|
|
62
|
+
<Box gap={0}>
|
|
63
|
+
{segments.map((seg, i) => (
|
|
64
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: version segments are always positional (major.minor.patch)
|
|
65
|
+
<Box key={i}>
|
|
66
|
+
{i > 0 && <Text dimColor>.</Text>}
|
|
67
|
+
<Text
|
|
68
|
+
bold={active && cursor === i}
|
|
69
|
+
color={active && cursor === i ? 'cyan' : undefined}
|
|
70
|
+
inverse={active && cursor === i}
|
|
71
|
+
>
|
|
72
|
+
{seg}
|
|
73
|
+
</Text>
|
|
74
|
+
</Box>
|
|
75
|
+
))}
|
|
76
|
+
{active && <Text dimColor>← → to select, ↑ ↓ to change, Enter to confirm</Text>}
|
|
77
|
+
</Box>
|
|
78
|
+
)
|
|
79
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Dispatch, ReactNode, SetStateAction } from 'react'
|
|
2
|
+
import { createContext, createElement, useContext, useMemo, useState } from 'react'
|
|
3
|
+
|
|
4
|
+
export type FocusMode =
|
|
5
|
+
| 'form-navigation'
|
|
6
|
+
| 'field-initial-entry'
|
|
7
|
+
| 'field-revision'
|
|
8
|
+
| 'exit-modal'
|
|
9
|
+
| 'form-confirmation'
|
|
10
|
+
|
|
11
|
+
interface FocusModeState {
|
|
12
|
+
mode: FocusMode
|
|
13
|
+
setMode: (mode: FocusMode) => void
|
|
14
|
+
exitSecondsLeft: number
|
|
15
|
+
setExitSecondsLeft: Dispatch<SetStateAction<number>>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const FocusModeContext = createContext<FocusModeState>({
|
|
19
|
+
mode: 'form-navigation',
|
|
20
|
+
setMode: () => {},
|
|
21
|
+
exitSecondsLeft: 0,
|
|
22
|
+
setExitSecondsLeft: () => {},
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
export function FocusModeProvider({ children }: { children: ReactNode }) {
|
|
26
|
+
const [mode, setMode] = useState<FocusMode>('form-navigation')
|
|
27
|
+
const [exitSecondsLeft, setExitSecondsLeft] = useState(0)
|
|
28
|
+
|
|
29
|
+
const value = useMemo(() => ({ mode, setMode, exitSecondsLeft, setExitSecondsLeft }), [mode, exitSecondsLeft])
|
|
30
|
+
|
|
31
|
+
return createElement(FocusModeContext.Provider, { value }, children)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function useFocusMode() {
|
|
35
|
+
return useContext(FocusModeContext)
|
|
36
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { Dispatch, ReactNode, SetStateAction } from 'react'
|
|
2
|
+
import { createContext, createElement, useCallback, useContext, useMemo, useState } from 'react'
|
|
3
|
+
|
|
4
|
+
interface FocusOrderState {
|
|
5
|
+
focusedId: string | null
|
|
6
|
+
focusIds: string[]
|
|
7
|
+
setFocusIds: (ids: string[]) => void
|
|
8
|
+
setFocusedId: Dispatch<SetStateAction<string | null>>
|
|
9
|
+
focusNext: () => void
|
|
10
|
+
focusPrevious: () => void
|
|
11
|
+
focus: (id: string) => void
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const FocusOrderContext = createContext<FocusOrderState>({
|
|
15
|
+
focusedId: null,
|
|
16
|
+
focusIds: [],
|
|
17
|
+
setFocusIds: () => {},
|
|
18
|
+
setFocusedId: () => {},
|
|
19
|
+
focusNext: () => {},
|
|
20
|
+
focusPrevious: () => {},
|
|
21
|
+
focus: () => {},
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
export function FocusOrderProvider({ children }: { children: ReactNode }) {
|
|
25
|
+
const [focusIds, setFocusIds] = useState<string[]>([])
|
|
26
|
+
const [focusedId, setFocusedId] = useState<string | null>(null)
|
|
27
|
+
|
|
28
|
+
const focusNext = useCallback(() => {
|
|
29
|
+
setFocusedId((current) => {
|
|
30
|
+
if (!current || focusIds.length === 0) return focusIds[0] ?? null
|
|
31
|
+
const idx = focusIds.indexOf(current)
|
|
32
|
+
if (idx === -1) return focusIds[0] ?? null
|
|
33
|
+
if (idx === focusIds.length - 1) return current
|
|
34
|
+
return focusIds[idx + 1] ?? null
|
|
35
|
+
})
|
|
36
|
+
}, [focusIds])
|
|
37
|
+
|
|
38
|
+
const focusPrevious = useCallback(() => {
|
|
39
|
+
setFocusedId((current) => {
|
|
40
|
+
if (!current || focusIds.length === 0) return focusIds[0] ?? null
|
|
41
|
+
const idx = focusIds.indexOf(current)
|
|
42
|
+
if (idx === -1) return focusIds[0] ?? null
|
|
43
|
+
if (idx === 0) return current
|
|
44
|
+
return focusIds[idx - 1] ?? null
|
|
45
|
+
})
|
|
46
|
+
}, [focusIds])
|
|
47
|
+
|
|
48
|
+
const focus = useCallback((id: string) => {
|
|
49
|
+
setFocusedId(id)
|
|
50
|
+
}, [])
|
|
51
|
+
|
|
52
|
+
const value = useMemo(
|
|
53
|
+
() => ({ focusedId, focusIds, setFocusIds, setFocusedId, focusNext, focusPrevious, focus }),
|
|
54
|
+
[focusedId, focusIds, focusNext, focusPrevious, focus],
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
return createElement(FocusOrderContext.Provider, { value }, children)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function useFocusOrder() {
|
|
61
|
+
return useContext(FocusOrderContext)
|
|
62
|
+
}
|