kodeui 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +30 -2
- package/bin/kodeui.mjs +89 -0
- package/package.json +37 -4
- package/skill/kodeui/SKILL.md +105 -0
- package/skill/kodeui/assets/tokens.css +40 -0
- package/skill/kodeui/components/aurora-input/AuroraInput.tsx +153 -0
- package/skill/kodeui/components/aurora-input/aurora-input.css +218 -0
- package/skill/kodeui/components/aurora-input/index.ts +2 -0
- package/skill/kodeui/components/bloom-wall/BloomWall.tsx +90 -0
- package/skill/kodeui/components/bloom-wall/bloom-wall.css +10 -0
- package/skill/kodeui/components/bloom-wall/engine.ts +1022 -0
- package/skill/kodeui/components/bloom-wall/icons/icons-a.ts +818 -0
- package/skill/kodeui/components/bloom-wall/icons/icons-b.ts +827 -0
- package/skill/kodeui/components/bloom-wall/icons/index.ts +25 -0
- package/skill/kodeui/components/bloom-wall/icons/types.ts +8 -0
- package/skill/kodeui/components/bloom-wall/index.ts +12 -0
- package/skill/kodeui/components/bloom-wall/paint.ts +113 -0
- package/skill/kodeui/components/bloom-wall/palettes.ts +32 -0
- package/skill/kodeui/components/blur-in-text/BlurInText.tsx +53 -0
- package/skill/kodeui/components/blur-in-text/blur-in-text.css +14 -0
- package/skill/kodeui/components/blur-in-text/index.ts +2 -0
- package/skill/kodeui/components/glass-reveal/GlassReveal.tsx +108 -0
- package/skill/kodeui/components/glass-reveal/glass-reveal.css +126 -0
- package/skill/kodeui/components/glass-reveal/index.ts +2 -0
- package/skill/kodeui/components/glow-card/GlowCard.tsx +67 -0
- package/skill/kodeui/components/glow-card/glow-card.css +134 -0
- package/skill/kodeui/components/glow-card/index.ts +2 -0
- package/skill/kodeui/components/launch-chip/LaunchChip.tsx +180 -0
- package/skill/kodeui/components/launch-chip/icons.tsx +119 -0
- package/skill/kodeui/components/launch-chip/index.ts +3 -0
- package/skill/kodeui/components/launch-chip/launch-chip.css +345 -0
- package/skill/kodeui/components/ring-reveal/RingReveal.tsx +84 -0
- package/skill/kodeui/components/ring-reveal/index.ts +4 -0
- package/skill/kodeui/components/ring-reveal/ring-reveal.css +10 -0
- package/skill/kodeui/components/ring-reveal/ring.ts +298 -0
- package/skill/kodeui/components/rolling-text/RollingText.tsx +121 -0
- package/skill/kodeui/components/rolling-text/index.ts +4 -0
- package/skill/kodeui/components/rolling-text/roll.ts +94 -0
- package/skill/kodeui/components/rolling-text/rolling-text.css +43 -0
- package/skill/kodeui/components/use-live-transcript/index.ts +2 -0
- package/skill/kodeui/components/use-live-transcript/useLiveTranscript.ts +215 -0
- package/skill/kodeui/components/voice-input/VoiceInput.tsx +273 -0
- package/skill/kodeui/components/voice-input/icons.tsx +27 -0
- package/skill/kodeui/components/voice-input/index.ts +4 -0
- package/skill/kodeui/components/voice-input/useVoiceRecorder.ts +289 -0
- package/skill/kodeui/components/voice-input/voice-input.css +34 -0
- package/skill/kodeui/components/voice-note/VoiceNote.tsx +170 -0
- package/skill/kodeui/components/voice-note/index.ts +3 -0
- package/skill/kodeui/components/voice-note/noteFile.ts +25 -0
- package/skill/kodeui/components/voice-note/voice-note.css +45 -0
- package/skill/kodeui/references/aurora-input.md +44 -0
- package/skill/kodeui/references/bloom-wall.md +59 -0
- package/skill/kodeui/references/blur-in-text.md +17 -0
- package/skill/kodeui/references/frameworks.md +46 -0
- package/skill/kodeui/references/glass-reveal.md +40 -0
- package/skill/kodeui/references/glow-card.md +48 -0
- package/skill/kodeui/references/launch-chip.md +37 -0
- package/skill/kodeui/references/recipes.md +77 -0
- package/skill/kodeui/references/ring-reveal.md +35 -0
- package/skill/kodeui/references/rolling-text.md +19 -0
- package/skill/kodeui/references/theming.md +45 -0
- package/skill/kodeui/references/use-live-transcript.md +28 -0
- package/skill/kodeui/references/voice-input.md +60 -0
- package/skill/kodeui/references/voice-note.md +22 -0
- package/skill/kodeui/scripts/add.mjs +192 -0
- package/skill/kodeui/scripts/manifest.json +92 -0
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import { getAudioContext } from 'voice-glow';
|
|
3
|
+
import { voiceNoteFileName } from '../voice-note/noteFile';
|
|
4
|
+
|
|
5
|
+
/** Seconds the "thinking" sweep runs at least, so it reads as thinking even when the file is ready at once. */
|
|
6
|
+
export const THINK_MIN = 1.6;
|
|
7
|
+
/** How long an error stays in the box, ms. */
|
|
8
|
+
const MESSAGE_MS = 3500;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* idle nothing is happening (a voice note may be showing)
|
|
12
|
+
* requesting waiting for the browser's microphone prompt
|
|
13
|
+
* listening recording
|
|
14
|
+
* thinking the recording is being finished (at least THINK_MIN seconds)
|
|
15
|
+
* playing the voice note is playing back
|
|
16
|
+
*/
|
|
17
|
+
export type VoiceRecorderState = 'idle' | 'requesting' | 'listening' | 'thinking' | 'playing';
|
|
18
|
+
|
|
19
|
+
export interface VoiceNoteInfo {
|
|
20
|
+
blob: Blob;
|
|
21
|
+
/** An object URL for the blob. It is revoked when the note is removed or replaced, or the recorder unmounts. */
|
|
22
|
+
url: string;
|
|
23
|
+
seconds: number;
|
|
24
|
+
mimeType: string;
|
|
25
|
+
/** voice-note-YYYYMMDD-HHMMSS.webm (or .m4a / .ogg, from the recorder's type). */
|
|
26
|
+
fileName: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface UseVoiceRecorderOptions {
|
|
30
|
+
/** 1 = real time. Scales the thinking time and the message timer. */
|
|
31
|
+
timeScale?: number;
|
|
32
|
+
/** Seconds the thinking state lasts at least. Default THINK_MIN (1.6). */
|
|
33
|
+
thinkMin?: number;
|
|
34
|
+
/** Recording has begun (the mic is live). A good moment to start a live transcript. */
|
|
35
|
+
onListen?: () => void;
|
|
36
|
+
/** The mic has closed: kept = true after finish() (the file is ready), false after cancel(). */
|
|
37
|
+
onMicClosed?: (kept: boolean) => void;
|
|
38
|
+
/** A new voice note is ready. */
|
|
39
|
+
onVoiceNote?: (note: VoiceNoteInfo) => void;
|
|
40
|
+
/** start() was called while the note plays: stop the playback. */
|
|
41
|
+
onStopPlayback?: () => void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface VoiceRecorder {
|
|
45
|
+
state: VoiceRecorderState;
|
|
46
|
+
/** The live microphone while listening, the playing note's stream while playing, else null. */
|
|
47
|
+
stream: MediaStream | null;
|
|
48
|
+
/** The last voice note, until it is removed. */
|
|
49
|
+
note: VoiceNoteInfo | null;
|
|
50
|
+
/** A short sentence after a failure (blocked mic, no mic, no recording support). Clears itself after 3.5 s. */
|
|
51
|
+
message: string | null;
|
|
52
|
+
start(): Promise<void>;
|
|
53
|
+
finish(): Promise<void>;
|
|
54
|
+
cancel(): void;
|
|
55
|
+
/** finish() while listening, start() otherwise. */
|
|
56
|
+
toggle(): void;
|
|
57
|
+
removeNote(): void;
|
|
58
|
+
/** Wire a voice note's onPlayingChange here, so the state becomes 'playing' and `stream` the note's audio. */
|
|
59
|
+
setPlaying(playing: boolean, stream: MediaStream | null): void;
|
|
60
|
+
/** The state right now, outside React's render (for animation frames and key handlers). */
|
|
61
|
+
getState(): VoiceRecorderState;
|
|
62
|
+
/** Seconds since thinking began (scaled by timeScale), or null when not thinking. */
|
|
63
|
+
sweepTime(): number | null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// the browser's voice clean-up is off, so a glow sees the real dynamics of the voice
|
|
67
|
+
const MIC_CONSTRAINTS: MediaStreamConstraints = {
|
|
68
|
+
audio: { echoCancellation: false, noiseSuppression: false, autoGainControl: false },
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const stopTracks = (s: MediaStream | null) => s?.getTracks().forEach((t) => t.stop());
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Records a voice note with MediaRecorder: idle → requesting → listening → thinking → idle (with a note),
|
|
75
|
+
* and idle ⇄ playing while the note plays. Works in every browser with getUserMedia and MediaRecorder;
|
|
76
|
+
* the audio stays on the page.
|
|
77
|
+
*/
|
|
78
|
+
export function useVoiceRecorder(options: UseVoiceRecorderOptions = {}): VoiceRecorder {
|
|
79
|
+
const opts = useRef(options);
|
|
80
|
+
opts.current = options;
|
|
81
|
+
|
|
82
|
+
const [state, setStateView] = useState<VoiceRecorderState>('idle');
|
|
83
|
+
const [stream, setStreamView] = useState<MediaStream | null>(null);
|
|
84
|
+
const [note, setNote] = useState<VoiceNoteInfo | null>(null);
|
|
85
|
+
const [message, setMessage] = useState<string | null>(null);
|
|
86
|
+
|
|
87
|
+
const r = useRef({
|
|
88
|
+
state: 'idle' as VoiceRecorderState,
|
|
89
|
+
mic: null as MediaStream | null,
|
|
90
|
+
recorder: null as MediaRecorder | null,
|
|
91
|
+
chunks: [] as Blob[],
|
|
92
|
+
recStart: 0,
|
|
93
|
+
sweepStart: 0,
|
|
94
|
+
request: 0, // bumps on every start / cancel, so a late microphone answer can tell it is stale
|
|
95
|
+
noteUrl: null as string | null,
|
|
96
|
+
timers: new Set<ReturnType<typeof setTimeout>>(),
|
|
97
|
+
messageTimer: 0 as ReturnType<typeof setTimeout> | 0,
|
|
98
|
+
alive: true,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const api = useMemo<Omit<VoiceRecorder, 'state' | 'stream' | 'note' | 'message'>>(() => {
|
|
102
|
+
const s = r.current;
|
|
103
|
+
const scale = () => Math.max(0.01, opts.current.timeScale ?? 1);
|
|
104
|
+
const now = (): VoiceRecorderState => s.state; // read through a call: the state changes while we await
|
|
105
|
+
|
|
106
|
+
const wait = (ms: number) => new Promise<void>((resolve) => {
|
|
107
|
+
const id = setTimeout(() => { s.timers.delete(id); resolve(); }, ms);
|
|
108
|
+
s.timers.add(id);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
const setState = (next: VoiceRecorderState) => {
|
|
112
|
+
s.state = next;
|
|
113
|
+
setStateView(next);
|
|
114
|
+
if (next !== 'idle') clearMessage();
|
|
115
|
+
};
|
|
116
|
+
const setStream = (next: MediaStream | null) => setStreamView(next);
|
|
117
|
+
|
|
118
|
+
const clearMessage = () => {
|
|
119
|
+
if (s.messageTimer) clearTimeout(s.messageTimer);
|
|
120
|
+
s.messageTimer = 0;
|
|
121
|
+
setMessage(null);
|
|
122
|
+
};
|
|
123
|
+
const flash = (text: string) => {
|
|
124
|
+
if (s.messageTimer) clearTimeout(s.messageTimer);
|
|
125
|
+
setMessage(text);
|
|
126
|
+
s.messageTimer = setTimeout(() => { s.messageTimer = 0; setMessage(null); }, MESSAGE_MS / scale());
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const closeMic = () => {
|
|
130
|
+
stopTracks(s.mic);
|
|
131
|
+
s.mic = null;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const setPlaying = (playing: boolean, playStream: MediaStream | null) => {
|
|
135
|
+
if (playing) {
|
|
136
|
+
if (s.state !== 'idle') return;
|
|
137
|
+
setStream(playStream);
|
|
138
|
+
setState('playing');
|
|
139
|
+
} else if (s.state === 'playing') {
|
|
140
|
+
setStream(null);
|
|
141
|
+
setState('idle');
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
const start = async () => {
|
|
146
|
+
if (s.state === 'playing') {
|
|
147
|
+
opts.current.onStopPlayback?.();
|
|
148
|
+
setPlaying(false, null);
|
|
149
|
+
}
|
|
150
|
+
if (s.state !== 'idle') return;
|
|
151
|
+
if (typeof navigator === 'undefined' || !navigator.mediaDevices?.getUserMedia || typeof MediaRecorder === 'undefined') {
|
|
152
|
+
flash('This browser cannot record audio.');
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
setState('requesting');
|
|
156
|
+
getAudioContext(); // inside the click, so Safari lets the audio context start
|
|
157
|
+
const ticket = ++s.request;
|
|
158
|
+
let mic: MediaStream;
|
|
159
|
+
try {
|
|
160
|
+
mic = await navigator.mediaDevices.getUserMedia(MIC_CONSTRAINTS);
|
|
161
|
+
} catch (err) {
|
|
162
|
+
if (ticket !== s.request || !s.alive) return;
|
|
163
|
+
setState('idle');
|
|
164
|
+
flash(err instanceof DOMException && err.name === 'NotAllowedError'
|
|
165
|
+
? 'Microphone blocked: allow it in the address bar.'
|
|
166
|
+
: 'No microphone found.');
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
// cancelled (or unmounted) while the prompt was open
|
|
170
|
+
if (ticket !== s.request || now() !== 'requesting' || !s.alive) { stopTracks(mic); return; }
|
|
171
|
+
|
|
172
|
+
let recorder: MediaRecorder;
|
|
173
|
+
try {
|
|
174
|
+
recorder = new MediaRecorder(mic);
|
|
175
|
+
} catch {
|
|
176
|
+
stopTracks(mic);
|
|
177
|
+
setState('idle');
|
|
178
|
+
flash('This browser cannot record audio.');
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
s.mic = mic;
|
|
182
|
+
s.chunks = [];
|
|
183
|
+
recorder.ondataavailable = (ev) => { if (ev.data && ev.data.size) s.chunks.push(ev.data); };
|
|
184
|
+
recorder.start(250);
|
|
185
|
+
s.recorder = recorder;
|
|
186
|
+
s.recStart = performance.now();
|
|
187
|
+
setStream(mic);
|
|
188
|
+
opts.current.onListen?.();
|
|
189
|
+
setState('listening');
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
const showNote = (blob: Blob, seconds: number) => {
|
|
193
|
+
if (s.noteUrl) URL.revokeObjectURL(s.noteUrl);
|
|
194
|
+
const url = URL.createObjectURL(blob);
|
|
195
|
+
s.noteUrl = url;
|
|
196
|
+
const info: VoiceNoteInfo = { blob, url, seconds, mimeType: blob.type, fileName: voiceNoteFileName(blob.type) };
|
|
197
|
+
setNote(info);
|
|
198
|
+
opts.current.onVoiceNote?.(info);
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
const finish = async () => {
|
|
202
|
+
const recorder = s.recorder;
|
|
203
|
+
if (s.state !== 'listening' || !recorder) return;
|
|
204
|
+
const seconds = (performance.now() - s.recStart) / 1000;
|
|
205
|
+
s.sweepStart = performance.now();
|
|
206
|
+
setState('thinking');
|
|
207
|
+
const blob = await new Promise<Blob>((resolve) => {
|
|
208
|
+
const pack = () => resolve(new Blob(s.chunks, { type: recorder.mimeType || 'audio/webm' }));
|
|
209
|
+
recorder.onstop = pack;
|
|
210
|
+
try { recorder.stop(); } catch { pack(); }
|
|
211
|
+
});
|
|
212
|
+
if (s.recorder === recorder) s.recorder = null;
|
|
213
|
+
if (!s.alive || now() !== 'thinking') return;
|
|
214
|
+
closeMic();
|
|
215
|
+
setStream(null);
|
|
216
|
+
opts.current.onMicClosed?.(true);
|
|
217
|
+
const thinkMs = (opts.current.thinkMin ?? THINK_MIN) * 1000 / scale();
|
|
218
|
+
const left = thinkMs - (performance.now() - s.sweepStart);
|
|
219
|
+
if (left > 0) await wait(left);
|
|
220
|
+
if (!s.alive || now() !== 'thinking') return;
|
|
221
|
+
showNote(blob, seconds);
|
|
222
|
+
setState('idle');
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const cancel = () => {
|
|
226
|
+
if (s.state === 'requesting') { s.request++; setState('idle'); return; }
|
|
227
|
+
if (s.state !== 'listening') return;
|
|
228
|
+
const recorder = s.recorder;
|
|
229
|
+
s.recorder = null;
|
|
230
|
+
if (recorder) {
|
|
231
|
+
recorder.ondataavailable = null;
|
|
232
|
+
recorder.onstop = null;
|
|
233
|
+
try { recorder.stop(); } catch { /* already stopped */ }
|
|
234
|
+
}
|
|
235
|
+
s.chunks = [];
|
|
236
|
+
closeMic();
|
|
237
|
+
setStream(null);
|
|
238
|
+
opts.current.onMicClosed?.(false);
|
|
239
|
+
setState('idle');
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const toggle = () => { if (s.state === 'listening') void finish(); else void start(); };
|
|
243
|
+
|
|
244
|
+
const removeNote = () => {
|
|
245
|
+
setPlaying(false, null);
|
|
246
|
+
if (s.noteUrl) URL.revokeObjectURL(s.noteUrl);
|
|
247
|
+
s.noteUrl = null;
|
|
248
|
+
setNote(null);
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
const getState = () => s.state;
|
|
252
|
+
const sweepTime = () => (s.state === 'thinking' ? ((performance.now() - s.sweepStart) / 1000) * scale() : null);
|
|
253
|
+
|
|
254
|
+
return { start, finish, cancel, toggle, removeNote, setPlaying, getState, sweepTime };
|
|
255
|
+
}, []);
|
|
256
|
+
|
|
257
|
+
useEffect(() => {
|
|
258
|
+
const s = r.current;
|
|
259
|
+
s.alive = true;
|
|
260
|
+
return () => {
|
|
261
|
+
s.alive = false;
|
|
262
|
+
s.request++;
|
|
263
|
+
const recorder = s.recorder;
|
|
264
|
+
s.recorder = null;
|
|
265
|
+
if (recorder) {
|
|
266
|
+
recorder.ondataavailable = null;
|
|
267
|
+
recorder.onstop = null;
|
|
268
|
+
try { recorder.stop(); } catch { /* already stopped */ }
|
|
269
|
+
}
|
|
270
|
+
s.chunks = [];
|
|
271
|
+
stopTracks(s.mic);
|
|
272
|
+
s.mic = null;
|
|
273
|
+
s.timers.forEach(clearTimeout);
|
|
274
|
+
s.timers.clear();
|
|
275
|
+
if (s.messageTimer) clearTimeout(s.messageTimer);
|
|
276
|
+
s.messageTimer = 0;
|
|
277
|
+
if (s.noteUrl) URL.revokeObjectURL(s.noteUrl);
|
|
278
|
+
s.noteUrl = null;
|
|
279
|
+
// leave a clean slate, so a remount (StrictMode, hot reload) starts from idle
|
|
280
|
+
s.state = 'idle';
|
|
281
|
+
setStateView('idle');
|
|
282
|
+
setStreamView(null);
|
|
283
|
+
setNote(null);
|
|
284
|
+
setMessage(null);
|
|
285
|
+
};
|
|
286
|
+
}, []);
|
|
287
|
+
|
|
288
|
+
return { state, stream, note, message, ...api };
|
|
289
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/* KodeUI Voice Input: the mic and cancel buttons, the glow layer and the note's place in the box. */
|
|
2
|
+
|
|
3
|
+
/* the glow fills the box, over its background and under the text and buttons */
|
|
4
|
+
.ku-voice-glow { position: absolute; inset: 0; border-radius: inherit; pointer-events: none; }
|
|
5
|
+
.ku-voice-glow-host { width: 100%; height: 100%; border-radius: 24px; }
|
|
6
|
+
|
|
7
|
+
/* the voice note sits between the text and the buttons */
|
|
8
|
+
.ku-voice-input .ku-aurora-input__field { order: 0; }
|
|
9
|
+
.ku-voice-input .ku-voice-note { order: 1; }
|
|
10
|
+
.ku-voice-input .ku-aurora-input__actions { order: 2; }
|
|
11
|
+
|
|
12
|
+
/* the round mic and cancel buttons (also fine without the Aurora Input's button styles) */
|
|
13
|
+
.ku-voice-btn {
|
|
14
|
+
flex: none; box-sizing: border-box; width: 36px; height: 36px; padding: 0; margin: 0;
|
|
15
|
+
display: inline-flex; align-items: center; justify-content: center;
|
|
16
|
+
border-radius: 9999px; border: 1px solid var(--ku-outline, #24342c);
|
|
17
|
+
background-color: transparent; color: inherit; font: inherit; cursor: pointer;
|
|
18
|
+
}
|
|
19
|
+
.ku-voice-btn svg { width: 16px; height: 16px; }
|
|
20
|
+
.ku-voice-btn[hidden] { display: none; }
|
|
21
|
+
.ku-voice-btn:disabled { opacity: .5; cursor: default; }
|
|
22
|
+
.ku-voice-btn:focus-visible { outline: 2px solid var(--ku-fern, #5ee08a); outline-offset: 2px; }
|
|
23
|
+
|
|
24
|
+
/* recording: the mic becomes a light stop button (also over the glass state) */
|
|
25
|
+
.ku-voice-btn.ku-voice-mic.is-recording,
|
|
26
|
+
[data-glass] .ku-voice-btn.ku-voice-mic.is-recording {
|
|
27
|
+
background-color: var(--ku-text, #e8f1eb); color: var(--ku-night, #060c09); border-color: transparent;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/* read out by screen readers, not shown */
|
|
31
|
+
.ku-voice-status {
|
|
32
|
+
position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden;
|
|
33
|
+
clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; border: 0;
|
|
34
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState } from 'react';
|
|
2
|
+
import { formatClock } from './noteFile';
|
|
3
|
+
import './voice-note.css';
|
|
4
|
+
|
|
5
|
+
export interface VoiceNoteProps {
|
|
6
|
+
/** The recording: an object URL or any audio URL. */
|
|
7
|
+
src: string;
|
|
8
|
+
/** Its length, shown as m:ss. */
|
|
9
|
+
seconds: number;
|
|
10
|
+
/** Name for the Save link's download. Default "voice-note". */
|
|
11
|
+
fileName?: string;
|
|
12
|
+
/** Show the ✕ and call this when it is clicked. */
|
|
13
|
+
onRemove?: () => void;
|
|
14
|
+
/**
|
|
15
|
+
* Playback started or stopped. While playing, `stream` is the note's audio as a
|
|
16
|
+
* MediaStream (from captureStream, where the browser has it), so a glow can listen to it.
|
|
17
|
+
*/
|
|
18
|
+
onPlayingChange?: (playing: boolean, stream: MediaStream | null) => void;
|
|
19
|
+
/** Called inside the play click, before playback starts (e.g. to wake an AudioContext in Safari). */
|
|
20
|
+
onBeforePlay?: () => void;
|
|
21
|
+
/** false = the play button does nothing (used while a new recording is under way). Default true. */
|
|
22
|
+
playable?: boolean;
|
|
23
|
+
/** The pill's words. Default "Voice note". */
|
|
24
|
+
label?: string;
|
|
25
|
+
/** The download link's words. Default "Save". */
|
|
26
|
+
saveLabel?: string;
|
|
27
|
+
className?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface VoiceNoteHandle {
|
|
31
|
+
/** Play from the start. */
|
|
32
|
+
play(): void;
|
|
33
|
+
/** Stop playback. */
|
|
34
|
+
stop(): void;
|
|
35
|
+
readonly playing: boolean;
|
|
36
|
+
readonly audio: HTMLAudioElement | null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
type CapturableAudio = HTMLAudioElement & {
|
|
40
|
+
captureStream?: () => MediaStream;
|
|
41
|
+
mozCaptureStream?: () => MediaStream;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const BARS = 12;
|
|
45
|
+
|
|
46
|
+
function PlayIcon() {
|
|
47
|
+
return (
|
|
48
|
+
<svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
|
|
49
|
+
<path d="M9 6.9c0-.8.9-1.3 1.6-.9l8.2 5.1c.6.4.6 1.4 0 1.8L10.6 18c-.7.4-1.6-.1-1.6-.9z" />
|
|
50
|
+
</svg>
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
function PauseIcon() {
|
|
54
|
+
return (
|
|
55
|
+
<svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
|
|
56
|
+
<rect x="7.4" y="6" width="3.3" height="12" rx="1.3" />
|
|
57
|
+
<rect x="13.3" y="6" width="3.3" height="12" rx="1.3" />
|
|
58
|
+
</svg>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
function CloseIcon() {
|
|
62
|
+
return (
|
|
63
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.9" strokeLinecap="round" aria-hidden="true">
|
|
64
|
+
<path d="M7.6 7.6l8.8 8.8M16.4 7.6l-8.8 8.8" />
|
|
65
|
+
</svg>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* A voice note pill: play / stop, moving bars while it plays, its length, Save and remove.
|
|
71
|
+
* It pops in (rise, unblur, fade) when it mounts; give it a new `key` to pop in again.
|
|
72
|
+
*/
|
|
73
|
+
export const VoiceNote = forwardRef<VoiceNoteHandle, VoiceNoteProps>(function VoiceNote(props, ref) {
|
|
74
|
+
const {
|
|
75
|
+
src, seconds, fileName = 'voice-note', onRemove, onPlayingChange, onBeforePlay,
|
|
76
|
+
playable = true, label = 'Voice note', saveLabel = 'Save', className,
|
|
77
|
+
} = props;
|
|
78
|
+
|
|
79
|
+
const [playing, setPlaying] = useState(false);
|
|
80
|
+
const audioRef = useRef<CapturableAudio | null>(null);
|
|
81
|
+
const captured = useRef<MediaStream | null>(null);
|
|
82
|
+
const playingRef = useRef(false);
|
|
83
|
+
const latest = useRef({ onPlayingChange, onBeforePlay, playable });
|
|
84
|
+
latest.current = { onPlayingChange, onBeforePlay, playable };
|
|
85
|
+
|
|
86
|
+
const release = useCallback(() => {
|
|
87
|
+
captured.current?.getTracks().forEach((t) => t.stop());
|
|
88
|
+
captured.current = null;
|
|
89
|
+
}, []);
|
|
90
|
+
|
|
91
|
+
const stop = useCallback(() => {
|
|
92
|
+
audioRef.current?.pause();
|
|
93
|
+
const was = playingRef.current;
|
|
94
|
+
playingRef.current = false;
|
|
95
|
+
setPlaying(false);
|
|
96
|
+
if (was) latest.current.onPlayingChange?.(false, null);
|
|
97
|
+
release();
|
|
98
|
+
}, [release]);
|
|
99
|
+
|
|
100
|
+
const play = useCallback(() => {
|
|
101
|
+
const audio = audioRef.current;
|
|
102
|
+
const { playable: canPlay, onBeforePlay: before } = latest.current;
|
|
103
|
+
if (!audio || !canPlay || playingRef.current) return;
|
|
104
|
+
before?.();
|
|
105
|
+
audio.currentTime = 0;
|
|
106
|
+
audio.play().then(() => {
|
|
107
|
+
if (audioRef.current !== audio || audio.paused) return;
|
|
108
|
+
// let a glow listen to the note while it plays (Chrome, Edge, Firefox)
|
|
109
|
+
const capture = audio.captureStream ?? audio.mozCaptureStream;
|
|
110
|
+
let stream: MediaStream | null = null;
|
|
111
|
+
try { stream = capture ? capture.call(audio) : null; } catch { stream = null; }
|
|
112
|
+
release();
|
|
113
|
+
captured.current = stream;
|
|
114
|
+
playingRef.current = true;
|
|
115
|
+
setPlaying(true);
|
|
116
|
+
latest.current.onPlayingChange?.(true, stream);
|
|
117
|
+
}).catch(() => { /* autoplay refused or the source failed */ });
|
|
118
|
+
}, [release]);
|
|
119
|
+
|
|
120
|
+
useEffect(() => {
|
|
121
|
+
const audio = new Audio(src) as CapturableAudio;
|
|
122
|
+
audio.preload = 'auto';
|
|
123
|
+
audio.onended = () => { if (audioRef.current === audio) stop(); };
|
|
124
|
+
audioRef.current = audio;
|
|
125
|
+
return () => {
|
|
126
|
+
audio.onended = null;
|
|
127
|
+
audio.pause();
|
|
128
|
+
if (audioRef.current === audio) audioRef.current = null;
|
|
129
|
+
if (playingRef.current) {
|
|
130
|
+
playingRef.current = false;
|
|
131
|
+
setPlaying(false);
|
|
132
|
+
latest.current.onPlayingChange?.(false, null);
|
|
133
|
+
}
|
|
134
|
+
release();
|
|
135
|
+
audio.removeAttribute('src');
|
|
136
|
+
audio.load();
|
|
137
|
+
};
|
|
138
|
+
}, [src, stop, release]);
|
|
139
|
+
|
|
140
|
+
useImperativeHandle(ref, () => ({
|
|
141
|
+
play,
|
|
142
|
+
stop,
|
|
143
|
+
get playing() { return playingRef.current; },
|
|
144
|
+
get audio() { return audioRef.current; },
|
|
145
|
+
}), [play, stop]);
|
|
146
|
+
|
|
147
|
+
return (
|
|
148
|
+
<div className={'ku-voice-note' + (playing ? ' is-playing' : '') + (className ? ' ' + className : '')}>
|
|
149
|
+
<button
|
|
150
|
+
type="button"
|
|
151
|
+
className="ku-voice-note-play"
|
|
152
|
+
aria-label={playing ? `Stop the ${label.toLowerCase()}` : `Play the ${label.toLowerCase()}`}
|
|
153
|
+
onClick={() => (playingRef.current ? stop() : play())}
|
|
154
|
+
>
|
|
155
|
+
{playing ? <PauseIcon /> : <PlayIcon />}
|
|
156
|
+
</button>
|
|
157
|
+
<span className="ku-voice-note-bars" aria-hidden="true">
|
|
158
|
+
{Array.from({ length: BARS }, (_, i) => <i key={i} />)}
|
|
159
|
+
</span>
|
|
160
|
+
<span className="ku-voice-note-label">{label}</span>
|
|
161
|
+
<span className="ku-voice-note-time">{formatClock(seconds)}</span>
|
|
162
|
+
<a className="ku-voice-note-save" href={src} download={fileName}>{saveLabel}</a>
|
|
163
|
+
{onRemove && (
|
|
164
|
+
<button type="button" className="ku-voice-note-remove" aria-label={`Remove the ${label.toLowerCase()}`} onClick={onRemove}>
|
|
165
|
+
<CloseIcon />
|
|
166
|
+
</button>
|
|
167
|
+
)}
|
|
168
|
+
</div>
|
|
169
|
+
);
|
|
170
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** File helpers for voice notes: the extension for a recording's type, a dated file name, and the m:ss clock. */
|
|
2
|
+
|
|
3
|
+
/** The file extension for an audio type: MediaRecorder gives audio/webm (Chrome, Edge, Firefox), audio/mp4 (Safari) or audio/ogg. */
|
|
4
|
+
export function audioExtension(mimeType: string): string {
|
|
5
|
+
if (/mp4/.test(mimeType)) return 'm4a';
|
|
6
|
+
if (/ogg/.test(mimeType)) return 'ogg';
|
|
7
|
+
if (/wav/.test(mimeType)) return 'wav';
|
|
8
|
+
if (/mpeg/.test(mimeType)) return 'mp3';
|
|
9
|
+
return 'webm';
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const two = (n: number) => String(n).padStart(2, '0');
|
|
13
|
+
|
|
14
|
+
/** voice-note-YYYYMMDD-HHMMSS.<ext>, in local time. */
|
|
15
|
+
export function voiceNoteFileName(mimeType: string, at: Date = new Date()): string {
|
|
16
|
+
const day = `${at.getFullYear()}${two(at.getMonth() + 1)}${two(at.getDate())}`;
|
|
17
|
+
const time = `${two(at.getHours())}${two(at.getMinutes())}${two(at.getSeconds())}`;
|
|
18
|
+
return `voice-note-${day}-${time}.${audioExtension(mimeType)}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** 0:07, 1:32. */
|
|
22
|
+
export function formatClock(seconds: number): string {
|
|
23
|
+
const whole = Math.max(0, Math.round(seconds));
|
|
24
|
+
return `${Math.floor(whole / 60)}:${two(whole % 60)}`;
|
|
25
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/* KodeUI Voice Note: a playable pill that pops in when a recording is ready. */
|
|
2
|
+
.ku-voice-note {
|
|
3
|
+
display: flex; align-items: center; gap: 10px; align-self: flex-start; max-width: 100%;
|
|
4
|
+
box-sizing: border-box; height: 40px; padding: 0 6px 0 4px; border-radius: 9999px;
|
|
5
|
+
background: rgba(232, 241, 235, .06); border: 1px solid rgba(232, 241, 235, .08);
|
|
6
|
+
color: var(--ku-text, #e8f1eb); font-size: 14px; line-height: 20px; white-space: nowrap;
|
|
7
|
+
animation: ku-voice-note-in .45s cubic-bezier(.22, 1, .36, 1) both;
|
|
8
|
+
}
|
|
9
|
+
@keyframes ku-voice-note-in {
|
|
10
|
+
from { opacity: 0; transform: translateY(6px); filter: blur(6px); }
|
|
11
|
+
to { opacity: 1; transform: none; filter: none; }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
.ku-voice-note-play {
|
|
15
|
+
flex: none; width: 32px; height: 32px; padding: 0; border-radius: 50%; border: 0;
|
|
16
|
+
display: grid; place-items: center; cursor: pointer;
|
|
17
|
+
background: var(--ku-text, #e8f1eb); color: var(--ku-night, #060c09);
|
|
18
|
+
}
|
|
19
|
+
.ku-voice-note-play svg { width: 15px; height: 15px; }
|
|
20
|
+
|
|
21
|
+
/* twelve bars of three heights; they breathe while the note plays */
|
|
22
|
+
.ku-voice-note-bars { display: inline-flex; align-items: center; gap: 2px; height: 18px; flex: none; }
|
|
23
|
+
.ku-voice-note-bars i { display: block; width: 2px; height: 30%; border-radius: 2px; background: currentColor; opacity: .55; }
|
|
24
|
+
.ku-voice-note-bars i:nth-child(3n+1) { height: 70%; }
|
|
25
|
+
.ku-voice-note-bars i:nth-child(4n+2) { height: 100%; }
|
|
26
|
+
.ku-voice-note-bars i:nth-child(5n+3) { height: 45%; }
|
|
27
|
+
.ku-voice-note.is-playing .ku-voice-note-bars i { animation: ku-voice-note-bar .9s ease-in-out infinite alternate; }
|
|
28
|
+
.ku-voice-note.is-playing .ku-voice-note-bars i:nth-child(odd) { animation-delay: -.45s; }
|
|
29
|
+
@keyframes ku-voice-note-bar { from { transform: scaleY(.45); } to { transform: scaleY(1); } }
|
|
30
|
+
|
|
31
|
+
/* on a narrow screen the label gives way first, so the pill stays inside its box */
|
|
32
|
+
.ku-voice-note-label { font-weight: 500; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
|
|
33
|
+
.ku-voice-note-time { flex: none; color: var(--ku-text-2, #a2b6aa); font-variant-numeric: tabular-nums; }
|
|
34
|
+
.ku-voice-note-save { flex: none; color: var(--ku-link, #7fe3b0); text-decoration: none; font-weight: 500; padding: 0 4px; }
|
|
35
|
+
.ku-voice-note-save:hover { text-decoration: underline; text-underline-offset: 3px; }
|
|
36
|
+
.ku-voice-note-remove {
|
|
37
|
+
flex: none; width: 28px; height: 28px; padding: 0; border-radius: 50%; border: 0; cursor: pointer;
|
|
38
|
+
display: grid; place-items: center; background: transparent; color: var(--ku-text-2, #a2b6aa);
|
|
39
|
+
}
|
|
40
|
+
.ku-voice-note-remove svg { width: 14px; height: 14px; }
|
|
41
|
+
.ku-voice-note-remove:hover { background: rgba(232, 241, 235, .08); color: var(--ku-text, #e8f1eb); }
|
|
42
|
+
|
|
43
|
+
.ku-voice-note-play:focus-visible, .ku-voice-note-remove:focus-visible, .ku-voice-note-save:focus-visible {
|
|
44
|
+
outline: 2px solid var(--ku-fern, #5ee08a); outline-offset: 2px;
|
|
45
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Aurora Input
|
|
2
|
+
|
|
3
|
+
A prompt box (textarea plus an action row) with two layers of an aurora light on its border. The
|
|
4
|
+
light holds for 2.2 s, then slides a quarter turn in 0.8 s, so it sits somewhere new every 3 s.
|
|
5
|
+
Enter submits, Shift+Enter adds a line.
|
|
6
|
+
|
|
7
|
+
**Folder:** `aurora-input` · **Needs:** nothing · **Exports:** `AuroraInput`, `AuroraInputAction`
|
|
8
|
+
|
|
9
|
+
## Props
|
|
10
|
+
|
|
11
|
+
| Prop | Type | Default | What it does |
|
|
12
|
+
|---|---|---|---|
|
|
13
|
+
| `value` / `defaultValue` / `onChange(value)` | | | Controlled or not; `onChange` gets the string |
|
|
14
|
+
| `onSubmit(value)` | | | Enter submits (without it Enter is a new line) |
|
|
15
|
+
| `placeholder` | string | | |
|
|
16
|
+
| `leftActions`, `rightActions` | ReactNode | | Buttons in the action row |
|
|
17
|
+
| `top` | ReactNode | | Above the text (a voice note) |
|
|
18
|
+
| `overlay` | ReactNode | | Behind the text, clipped to the box (a glow) |
|
|
19
|
+
| `stepEvery` | seconds | 3 | Time between light moves (hold and move keep their 2.2 : 0.8 split) |
|
|
20
|
+
| `colors` | string[] | sea glass, fern, aurora, bloom | The border lights |
|
|
21
|
+
| `rows` | number | 2 | Rows before it scrolls |
|
|
22
|
+
| `textareaRef`, `textareaProps` | | | Reach the textarea (id, name, autoFocus…) |
|
|
23
|
+
| `disabled`, `aria-label`, `className`, `style` | | | |
|
|
24
|
+
|
|
25
|
+
`AuroraInputAction` is a button for the action row: a pill by default, `round` for a 36 px icon
|
|
26
|
+
button. Pass `aria-label` for icon-only buttons.
|
|
27
|
+
|
|
28
|
+
## Example
|
|
29
|
+
|
|
30
|
+
```tsx
|
|
31
|
+
import { AuroraInput, AuroraInputAction } from '@/components/kodeui/aurora-input';
|
|
32
|
+
|
|
33
|
+
const [text, setText] = useState('');
|
|
34
|
+
<AuroraInput
|
|
35
|
+
value={text}
|
|
36
|
+
onChange={setText}
|
|
37
|
+
onSubmit={(t) => { ask(t); setText(''); }}
|
|
38
|
+
placeholder="Ask anything"
|
|
39
|
+
leftActions={<AuroraInputAction round aria-label="Attach">+</AuroraInputAction>}
|
|
40
|
+
rightActions={<AuroraInputAction onClick={() => ask(text)}>Send</AuroraInputAction>}
|
|
41
|
+
/>
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
For a mic, use Voice Input instead: it is this box plus recording.
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# Bloom Wall
|
|
2
|
+
|
|
3
|
+
A canvas wall of glowing nature tiles (16 drawn icons, 6 palettes) that grows out of a trigger on
|
|
4
|
+
hover: a soft ring opens from the trigger (0.85 s), tiles pop in by distance, they breathe and
|
|
5
|
+
some swell while the pointer stays, and on leave the ring folds back in over 2.45 s while a few
|
|
6
|
+
tiles linger and sparkle out. It adjusts its own quality on slow devices, pauses in hidden tabs
|
|
7
|
+
and follows reduced motion.
|
|
8
|
+
|
|
9
|
+
**Folder:** `bloom-wall` · **Needs:** nothing · **Exports:** `BloomWall`, `NATURE_PALETTES`, `Palette` (type)
|
|
10
|
+
|
|
11
|
+
## Props
|
|
12
|
+
|
|
13
|
+
| Prop | Type | Default | What it does |
|
|
14
|
+
|---|---|---|---|
|
|
15
|
+
| `active` | boolean | required | `true` grows the wall, `false` folds it back |
|
|
16
|
+
| `origin` | `RefObject<HTMLElement>` or `() => ({x, y})` | wall centre | Where the ring starts: an element's centre, or a point in the wall's own px |
|
|
17
|
+
| `fastFade` | boolean | `false` | The quicker fold, for when a click opens something else |
|
|
18
|
+
| `palettes` | `Palette[]` | `NATURE_PALETTES` | Tile colours: `{ id, name, primary, secondary, accent, glow }` (hex) |
|
|
19
|
+
| `timeScale` | number | 1 | 0.25 = slow motion |
|
|
20
|
+
| `onIdle` | () => void | | Fold finished, canvas clear |
|
|
21
|
+
| `className`, `style` | | | On the canvas |
|
|
22
|
+
|
|
23
|
+
## Example
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
import { useRef, useState } from 'react';
|
|
27
|
+
import { BloomWall } from '@/components/kodeui/bloom-wall';
|
|
28
|
+
|
|
29
|
+
export function Hero() {
|
|
30
|
+
const trigger = useRef<HTMLButtonElement>(null);
|
|
31
|
+
const [open, setOpen] = useState(false);
|
|
32
|
+
return (
|
|
33
|
+
<section style={{ position: 'relative', overflow: 'hidden', minHeight: 640, background: '#0e1712' }}>
|
|
34
|
+
<BloomWall active={open} origin={trigger} />
|
|
35
|
+
<div style={{ position: 'relative', zIndex: 1, display: 'grid', placeItems: 'center', gap: 24, paddingTop: 160 }}>
|
|
36
|
+
<button ref={trigger} onMouseEnter={() => setOpen(true)} onMouseLeave={() => setOpen(false)}>
|
|
37
|
+
See what's new
|
|
38
|
+
</button>
|
|
39
|
+
<h1>Build your ideas faster</h1>
|
|
40
|
+
</div>
|
|
41
|
+
</section>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Traps
|
|
47
|
+
|
|
48
|
+
- The canvas fills its **nearest positioned parent** and sits under the content: give the parent
|
|
49
|
+
`position: relative` and `overflow: hidden`, put `<BloomWall>` first, and lift the content with
|
|
50
|
+
`position: relative; z-index: 1`. Without a positioned parent it covers the whole page.
|
|
51
|
+
- `origin` must point at the element the pointer enters (a ref on that element). It is a hover
|
|
52
|
+
effect: on a touch screen a tap fires `mouseenter` (the wall opens) and tapping elsewhere fires
|
|
53
|
+
`mouseleave` (it folds). Don't also fold it in `onClick`: a tap fires enter and click together,
|
|
54
|
+
so the wall would open and fold at once. If a click opens something else (a dialog, a page),
|
|
55
|
+
fold it with `fastFade` in that click handler only for mouse clicks, e.g.
|
|
56
|
+
`onPointerUp={(e) => { if (e.pointerType === 'mouse') { setFast(true); setOpen(false); } }}`.
|
|
57
|
+
- Pair it with Glass Reveal (same `active` state) so the content turns to glass over the wall,
|
|
58
|
+
and with Launch Chip as the trigger (`intro="grid"`): see `recipes.md`.
|
|
59
|
+
- Keep the tiles readable behind text: the wall stays dim in the middle on purpose.
|