@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
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from '@storybook/react-vite';
|
|
2
|
+
import { useState } from 'react';
|
|
3
|
+
import { fn } from 'storybook/test';
|
|
4
|
+
|
|
5
|
+
import { ChatPromptBoxVoiceStateWord } from '../../chat-prompt-box-voice';
|
|
6
|
+
import type { ChatPromptBoxVoiceBarIconState } from '../chat-prompt-box-voice-bar-icon';
|
|
7
|
+
import { ChatPromptBoxVoiceButton } from '../chat-prompt-box-voice-button';
|
|
8
|
+
import { ChatPromptBoxVoiceMicButton } from '../chat-prompt-box-voice-mic-button';
|
|
9
|
+
import { ChatPromptBoxVoiceSoundButton } from '../chat-prompt-box-voice-sound-button';
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
ChatPromptBoxVoicePanel,
|
|
13
|
+
type ChatPromptBoxVoicePanelProps,
|
|
14
|
+
} from './chat-prompt-box-voice-panel';
|
|
15
|
+
|
|
16
|
+
const BAR_STATES: ChatPromptBoxVoiceBarIconState[] = [
|
|
17
|
+
'still',
|
|
18
|
+
'listening',
|
|
19
|
+
'voice-starts',
|
|
20
|
+
'thinking',
|
|
21
|
+
'voice',
|
|
22
|
+
'muted',
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
type PlaygroundArgs = ChatPromptBoxVoicePanelProps & {
|
|
26
|
+
micMuted: boolean;
|
|
27
|
+
soundMuted: boolean;
|
|
28
|
+
stateWord: string;
|
|
29
|
+
bars: ChatPromptBoxVoiceBarIconState;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const meta: Meta<PlaygroundArgs> = {
|
|
33
|
+
title: 'Chat/ChatPromptBox/Components/ChatPromptBoxVoicePanel',
|
|
34
|
+
component: ChatPromptBoxVoicePanel,
|
|
35
|
+
subcomponents: {
|
|
36
|
+
ChatPromptBoxVoiceMicButton,
|
|
37
|
+
ChatPromptBoxVoiceSoundButton,
|
|
38
|
+
ChatPromptBoxVoiceStateWord,
|
|
39
|
+
ChatPromptBoxVoiceButton,
|
|
40
|
+
},
|
|
41
|
+
argTypes: {
|
|
42
|
+
children: { table: { disable: true } },
|
|
43
|
+
device: {
|
|
44
|
+
control: 'inline-radio',
|
|
45
|
+
options: ['desktop', 'mobile'],
|
|
46
|
+
description:
|
|
47
|
+
'`desktop`: 8px padding and gaps, 104px state word. `mobile`: 4px, fills its width, the word takes the rest.',
|
|
48
|
+
table: { defaultValue: { summary: 'desktop' } },
|
|
49
|
+
},
|
|
50
|
+
micMuted: {
|
|
51
|
+
control: 'boolean',
|
|
52
|
+
description: 'Story only: `muted` of `ChatPromptBoxVoiceMicButton`.',
|
|
53
|
+
},
|
|
54
|
+
soundMuted: {
|
|
55
|
+
control: 'boolean',
|
|
56
|
+
description: 'Story only: `muted` of `ChatPromptBoxVoiceSoundButton`.',
|
|
57
|
+
},
|
|
58
|
+
stateWord: {
|
|
59
|
+
control: 'text',
|
|
60
|
+
description: 'Story only: children of `ChatPromptBoxVoiceStateWord`.',
|
|
61
|
+
},
|
|
62
|
+
bars: {
|
|
63
|
+
control: 'select',
|
|
64
|
+
options: BAR_STATES,
|
|
65
|
+
description: 'Story only: `state` of `ChatPromptBoxVoiceButton`.',
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
args: {
|
|
69
|
+
device: 'desktop',
|
|
70
|
+
micMuted: false,
|
|
71
|
+
soundMuted: false,
|
|
72
|
+
stateWord: 'Listening',
|
|
73
|
+
bars: 'listening',
|
|
74
|
+
},
|
|
75
|
+
decorators: [
|
|
76
|
+
(Story, { args }) => (
|
|
77
|
+
<div className={args.device === 'mobile' ? 'w-[272px]' : undefined}>
|
|
78
|
+
<Story />
|
|
79
|
+
</div>
|
|
80
|
+
),
|
|
81
|
+
],
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export default meta;
|
|
85
|
+
type Story = StoryObj<PlaygroundArgs>;
|
|
86
|
+
|
|
87
|
+
export const Playground: Story = {
|
|
88
|
+
render: ({ micMuted, soundMuted, stateWord, bars, children: _children, ...args }) => (
|
|
89
|
+
<ChatPromptBoxVoicePanel {...args}>
|
|
90
|
+
<ChatPromptBoxVoiceMicButton muted={micMuted} onMutedChange={fn()} />
|
|
91
|
+
<ChatPromptBoxVoiceSoundButton muted={soundMuted} onMutedChange={fn()} />
|
|
92
|
+
<ChatPromptBoxVoiceStateWord>{stateWord}</ChatPromptBoxVoiceStateWord>
|
|
93
|
+
<ChatPromptBoxVoiceButton state={bars} onClick={fn()} />
|
|
94
|
+
</ChatPromptBoxVoicePanel>
|
|
95
|
+
),
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const VARIANTS = [
|
|
99
|
+
{ micMuted: false, soundMuted: false },
|
|
100
|
+
{ micMuted: false, soundMuted: true },
|
|
101
|
+
{ micMuted: true, soundMuted: false },
|
|
102
|
+
{ micMuted: true, soundMuted: true },
|
|
103
|
+
];
|
|
104
|
+
|
|
105
|
+
/** The Echo DS `_Voice-panel` frame: desktop on top, mobile below; mic and sound on and off. */
|
|
106
|
+
export const AllVariants: Story = {
|
|
107
|
+
parameters: { controls: { disable: true } },
|
|
108
|
+
render: () => (
|
|
109
|
+
<div className="flex flex-col gap-24">
|
|
110
|
+
{(['desktop', 'mobile'] as const).map((device) => (
|
|
111
|
+
<div key={device} className="flex flex-wrap items-center gap-24">
|
|
112
|
+
{VARIANTS.map(({ micMuted, soundMuted }) => (
|
|
113
|
+
<div
|
|
114
|
+
key={`${micMuted}-${soundMuted}`}
|
|
115
|
+
className={device === 'mobile' ? 'w-[272px]' : undefined}
|
|
116
|
+
>
|
|
117
|
+
<ChatPromptBoxVoicePanel device={device}>
|
|
118
|
+
<ChatPromptBoxVoiceMicButton muted={micMuted} />
|
|
119
|
+
<ChatPromptBoxVoiceSoundButton muted={soundMuted} />
|
|
120
|
+
<ChatPromptBoxVoiceStateWord>Listening</ChatPromptBoxVoiceStateWord>
|
|
121
|
+
<ChatPromptBoxVoiceButton state="listening" />
|
|
122
|
+
</ChatPromptBoxVoicePanel>
|
|
123
|
+
</div>
|
|
124
|
+
))}
|
|
125
|
+
</div>
|
|
126
|
+
))}
|
|
127
|
+
</div>
|
|
128
|
+
),
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
function TogglesDemo() {
|
|
132
|
+
const [micMuted, setMicMuted] = useState(false);
|
|
133
|
+
const [soundMuted, setSoundMuted] = useState(false);
|
|
134
|
+
|
|
135
|
+
return (
|
|
136
|
+
<ChatPromptBoxVoicePanel>
|
|
137
|
+
<ChatPromptBoxVoiceMicButton muted={micMuted} onMutedChange={setMicMuted} />
|
|
138
|
+
<ChatPromptBoxVoiceSoundButton muted={soundMuted} onMutedChange={setSoundMuted} />
|
|
139
|
+
<ChatPromptBoxVoiceStateWord>Listening</ChatPromptBoxVoiceStateWord>
|
|
140
|
+
<ChatPromptBoxVoiceButton state={micMuted ? 'muted' : 'listening'} />
|
|
141
|
+
</ChatPromptBoxVoicePanel>
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Click mic and sound: the icons swap and the bars go to Muted with the mic. */
|
|
146
|
+
export const Toggles: Story = {
|
|
147
|
+
parameters: { controls: { disable: true } },
|
|
148
|
+
render: () => <TogglesDemo />,
|
|
149
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { forwardRef, type ComponentPropsWithoutRef, type ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
import { cn } from '@swisper.ai/web-core';
|
|
4
|
+
|
|
5
|
+
/** `desktop`: 8px padding and gaps, flush in the box's corner. `mobile`: 4px, fills its width. */
|
|
6
|
+
export type ChatPromptBoxVoicePanelDevice = 'desktop' | 'mobile';
|
|
7
|
+
|
|
8
|
+
export interface ChatPromptBoxVoicePanelProps extends ComponentPropsWithoutRef<'div'> {
|
|
9
|
+
/** The controls: `ChatPromptBoxVoiceMicButton`, `ChatPromptBoxVoiceSoundButton`, `ChatPromptBoxVoiceStateWord`, `ChatPromptBoxVoiceButton`. */
|
|
10
|
+
children: ReactNode;
|
|
11
|
+
/** Sets the padding, gaps, height and the state word's width. Defaults to `desktop`. */
|
|
12
|
+
device?: ChatPromptBoxVoicePanelDevice;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The voice controls inside the prompt box while voice is on (Echo DS `_Voice-panel`): a
|
|
17
|
+
* `role="group"` for `ChatPromptBoxVoiceMicButton`, `ChatPromptBoxVoiceSoundButton`,
|
|
18
|
+
* `ChatPromptBoxVoiceStateWord` and `ChatPromptBoxVoiceButton`, in that order. Put it in
|
|
19
|
+
* `ChatPromptBoxActionsRight`; on desktop it sits flush in the box's corner. The background is the
|
|
20
|
+
* same in every state. `aria-label` defaults to "Voice".
|
|
21
|
+
*/
|
|
22
|
+
export const ChatPromptBoxVoicePanel = forwardRef<HTMLDivElement, ChatPromptBoxVoicePanelProps>(
|
|
23
|
+
function ChatPromptBoxVoicePanel({ device = 'desktop', className, children, ...rest }, ref) {
|
|
24
|
+
return (
|
|
25
|
+
<div
|
|
26
|
+
ref={ref}
|
|
27
|
+
aria-label="Voice"
|
|
28
|
+
data-slot="chat-prompt-box-voice-panel"
|
|
29
|
+
data-testid="chat-prompt-box-voice-panel"
|
|
30
|
+
data-device={device}
|
|
31
|
+
role="group"
|
|
32
|
+
className={cn(
|
|
33
|
+
'group/voice-panel flex items-center',
|
|
34
|
+
'rounded-promptbox-voice-panel',
|
|
35
|
+
'bg-promptbox-voice-panel-background',
|
|
36
|
+
device === 'desktop'
|
|
37
|
+
? [
|
|
38
|
+
'h-[56px] -my-[8px] -mr-[8px]',
|
|
39
|
+
'p-promptbox-voice-panel-desktop gap-promptbox-voice-panel-gap-desktop',
|
|
40
|
+
]
|
|
41
|
+
: [
|
|
42
|
+
'h-[48px] w-full',
|
|
43
|
+
'p-promptbox-voice-panel-mobile gap-promptbox-voice-panel-gap-mobile',
|
|
44
|
+
],
|
|
45
|
+
className,
|
|
46
|
+
)}
|
|
47
|
+
{...rest}
|
|
48
|
+
>
|
|
49
|
+
{children}
|
|
50
|
+
</div>
|
|
51
|
+
);
|
|
52
|
+
},
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
ChatPromptBoxVoicePanel.displayName = 'ChatPromptBoxVoicePanel';
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from '@storybook/react-vite';
|
|
2
|
+
import { useState } from 'react';
|
|
3
|
+
import { fn } from 'storybook/test';
|
|
4
|
+
|
|
5
|
+
import { ChatPromptBoxVoiceSoundButton } from './chat-prompt-box-voice-sound-button';
|
|
6
|
+
|
|
7
|
+
const meta: Meta<typeof ChatPromptBoxVoiceSoundButton> = {
|
|
8
|
+
title: 'Chat/ChatPromptBox/Components/ChatPromptBoxVoiceSoundButton',
|
|
9
|
+
component: ChatPromptBoxVoiceSoundButton,
|
|
10
|
+
argTypes: {
|
|
11
|
+
muted: {
|
|
12
|
+
control: 'boolean',
|
|
13
|
+
description: 'Shows the sound-off icon; the button is `aria-pressed`.',
|
|
14
|
+
table: { defaultValue: { summary: 'false' } },
|
|
15
|
+
},
|
|
16
|
+
onMutedChange: { description: 'Called with the new value when the button is clicked.' },
|
|
17
|
+
},
|
|
18
|
+
args: {
|
|
19
|
+
muted: false,
|
|
20
|
+
onMutedChange: fn(),
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export default meta;
|
|
25
|
+
type Story = StoryObj<typeof ChatPromptBoxVoiceSoundButton>;
|
|
26
|
+
|
|
27
|
+
export const Playground: Story = {};
|
|
28
|
+
|
|
29
|
+
/** On, then off. */
|
|
30
|
+
export const AllStates: Story = {
|
|
31
|
+
parameters: { controls: { disable: true } },
|
|
32
|
+
render: () => (
|
|
33
|
+
<div className="flex items-center gap-24">
|
|
34
|
+
<ChatPromptBoxVoiceSoundButton />
|
|
35
|
+
<ChatPromptBoxVoiceSoundButton muted />
|
|
36
|
+
</div>
|
|
37
|
+
),
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
function ToggleDemo() {
|
|
41
|
+
const [muted, setMuted] = useState(false);
|
|
42
|
+
|
|
43
|
+
return <ChatPromptBoxVoiceSoundButton muted={muted} onMutedChange={setMuted} />;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Click it: the icon swaps and `aria-pressed` flips. */
|
|
47
|
+
export const Toggle: Story = {
|
|
48
|
+
parameters: { controls: { disable: true } },
|
|
49
|
+
render: () => <ToggleDemo />,
|
|
50
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { forwardRef, type MouseEvent } from 'react';
|
|
2
|
+
|
|
3
|
+
import { VolumeOffIcon, VolumeOnIcon } from '@swisper.ai/icons';
|
|
4
|
+
import { IconButton, type IconButtonProps } from '@swisper.ai/ui';
|
|
5
|
+
|
|
6
|
+
export interface ChatPromptBoxVoiceSoundButtonProps extends Omit<
|
|
7
|
+
IconButtonProps,
|
|
8
|
+
'variant' | 'size' | 'children' | 'aria-pressed'
|
|
9
|
+
> {
|
|
10
|
+
/** The sound is off: shows the sound-off icon and presses the button. Defaults to `false`. */
|
|
11
|
+
muted?: boolean;
|
|
12
|
+
/** Called with the new value when the button is clicked. */
|
|
13
|
+
onMutedChange?: (muted: boolean) => void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The sound toggle of the voice panel (Echo DS `_Voice-panel`): a Tertiary `aria-pressed` button
|
|
18
|
+
* that swaps to the sound-off icon while `muted`. Put it in `ChatPromptBoxVoicePanel`.
|
|
19
|
+
* `aria-label` defaults to "Mute sound".
|
|
20
|
+
*/
|
|
21
|
+
export const ChatPromptBoxVoiceSoundButton = forwardRef<
|
|
22
|
+
HTMLButtonElement | HTMLSpanElement,
|
|
23
|
+
ChatPromptBoxVoiceSoundButtonProps
|
|
24
|
+
>(function ChatPromptBoxVoiceSoundButton({ muted = false, onMutedChange, onClick, ...rest }, ref) {
|
|
25
|
+
const handleClick = (event: MouseEvent<HTMLButtonElement>) => {
|
|
26
|
+
onClick?.(event);
|
|
27
|
+
onMutedChange?.(!muted);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<IconButton
|
|
32
|
+
ref={ref}
|
|
33
|
+
variant="tertiary"
|
|
34
|
+
size="lg"
|
|
35
|
+
aria-label="Mute sound"
|
|
36
|
+
aria-pressed={muted}
|
|
37
|
+
data-slot="chat-prompt-box-voice-sound-button"
|
|
38
|
+
onClick={handleClick}
|
|
39
|
+
{...rest}
|
|
40
|
+
>
|
|
41
|
+
{muted ? <VolumeOffIcon /> : <VolumeOnIcon />}
|
|
42
|
+
</IconButton>
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
ChatPromptBoxVoiceSoundButton.displayName = 'ChatPromptBoxVoiceSoundButton';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from './chat-prompt-box-voice-bar-icon';
|
|
2
|
+
export * from './chat-prompt-box-voice-button';
|
|
3
|
+
export * from './chat-prompt-box-voice-light';
|
|
4
|
+
export * from './chat-prompt-box-voice-mic-button';
|
|
5
|
+
export * from './chat-prompt-box-voice-panel';
|
|
6
|
+
export * from './chat-prompt-box-voice-sound-button';
|
|
@@ -7,6 +7,7 @@ export {
|
|
|
7
7
|
ChatPromptBoxActionsRight,
|
|
8
8
|
} from './chat-prompt-box';
|
|
9
9
|
export type {
|
|
10
|
+
ChatPromptBoxMode,
|
|
10
11
|
ChatPromptBoxProps,
|
|
11
12
|
ChatPromptBoxInputProps,
|
|
12
13
|
ChatPromptBoxToolbarProps,
|
|
@@ -14,3 +15,25 @@ export type {
|
|
|
14
15
|
ChatPromptBoxActionsLeftProps,
|
|
15
16
|
ChatPromptBoxActionsRightProps,
|
|
16
17
|
} from './chat-prompt-box';
|
|
18
|
+
export { ChatPromptBoxVoiceStateWord } from './chat-prompt-box-voice';
|
|
19
|
+
export type { ChatPromptBoxVoiceStateWordProps } from './chat-prompt-box-voice';
|
|
20
|
+
export {
|
|
21
|
+
ChatPromptBoxVoiceBarIcon,
|
|
22
|
+
ChatPromptBoxVoiceButton,
|
|
23
|
+
ChatPromptBoxVoiceLight,
|
|
24
|
+
ChatPromptBoxVoiceMicButton,
|
|
25
|
+
ChatPromptBoxVoicePanel,
|
|
26
|
+
ChatPromptBoxVoiceSoundButton,
|
|
27
|
+
} from './components';
|
|
28
|
+
export type {
|
|
29
|
+
ChatPromptBoxVoiceBarIconProps,
|
|
30
|
+
ChatPromptBoxVoiceBarIconState,
|
|
31
|
+
ChatPromptBoxVoiceButtonProps,
|
|
32
|
+
ChatPromptBoxVoiceButtonShow,
|
|
33
|
+
ChatPromptBoxVoiceLightProps,
|
|
34
|
+
ChatPromptBoxVoiceLightState,
|
|
35
|
+
ChatPromptBoxVoiceMicButtonProps,
|
|
36
|
+
ChatPromptBoxVoicePanelDevice,
|
|
37
|
+
ChatPromptBoxVoicePanelProps,
|
|
38
|
+
ChatPromptBoxVoiceSoundButtonProps,
|
|
39
|
+
} from './components';
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, type Ref } from 'react';
|
|
2
|
+
|
|
3
|
+
import { useReducedMotion } from '@swisper.ai/web-core';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
createVoiceMotion,
|
|
7
|
+
restVoiceMotion,
|
|
8
|
+
voiceLightVars,
|
|
9
|
+
type VoiceMotion,
|
|
10
|
+
type VoiceMotionFrame,
|
|
11
|
+
type VoiceMotionKinds,
|
|
12
|
+
} from './chat-prompt-box-voice-motion';
|
|
13
|
+
|
|
14
|
+
const useIsomorphicLayoutEffect = typeof window === 'undefined' ? useEffect : useLayoutEffect;
|
|
15
|
+
|
|
16
|
+
/** The parts the clock moves: the border ring, the light behind the box and the voice bars. */
|
|
17
|
+
export type VoiceMotionChannel = 'ring' | 'light' | 'bars';
|
|
18
|
+
|
|
19
|
+
export interface VoiceMotionRegistry {
|
|
20
|
+
register: (channel: VoiceMotionChannel, element: HTMLElement) => void;
|
|
21
|
+
unregister: (channel: VoiceMotionChannel, element: HTMLElement) => void;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function percent(share: number) {
|
|
25
|
+
return `${(share * 100).toFixed(3)}%`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function paint(channel: VoiceMotionChannel, element: HTMLElement, frame: VoiceMotionFrame) {
|
|
29
|
+
const { style } = element;
|
|
30
|
+
|
|
31
|
+
if (channel === 'ring') {
|
|
32
|
+
style.setProperty('--voice-spot-a', percent(frame.spotA));
|
|
33
|
+
style.setProperty('--voice-spot-b', percent(frame.spotB));
|
|
34
|
+
style.setProperty('--voice-spot-strength', frame.spotStrength.toFixed(3));
|
|
35
|
+
style.setProperty('--voice-line-strength', frame.lineStrength.toFixed(3));
|
|
36
|
+
style.setProperty('--voice-pulse-mix', frame.pulseMix.toFixed(3));
|
|
37
|
+
style.setProperty('--voice-pulse', frame.pulse.toFixed(3));
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (channel === 'light') {
|
|
42
|
+
const vars = voiceLightVars(frame.lightState, frame.lightRise, frame.lightPresence);
|
|
43
|
+
for (const [name, value] of Object.entries(vars)) {
|
|
44
|
+
style.setProperty(name, value);
|
|
45
|
+
}
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
frame.bars.forEach((height, index) => {
|
|
50
|
+
style.setProperty(`--voice-bar-${index}`, `${height}px`);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function kindsKey(kinds: VoiceMotionKinds | null) {
|
|
55
|
+
return kinds ? `${kinds.border} ${kinds.light} ${kinds.bars}` : '';
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function kindsOf(key: string): VoiceMotionKinds {
|
|
59
|
+
const [border, light, bars] = key.split(' ');
|
|
60
|
+
return { border, light, bars } as VoiceMotionKinds;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Runs the voice motion (see `chat-prompt-box-voice-motion.ts`) on one `requestAnimationFrame`
|
|
65
|
+
* clock while `kinds` is set, and writes it as CSS variables on the elements registered for each
|
|
66
|
+
* part. Only those elements restyle each frame. `voice` (the speaker's loudness, 0 to 1) is read
|
|
67
|
+
* each frame, so changing it doesn't restart the clock. With reduced motion there is no clock: each
|
|
68
|
+
* state rests on its Base look, and the voice states still follow `voice`.
|
|
69
|
+
*/
|
|
70
|
+
export function useVoiceMotion(
|
|
71
|
+
kinds: VoiceMotionKinds | null,
|
|
72
|
+
voice: number | undefined,
|
|
73
|
+
): VoiceMotionRegistry {
|
|
74
|
+
const reducedMotion = useReducedMotion();
|
|
75
|
+
const key = kindsKey(kinds);
|
|
76
|
+
|
|
77
|
+
const engineRef = useRef<VoiceMotion | null>(null);
|
|
78
|
+
const frameRef = useRef<VoiceMotionFrame | null>(null);
|
|
79
|
+
const voiceRef = useRef(voice ?? 0);
|
|
80
|
+
const targetsRef = useRef<Record<VoiceMotionChannel, Set<HTMLElement>>>({
|
|
81
|
+
ring: new Set(),
|
|
82
|
+
light: new Set(),
|
|
83
|
+
bars: new Set(),
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
useEffect(() => {
|
|
87
|
+
voiceRef.current = voice ?? 0;
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const paintAll = useCallback((frame: VoiceMotionFrame) => {
|
|
91
|
+
frameRef.current = frame;
|
|
92
|
+
const targets = targetsRef.current;
|
|
93
|
+
(Object.keys(targets) as VoiceMotionChannel[]).forEach((channel) => {
|
|
94
|
+
targets[channel].forEach((element) => paint(channel, element, frame));
|
|
95
|
+
});
|
|
96
|
+
}, []);
|
|
97
|
+
|
|
98
|
+
// A layout effect, so the first frame is painted before the browser draws the new state.
|
|
99
|
+
useIsomorphicLayoutEffect(() => {
|
|
100
|
+
if (!key) {
|
|
101
|
+
engineRef.current = null;
|
|
102
|
+
frameRef.current = null;
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const next = kindsOf(key);
|
|
107
|
+
|
|
108
|
+
if (reducedMotion) {
|
|
109
|
+
const lightState = frameRef.current?.lightState;
|
|
110
|
+
paintAll(
|
|
111
|
+
restVoiceMotion(next, voiceRef.current, next.light === 'none' ? lightState : undefined),
|
|
112
|
+
);
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (engineRef.current) {
|
|
117
|
+
engineRef.current.setKinds(next);
|
|
118
|
+
} else {
|
|
119
|
+
engineRef.current = createVoiceMotion(next);
|
|
120
|
+
}
|
|
121
|
+
const engine = engineRef.current;
|
|
122
|
+
paintAll(engine.frame());
|
|
123
|
+
|
|
124
|
+
let frameId = 0;
|
|
125
|
+
let last: number | null = null;
|
|
126
|
+
const tick = (now: number) => {
|
|
127
|
+
// Cap the step so a tab coming back from the background doesn't jump.
|
|
128
|
+
const dt = last === null ? 0 : Math.min(0.1, (now - last) / 1000);
|
|
129
|
+
last = now;
|
|
130
|
+
engine.step(dt, voiceRef.current);
|
|
131
|
+
paintAll(engine.frame());
|
|
132
|
+
frameId = requestAnimationFrame(tick);
|
|
133
|
+
};
|
|
134
|
+
frameId = requestAnimationFrame(tick);
|
|
135
|
+
|
|
136
|
+
return () => cancelAnimationFrame(frameId);
|
|
137
|
+
}, [key, reducedMotion, paintAll]);
|
|
138
|
+
|
|
139
|
+
// Without the clock, the voice states still follow the voice.
|
|
140
|
+
useEffect(() => {
|
|
141
|
+
if (!key || !reducedMotion) return;
|
|
142
|
+
paintAll(restVoiceMotion(kindsOf(key), voice ?? 0));
|
|
143
|
+
}, [key, reducedMotion, voice, paintAll]);
|
|
144
|
+
|
|
145
|
+
return useMemo(
|
|
146
|
+
() => ({
|
|
147
|
+
register(channel, element) {
|
|
148
|
+
targetsRef.current[channel].add(element);
|
|
149
|
+
if (frameRef.current) paint(channel, element, frameRef.current);
|
|
150
|
+
},
|
|
151
|
+
unregister(channel, element) {
|
|
152
|
+
targetsRef.current[channel].delete(element);
|
|
153
|
+
},
|
|
154
|
+
}),
|
|
155
|
+
[],
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* A callback ref that registers its element with `registry` for `channel`, merged with `forwarded`.
|
|
161
|
+
* With no registry the element is left alone.
|
|
162
|
+
*/
|
|
163
|
+
export function useVoiceMotionRef<T extends HTMLElement>(
|
|
164
|
+
registry: VoiceMotionRegistry | null,
|
|
165
|
+
channel: VoiceMotionChannel,
|
|
166
|
+
forwarded?: Ref<T>,
|
|
167
|
+
) {
|
|
168
|
+
const elementRef = useRef<T | null>(null);
|
|
169
|
+
|
|
170
|
+
return useCallback(
|
|
171
|
+
(element: T | null) => {
|
|
172
|
+
if (elementRef.current && registry) registry.unregister(channel, elementRef.current);
|
|
173
|
+
elementRef.current = element;
|
|
174
|
+
if (element && registry) registry.register(channel, element);
|
|
175
|
+
|
|
176
|
+
if (typeof forwarded === 'function') {
|
|
177
|
+
forwarded(element);
|
|
178
|
+
} else if (forwarded) {
|
|
179
|
+
(forwarded as { current: T | null }).current = element;
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
[registry, channel, forwarded],
|
|
183
|
+
);
|
|
184
|
+
}
|