@swisper.ai/web-chat-ui 0.3.2 → 0.4.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/dist/components/chat-prompt-box/chat-prompt-box-context.d.ts +10 -1
- package/dist/components/chat-prompt-box/chat-prompt-box-context.d.ts.map +1 -1
- package/dist/components/chat-prompt-box/chat-prompt-box-voice-motion.d.ts +85 -0
- package/dist/components/chat-prompt-box/chat-prompt-box-voice-motion.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/chat-prompt-box-voice.d.ts +20 -0
- package/dist/components/chat-prompt-box/chat-prompt-box-voice.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/chat-prompt-box-voice.stories.d.ts +45 -0
- package/dist/components/chat-prompt-box/chat-prompt-box-voice.stories.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/chat-prompt-box.d.ts +24 -1
- package/dist/components/chat-prompt-box/chat-prompt-box.d.ts.map +1 -1
- package/dist/components/chat-prompt-box/chat-prompt-box.stories.d.ts.map +1 -1
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/chat-prompt-box-voice-bar-icon.d.ts +31 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/chat-prompt-box-voice-bar-icon.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/chat-prompt-box-voice-bar-icon.stories.d.ts +17 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/chat-prompt-box-voice-bar-icon.stories.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/index.d.ts +3 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/index.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-button/chat-prompt-box-voice-button.d.ts +29 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-button/chat-prompt-box-voice-button.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-button/chat-prompt-box-voice-button.stories.d.ts +12 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-button/chat-prompt-box-voice-button.stories.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-button/index.d.ts +3 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-button/index.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-light/chat-prompt-box-voice-light.d.ts +44 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-light/chat-prompt-box-voice-light.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-light/chat-prompt-box-voice-light.stories.d.ts +17 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-light/chat-prompt-box-voice-light.stories.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-light/index.d.ts +3 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-light/index.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/chat-prompt-box-voice-mic-button.d.ts +14 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/chat-prompt-box-voice-mic-button.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/chat-prompt-box-voice-mic-button.stories.d.ts +11 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/chat-prompt-box-voice-mic-button.stories.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/index.d.ts +3 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/index.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-panel/chat-prompt-box-voice-panel.d.ts +18 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-panel/chat-prompt-box-voice-panel.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-panel/chat-prompt-box-voice-panel.stories.d.ts +18 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-panel/chat-prompt-box-voice-panel.stories.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-panel/index.d.ts +3 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-panel/index.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/chat-prompt-box-voice-sound-button.d.ts +14 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/chat-prompt-box-voice-sound-button.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/chat-prompt-box-voice-sound-button.stories.d.ts +11 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/chat-prompt-box-voice-sound-button.stories.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/index.d.ts +3 -0
- package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/index.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/components/index.d.ts +7 -0
- package/dist/components/chat-prompt-box/components/index.d.ts.map +1 -0
- package/dist/components/chat-prompt-box/index.d.ts +5 -1
- package/dist/components/chat-prompt-box/index.d.ts.map +1 -1
- package/dist/components/chat-prompt-box/use-voice-motion.d.ts +22 -0
- package/dist/components/chat-prompt-box/use-voice-motion.d.ts.map +1 -0
- package/dist/index.css +1 -1
- package/dist/index.es.js +2285 -1443
- package/dist/index.es.js.map +1 -1
- package/package.json +3 -3
- package/src/components/chat-prompt-box/chat-prompt-box-context.tsx +35 -1
- package/src/components/chat-prompt-box/chat-prompt-box-voice-motion.ts +436 -0
- package/src/components/chat-prompt-box/chat-prompt-box-voice.stories.tsx +243 -0
- package/src/components/chat-prompt-box/chat-prompt-box-voice.tsx +191 -0
- package/src/components/chat-prompt-box/chat-prompt-box.json +66 -7
- package/src/components/chat-prompt-box/chat-prompt-box.stories.tsx +16 -0
- package/src/components/chat-prompt-box/chat-prompt-box.tsx +131 -77
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/chat-prompt-box-voice-bar-icon.stories.tsx +155 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/chat-prompt-box-voice-bar-icon.tsx +80 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/index.ts +5 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-button/chat-prompt-box-voice-button.stories.tsx +107 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-button/chat-prompt-box-voice-button.tsx +101 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-button/index.ts +5 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-light/chat-prompt-box-voice-light.stories.tsx +177 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-light/chat-prompt-box-voice-light.tsx +283 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-light/index.ts +5 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/chat-prompt-box-voice-mic-button.stories.tsx +50 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/chat-prompt-box-voice-mic-button.tsx +46 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/index.ts +2 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-panel/chat-prompt-box-voice-panel.stories.tsx +149 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-panel/chat-prompt-box-voice-panel.tsx +55 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-panel/index.ts +5 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/chat-prompt-box-voice-sound-button.stories.tsx +50 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/chat-prompt-box-voice-sound-button.tsx +46 -0
- package/src/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/index.ts +2 -0
- package/src/components/chat-prompt-box/components/index.ts +6 -0
- package/src/components/chat-prompt-box/index.ts +23 -0
- package/src/components/chat-prompt-box/use-voice-motion.ts +184 -0
|
@@ -7,11 +7,37 @@ import { ChatInput, ChatInputTextarea, useChatInputContext } from '../chat-input
|
|
|
7
7
|
import type { ChatInputProps, ChatInputTextareaProps } from '../chat-input';
|
|
8
8
|
|
|
9
9
|
import { ChatPromptBoxContextProvider, useChatPromptBoxContext } from './chat-prompt-box-context';
|
|
10
|
+
import type { ChatPromptBoxMode } from './chat-prompt-box-context';
|
|
10
11
|
import { ChatPromptBoxFocusPlugin } from './chat-prompt-box-focus-plugin';
|
|
12
|
+
import { ChatPromptBoxVoiceDecor } from './chat-prompt-box-voice';
|
|
13
|
+
import { voiceMotionKindsFor } from './chat-prompt-box-voice-motion';
|
|
14
|
+
import { useVoiceMotion } from './use-voice-motion';
|
|
15
|
+
|
|
16
|
+
export type { ChatPromptBoxMode } from './chat-prompt-box-context';
|
|
11
17
|
|
|
12
18
|
export interface ChatPromptBoxProps extends ChatInputProps {
|
|
13
19
|
/** Classes merged last onto the inner surface, the element that carries the box's background and padding. */
|
|
14
20
|
innerClassName?: string;
|
|
21
|
+
/**
|
|
22
|
+
* `text` (default) is the regular composer. The voice modes restyle the box for a voice
|
|
23
|
+
* conversation (Echo DS voice motion): `connecting`, `reconnecting` and `mic-muted` pulse the
|
|
24
|
+
* border grey with no light; `listening`, `user-speaking` and `answering` run two lights round
|
|
25
|
+
* the border once every 30s; `thinking` surges them with the 1.1s beat. The light above the box
|
|
26
|
+
* breathes while `listening`, beats while `thinking` and follows `voiceLevel` while
|
|
27
|
+
* `user-speaking` and `answering`. Moving from one mode to the next eases, nothing jumps.
|
|
28
|
+
*/
|
|
29
|
+
mode?: ChatPromptBoxMode;
|
|
30
|
+
/**
|
|
31
|
+
* How loud the speaker is, 0 to 1: the user in `user-speaking`, Swisper in `answering`. Update
|
|
32
|
+
* it every frame from the audio; the light rises in 0.08s and falls in 0.6s. Also drives the
|
|
33
|
+
* voice button's bars inside the box.
|
|
34
|
+
*/
|
|
35
|
+
voiceLevel?: number;
|
|
36
|
+
/**
|
|
37
|
+
* Swisper's voice by frequency while `answering`, 0 to 1 each, low to high (e.g. 16 bands from
|
|
38
|
+
* an `AnalyserNode`, 90Hz to 9kHz): low bands lift the middle of the light, high bands the sides.
|
|
39
|
+
*/
|
|
40
|
+
voiceBands?: readonly number[];
|
|
15
41
|
}
|
|
16
42
|
|
|
17
43
|
const COMPACT_INPUT_HEIGHT_THRESHOLD = 40;
|
|
@@ -28,96 +54,124 @@ function shouldSkipFocus(target: EventTarget | null): boolean {
|
|
|
28
54
|
|
|
29
55
|
/**
|
|
30
56
|
* The styled chat composer: a `ChatInput` with the box's border, background and focus glow. Sets
|
|
31
|
-
* `data-compact` on the root, which the parts read to move the actions under the text
|
|
57
|
+
* `data-compact` on the root, which the parts read to move the actions under the text, and
|
|
58
|
+
* `data-mode`, which switches the box between text and its voice states.
|
|
32
59
|
*/
|
|
33
|
-
export const ChatPromptBox = forwardRef<HTMLDivElement, ChatPromptBoxProps>(
|
|
34
|
-
|
|
35
|
-
|
|
60
|
+
export const ChatPromptBox = forwardRef<HTMLDivElement, ChatPromptBoxProps>(function ChatPromptBox(
|
|
61
|
+
{ mode = 'text', voiceLevel, ...props },
|
|
62
|
+
ref,
|
|
63
|
+
) {
|
|
64
|
+
const [isCompact, setIsCompact] = useState(true);
|
|
65
|
+
const [modes, setModes] = useState({ mode, previous: mode });
|
|
66
|
+
|
|
67
|
+
if (modes.mode !== mode) {
|
|
68
|
+
setModes({ mode, previous: modes.mode });
|
|
69
|
+
}
|
|
36
70
|
|
|
37
|
-
|
|
38
|
-
if (height > COMPACT_INPUT_HEIGHT_THRESHOLD) {
|
|
39
|
-
setIsCompact(false);
|
|
40
|
-
}
|
|
41
|
-
}, []);
|
|
71
|
+
const voiceMotion = useVoiceMotion(voiceMotionKindsFor(mode, modes.previous), voiceLevel);
|
|
42
72
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
73
|
+
const reportInputHeight = useCallback((height: number) => {
|
|
74
|
+
if (height > COMPACT_INPUT_HEIGHT_THRESHOLD) {
|
|
75
|
+
setIsCompact(false);
|
|
76
|
+
}
|
|
77
|
+
}, []);
|
|
46
78
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
resetCompact={resetCompact}
|
|
51
|
-
>
|
|
52
|
-
<ChatPromptBoxContainer ref={ref} data-compact={isCompact} {...props} />
|
|
53
|
-
</ChatPromptBoxContextProvider>
|
|
54
|
-
);
|
|
55
|
-
},
|
|
56
|
-
);
|
|
79
|
+
const resetCompact = useCallback(() => {
|
|
80
|
+
setIsCompact(true);
|
|
81
|
+
}, []);
|
|
57
82
|
|
|
58
|
-
|
|
83
|
+
return (
|
|
84
|
+
<ChatPromptBoxContextProvider
|
|
85
|
+
mode={mode}
|
|
86
|
+
voiceMotion={voiceMotion}
|
|
87
|
+
reportInputHeight={reportInputHeight}
|
|
88
|
+
resetCompact={resetCompact}
|
|
89
|
+
>
|
|
90
|
+
<ChatPromptBoxContainer ref={ref} data-compact={isCompact} data-mode={mode} {...props} />
|
|
91
|
+
</ChatPromptBoxContextProvider>
|
|
92
|
+
);
|
|
93
|
+
});
|
|
59
94
|
|
|
60
|
-
|
|
61
|
-
function ChatPromptBoxContainer(
|
|
62
|
-
{ className, innerClassName, children, onMouseDown, ...rest },
|
|
63
|
-
ref,
|
|
64
|
-
) {
|
|
65
|
-
const { focusInput } = useChatPromptBoxContext();
|
|
95
|
+
ChatPromptBox.displayName = 'ChatPromptBox';
|
|
66
96
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
97
|
+
const ChatPromptBoxContainer = forwardRef<
|
|
98
|
+
HTMLDivElement,
|
|
99
|
+
Omit<ChatPromptBoxProps, 'mode' | 'voiceLevel'>
|
|
100
|
+
>(function ChatPromptBoxContainer(
|
|
101
|
+
{ className, innerClassName, voiceBands, children, onMouseDown, ...rest },
|
|
102
|
+
ref,
|
|
103
|
+
) {
|
|
104
|
+
const { focusInput, mode } = useChatPromptBoxContext();
|
|
105
|
+
const isText = mode === 'text';
|
|
106
|
+
|
|
107
|
+
const handleMouseDown = useCallback(
|
|
108
|
+
(event: MouseEvent<HTMLDivElement>) => {
|
|
109
|
+
if (!shouldSkipFocus(event.target)) {
|
|
110
|
+
focusInput();
|
|
111
|
+
}
|
|
72
112
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
113
|
+
onMouseDown?.(event);
|
|
114
|
+
},
|
|
115
|
+
[focusInput, onMouseDown],
|
|
116
|
+
);
|
|
77
117
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
118
|
+
return (
|
|
119
|
+
<ChatInput
|
|
120
|
+
ref={ref}
|
|
121
|
+
data-slot="chat-prompt-box"
|
|
122
|
+
className={cn(
|
|
123
|
+
'group/chat-prompt-box',
|
|
124
|
+
'relative',
|
|
125
|
+
'cursor-text',
|
|
126
|
+
'p-px',
|
|
127
|
+
'rounded-[16px]',
|
|
128
|
+
isText
|
|
129
|
+
? [
|
|
130
|
+
'overflow-hidden',
|
|
131
|
+
// border color
|
|
132
|
+
'bg-transparent',
|
|
133
|
+
'hover:bg-promptbox-background-border-base',
|
|
134
|
+
'data-[focused=true]:bg-promptbox-background-border-base',
|
|
135
|
+
]
|
|
136
|
+
: // the voice ring and light are drawn by ChatPromptBoxVoiceDecor, the light outside the box
|
|
137
|
+
'isolate',
|
|
138
|
+
className,
|
|
139
|
+
)}
|
|
140
|
+
onMouseDown={handleMouseDown}
|
|
141
|
+
{...rest}
|
|
142
|
+
>
|
|
143
|
+
<div
|
|
144
|
+
data-slot="chat-prompt-box-inner"
|
|
82
145
|
className={cn(
|
|
83
|
-
'
|
|
84
|
-
'
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
146
|
+
'rounded-[15px]',
|
|
147
|
+
'p-[4px]',
|
|
148
|
+
isText
|
|
149
|
+
? [
|
|
150
|
+
// regular bg
|
|
151
|
+
'bg-[#171A20]',
|
|
152
|
+
// hover bg
|
|
153
|
+
'group-hover/chat-prompt-box:bg-[#252931]',
|
|
154
|
+
// focused bg
|
|
155
|
+
'group-data-[focused=true]/chat-prompt-box:bg-[linear-gradient(var(--color-promptbox-background-hover),var(--color-promptbox-background-hover)),var(--color-promptbox-background-base)]',
|
|
156
|
+
// focused & hover bg
|
|
157
|
+
'group-data-[focused=true]/chat-prompt-box:group-hover/chat-prompt-box:bg-[linear-gradient(var(--color-promptbox-background-hover),var(--color-promptbox-background-hover)),var(--color-promptbox-background-base)]',
|
|
158
|
+
]
|
|
159
|
+
: // voice: one background whatever the hover and focus state, above the ring
|
|
160
|
+
['relative z-[1]', 'bg-promptbox-background'],
|
|
161
|
+
innerClassName,
|
|
93
162
|
)}
|
|
94
|
-
onMouseDown={handleMouseDown}
|
|
95
|
-
{...rest}
|
|
96
163
|
>
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
className={cn(
|
|
100
|
-
'rounded-[15px]',
|
|
101
|
-
'p-[4px]',
|
|
102
|
-
// regular bg
|
|
103
|
-
'bg-[#171A20]',
|
|
104
|
-
// hover bg
|
|
105
|
-
'group-hover/chat-prompt-box:bg-[#252931]',
|
|
106
|
-
// focused bg
|
|
107
|
-
'group-data-[focused=true]/chat-prompt-box:bg-[linear-gradient(var(--color-promptbox-background-hover),var(--color-promptbox-background-hover)),var(--color-promptbox-background-base)]',
|
|
108
|
-
// focused & hover bg
|
|
109
|
-
'group-data-[focused=true]/chat-prompt-box:group-hover/chat-prompt-box:bg-[linear-gradient(var(--color-promptbox-background-hover),var(--color-promptbox-background-hover)),var(--color-promptbox-background-base)]',
|
|
110
|
-
innerClassName,
|
|
111
|
-
)}
|
|
112
|
-
>
|
|
113
|
-
{children}
|
|
114
|
-
</div>
|
|
164
|
+
{children}
|
|
165
|
+
</div>
|
|
115
166
|
|
|
167
|
+
{isText ? (
|
|
116
168
|
<ChatPromptBoxFocusRadials />
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
169
|
+
) : (
|
|
170
|
+
<ChatPromptBoxVoiceDecor mode={mode} bands={voiceBands} />
|
|
171
|
+
)}
|
|
172
|
+
</ChatInput>
|
|
173
|
+
);
|
|
174
|
+
});
|
|
121
175
|
|
|
122
176
|
ChatPromptBoxContainer.displayName = 'ChatPromptBoxContainer';
|
|
123
177
|
|
|
@@ -191,7 +245,7 @@ export function ChatPromptBoxContent({ className, children, ...rest }: ChatPromp
|
|
|
191
245
|
'relative',
|
|
192
246
|
'px-[8px] py-[8px]',
|
|
193
247
|
'flex flex-row gap-[8px]',
|
|
194
|
-
'group-data-[compact=false]/chat-prompt-box:pb-[calc(8px+4px+40px)]',
|
|
248
|
+
'group-data-[compact=false]/chat-prompt-box:pb-[calc(8px+4px+8px+40px)]',
|
|
195
249
|
className,
|
|
196
250
|
)}
|
|
197
251
|
{...rest}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from '@storybook/react-vite';
|
|
2
|
+
import { useEffect, useState } from 'react';
|
|
3
|
+
|
|
4
|
+
import { Button, IconButton, Typography } from '@swisper.ai/ui';
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
ChatPromptBoxVoiceBarIcon,
|
|
8
|
+
type ChatPromptBoxVoiceBarIconState,
|
|
9
|
+
} from './chat-prompt-box-voice-bar-icon';
|
|
10
|
+
|
|
11
|
+
const STATES: ChatPromptBoxVoiceBarIconState[] = [
|
|
12
|
+
'still',
|
|
13
|
+
'listening',
|
|
14
|
+
'voice-starts',
|
|
15
|
+
'thinking',
|
|
16
|
+
'voice',
|
|
17
|
+
'muted',
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
const STATE_LABELS: Record<ChatPromptBoxVoiceBarIconState, string> = {
|
|
21
|
+
still: 'Still (connecting)',
|
|
22
|
+
listening: 'Listening',
|
|
23
|
+
'voice-starts': 'Voice starts',
|
|
24
|
+
thinking: 'Thinking',
|
|
25
|
+
voice: 'Voice',
|
|
26
|
+
muted: 'Muted',
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const meta: Meta<typeof ChatPromptBoxVoiceBarIcon> = {
|
|
30
|
+
title: 'Chat/ChatPromptBox/Components/ChatPromptBoxVoiceBarIcon',
|
|
31
|
+
component: ChatPromptBoxVoiceBarIcon,
|
|
32
|
+
argTypes: {
|
|
33
|
+
state: {
|
|
34
|
+
control: 'select',
|
|
35
|
+
options: STATES,
|
|
36
|
+
description:
|
|
37
|
+
'What the voice session is doing; each state has its own motion. `voice-starts` kicks with the sound, then settles into Listening by itself. Unset inside a voice-mode `ChatPromptBox`, the bars follow the box.',
|
|
38
|
+
table: { defaultValue: { summary: 'still (outside a box)' } },
|
|
39
|
+
},
|
|
40
|
+
level: {
|
|
41
|
+
control: { type: 'range', min: 0, max: 1, step: 0.01 },
|
|
42
|
+
description:
|
|
43
|
+
'How loud the speaker is, 0 (silence) to 1 (16px bars). Only read when `state` is `voice`; update it every frame from the audio.',
|
|
44
|
+
table: { defaultValue: { summary: '0' } },
|
|
45
|
+
},
|
|
46
|
+
className: { description: 'Extra classes merged onto the 20×20 box.' },
|
|
47
|
+
},
|
|
48
|
+
args: {
|
|
49
|
+
state: 'listening',
|
|
50
|
+
level: 0.5,
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export default meta;
|
|
55
|
+
type Story = StoryObj<typeof ChatPromptBoxVoiceBarIcon>;
|
|
56
|
+
|
|
57
|
+
/** A level that moves like speech: a slow swell with some jitter, updated every frame. */
|
|
58
|
+
function useSimulatedLevel() {
|
|
59
|
+
const [level, setLevel] = useState(0);
|
|
60
|
+
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
let frame = 0;
|
|
63
|
+
const tick = (time: number) => {
|
|
64
|
+
const swell = 0.5 + 0.35 * Math.sin(time / 260) * Math.sin(time / 710);
|
|
65
|
+
setLevel(Math.min(1, Math.max(0, swell + (Math.random() - 0.5) * 0.2)));
|
|
66
|
+
frame = requestAnimationFrame(tick);
|
|
67
|
+
};
|
|
68
|
+
frame = requestAnimationFrame(tick);
|
|
69
|
+
return () => cancelAnimationFrame(frame);
|
|
70
|
+
}, []);
|
|
71
|
+
|
|
72
|
+
return level;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Try each state and, for `voice`, drag `level`. Switch `state` back to `voice-starts` to replay it. */
|
|
76
|
+
export const Playground: Story = {
|
|
77
|
+
render: (args) => (
|
|
78
|
+
<IconButton size="md" variant="tertiary" aria-label="Voice mode">
|
|
79
|
+
<ChatPromptBoxVoiceBarIcon key={args.state} {...args} />
|
|
80
|
+
</IconButton>
|
|
81
|
+
),
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
function AllStatesGallery() {
|
|
85
|
+
const level = useSimulatedLevel();
|
|
86
|
+
|
|
87
|
+
return (
|
|
88
|
+
<div className="flex flex-col gap-12">
|
|
89
|
+
{STATES.map((state) => (
|
|
90
|
+
<div key={state} className="flex items-center gap-12">
|
|
91
|
+
<IconButton
|
|
92
|
+
size="md"
|
|
93
|
+
variant="tertiary"
|
|
94
|
+
aria-label={`Voice mode: ${STATE_LABELS[state]}`}
|
|
95
|
+
>
|
|
96
|
+
<ChatPromptBoxVoiceBarIcon state={state} level={level} />
|
|
97
|
+
</IconButton>
|
|
98
|
+
<Typography variant="body-sm">{STATE_LABELS[state]}</Typography>
|
|
99
|
+
</div>
|
|
100
|
+
))}
|
|
101
|
+
</div>
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Every state in the voice button. Voice follows a simulated level; Voice starts plays once (reload
|
|
107
|
+
* the story to replay it). With reduced motion the bars rest on each state's Base heights.
|
|
108
|
+
*/
|
|
109
|
+
export const AllStates: Story = {
|
|
110
|
+
parameters: { controls: { disable: true } },
|
|
111
|
+
render: () => <AllStatesGallery />,
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
function VoiceFollowsLevelDemo() {
|
|
115
|
+
const level = useSimulatedLevel();
|
|
116
|
+
|
|
117
|
+
return (
|
|
118
|
+
<div className="flex items-center gap-12">
|
|
119
|
+
<IconButton size="md" variant="tertiary" aria-label="Voice mode">
|
|
120
|
+
<ChatPromptBoxVoiceBarIcon state="voice" level={level} />
|
|
121
|
+
</IconButton>
|
|
122
|
+
<Typography variant="body-sm" className="tabular-nums">
|
|
123
|
+
level {level.toFixed(2)}
|
|
124
|
+
</Typography>
|
|
125
|
+
</div>
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Voice: the bars follow whoever speaks, from 3.5px (silence) to 16px (loud), each flickering on its own. */
|
|
130
|
+
export const VoiceFollowsLevel: Story = {
|
|
131
|
+
parameters: { controls: { disable: true } },
|
|
132
|
+
render: () => <VoiceFollowsLevelDemo />,
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
function VoiceStartsDemo() {
|
|
136
|
+
const [run, setRun] = useState(0);
|
|
137
|
+
|
|
138
|
+
return (
|
|
139
|
+
<div className="flex items-center gap-12">
|
|
140
|
+
<IconButton size="md" variant="tertiary" aria-label="Voice mode">
|
|
141
|
+
<ChatPromptBoxVoiceBarIcon key={run} state="voice-starts" />
|
|
142
|
+
</IconButton>
|
|
143
|
+
<Typography variant="body-sm">{STATE_LABELS['voice-starts']}</Typography>
|
|
144
|
+
<Button size="sm" variant="secondary" onClick={() => setRun((value) => value + 1)}>
|
|
145
|
+
Replay
|
|
146
|
+
</Button>
|
|
147
|
+
</div>
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Listening begins: the bars jump with the "Voice starts" sound's two notes, then settle into Listening. */
|
|
152
|
+
export const VoiceStartsToListening: Story = {
|
|
153
|
+
parameters: { controls: { disable: true } },
|
|
154
|
+
render: () => <VoiceStartsDemo />,
|
|
155
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { forwardRef, type ComponentProps } from 'react';
|
|
2
|
+
|
|
3
|
+
import { cn } from '@swisper.ai/web-core';
|
|
4
|
+
|
|
5
|
+
import { useOptionalChatPromptBoxContext } from '../../chat-prompt-box-context';
|
|
6
|
+
import { VOICE_MOTION_BY_MODE, type VoiceBarsKind } from '../../chat-prompt-box-voice-motion';
|
|
7
|
+
import { useVoiceMotion, useVoiceMotionRef } from '../../use-voice-motion';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* What the voice session is doing. `still`: connecting or reconnecting, five 2px dots.
|
|
11
|
+
* `listening`: low and gentle, one sway every 3.9s. `voice-starts`: Listening with the two kicks
|
|
12
|
+
* of the "Voice starts" sound, then Listening. `thinking`: one wave per 1.1s beat, entering on the
|
|
13
|
+
* left. `voice`: follows `level`. `muted`: the mic is off, low, one sway every 5.7s.
|
|
14
|
+
*/
|
|
15
|
+
export type ChatPromptBoxVoiceBarIconState = VoiceBarsKind;
|
|
16
|
+
|
|
17
|
+
const BARS = [0, 1, 2, 3, 4] as const;
|
|
18
|
+
|
|
19
|
+
export interface ChatPromptBoxVoiceBarIconProps extends Omit<ComponentProps<'span'>, 'children'> {
|
|
20
|
+
/**
|
|
21
|
+
* What the voice session is doing; each state has its own motion. Unset inside a voice-mode
|
|
22
|
+
* `ChatPromptBox`, the bars follow the box's mode and `voiceLevel` on the box's clock (so in
|
|
23
|
+
* Thinking they beat with the border); elsewhere it defaults to `still`.
|
|
24
|
+
*/
|
|
25
|
+
state?: ChatPromptBoxVoiceBarIconState;
|
|
26
|
+
/**
|
|
27
|
+
* How loud the speaker is, 0 (silence) to 1 (a loud voice, 16px bars). Only read when `state` is
|
|
28
|
+
* `voice`; update it every frame from the audio.
|
|
29
|
+
*/
|
|
30
|
+
level?: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The five live bars inside the voice button (Echo DS `_Voice-bars`), moved by the voice clock in
|
|
35
|
+
* 0.5px steps. Decorative (`aria-hidden`): the button around it carries the label. Sets
|
|
36
|
+
* `data-state`. `voice-starts` settles back into Listening by itself (about 1.5s); remount it
|
|
37
|
+
* (`key`) to replay. With reduced motion the bars rest on each state's Base heights; `voice` still
|
|
38
|
+
* follows `level`.
|
|
39
|
+
*/
|
|
40
|
+
export const ChatPromptBoxVoiceBarIcon = forwardRef<
|
|
41
|
+
HTMLSpanElement,
|
|
42
|
+
ChatPromptBoxVoiceBarIconProps
|
|
43
|
+
>(function ChatPromptBoxVoiceBarIcon({ state, level, className, ...rest }, ref) {
|
|
44
|
+
const context = useOptionalChatPromptBoxContext();
|
|
45
|
+
const box = state === undefined && context && context.mode !== 'text' ? context : null;
|
|
46
|
+
const ownState = state ?? 'still';
|
|
47
|
+
|
|
48
|
+
const motion = useVoiceMotion(
|
|
49
|
+
box ? null : { border: 'pulse', light: 'none', bars: ownState },
|
|
50
|
+
level,
|
|
51
|
+
);
|
|
52
|
+
const motionRef = useVoiceMotionRef(box ? box.voiceMotion : motion, 'bars', ref);
|
|
53
|
+
const shownState = box && box.mode !== 'text' ? VOICE_MOTION_BY_MODE[box.mode].bars : ownState;
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<span
|
|
57
|
+
ref={motionRef}
|
|
58
|
+
aria-hidden
|
|
59
|
+
data-slot="voice-bar-icon"
|
|
60
|
+
data-state={shownState}
|
|
61
|
+
className={cn(
|
|
62
|
+
'inline-flex shrink-0 items-center justify-center',
|
|
63
|
+
'size-20 gap-promptbox-voice-bars-gap',
|
|
64
|
+
className,
|
|
65
|
+
)}
|
|
66
|
+
{...rest}
|
|
67
|
+
>
|
|
68
|
+
{BARS.map((index) => (
|
|
69
|
+
<span
|
|
70
|
+
key={index}
|
|
71
|
+
data-slot="voice-bar-icon-bar"
|
|
72
|
+
className="w-2 shrink-0 rounded-promptbox-voice-bars bg-promptbox-voice-bars-bar"
|
|
73
|
+
style={{ height: `var(--voice-bar-${index}, 2px)` }}
|
|
74
|
+
/>
|
|
75
|
+
))}
|
|
76
|
+
</span>
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
ChatPromptBoxVoiceBarIcon.displayName = 'ChatPromptBoxVoiceBarIcon';
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from '@storybook/react-vite';
|
|
2
|
+
import { useEffect, useState } from 'react';
|
|
3
|
+
import { fn } from 'storybook/test';
|
|
4
|
+
|
|
5
|
+
import { Typography } from '@swisper.ai/ui';
|
|
6
|
+
|
|
7
|
+
import type { ChatPromptBoxVoiceBarIconState } from '../chat-prompt-box-voice-bar-icon';
|
|
8
|
+
|
|
9
|
+
import { ChatPromptBoxVoiceButton } from './chat-prompt-box-voice-button';
|
|
10
|
+
|
|
11
|
+
const STATES: ChatPromptBoxVoiceBarIconState[] = [
|
|
12
|
+
'still',
|
|
13
|
+
'listening',
|
|
14
|
+
'voice-starts',
|
|
15
|
+
'thinking',
|
|
16
|
+
'voice',
|
|
17
|
+
'muted',
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
const meta: Meta<typeof ChatPromptBoxVoiceButton> = {
|
|
21
|
+
title: 'Chat/ChatPromptBox/Components/ChatPromptBoxVoiceButton',
|
|
22
|
+
component: ChatPromptBoxVoiceButton,
|
|
23
|
+
argTypes: {
|
|
24
|
+
show: {
|
|
25
|
+
control: 'inline-radio',
|
|
26
|
+
options: ['bars', 'send'],
|
|
27
|
+
description:
|
|
28
|
+
'`bars`: the voice bars on a pressed Prominent button, Close on hover or keyboard focus. `send`: the Primary send button, while the field has text.',
|
|
29
|
+
table: { defaultValue: { summary: 'bars' } },
|
|
30
|
+
},
|
|
31
|
+
state: {
|
|
32
|
+
control: 'select',
|
|
33
|
+
options: STATES,
|
|
34
|
+
description: 'What the voice session is doing; passed to `ChatPromptBoxVoiceBarIcon`.',
|
|
35
|
+
table: { defaultValue: { summary: 'still' } },
|
|
36
|
+
},
|
|
37
|
+
level: {
|
|
38
|
+
control: { type: 'range', min: 0, max: 1, step: 0.01 },
|
|
39
|
+
description: 'How loud the speaker is, 0 to 1. Only read when `state` is `voice`.',
|
|
40
|
+
},
|
|
41
|
+
onClick: { description: 'Ends voice mode (`bars`) or sends the message (`send`).' },
|
|
42
|
+
},
|
|
43
|
+
args: {
|
|
44
|
+
show: 'bars',
|
|
45
|
+
state: 'listening',
|
|
46
|
+
level: 0.5,
|
|
47
|
+
onClick: fn(),
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export default meta;
|
|
52
|
+
type Story = StoryObj<typeof ChatPromptBoxVoiceButton>;
|
|
53
|
+
|
|
54
|
+
/** Hover the bars button (or Tab to it) to see Close. */
|
|
55
|
+
export const Playground: Story = {
|
|
56
|
+
render: (args) => <ChatPromptBoxVoiceButton key={args.state} {...args} />,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/** The Echo DS `_Voice-button` row: the six bar states, then Send. Hover any bars button for Close. */
|
|
60
|
+
export const AllShows: Story = {
|
|
61
|
+
parameters: { controls: { disable: true } },
|
|
62
|
+
render: () => (
|
|
63
|
+
<div className="flex items-center gap-24">
|
|
64
|
+
{STATES.map((state) => (
|
|
65
|
+
<ChatPromptBoxVoiceButton key={state} state={state} level={0.6} />
|
|
66
|
+
))}
|
|
67
|
+
<ChatPromptBoxVoiceButton show="send" />
|
|
68
|
+
</div>
|
|
69
|
+
),
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/** A level that moves like speech: a slow swell with some jitter, updated every frame. */
|
|
73
|
+
function useSimulatedLevel() {
|
|
74
|
+
const [level, setLevel] = useState(0);
|
|
75
|
+
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
let frame = 0;
|
|
78
|
+
const tick = (time: number) => {
|
|
79
|
+
const swell = 0.5 + 0.35 * Math.sin(time / 260) * Math.sin(time / 710);
|
|
80
|
+
setLevel(Math.min(1, Math.max(0, swell + (Math.random() - 0.5) * 0.2)));
|
|
81
|
+
frame = requestAnimationFrame(tick);
|
|
82
|
+
};
|
|
83
|
+
frame = requestAnimationFrame(tick);
|
|
84
|
+
return () => cancelAnimationFrame(frame);
|
|
85
|
+
}, []);
|
|
86
|
+
|
|
87
|
+
return level;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function VoiceFollowsLevelDemo() {
|
|
91
|
+
const level = useSimulatedLevel();
|
|
92
|
+
|
|
93
|
+
return (
|
|
94
|
+
<div className="flex items-center gap-12">
|
|
95
|
+
<ChatPromptBoxVoiceButton state="voice" level={level} />
|
|
96
|
+
<Typography variant="body-sm" className="tabular-nums">
|
|
97
|
+
level {level.toFixed(2)}
|
|
98
|
+
</Typography>
|
|
99
|
+
</div>
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Voice: the bars follow whoever speaks, frame by frame. */
|
|
104
|
+
export const VoiceFollowsLevel: Story = {
|
|
105
|
+
parameters: { controls: { disable: true } },
|
|
106
|
+
render: () => <VoiceFollowsLevelDemo />,
|
|
107
|
+
};
|