@bakapiano/ccsm 0.14.0 → 0.15.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.
- package/CLAUDE.md +474 -475
- package/README.md +189 -190
- package/bin/ccsm.js +194 -194
- package/lib/cliActivity.js +118 -0
- package/lib/codexSeed.js +147 -0
- package/lib/config.js +211 -188
- package/lib/folders.js +105 -105
- package/lib/localCliSessions.js +489 -489
- package/lib/persistedSessions.js +144 -142
- package/lib/webTerminal.js +224 -224
- package/lib/workspace.js +230 -230
- package/package.json +57 -57
- package/public/css/base.css +99 -99
- package/public/css/cards.css +183 -183
- package/public/css/feedback.css +303 -303
- package/public/css/forms.css +405 -405
- package/public/css/layout.css +160 -160
- package/public/css/modal.css +190 -190
- package/public/css/responsive.css +10 -10
- package/public/css/sidebar.css +613 -608
- package/public/css/terminals.css +294 -294
- package/public/css/tokens.css +81 -81
- package/public/css/wco.css +98 -98
- package/public/css/widgets.css +1628 -1628
- package/public/index.html +111 -105
- package/public/js/api.js +296 -280
- package/public/js/components/AdoptModal.js +343 -343
- package/public/js/components/App.js +35 -35
- package/public/js/components/DirectoryPicker.js +203 -203
- package/public/js/components/EntityFormModal.js +141 -141
- package/public/js/components/Modal.js +51 -51
- package/public/js/components/OfflineBanner.js +93 -93
- package/public/js/components/PageTitleBar.js +13 -13
- package/public/js/components/Picker.js +179 -179
- package/public/js/components/Popover.js +55 -55
- package/public/js/components/Sidebar.js +299 -299
- package/public/js/components/TerminalView.js +314 -314
- package/public/js/components/useDragSort.js +67 -67
- package/public/js/dialog.js +67 -67
- package/public/js/icons.js +177 -177
- package/public/js/main.js +132 -132
- package/public/js/pages/AboutPage.js +165 -165
- package/public/js/pages/ConfigurePage.js +505 -475
- package/public/js/pages/LaunchPage.js +369 -369
- package/public/js/pages/SessionsPage.js +101 -97
- package/public/js/state.js +231 -231
- package/scripts/dev.js +44 -11
- package/scripts/install.js +158 -158
- package/scripts/restart-helper.js +91 -0
- package/server.js +1278 -1254
- package/lib/cliSessionWatcher.js +0 -275
- package/public/manifest.webmanifest +0 -15
|
@@ -1,179 +1,179 @@
|
|
|
1
|
-
// Unified picker used inside a Popover. Renders:
|
|
2
|
-
// - optional search box (filters items by `label` + `meta`)
|
|
3
|
-
// - scrollable item list (single or multi select)
|
|
4
|
-
// - footer "+ New <thing>" that expands an inline form
|
|
5
|
-
//
|
|
6
|
-
// items: [{ id, label, meta?, disabled? }]
|
|
7
|
-
// onSelect(id) — single select. Closes popover.
|
|
8
|
-
// onToggle(id, on) — multi select. Doesn't close.
|
|
9
|
-
// selectedIds: Set<string> — for multi select highlight
|
|
10
|
-
// selectedId: string — for single select highlight
|
|
11
|
-
// onCreate(values) — async; returns the newly-created id (selected immediately
|
|
12
|
-
// in single mode; added to selection in multi mode)
|
|
13
|
-
// createFields: [{ key, label, type?, placeholder?, required? }]
|
|
14
|
-
|
|
15
|
-
import { html } from '../html.js';
|
|
16
|
-
import { useState, useRef, useEffect } from 'preact/hooks';
|
|
17
|
-
import { IconSearch, IconPlus, IconClose } from '../icons.js';
|
|
18
|
-
|
|
19
|
-
export function PickerPanel({
|
|
20
|
-
title,
|
|
21
|
-
items,
|
|
22
|
-
selectedId,
|
|
23
|
-
selectedIds,
|
|
24
|
-
multi = false,
|
|
25
|
-
showSearch = true,
|
|
26
|
-
emptyHint = 'Nothing yet.',
|
|
27
|
-
createLabel = '+ New',
|
|
28
|
-
createFields = [],
|
|
29
|
-
onSelect,
|
|
30
|
-
onToggle,
|
|
31
|
-
onCreate,
|
|
32
|
-
onClose,
|
|
33
|
-
dnd,
|
|
34
|
-
}) {
|
|
35
|
-
const [q, setQ] = useState('');
|
|
36
|
-
const [creating, setCreating] = useState(false);
|
|
37
|
-
const [draft, setDraft] = useState(() => initialDraft(createFields));
|
|
38
|
-
const [saving, setSaving] = useState(false);
|
|
39
|
-
const searchRef = useRef(null);
|
|
40
|
-
|
|
41
|
-
useEffect(() => {
|
|
42
|
-
if (showSearch) searchRef.current?.focus();
|
|
43
|
-
}, [showSearch]);
|
|
44
|
-
|
|
45
|
-
const filtered = items.filter((it) => {
|
|
46
|
-
if (!q.trim()) return true;
|
|
47
|
-
const hay = (it.label + ' ' + (it.meta || '')).toLowerCase();
|
|
48
|
-
return hay.includes(q.trim().toLowerCase());
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
const submitCreate = async (ev) => {
|
|
52
|
-
ev?.preventDefault?.();
|
|
53
|
-
for (const f of createFields) {
|
|
54
|
-
if (f.required && !String(draft[f.key] || '').trim()) return;
|
|
55
|
-
}
|
|
56
|
-
setSaving(true);
|
|
57
|
-
try {
|
|
58
|
-
const id = await onCreate?.(draft);
|
|
59
|
-
setDraft(initialDraft(createFields));
|
|
60
|
-
setCreating(false);
|
|
61
|
-
if (id != null) {
|
|
62
|
-
if (multi) onToggle?.(id, true);
|
|
63
|
-
else { onSelect?.(id); onClose?.(); }
|
|
64
|
-
}
|
|
65
|
-
} catch (e) {
|
|
66
|
-
// Caller is expected to toast; we just stay open with the form
|
|
67
|
-
console.warn(e);
|
|
68
|
-
} finally {
|
|
69
|
-
setSaving(false);
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
return html`
|
|
74
|
-
<div class="picker">
|
|
75
|
-
${title ? html`<div class="picker-title">${title}</div>` : null}
|
|
76
|
-
|
|
77
|
-
${showSearch ? html`
|
|
78
|
-
<div class="picker-search">
|
|
79
|
-
<span class="picker-search-icon"><${IconSearch} /></span>
|
|
80
|
-
<input ref=${searchRef} class="picker-search-input"
|
|
81
|
-
placeholder="Search…" value=${q}
|
|
82
|
-
onInput=${(e) => setQ(e.target.value)} />
|
|
83
|
-
${q ? html`<button class="picker-search-clear" onClick=${() => setQ('')}>
|
|
84
|
-
<${IconClose} />
|
|
85
|
-
</button>` : null}
|
|
86
|
-
</div>` : null}
|
|
87
|
-
|
|
88
|
-
<div class="picker-list">
|
|
89
|
-
${filtered.length === 0 ? html`
|
|
90
|
-
<div class="picker-empty">${q ? 'No matches.' : emptyHint}</div>
|
|
91
|
-
` : filtered.map((it) => {
|
|
92
|
-
const isSel = multi ? selectedIds?.has(it.id) : selectedId === it.id;
|
|
93
|
-
const enableDnd = dnd && !it.disabled && !it.undraggable && !q.trim();
|
|
94
|
-
const rowProps = enableDnd ? dnd.rowProps(it.id) : {};
|
|
95
|
-
const handleProps = enableDnd ? dnd.handleProps(it.id) : {};
|
|
96
|
-
return html`
|
|
97
|
-
<div key=${it.id} class=${`picker-item-wrap${enableDnd ? ' is-draggable' : ''}`} ...${rowProps} ...${handleProps}>
|
|
98
|
-
${enableDnd ? html`<span class="picker-item-grip" aria-hidden="true">⋮⋮</span>` : null}
|
|
99
|
-
<button type="button"
|
|
100
|
-
class=${`picker-item${isSel ? ' is-selected' : ''}`}
|
|
101
|
-
disabled=${it.disabled}
|
|
102
|
-
onClick=${() => {
|
|
103
|
-
if (multi) onToggle?.(it.id, !isSel);
|
|
104
|
-
else { onSelect?.(it.id); onClose?.(); }
|
|
105
|
-
}}>
|
|
106
|
-
${it.icon ? html`<span class="picker-item-icon">${it.icon}</span>` : null}
|
|
107
|
-
<span class="picker-item-label">${it.label}</span>
|
|
108
|
-
${it.meta ? html`<span class="picker-item-meta">${it.meta}</span>` : null}
|
|
109
|
-
${isSel ? html`<span class="picker-item-check">✓</span>` : null}
|
|
110
|
-
</button>
|
|
111
|
-
</div>`;
|
|
112
|
-
})}
|
|
113
|
-
</div>
|
|
114
|
-
|
|
115
|
-
${onCreate ? html`
|
|
116
|
-
<div class="picker-create">
|
|
117
|
-
${!creating ? html`
|
|
118
|
-
<button class="picker-create-toggle" type="button"
|
|
119
|
-
onClick=${() => setCreating(true)}>
|
|
120
|
-
<${IconPlus} />
|
|
121
|
-
<span>${createLabel}</span>
|
|
122
|
-
</button>
|
|
123
|
-
` : html`
|
|
124
|
-
<form class="picker-create-form" onSubmit=${submitCreate}>
|
|
125
|
-
${createFields.map((f) => html`
|
|
126
|
-
<label class="picker-field" key=${f.key}>
|
|
127
|
-
<span class="picker-field-label">${f.label}</span>
|
|
128
|
-
${f.type === 'select' ? html`
|
|
129
|
-
<select class="input" value=${draft[f.key] || ''}
|
|
130
|
-
onChange=${(e) => {
|
|
131
|
-
const next = { ...draft, [f.key]: e.target.value };
|
|
132
|
-
const side = f.onChange?.(e.target.value, next);
|
|
133
|
-
setDraft(side ? { ...next, ...side } : next);
|
|
134
|
-
}}>
|
|
135
|
-
${(f.options || []).map((opt) => html`
|
|
136
|
-
<option value=${opt.value}>${opt.label}</option>`)}
|
|
137
|
-
</select>
|
|
138
|
-
` : f.type === 'iconRadio' ? html`
|
|
139
|
-
<div class="icon-radio">
|
|
140
|
-
${(f.options || []).map((opt) => html`
|
|
141
|
-
<button type="button" key=${opt.value}
|
|
142
|
-
class=${`icon-radio-opt${draft[f.key] === opt.value ? ' is-active' : ''}`}
|
|
143
|
-
onClick=${() => {
|
|
144
|
-
const next = { ...draft, [f.key]: opt.value };
|
|
145
|
-
const side = f.onChange?.(opt.value, next);
|
|
146
|
-
setDraft(side ? { ...next, ...side } : next);
|
|
147
|
-
}}>
|
|
148
|
-
${opt.icon ? html`<span class="icon-radio-icon">${opt.icon}</span>` : null}
|
|
149
|
-
<span>${opt.label}</span>
|
|
150
|
-
</button>`)}
|
|
151
|
-
</div>
|
|
152
|
-
` : html`
|
|
153
|
-
<input type=${f.type || 'text'}
|
|
154
|
-
class=${`input${f.mono ? ' mono' : ''}`}
|
|
155
|
-
placeholder=${f.placeholder || ''}
|
|
156
|
-
value=${draft[f.key] || ''}
|
|
157
|
-
onInput=${(e) => setDraft({ ...draft, [f.key]: e.target.value })}
|
|
158
|
-
autoFocus=${f.autoFocus} />`}
|
|
159
|
-
${f.hint ? html`<span class="picker-field-hint">${f.hint}</span>` : null}
|
|
160
|
-
</label>`)}
|
|
161
|
-
<div class="picker-create-actions">
|
|
162
|
-
<button type="button" class="action small subtle"
|
|
163
|
-
onClick=${() => { setCreating(false); setDraft(initialDraft(createFields)); }}>
|
|
164
|
-
Cancel
|
|
165
|
-
</button>
|
|
166
|
-
<button type="submit" class="action small primary" disabled=${saving}>
|
|
167
|
-
${saving ? 'Creating…' : 'Create'}
|
|
168
|
-
</button>
|
|
169
|
-
</div>
|
|
170
|
-
</form>`}
|
|
171
|
-
</div>` : null}
|
|
172
|
-
</div>`;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
function initialDraft(fields) {
|
|
176
|
-
const out = {};
|
|
177
|
-
for (const f of fields) out[f.key] = f.default ?? '';
|
|
178
|
-
return out;
|
|
179
|
-
}
|
|
1
|
+
// Unified picker used inside a Popover. Renders:
|
|
2
|
+
// - optional search box (filters items by `label` + `meta`)
|
|
3
|
+
// - scrollable item list (single or multi select)
|
|
4
|
+
// - footer "+ New <thing>" that expands an inline form
|
|
5
|
+
//
|
|
6
|
+
// items: [{ id, label, meta?, disabled? }]
|
|
7
|
+
// onSelect(id) — single select. Closes popover.
|
|
8
|
+
// onToggle(id, on) — multi select. Doesn't close.
|
|
9
|
+
// selectedIds: Set<string> — for multi select highlight
|
|
10
|
+
// selectedId: string — for single select highlight
|
|
11
|
+
// onCreate(values) — async; returns the newly-created id (selected immediately
|
|
12
|
+
// in single mode; added to selection in multi mode)
|
|
13
|
+
// createFields: [{ key, label, type?, placeholder?, required? }]
|
|
14
|
+
|
|
15
|
+
import { html } from '../html.js';
|
|
16
|
+
import { useState, useRef, useEffect } from 'preact/hooks';
|
|
17
|
+
import { IconSearch, IconPlus, IconClose } from '../icons.js';
|
|
18
|
+
|
|
19
|
+
export function PickerPanel({
|
|
20
|
+
title,
|
|
21
|
+
items,
|
|
22
|
+
selectedId,
|
|
23
|
+
selectedIds,
|
|
24
|
+
multi = false,
|
|
25
|
+
showSearch = true,
|
|
26
|
+
emptyHint = 'Nothing yet.',
|
|
27
|
+
createLabel = '+ New',
|
|
28
|
+
createFields = [],
|
|
29
|
+
onSelect,
|
|
30
|
+
onToggle,
|
|
31
|
+
onCreate,
|
|
32
|
+
onClose,
|
|
33
|
+
dnd,
|
|
34
|
+
}) {
|
|
35
|
+
const [q, setQ] = useState('');
|
|
36
|
+
const [creating, setCreating] = useState(false);
|
|
37
|
+
const [draft, setDraft] = useState(() => initialDraft(createFields));
|
|
38
|
+
const [saving, setSaving] = useState(false);
|
|
39
|
+
const searchRef = useRef(null);
|
|
40
|
+
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
if (showSearch) searchRef.current?.focus();
|
|
43
|
+
}, [showSearch]);
|
|
44
|
+
|
|
45
|
+
const filtered = items.filter((it) => {
|
|
46
|
+
if (!q.trim()) return true;
|
|
47
|
+
const hay = (it.label + ' ' + (it.meta || '')).toLowerCase();
|
|
48
|
+
return hay.includes(q.trim().toLowerCase());
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const submitCreate = async (ev) => {
|
|
52
|
+
ev?.preventDefault?.();
|
|
53
|
+
for (const f of createFields) {
|
|
54
|
+
if (f.required && !String(draft[f.key] || '').trim()) return;
|
|
55
|
+
}
|
|
56
|
+
setSaving(true);
|
|
57
|
+
try {
|
|
58
|
+
const id = await onCreate?.(draft);
|
|
59
|
+
setDraft(initialDraft(createFields));
|
|
60
|
+
setCreating(false);
|
|
61
|
+
if (id != null) {
|
|
62
|
+
if (multi) onToggle?.(id, true);
|
|
63
|
+
else { onSelect?.(id); onClose?.(); }
|
|
64
|
+
}
|
|
65
|
+
} catch (e) {
|
|
66
|
+
// Caller is expected to toast; we just stay open with the form
|
|
67
|
+
console.warn(e);
|
|
68
|
+
} finally {
|
|
69
|
+
setSaving(false);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
return html`
|
|
74
|
+
<div class="picker">
|
|
75
|
+
${title ? html`<div class="picker-title">${title}</div>` : null}
|
|
76
|
+
|
|
77
|
+
${showSearch ? html`
|
|
78
|
+
<div class="picker-search">
|
|
79
|
+
<span class="picker-search-icon"><${IconSearch} /></span>
|
|
80
|
+
<input ref=${searchRef} class="picker-search-input"
|
|
81
|
+
placeholder="Search…" value=${q}
|
|
82
|
+
onInput=${(e) => setQ(e.target.value)} />
|
|
83
|
+
${q ? html`<button class="picker-search-clear" onClick=${() => setQ('')}>
|
|
84
|
+
<${IconClose} />
|
|
85
|
+
</button>` : null}
|
|
86
|
+
</div>` : null}
|
|
87
|
+
|
|
88
|
+
<div class="picker-list">
|
|
89
|
+
${filtered.length === 0 ? html`
|
|
90
|
+
<div class="picker-empty">${q ? 'No matches.' : emptyHint}</div>
|
|
91
|
+
` : filtered.map((it) => {
|
|
92
|
+
const isSel = multi ? selectedIds?.has(it.id) : selectedId === it.id;
|
|
93
|
+
const enableDnd = dnd && !it.disabled && !it.undraggable && !q.trim();
|
|
94
|
+
const rowProps = enableDnd ? dnd.rowProps(it.id) : {};
|
|
95
|
+
const handleProps = enableDnd ? dnd.handleProps(it.id) : {};
|
|
96
|
+
return html`
|
|
97
|
+
<div key=${it.id} class=${`picker-item-wrap${enableDnd ? ' is-draggable' : ''}`} ...${rowProps} ...${handleProps}>
|
|
98
|
+
${enableDnd ? html`<span class="picker-item-grip" aria-hidden="true">⋮⋮</span>` : null}
|
|
99
|
+
<button type="button"
|
|
100
|
+
class=${`picker-item${isSel ? ' is-selected' : ''}`}
|
|
101
|
+
disabled=${it.disabled}
|
|
102
|
+
onClick=${() => {
|
|
103
|
+
if (multi) onToggle?.(it.id, !isSel);
|
|
104
|
+
else { onSelect?.(it.id); onClose?.(); }
|
|
105
|
+
}}>
|
|
106
|
+
${it.icon ? html`<span class="picker-item-icon">${it.icon}</span>` : null}
|
|
107
|
+
<span class="picker-item-label">${it.label}</span>
|
|
108
|
+
${it.meta ? html`<span class="picker-item-meta">${it.meta}</span>` : null}
|
|
109
|
+
${isSel ? html`<span class="picker-item-check">✓</span>` : null}
|
|
110
|
+
</button>
|
|
111
|
+
</div>`;
|
|
112
|
+
})}
|
|
113
|
+
</div>
|
|
114
|
+
|
|
115
|
+
${onCreate ? html`
|
|
116
|
+
<div class="picker-create">
|
|
117
|
+
${!creating ? html`
|
|
118
|
+
<button class="picker-create-toggle" type="button"
|
|
119
|
+
onClick=${() => setCreating(true)}>
|
|
120
|
+
<${IconPlus} />
|
|
121
|
+
<span>${createLabel}</span>
|
|
122
|
+
</button>
|
|
123
|
+
` : html`
|
|
124
|
+
<form class="picker-create-form" onSubmit=${submitCreate}>
|
|
125
|
+
${createFields.map((f) => html`
|
|
126
|
+
<label class="picker-field" key=${f.key}>
|
|
127
|
+
<span class="picker-field-label">${f.label}</span>
|
|
128
|
+
${f.type === 'select' ? html`
|
|
129
|
+
<select class="input" value=${draft[f.key] || ''}
|
|
130
|
+
onChange=${(e) => {
|
|
131
|
+
const next = { ...draft, [f.key]: e.target.value };
|
|
132
|
+
const side = f.onChange?.(e.target.value, next);
|
|
133
|
+
setDraft(side ? { ...next, ...side } : next);
|
|
134
|
+
}}>
|
|
135
|
+
${(f.options || []).map((opt) => html`
|
|
136
|
+
<option value=${opt.value}>${opt.label}</option>`)}
|
|
137
|
+
</select>
|
|
138
|
+
` : f.type === 'iconRadio' ? html`
|
|
139
|
+
<div class="icon-radio">
|
|
140
|
+
${(f.options || []).map((opt) => html`
|
|
141
|
+
<button type="button" key=${opt.value}
|
|
142
|
+
class=${`icon-radio-opt${draft[f.key] === opt.value ? ' is-active' : ''}`}
|
|
143
|
+
onClick=${() => {
|
|
144
|
+
const next = { ...draft, [f.key]: opt.value };
|
|
145
|
+
const side = f.onChange?.(opt.value, next);
|
|
146
|
+
setDraft(side ? { ...next, ...side } : next);
|
|
147
|
+
}}>
|
|
148
|
+
${opt.icon ? html`<span class="icon-radio-icon">${opt.icon}</span>` : null}
|
|
149
|
+
<span>${opt.label}</span>
|
|
150
|
+
</button>`)}
|
|
151
|
+
</div>
|
|
152
|
+
` : html`
|
|
153
|
+
<input type=${f.type || 'text'}
|
|
154
|
+
class=${`input${f.mono ? ' mono' : ''}`}
|
|
155
|
+
placeholder=${f.placeholder || ''}
|
|
156
|
+
value=${draft[f.key] || ''}
|
|
157
|
+
onInput=${(e) => setDraft({ ...draft, [f.key]: e.target.value })}
|
|
158
|
+
autoFocus=${f.autoFocus} />`}
|
|
159
|
+
${f.hint ? html`<span class="picker-field-hint">${f.hint}</span>` : null}
|
|
160
|
+
</label>`)}
|
|
161
|
+
<div class="picker-create-actions">
|
|
162
|
+
<button type="button" class="action small subtle"
|
|
163
|
+
onClick=${() => { setCreating(false); setDraft(initialDraft(createFields)); }}>
|
|
164
|
+
Cancel
|
|
165
|
+
</button>
|
|
166
|
+
<button type="submit" class="action small primary" disabled=${saving}>
|
|
167
|
+
${saving ? 'Creating…' : 'Create'}
|
|
168
|
+
</button>
|
|
169
|
+
</div>
|
|
170
|
+
</form>`}
|
|
171
|
+
</div>` : null}
|
|
172
|
+
</div>`;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function initialDraft(fields) {
|
|
176
|
+
const out = {};
|
|
177
|
+
for (const f of fields) out[f.key] = f.default ?? '';
|
|
178
|
+
return out;
|
|
179
|
+
}
|
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
// Tiny popover primitive — positions a floating panel relative to an
|
|
2
|
-
// anchor element, closes on outside click + Escape. Used by the unified
|
|
3
|
-
// pickers (CLI / Folder / Repo) so they all share interaction behavior.
|
|
4
|
-
//
|
|
5
|
-
// Usage:
|
|
6
|
-
// const [open, setOpen] = useState(false);
|
|
7
|
-
// const anchor = useRef(null);
|
|
8
|
-
// <button ref=${anchor} onClick=${() => setOpen(true)}>Trigger</button>
|
|
9
|
-
// ${open ? html`<${Popover} anchor=${anchor} onClose=${() => setOpen(false)}>
|
|
10
|
-
// ...panel contents...
|
|
11
|
-
// </${Popover}>` : null}
|
|
12
|
-
|
|
13
|
-
import { html } from '../html.js';
|
|
14
|
-
import { useEffect, useLayoutEffect, useRef, useState } from 'preact/hooks';
|
|
15
|
-
import { createPortal } from 'preact/compat';
|
|
16
|
-
|
|
17
|
-
export function Popover({ anchor, onClose, align = 'left', width, children }) {
|
|
18
|
-
const panelRef = useRef(null);
|
|
19
|
-
const [pos, setPos] = useState({ top: 0, left: 0, width: width || 320 });
|
|
20
|
-
|
|
21
|
-
useLayoutEffect(() => {
|
|
22
|
-
const a = anchor && anchor.current;
|
|
23
|
-
if (!a) return;
|
|
24
|
-
const rect = a.getBoundingClientRect();
|
|
25
|
-
const w = width || Math.max(rect.width, 320);
|
|
26
|
-
let left = align === 'right' ? rect.right - w : rect.left;
|
|
27
|
-
// Clamp to viewport with 8px margin.
|
|
28
|
-
left = Math.max(8, Math.min(window.innerWidth - w - 8, left));
|
|
29
|
-
const top = rect.bottom + 6;
|
|
30
|
-
setPos({ top, left, width: w });
|
|
31
|
-
}, [anchor, align, width]);
|
|
32
|
-
|
|
33
|
-
useEffect(() => {
|
|
34
|
-
const onDown = (ev) => {
|
|
35
|
-
if (panelRef.current?.contains(ev.target)) return;
|
|
36
|
-
if (anchor.current?.contains(ev.target)) return;
|
|
37
|
-
onClose?.();
|
|
38
|
-
};
|
|
39
|
-
const onKey = (ev) => { if (ev.key === 'Escape') onClose?.(); };
|
|
40
|
-
document.addEventListener('mousedown', onDown, true);
|
|
41
|
-
document.addEventListener('keydown', onKey, true);
|
|
42
|
-
return () => {
|
|
43
|
-
document.removeEventListener('mousedown', onDown, true);
|
|
44
|
-
document.removeEventListener('keydown', onKey, true);
|
|
45
|
-
};
|
|
46
|
-
}, [anchor, onClose]);
|
|
47
|
-
|
|
48
|
-
return createPortal(
|
|
49
|
-
html`<div ref=${panelRef} class="popover-panel"
|
|
50
|
-
style=${`top:${pos.top}px;left:${pos.left}px;width:${pos.width}px;`}>
|
|
51
|
-
${children}
|
|
52
|
-
</div>`,
|
|
53
|
-
document.body
|
|
54
|
-
);
|
|
55
|
-
}
|
|
1
|
+
// Tiny popover primitive — positions a floating panel relative to an
|
|
2
|
+
// anchor element, closes on outside click + Escape. Used by the unified
|
|
3
|
+
// pickers (CLI / Folder / Repo) so they all share interaction behavior.
|
|
4
|
+
//
|
|
5
|
+
// Usage:
|
|
6
|
+
// const [open, setOpen] = useState(false);
|
|
7
|
+
// const anchor = useRef(null);
|
|
8
|
+
// <button ref=${anchor} onClick=${() => setOpen(true)}>Trigger</button>
|
|
9
|
+
// ${open ? html`<${Popover} anchor=${anchor} onClose=${() => setOpen(false)}>
|
|
10
|
+
// ...panel contents...
|
|
11
|
+
// </${Popover}>` : null}
|
|
12
|
+
|
|
13
|
+
import { html } from '../html.js';
|
|
14
|
+
import { useEffect, useLayoutEffect, useRef, useState } from 'preact/hooks';
|
|
15
|
+
import { createPortal } from 'preact/compat';
|
|
16
|
+
|
|
17
|
+
export function Popover({ anchor, onClose, align = 'left', width, children }) {
|
|
18
|
+
const panelRef = useRef(null);
|
|
19
|
+
const [pos, setPos] = useState({ top: 0, left: 0, width: width || 320 });
|
|
20
|
+
|
|
21
|
+
useLayoutEffect(() => {
|
|
22
|
+
const a = anchor && anchor.current;
|
|
23
|
+
if (!a) return;
|
|
24
|
+
const rect = a.getBoundingClientRect();
|
|
25
|
+
const w = width || Math.max(rect.width, 320);
|
|
26
|
+
let left = align === 'right' ? rect.right - w : rect.left;
|
|
27
|
+
// Clamp to viewport with 8px margin.
|
|
28
|
+
left = Math.max(8, Math.min(window.innerWidth - w - 8, left));
|
|
29
|
+
const top = rect.bottom + 6;
|
|
30
|
+
setPos({ top, left, width: w });
|
|
31
|
+
}, [anchor, align, width]);
|
|
32
|
+
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
const onDown = (ev) => {
|
|
35
|
+
if (panelRef.current?.contains(ev.target)) return;
|
|
36
|
+
if (anchor.current?.contains(ev.target)) return;
|
|
37
|
+
onClose?.();
|
|
38
|
+
};
|
|
39
|
+
const onKey = (ev) => { if (ev.key === 'Escape') onClose?.(); };
|
|
40
|
+
document.addEventListener('mousedown', onDown, true);
|
|
41
|
+
document.addEventListener('keydown', onKey, true);
|
|
42
|
+
return () => {
|
|
43
|
+
document.removeEventListener('mousedown', onDown, true);
|
|
44
|
+
document.removeEventListener('keydown', onKey, true);
|
|
45
|
+
};
|
|
46
|
+
}, [anchor, onClose]);
|
|
47
|
+
|
|
48
|
+
return createPortal(
|
|
49
|
+
html`<div ref=${panelRef} class="popover-panel"
|
|
50
|
+
style=${`top:${pos.top}px;left:${pos.left}px;width:${pos.width}px;`}>
|
|
51
|
+
${children}
|
|
52
|
+
</div>`,
|
|
53
|
+
document.body
|
|
54
|
+
);
|
|
55
|
+
}
|