@zentauri-ui/zentauri-components 2.3.1 → 2.3.2

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.
Files changed (144) hide show
  1. package/README.md +7 -5
  2. package/cli/props.json +664 -0
  3. package/cli/registry.json +12 -0
  4. package/dist/chunk-2RBBXVFA.js +19 -0
  5. package/dist/{chunk-MY3DQVNF.js.map → chunk-2RBBXVFA.js.map} +1 -1
  6. package/dist/{chunk-VMVG2RVZ.mjs → chunk-425DAZTS.mjs} +4 -4
  7. package/dist/{chunk-VMVG2RVZ.mjs.map → chunk-425DAZTS.mjs.map} +1 -1
  8. package/dist/{chunk-RBNZNWYQ.js → chunk-6GP74P4F.js} +6 -6
  9. package/dist/{chunk-RBNZNWYQ.js.map → chunk-6GP74P4F.js.map} +1 -1
  10. package/dist/chunk-DUKHIN2W.js +44 -0
  11. package/dist/chunk-DUKHIN2W.js.map +1 -0
  12. package/dist/chunk-HJ7EFBED.js +86 -0
  13. package/dist/chunk-HJ7EFBED.js.map +1 -0
  14. package/dist/chunk-K2VCZK4I.mjs +75 -0
  15. package/dist/chunk-K2VCZK4I.mjs.map +1 -0
  16. package/dist/{chunk-UN5RRNPV.js → chunk-KVBFWRPF.js} +12 -12
  17. package/dist/{chunk-UN5RRNPV.js.map → chunk-KVBFWRPF.js.map} +1 -1
  18. package/dist/chunk-N4MLFU2Q.mjs +69 -0
  19. package/dist/chunk-N4MLFU2Q.mjs.map +1 -0
  20. package/dist/{chunk-K7UU3K54.js → chunk-NZQA4M35.js} +24 -4
  21. package/dist/chunk-NZQA4M35.js.map +1 -0
  22. package/dist/chunk-OME7XOPN.js +78 -0
  23. package/dist/chunk-OME7XOPN.js.map +1 -0
  24. package/dist/chunk-PVDWJUMF.mjs +34 -0
  25. package/dist/chunk-PVDWJUMF.mjs.map +1 -0
  26. package/dist/{chunk-TTKTPERV.mjs → chunk-R2JXARKB.mjs} +3 -3
  27. package/dist/{chunk-TTKTPERV.mjs.map → chunk-R2JXARKB.mjs.map} +1 -1
  28. package/dist/chunk-SD7YXMNV.js +40 -0
  29. package/dist/chunk-SD7YXMNV.js.map +1 -0
  30. package/dist/{chunk-PJATBFEK.mjs → chunk-TZTUL6C4.mjs} +24 -4
  31. package/dist/chunk-TZTUL6C4.mjs.map +1 -0
  32. package/dist/chunk-V5EE5ATH.mjs +36 -0
  33. package/dist/chunk-V5EE5ATH.mjs.map +1 -0
  34. package/dist/{chunk-STWXN5EM.mjs → chunk-YDD5HQGX.mjs} +3 -3
  35. package/dist/{chunk-STWXN5EM.mjs.map → chunk-YDD5HQGX.mjs.map} +1 -1
  36. package/dist/design-system/facade.js +6 -4
  37. package/dist/design-system/facade.js.map +1 -1
  38. package/dist/design-system/facade.mjs +5 -3
  39. package/dist/design-system/facade.mjs.map +1 -1
  40. package/dist/design-system/index.d.ts +2 -0
  41. package/dist/design-system/index.d.ts.map +1 -1
  42. package/dist/design-system/speech-recognition.d.ts +39 -0
  43. package/dist/design-system/speech-recognition.d.ts.map +1 -0
  44. package/dist/design-system/speech-synthesizer.d.ts +41 -0
  45. package/dist/design-system/speech-synthesizer.d.ts.map +1 -0
  46. package/dist/ui/buttons/animated.js +8 -6
  47. package/dist/ui/buttons/animated.js.map +1 -1
  48. package/dist/ui/buttons/animated.mjs +6 -4
  49. package/dist/ui/buttons/animated.mjs.map +1 -1
  50. package/dist/ui/buttons.js +9 -7
  51. package/dist/ui/buttons.mjs +7 -5
  52. package/dist/ui/data-table.js +19 -17
  53. package/dist/ui/data-table.js.map +1 -1
  54. package/dist/ui/data-table.mjs +9 -7
  55. package/dist/ui/data-table.mjs.map +1 -1
  56. package/dist/ui/dynamic-stepper.js +18 -16
  57. package/dist/ui/dynamic-stepper.js.map +1 -1
  58. package/dist/ui/dynamic-stepper.mjs +7 -5
  59. package/dist/ui/dynamic-stepper.mjs.map +1 -1
  60. package/dist/ui/pagination.js +10 -8
  61. package/dist/ui/pagination.mjs +7 -5
  62. package/dist/ui/speech-recognition/animated/animations.d.ts +8 -0
  63. package/dist/ui/speech-recognition/animated/animations.d.ts.map +1 -0
  64. package/dist/ui/speech-recognition/animated/index.d.ts +4 -0
  65. package/dist/ui/speech-recognition/animated/index.d.ts.map +1 -0
  66. package/dist/ui/speech-recognition/animated/speech-recognition-animated.d.ts +6 -0
  67. package/dist/ui/speech-recognition/animated/speech-recognition-animated.d.ts.map +1 -0
  68. package/dist/ui/speech-recognition/animated/types.d.ts +9 -0
  69. package/dist/ui/speech-recognition/animated/types.d.ts.map +1 -0
  70. package/dist/ui/speech-recognition/animated.js +288 -0
  71. package/dist/ui/speech-recognition/animated.js.map +1 -0
  72. package/dist/ui/speech-recognition/animated.mjs +285 -0
  73. package/dist/ui/speech-recognition/animated.mjs.map +1 -0
  74. package/dist/ui/speech-recognition/index.d.ts +4 -0
  75. package/dist/ui/speech-recognition/index.d.ts.map +1 -0
  76. package/dist/ui/speech-recognition/speech-recognition-base.d.ts +6 -0
  77. package/dist/ui/speech-recognition/speech-recognition-base.d.ts.map +1 -0
  78. package/dist/ui/speech-recognition/speech-recognition.d.ts +2 -0
  79. package/dist/ui/speech-recognition/speech-recognition.d.ts.map +1 -0
  80. package/dist/ui/speech-recognition/types.d.ts +31 -0
  81. package/dist/ui/speech-recognition/types.d.ts.map +1 -0
  82. package/dist/ui/speech-recognition/variants.d.ts +11 -0
  83. package/dist/ui/speech-recognition/variants.d.ts.map +1 -0
  84. package/dist/ui/speech-recognition.js +242 -0
  85. package/dist/ui/speech-recognition.js.map +1 -0
  86. package/dist/ui/speech-recognition.mjs +233 -0
  87. package/dist/ui/speech-recognition.mjs.map +1 -0
  88. package/dist/ui/speech-synthesizer/animated/animations.d.ts +8 -0
  89. package/dist/ui/speech-synthesizer/animated/animations.d.ts.map +1 -0
  90. package/dist/ui/speech-synthesizer/animated/index.d.ts +4 -0
  91. package/dist/ui/speech-synthesizer/animated/index.d.ts.map +1 -0
  92. package/dist/ui/speech-synthesizer/animated/speech-synthesizer-animated.d.ts +6 -0
  93. package/dist/ui/speech-synthesizer/animated/speech-synthesizer-animated.d.ts.map +1 -0
  94. package/dist/ui/speech-synthesizer/animated/types.d.ts +9 -0
  95. package/dist/ui/speech-synthesizer/animated/types.d.ts.map +1 -0
  96. package/dist/ui/speech-synthesizer/animated.js +269 -0
  97. package/dist/ui/speech-synthesizer/animated.js.map +1 -0
  98. package/dist/ui/speech-synthesizer/animated.mjs +266 -0
  99. package/dist/ui/speech-synthesizer/animated.mjs.map +1 -0
  100. package/dist/ui/speech-synthesizer/index.d.ts +4 -0
  101. package/dist/ui/speech-synthesizer/index.d.ts.map +1 -0
  102. package/dist/ui/speech-synthesizer/speech-synthesizer-base.d.ts +6 -0
  103. package/dist/ui/speech-synthesizer/speech-synthesizer-base.d.ts.map +1 -0
  104. package/dist/ui/speech-synthesizer/speech-synthesizer.d.ts +2 -0
  105. package/dist/ui/speech-synthesizer/speech-synthesizer.d.ts.map +1 -0
  106. package/dist/ui/speech-synthesizer/types.d.ts +43 -0
  107. package/dist/ui/speech-synthesizer/types.d.ts.map +1 -0
  108. package/dist/ui/speech-synthesizer/variants.d.ts +13 -0
  109. package/dist/ui/speech-synthesizer/variants.d.ts.map +1 -0
  110. package/dist/ui/speech-synthesizer.js +220 -0
  111. package/dist/ui/speech-synthesizer.js.map +1 -0
  112. package/dist/ui/speech-synthesizer.mjs +211 -0
  113. package/dist/ui/speech-synthesizer.mjs.map +1 -0
  114. package/dist/ui/split-button.js +20 -18
  115. package/dist/ui/split-button.js.map +1 -1
  116. package/dist/ui/split-button.mjs +7 -5
  117. package/dist/ui/split-button.mjs.map +1 -1
  118. package/package.json +1 -1
  119. package/src/design-system/index.ts +2 -0
  120. package/src/design-system/speech-recognition.ts +82 -0
  121. package/src/design-system/speech-synthesizer.ts +90 -0
  122. package/src/ui/speech-recognition/animated/animations.ts +62 -0
  123. package/src/ui/speech-recognition/animated/index.ts +8 -0
  124. package/src/ui/speech-recognition/animated/speech-recognition-animated.tsx +276 -0
  125. package/src/ui/speech-recognition/animated/types.ts +11 -0
  126. package/src/ui/speech-recognition/index.ts +15 -0
  127. package/src/ui/speech-recognition/speech-recognition-base.tsx +276 -0
  128. package/src/ui/speech-recognition/speech-recognition.test.tsx +74 -0
  129. package/src/ui/speech-recognition/speech-recognition.tsx +1 -0
  130. package/src/ui/speech-recognition/types.ts +50 -0
  131. package/src/ui/speech-recognition/variants.ts +47 -0
  132. package/src/ui/speech-synthesizer/animated/animations.ts +62 -0
  133. package/src/ui/speech-synthesizer/animated/index.ts +8 -0
  134. package/src/ui/speech-synthesizer/animated/speech-synthesizer-animated.tsx +260 -0
  135. package/src/ui/speech-synthesizer/animated/types.ts +11 -0
  136. package/src/ui/speech-synthesizer/index.ts +14 -0
  137. package/src/ui/speech-synthesizer/speech-synthesizer-base.tsx +255 -0
  138. package/src/ui/speech-synthesizer/speech-synthesizer.test.tsx +87 -0
  139. package/src/ui/speech-synthesizer/speech-synthesizer.tsx +1 -0
  140. package/src/ui/speech-synthesizer/types.ts +57 -0
  141. package/src/ui/speech-synthesizer/variants.ts +55 -0
  142. package/dist/chunk-K7UU3K54.js.map +0 -1
  143. package/dist/chunk-MY3DQVNF.js +0 -19
  144. package/dist/chunk-PJATBFEK.mjs.map +0 -1
@@ -0,0 +1,47 @@
1
+ import { cva } from "class-variance-authority";
2
+
3
+ import {
4
+ zuiSpeechRecognitionAppearances,
5
+ zuiSpeechRecognitionBase,
6
+ zuiSpeechRecognitionBtnActive,
7
+ zuiSpeechRecognitionBtnBase,
8
+ zuiSpeechRecognitionBtnSizes,
9
+ zuiSpeechRecognitionSizes,
10
+ zuiSpeechRecognitionStatusBase,
11
+ zuiSpeechRecognitionTranscriptBase,
12
+ } from "../../design-system/speech-recognition";
13
+
14
+ export const speechRecognitionVariants = cva(
15
+ zuiSpeechRecognitionBase.join(" "),
16
+ {
17
+ variants: {
18
+ size: zuiSpeechRecognitionSizes,
19
+ },
20
+ defaultVariants: {
21
+ size: "md",
22
+ },
23
+ },
24
+ );
25
+
26
+ export const speechRecognitionBtnVariants = cva(
27
+ zuiSpeechRecognitionBtnBase.join(" "),
28
+ {
29
+ variants: {
30
+ appearance: zuiSpeechRecognitionAppearances,
31
+ size: zuiSpeechRecognitionBtnSizes,
32
+ },
33
+ defaultVariants: {
34
+ appearance: "default",
35
+ size: "md",
36
+ },
37
+ },
38
+ );
39
+
40
+ export const speechRecognitionStatusClasses =
41
+ zuiSpeechRecognitionStatusBase.join(" ");
42
+
43
+ export const speechRecognitionTranscriptClasses =
44
+ zuiSpeechRecognitionTranscriptBase.join(" ");
45
+
46
+ export const speechRecognitionBtnActiveClasses =
47
+ zuiSpeechRecognitionBtnActive.join(" ");
@@ -0,0 +1,62 @@
1
+ import type { Transition, Variants } from "framer-motion";
2
+
3
+ export type SpeechSynthesizerAnimation = "none" | "pulse" | "wave" | "glow";
4
+
5
+ export type SpeechSynthesizerAnimationPresets = Record<
6
+ SpeechSynthesizerAnimation,
7
+ {
8
+ transition: Transition;
9
+ variants: Variants;
10
+ }
11
+ >;
12
+
13
+ export const speechSynthesizerAnimationPresets: SpeechSynthesizerAnimationPresets =
14
+ {
15
+ none: {
16
+ transition: { duration: 0 },
17
+ variants: {
18
+ initial: { scale: 1 },
19
+ animate: { scale: 1 },
20
+ },
21
+ },
22
+ pulse: {
23
+ transition: {
24
+ duration: 1,
25
+ repeat: Infinity,
26
+ repeatType: "reverse",
27
+ ease: "easeInOut",
28
+ },
29
+ variants: {
30
+ initial: { scale: 1, opacity: 0.7 },
31
+ animate: { scale: 1.15, opacity: 1 },
32
+ },
33
+ },
34
+ wave: {
35
+ transition: {
36
+ duration: 0.8,
37
+ repeat: Infinity,
38
+ repeatType: "reverse",
39
+ ease: "easeInOut",
40
+ },
41
+ variants: {
42
+ initial: { scale: 1, rotate: 0 },
43
+ animate: { scale: 1.1, rotate: -5 },
44
+ },
45
+ },
46
+ glow: {
47
+ transition: {
48
+ duration: 1.5,
49
+ repeat: Infinity,
50
+ repeatType: "reverse",
51
+ ease: "easeInOut",
52
+ },
53
+ variants: {
54
+ initial: {
55
+ boxShadow: "0 0 0px rgba(59,130,246,0)",
56
+ },
57
+ animate: {
58
+ boxShadow: "0 0 20px rgba(59,130,246,0.5)",
59
+ },
60
+ },
61
+ },
62
+ };
@@ -0,0 +1,8 @@
1
+ "use client";
2
+
3
+ export { SpeechSynthesizerAnimated } from "./speech-synthesizer-animated";
4
+ export type {
5
+ SpeechSynthesizerAnimation,
6
+ SpeechSynthesizerAnimatedProps,
7
+ } from "./types";
8
+ export { speechSynthesizerAnimationPresets } from "./animations";
@@ -0,0 +1,260 @@
1
+ "use client";
2
+
3
+ import { motion } from "framer-motion";
4
+ import {
5
+ useCallback,
6
+ useEffect,
7
+ useImperativeHandle,
8
+ useRef,
9
+ useState,
10
+ } from "react";
11
+
12
+ import { cn } from "../../../lib/utils";
13
+
14
+ import type { SpeechSynthesizerRef, SpeechSynthesizerState } from "../types";
15
+ import {
16
+ speechSynthesizerBtnActiveClasses,
17
+ speechSynthesizerBtnVariants,
18
+ speechSynthesizerControlsClasses,
19
+ speechSynthesizerProgressBarClasses,
20
+ speechSynthesizerProgressClasses,
21
+ speechSynthesizerTextClasses,
22
+ speechSynthesizerVariants,
23
+ } from "../variants";
24
+
25
+ import { speechSynthesizerAnimationPresets } from "./animations";
26
+ import type { SpeechSynthesizerAnimatedProps } from "./types";
27
+
28
+ function DefaultPlayIcon() {
29
+ return (
30
+ <svg viewBox="0 0 24 24" fill="currentColor" className="size-5">
31
+ <path d="M8 5v14l11-7z" />
32
+ </svg>
33
+ );
34
+ }
35
+
36
+ function DefaultPauseIcon() {
37
+ return (
38
+ <svg viewBox="0 0 24 24" fill="currentColor" className="size-5">
39
+ <rect x="6" y="4" width="4" height="16" />
40
+ <rect x="14" y="4" width="4" height="16" />
41
+ </svg>
42
+ );
43
+ }
44
+
45
+ function DefaultStopIcon() {
46
+ return (
47
+ <svg viewBox="0 0 24 24" fill="currentColor" className="size-5">
48
+ <rect x="6" y="6" width="12" height="12" />
49
+ </svg>
50
+ );
51
+ }
52
+
53
+ export function SpeechSynthesizerAnimated({
54
+ appearance,
55
+ size,
56
+ text,
57
+ lang,
58
+ rate = 1,
59
+ pitch = 1,
60
+ volume = 1,
61
+ autoSpeak = false,
62
+ onStart,
63
+ onEnd,
64
+ onError,
65
+ onStateChange,
66
+ renderPlay,
67
+ renderPause,
68
+ renderStop,
69
+ showProgress = false,
70
+ animation = "pulse",
71
+ className,
72
+ children,
73
+ ...rest
74
+ }: SpeechSynthesizerAnimatedProps) {
75
+ const [state, setState] = useState<SpeechSynthesizerState>("idle");
76
+ const utteranceRef = useRef<SpeechSynthesisUtterance | null>(null);
77
+ const stateRef = useRef<SpeechSynthesizerState>("idle");
78
+ const preset = speechSynthesizerAnimationPresets[animation];
79
+
80
+ const setStateSafe = useCallback(
81
+ (newState: SpeechSynthesizerState) => {
82
+ stateRef.current = newState;
83
+ setState(newState);
84
+ onStateChange?.(newState);
85
+ },
86
+ [onStateChange],
87
+ );
88
+
89
+ const speak = useCallback(() => {
90
+ if (!text || typeof window === "undefined") return;
91
+ window.speechSynthesis.cancel();
92
+
93
+ const utterance = new SpeechSynthesisUtterance(text);
94
+ if (lang) utterance.lang = lang;
95
+ utterance.rate = rate;
96
+ utterance.pitch = pitch;
97
+ utterance.volume = volume;
98
+
99
+ utterance.onstart = () => {
100
+ setStateSafe("speaking");
101
+ onStart?.();
102
+ };
103
+ utterance.onend = () => {
104
+ setStateSafe("idle");
105
+ onEnd?.();
106
+ };
107
+ utterance.onerror = (event) => {
108
+ setStateSafe("idle");
109
+ onError?.(event.error);
110
+ };
111
+ utterance.onpause = () => setStateSafe("paused");
112
+ utterance.onresume = () => setStateSafe("speaking");
113
+
114
+ utteranceRef.current = utterance;
115
+ window.speechSynthesis.speak(utterance);
116
+ }, [text, lang, rate, pitch, volume, onStart, onEnd, onError, setStateSafe]);
117
+
118
+ const pauseFn = useCallback(() => {
119
+ if (typeof window !== "undefined") window.speechSynthesis.pause();
120
+ }, []);
121
+
122
+ const resumeFn = useCallback(() => {
123
+ if (typeof window !== "undefined") window.speechSynthesis.resume();
124
+ }, []);
125
+
126
+ const stop = useCallback(() => {
127
+ if (typeof window !== "undefined") window.speechSynthesis.cancel();
128
+ setStateSafe("idle");
129
+ }, [setStateSafe]);
130
+
131
+ const imperativeHandle: SpeechSynthesizerRef = {
132
+ speak,
133
+ pause: pauseFn,
134
+ resume: resumeFn,
135
+ stop,
136
+ get state() {
137
+ return stateRef.current;
138
+ },
139
+ };
140
+
141
+ useImperativeHandle((rest as any).ref, () => imperativeHandle, [
142
+ speak,
143
+ pauseFn,
144
+ resumeFn,
145
+ stop,
146
+ ]);
147
+
148
+ useEffect(() => {
149
+ if (autoSpeak && text) speak();
150
+ return () => {
151
+ if (typeof window !== "undefined") window.speechSynthesis.cancel();
152
+ };
153
+ }, []);
154
+
155
+ const isSpeaking = state === "speaking";
156
+ const isPaused = state === "paused";
157
+ const showPlay = state === "idle" || state === "paused";
158
+ const showPause = state === "speaking";
159
+
160
+ return (
161
+ <motion.div
162
+ data-slot="speech-synthesizer"
163
+ className={cn(speechSynthesizerVariants({ size }), className)}
164
+ {...(rest as any)}
165
+ >
166
+ {text && (
167
+ <motion.p
168
+ data-slot="speech-synthesizer-text"
169
+ className={speechSynthesizerTextClasses}
170
+ initial="initial"
171
+ animate={isSpeaking ? "animate" : "initial"}
172
+ variants={preset.variants}
173
+ transition={preset.transition}
174
+ >
175
+ {text}
176
+ </motion.p>
177
+ )}
178
+ {showProgress && isSpeaking && (
179
+ <motion.div
180
+ data-slot="speech-synthesizer-progress"
181
+ className={speechSynthesizerProgressClasses}
182
+ initial={{ scaleX: 0 }}
183
+ animate={{ scaleX: 1 }}
184
+ transition={{ duration: 0.3 }}
185
+ style={{ originX: 0 }}
186
+ >
187
+ <div
188
+ data-slot="speech-synthesizer-progress-bar"
189
+ className={cn(
190
+ speechSynthesizerProgressBarClasses,
191
+ "w-full animate-pulse",
192
+ )}
193
+ />
194
+ </motion.div>
195
+ )}
196
+ <motion.div
197
+ data-slot="speech-synthesizer-controls"
198
+ className={speechSynthesizerControlsClasses}
199
+ >
200
+ {showPlay && (
201
+ <motion.button
202
+ type="button"
203
+ data-slot="speech-synthesizer-play-btn"
204
+ className={cn(
205
+ speechSynthesizerBtnVariants({ appearance, size }),
206
+ isPaused && speechSynthesizerBtnActiveClasses,
207
+ )}
208
+ onClick={isPaused ? resumeFn : speak}
209
+ aria-label={isPaused ? "Resume" : "Play"}
210
+ whileHover={{ scale: 1.1 }}
211
+ whileTap={{ scale: 0.9 }}
212
+ >
213
+ {renderPlay ? (
214
+ renderPlay({ isSpeaking, isPaused, state })
215
+ ) : (
216
+ <DefaultPlayIcon />
217
+ )}
218
+ </motion.button>
219
+ )}
220
+ {showPause && (
221
+ <motion.button
222
+ type="button"
223
+ data-slot="speech-synthesizer-pause-btn"
224
+ className={speechSynthesizerBtnVariants({ appearance, size })}
225
+ onClick={pauseFn}
226
+ aria-label="Pause"
227
+ whileHover={{ scale: 1.1 }}
228
+ whileTap={{ scale: 0.9 }}
229
+ >
230
+ {renderPause ? (
231
+ renderPause({ isSpeaking, isPaused, state })
232
+ ) : (
233
+ <DefaultPauseIcon />
234
+ )}
235
+ </motion.button>
236
+ )}
237
+ {(isSpeaking || isPaused) && (
238
+ <motion.button
239
+ type="button"
240
+ data-slot="speech-synthesizer-stop-btn"
241
+ className={speechSynthesizerBtnVariants({ appearance, size })}
242
+ onClick={stop}
243
+ aria-label="Stop"
244
+ whileHover={{ scale: 1.1 }}
245
+ whileTap={{ scale: 0.9 }}
246
+ >
247
+ {renderStop ? (
248
+ renderStop({ isSpeaking, isPaused, state })
249
+ ) : (
250
+ <DefaultStopIcon />
251
+ )}
252
+ </motion.button>
253
+ )}
254
+ </motion.div>
255
+ {children}
256
+ </motion.div>
257
+ );
258
+ }
259
+
260
+ SpeechSynthesizerAnimated.displayName = "SpeechSynthesizerAnimated";
@@ -0,0 +1,11 @@
1
+ import type { Ref } from "react";
2
+
3
+ import type { SpeechSynthesizerBaseProps } from "../types";
4
+ import type { SpeechSynthesizerAnimation } from "./animations";
5
+
6
+ export type { SpeechSynthesizerAnimation };
7
+
8
+ export type SpeechSynthesizerAnimatedProps = SpeechSynthesizerBaseProps & {
9
+ animation?: SpeechSynthesizerAnimation;
10
+ ref?: Ref<HTMLDivElement>;
11
+ };
@@ -0,0 +1,14 @@
1
+ "use client";
2
+
3
+ export { SpeechSynthesizer } from "./speech-synthesizer";
4
+ export type {
5
+ SpeechSynthesizerBaseProps,
6
+ SpeechSynthesizerProps,
7
+ SpeechSynthesizerRef,
8
+ SpeechSynthesizerState,
9
+ SpeechSynthesizerVariantProps,
10
+ } from "./types";
11
+ export {
12
+ speechSynthesizerBtnVariants,
13
+ speechSynthesizerVariants,
14
+ } from "./variants";
@@ -0,0 +1,255 @@
1
+ "use client";
2
+
3
+ import {
4
+ useCallback,
5
+ useEffect,
6
+ useImperativeHandle,
7
+ useRef,
8
+ useState,
9
+ } from "react";
10
+
11
+ import { cn } from "../../lib/utils";
12
+
13
+ import type {
14
+ SpeechSynthesizerBaseProps,
15
+ SpeechSynthesizerRef,
16
+ SpeechSynthesizerState,
17
+ } from "./types";
18
+ import {
19
+ speechSynthesizerBtnActiveClasses,
20
+ speechSynthesizerBtnVariants,
21
+ speechSynthesizerControlsClasses,
22
+ speechSynthesizerProgressBarClasses,
23
+ speechSynthesizerProgressClasses,
24
+ speechSynthesizerTextClasses,
25
+ speechSynthesizerVariants,
26
+ } from "./variants";
27
+
28
+ function DefaultPlayIcon() {
29
+ return (
30
+ <svg viewBox="0 0 24 24" fill="currentColor" className="size-5">
31
+ <path d="M8 5v14l11-7z" />
32
+ </svg>
33
+ );
34
+ }
35
+
36
+ function DefaultPauseIcon() {
37
+ return (
38
+ <svg viewBox="0 0 24 24" fill="currentColor" className="size-5">
39
+ <rect x="6" y="4" width="4" height="16" />
40
+ <rect x="14" y="4" width="4" height="16" />
41
+ </svg>
42
+ );
43
+ }
44
+
45
+ function DefaultStopIcon() {
46
+ return (
47
+ <svg viewBox="0 0 24 24" fill="currentColor" className="size-5">
48
+ <rect x="6" y="6" width="12" height="12" />
49
+ </svg>
50
+ );
51
+ }
52
+
53
+ export function SpeechSynthesizerBase({
54
+ appearance,
55
+ size,
56
+ text,
57
+ lang,
58
+ rate = 1,
59
+ pitch = 1,
60
+ volume = 1,
61
+ autoSpeak = false,
62
+ onStart,
63
+ onEnd,
64
+ onError,
65
+ onStateChange,
66
+ renderPlay,
67
+ renderPause,
68
+ renderStop,
69
+ showProgress = false,
70
+ className,
71
+ children,
72
+ ...rest
73
+ }: SpeechSynthesizerBaseProps) {
74
+ const [state, setState] = useState<SpeechSynthesizerState>("idle");
75
+ const utteranceRef = useRef<SpeechSynthesisUtterance | null>(null);
76
+ const stateRef = useRef<SpeechSynthesizerState>("idle");
77
+ const localRef = useRef<HTMLDivElement>(null);
78
+
79
+ const setStateSafe = useCallback(
80
+ (newState: SpeechSynthesizerState) => {
81
+ stateRef.current = newState;
82
+ setState(newState);
83
+ onStateChange?.(newState);
84
+ },
85
+ [onStateChange],
86
+ );
87
+
88
+ const speak = useCallback(() => {
89
+ if (!text || typeof window === "undefined") return;
90
+ window.speechSynthesis.cancel();
91
+
92
+ const utterance = new SpeechSynthesisUtterance(text);
93
+ if (lang) utterance.lang = lang;
94
+ utterance.rate = rate;
95
+ utterance.pitch = pitch;
96
+ utterance.volume = volume;
97
+
98
+ utterance.onstart = () => {
99
+ setStateSafe("speaking");
100
+ onStart?.();
101
+ };
102
+ utterance.onend = () => {
103
+ setStateSafe("idle");
104
+ onEnd?.();
105
+ };
106
+ utterance.onerror = (event) => {
107
+ setStateSafe("idle");
108
+ onError?.(event.error);
109
+ };
110
+ utterance.onpause = () => setStateSafe("paused");
111
+ utterance.onresume = () => setStateSafe("speaking");
112
+
113
+ utteranceRef.current = utterance;
114
+ window.speechSynthesis.speak(utterance);
115
+ }, [text, lang, rate, pitch, volume, onStart, onEnd, onError, setStateSafe]);
116
+
117
+ const pause = useCallback(() => {
118
+ if (typeof window !== "undefined") {
119
+ window.speechSynthesis.pause();
120
+ }
121
+ }, []);
122
+
123
+ const resume = useCallback(() => {
124
+ if (typeof window !== "undefined") {
125
+ window.speechSynthesis.resume();
126
+ }
127
+ }, []);
128
+
129
+ const stop = useCallback(() => {
130
+ if (typeof window !== "undefined" && window.speechSynthesis) {
131
+ window.speechSynthesis.cancel();
132
+ }
133
+ setStateSafe("idle");
134
+ }, [setStateSafe]);
135
+
136
+ const imperativeHandle: SpeechSynthesizerRef = {
137
+ speak,
138
+ pause,
139
+ resume,
140
+ stop,
141
+ get state() {
142
+ return stateRef.current;
143
+ },
144
+ };
145
+
146
+ useImperativeHandle((rest as any).ref, () => imperativeHandle, [
147
+ speak,
148
+ pause,
149
+ resume,
150
+ stop,
151
+ ]);
152
+
153
+ useEffect(() => {
154
+ if (autoSpeak && text) speak();
155
+ return () => {
156
+ if (typeof window !== "undefined" && window.speechSynthesis) {
157
+ window.speechSynthesis.cancel();
158
+ }
159
+ };
160
+ }, []);
161
+
162
+ const isSpeaking = state === "speaking";
163
+ const isPaused = state === "paused";
164
+ const showPlay = state === "idle" || state === "paused";
165
+ const showPause = state === "speaking";
166
+
167
+ return (
168
+ <div
169
+ ref={localRef}
170
+ data-slot="speech-synthesizer"
171
+ className={cn(speechSynthesizerVariants({ size }), className)}
172
+ {...rest}
173
+ >
174
+ {text && (
175
+ <p
176
+ data-slot="speech-synthesizer-text"
177
+ className={speechSynthesizerTextClasses}
178
+ >
179
+ {text}
180
+ </p>
181
+ )}
182
+ {showProgress && isSpeaking && (
183
+ <div
184
+ data-slot="speech-synthesizer-progress"
185
+ className={speechSynthesizerProgressClasses}
186
+ >
187
+ <div
188
+ data-slot="speech-synthesizer-progress-bar"
189
+ className={cn(
190
+ speechSynthesizerProgressBarClasses,
191
+ "w-full animate-pulse",
192
+ )}
193
+ style={{ width: "100%" }}
194
+ />
195
+ </div>
196
+ )}
197
+ <div
198
+ data-slot="speech-synthesizer-controls"
199
+ className={speechSynthesizerControlsClasses}
200
+ >
201
+ {showPlay && (
202
+ <button
203
+ type="button"
204
+ data-slot="speech-synthesizer-play-btn"
205
+ className={cn(
206
+ speechSynthesizerBtnVariants({ appearance, size }),
207
+ isPaused && speechSynthesizerBtnActiveClasses,
208
+ )}
209
+ onClick={isPaused ? resume : speak}
210
+ aria-label={isPaused ? "Resume" : "Play"}
211
+ >
212
+ {renderPlay ? (
213
+ renderPlay({ isSpeaking, isPaused, state })
214
+ ) : (
215
+ <DefaultPlayIcon />
216
+ )}
217
+ </button>
218
+ )}
219
+ {showPause && (
220
+ <button
221
+ type="button"
222
+ data-slot="speech-synthesizer-pause-btn"
223
+ className={speechSynthesizerBtnVariants({ appearance, size })}
224
+ onClick={pause}
225
+ aria-label="Pause"
226
+ >
227
+ {renderPause ? (
228
+ renderPause({ isSpeaking, isPaused, state })
229
+ ) : (
230
+ <DefaultPauseIcon />
231
+ )}
232
+ </button>
233
+ )}
234
+ {(isSpeaking || isPaused) && (
235
+ <button
236
+ type="button"
237
+ data-slot="speech-synthesizer-stop-btn"
238
+ className={speechSynthesizerBtnVariants({ appearance, size })}
239
+ onClick={stop}
240
+ aria-label="Stop"
241
+ >
242
+ {renderStop ? (
243
+ renderStop({ isSpeaking, isPaused, state })
244
+ ) : (
245
+ <DefaultStopIcon />
246
+ )}
247
+ </button>
248
+ )}
249
+ </div>
250
+ {children}
251
+ </div>
252
+ );
253
+ }
254
+
255
+ SpeechSynthesizerBase.displayName = "SpeechSynthesizer";