@moises.ai/design-system 4.15.1 → 4.15.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moises.ai/design-system",
3
- "version": "4.15.1",
3
+ "version": "4.15.2",
4
4
  "description": "Design System package based on @radix-ui/themes with custom defaults",
5
5
  "private": false,
6
6
  "type": "module",
@@ -0,0 +1,256 @@
1
+ import { useState, useCallback, useEffect, useRef } from 'react'
2
+ import WavesurferPlayer from '@wavesurfer/react'
3
+ import classNames from 'classnames'
4
+ import { PlayIcon, PauseIcon } from '../../icons'
5
+ import styles from './PreviewCard.module.css'
6
+
7
+ export function PreviewCard({
8
+ audio,
9
+ selected = false,
10
+ loading = false,
11
+ wavegroup,
12
+ onSelect,
13
+ onPlayStateChange,
14
+ actions,
15
+ autoRepeat = false,
16
+ waveColor = 'rgba(90, 97, 105, 1)',
17
+ progressColor = 'rgba(255, 255, 255, 1)',
18
+ className,
19
+ }) {
20
+ const [isPlaying, setIsPlaying] = useState(false)
21
+ const [isReady, setIsReady] = useState(false)
22
+ const [isSafari, setIsSafari] = useState(false)
23
+
24
+ const waveSurferRef = useRef(null)
25
+
26
+ const showSkeleton = loading || !isReady
27
+
28
+ useEffect(() => {
29
+ if (typeof navigator !== 'undefined') {
30
+ setIsSafari(/^((?!chrome|android).)*safari/i.test(navigator.userAgent))
31
+ }
32
+ }, [])
33
+
34
+ const pause = useCallback(() => {
35
+ waveSurferRef.current?.pause()
36
+ }, [])
37
+
38
+ const play = useCallback(() => {
39
+ if (!waveSurferRef.current) return
40
+
41
+ if (
42
+ wavegroup?.state?.active &&
43
+ wavegroup.state.active !== waveSurferRef.current
44
+ ) {
45
+ wavegroup.state.active.pause()
46
+ }
47
+
48
+ if (!isSafari) {
49
+ waveSurferRef.current.setOptions({ progressColor })
50
+ }
51
+ waveSurferRef.current.play()
52
+ }, [isSafari, progressColor, wavegroup])
53
+
54
+ useEffect(() => {
55
+ if (waveSurferRef.current && audio) {
56
+ waveSurferRef.current.load(audio)
57
+ setIsReady(false)
58
+ }
59
+ }, [audio])
60
+
61
+ const onReady = useCallback((ws) => {
62
+ waveSurferRef.current = ws
63
+ setIsReady(true)
64
+ setIsPlaying(false)
65
+ }, [])
66
+
67
+ const onPlay = useCallback(
68
+ (ws) => {
69
+ setIsPlaying(true)
70
+ onPlayStateChange?.(true)
71
+
72
+ if (wavegroup) {
73
+ if (wavegroup.state.active && wavegroup.state.active !== ws) {
74
+ const currentTime = wavegroup.state.active.getCurrentTime()
75
+ wavegroup.state.active.pause()
76
+ ws.setTime(currentTime)
77
+ }
78
+
79
+ wavegroup.setter((prev) => ({
80
+ ...prev,
81
+ isPlaying: true,
82
+ lastPlayed: ws,
83
+ active: ws,
84
+ }))
85
+ }
86
+ },
87
+ [onPlayStateChange, wavegroup],
88
+ )
89
+
90
+ const onPause = useCallback(() => {
91
+ setIsPlaying(false)
92
+ onPlayStateChange?.(false)
93
+
94
+ if (wavegroup) {
95
+ wavegroup.setter((prev) => ({
96
+ ...prev,
97
+ isPlaying: false,
98
+ }))
99
+ }
100
+ }, [onPlayStateChange, wavegroup])
101
+
102
+ const onSeeking = useCallback(
103
+ (ws) => {
104
+ if (!wavegroup) return
105
+
106
+ wavegroup.setter((prev) => ({
107
+ ...prev,
108
+ active: ws,
109
+ }))
110
+ },
111
+ [wavegroup],
112
+ )
113
+
114
+ const onFinish = useCallback(() => {
115
+ if (autoRepeat) {
116
+ waveSurferRef.current?.play()
117
+ } else {
118
+ setIsPlaying(false)
119
+ onPlayStateChange?.(false)
120
+
121
+ if (wavegroup) {
122
+ wavegroup.setter((prev) => ({
123
+ ...prev,
124
+ isPlaying: false,
125
+ active: null,
126
+ }))
127
+ }
128
+ }
129
+ }, [autoRepeat, onPlayStateChange, wavegroup])
130
+
131
+ const handleTogglePlay = useCallback(() => {
132
+ if (showSkeleton || !waveSurferRef.current) return
133
+
134
+ if (waveSurferRef.current.isPlaying()) {
135
+ pause()
136
+ return
137
+ }
138
+
139
+ if (
140
+ wavegroup?.state?.active &&
141
+ wavegroup.state.active !== waveSurferRef.current
142
+ ) {
143
+ wavegroup.state.active.pause()
144
+
145
+ wavegroup.setter((prev) => ({
146
+ ...prev,
147
+ isPlaying: false,
148
+ active: null,
149
+ }))
150
+ }
151
+
152
+ if (
153
+ wavegroup?.state?.active &&
154
+ wavegroup.state.active !== waveSurferRef.current &&
155
+ wavegroup.state.active.getCurrentTime
156
+ ) {
157
+ waveSurferRef.current.setTime(wavegroup.state.active.getCurrentTime())
158
+ }
159
+
160
+ play()
161
+ }, [showSkeleton, pause, play, wavegroup])
162
+
163
+ const handleCardClick = useCallback(() => {
164
+ if (showSkeleton) return
165
+ onSelect?.()
166
+ }, [showSkeleton, onSelect])
167
+
168
+ return (
169
+ <div
170
+ className={classNames(
171
+ styles.previewCard,
172
+ selected && styles.selected,
173
+ isPlaying && styles.playing,
174
+ showSkeleton && styles.skeleton,
175
+ className,
176
+ )}
177
+ onClick={handleCardClick}
178
+ role="button"
179
+ tabIndex={showSkeleton ? -1 : 0}
180
+ onKeyDown={(e) => {
181
+ if (e.key === 'Enter' || e.key === ' ') {
182
+ e.preventDefault()
183
+ handleCardClick()
184
+ }
185
+ }}
186
+ >
187
+ <button
188
+ type="button"
189
+ className={classNames(styles.leftButton, isPlaying && styles.isPlaying)}
190
+ onClick={(e) => {
191
+ e.stopPropagation()
192
+ handleTogglePlay()
193
+ }}
194
+ disabled={showSkeleton}
195
+ aria-label={isPlaying ? 'Pause' : 'Play'}
196
+ >
197
+ <span className={styles.equalizer} aria-hidden="true">
198
+ <span className={styles.equalizerBar} />
199
+ <span className={styles.equalizerBar} />
200
+ <span className={styles.equalizerBar} />
201
+ <span className={styles.equalizerBar} />
202
+ <span className={styles.equalizerBar} />
203
+ </span>
204
+ <PauseIcon
205
+ width={18}
206
+ height={18}
207
+ className={styles.pauseOnHover}
208
+ aria-hidden="true"
209
+ />
210
+ <PlayIcon
211
+ width={18}
212
+ height={18}
213
+ className={styles.playIcon}
214
+ aria-hidden="true"
215
+ />
216
+ </button>
217
+
218
+ <div
219
+ className={styles.waveformContainer}
220
+ onClick={(e) => e.stopPropagation()}
221
+ >
222
+ {audio && (
223
+ <WavesurferPlayer
224
+ url={audio}
225
+ waveColor={waveColor}
226
+ progressColor={progressColor}
227
+ normalize
228
+ barWidth={2}
229
+ barHeight={6}
230
+ cursorWidth={0}
231
+ height={40}
232
+ onReady={onReady}
233
+ onSeeking={onSeeking}
234
+ onPlay={onPlay}
235
+ onPause={onPause}
236
+ onFinish={onFinish}
237
+ backend={isSafari ? 'WebAudio' : 'MediaElement'}
238
+ />
239
+ )}
240
+ {showSkeleton && (
241
+ <span className={styles.skeletonWaveform} aria-hidden="true">
242
+ <span className={styles.skeletonMask} />
243
+ </span>
244
+ )}
245
+ </div>
246
+
247
+ {actions && (
248
+ <div className={styles.actions} onClick={(e) => e.stopPropagation()}>
249
+ {actions}
250
+ </div>
251
+ )}
252
+ </div>
253
+ )
254
+ }
255
+
256
+ PreviewCard.displayName = 'PreviewCard'
@@ -0,0 +1,257 @@
1
+ @keyframes skeleton_animate_mask {
2
+ 0% {
3
+ mask-position: 200% 50%;
4
+ }
5
+ 100% {
6
+ mask-position: -100% 50%;
7
+ }
8
+ }
9
+
10
+ @keyframes equalizerBar1 {
11
+ 0%,
12
+ 100% {
13
+ height: 17px;
14
+ }
15
+ 50% {
16
+ height: 7px;
17
+ }
18
+ }
19
+
20
+ @keyframes equalizerBar2 {
21
+ 0%,
22
+ 100% {
23
+ height: 5px;
24
+ }
25
+ 50% {
26
+ height: 17px;
27
+ }
28
+ }
29
+
30
+ @keyframes equalizerBar3 {
31
+ 0%,
32
+ 100% {
33
+ height: 21px;
34
+ }
35
+ 50% {
36
+ height: 9px;
37
+ }
38
+ }
39
+
40
+ @keyframes equalizerBar4 {
41
+ 0%,
42
+ 100% {
43
+ height: 17px;
44
+ }
45
+ 50% {
46
+ height: 5px;
47
+ }
48
+ }
49
+
50
+ @keyframes equalizerBar5 {
51
+ 0%,
52
+ 100% {
53
+ height: 7px;
54
+ }
55
+ 50% {
56
+ height: 17px;
57
+ }
58
+ }
59
+
60
+ .previewCard {
61
+ display: flex;
62
+ align-items: center;
63
+ gap: 12px;
64
+ width: 100%;
65
+ max-width: 344px;
66
+ padding: 4px;
67
+ border-radius: 8px;
68
+ border: 1px solid var(--neutral-alpha-4);
69
+ background: transparent;
70
+ cursor: pointer;
71
+ box-sizing: border-box;
72
+ transition: background-color 0.2s ease, border-color 0.2s ease;
73
+ }
74
+
75
+ .previewCard:hover:not(.selected):not(.skeleton):not(.playing),
76
+ .previewCard:has(.leftButton:hover):not(.selected):not(.skeleton) {
77
+ background-color: var(--neutral-alpha-2);
78
+ }
79
+
80
+ .selected {
81
+ background-color: var(--neutral-alpha-2);
82
+ border: 2px solid var(--colors-accent-accent-10);
83
+ padding: 3px;
84
+ }
85
+
86
+ .skeleton {
87
+ background: linear-gradient(
88
+ 90deg,
89
+ var(--neutral-alpha-3) 0%,
90
+ rgba(221, 235, 236, 0.02) 61.19%
91
+ );
92
+ pointer-events: none;
93
+ border-color: transparent;
94
+ }
95
+
96
+ .leftButton {
97
+ display: flex;
98
+ align-items: center;
99
+ justify-content: center;
100
+ flex-shrink: 0;
101
+ width: 40px;
102
+ height: 40px;
103
+ padding: 0;
104
+ border: none;
105
+ border-radius: 6px;
106
+ background: var(--neutral-alpha-3);
107
+ color: var(--neutral-alpha-11);
108
+ cursor: pointer;
109
+ }
110
+
111
+ .leftButton:disabled {
112
+ cursor: default;
113
+ pointer-events: none;
114
+ }
115
+
116
+ .skeleton .leftButton,
117
+ .skeleton .actions {
118
+ opacity: 0;
119
+ }
120
+
121
+ .equalizer {
122
+ display: none;
123
+ align-items: center;
124
+ justify-content: center;
125
+ gap: 1.8px;
126
+ width: 19px;
127
+ height: 21px;
128
+ }
129
+
130
+ .equalizerBar {
131
+ display: block;
132
+ width: 2px;
133
+ border-radius: 4.8px;
134
+ background: var(--neutral-alpha-11);
135
+ flex-shrink: 0;
136
+ }
137
+
138
+ .equalizerBar:nth-child(1) {
139
+ height: 17px;
140
+ animation: equalizerBar1 0.8s ease-in-out infinite;
141
+ }
142
+
143
+ .equalizerBar:nth-child(2) {
144
+ height: 5px;
145
+ animation: equalizerBar2 0.8s ease-in-out infinite;
146
+ }
147
+
148
+ .equalizerBar:nth-child(3) {
149
+ height: 21px;
150
+ animation: equalizerBar3 0.8s ease-in-out infinite;
151
+ }
152
+
153
+ .equalizerBar:nth-child(4) {
154
+ height: 17px;
155
+ animation: equalizerBar4 0.8s ease-in-out infinite;
156
+ }
157
+
158
+ .equalizerBar:nth-child(5) {
159
+ height: 7px;
160
+ animation: equalizerBar5 0.8s ease-in-out infinite;
161
+ }
162
+
163
+ .pauseOnHover {
164
+ display: none;
165
+ }
166
+
167
+ .playIcon {
168
+ display: flex;
169
+ }
170
+
171
+ .leftButton.isPlaying .equalizer {
172
+ display: flex;
173
+ }
174
+
175
+ .leftButton.isPlaying .playIcon {
176
+ display: none !important;
177
+ }
178
+
179
+ .previewCard.playing:has(.leftButton:hover) .equalizer {
180
+ display: none;
181
+ }
182
+
183
+ .previewCard.playing:has(.leftButton:hover) .pauseOnHover {
184
+ display: flex;
185
+ }
186
+
187
+ .waveformContainer {
188
+ position: relative;
189
+ flex: 1 1 0;
190
+ min-width: 0;
191
+ height: 40px;
192
+ overflow: hidden;
193
+ cursor: pointer;
194
+ }
195
+
196
+ .waveformContainer :global(wave) {
197
+ display: block;
198
+ }
199
+
200
+ .skeleton .waveformContainer :global(wave) {
201
+ visibility: hidden;
202
+ }
203
+
204
+ .skeletonWaveform {
205
+ position: absolute;
206
+ inset: 0;
207
+ overflow: hidden;
208
+ border-radius: 4px;
209
+ }
210
+
211
+ .skeletonMask {
212
+ width: 100%;
213
+ height: 100%;
214
+ display: block;
215
+ background-repeat: repeat-x;
216
+ background-position: 30px center;
217
+ mask-image: radial-gradient(circle, blue 50%, rgba(255, 255, 0, 0.5) 50%);
218
+ mask-size: 400% 400%;
219
+ animation: skeleton_animate_mask 3s linear infinite;
220
+ -webkit-animation: skeleton_animate_mask 3s linear infinite;
221
+ background-image: url('/waveform.svg');
222
+ }
223
+
224
+ .actions {
225
+ display: flex;
226
+ flex-shrink: 0;
227
+ align-items: center;
228
+ justify-content: center;
229
+ width: 24px;
230
+ height: 24px;
231
+ border-radius: 4px;
232
+ opacity: 0.4;
233
+ transition: opacity 0.2s ease;
234
+ }
235
+
236
+ .actions :global(button) {
237
+ display: inline-flex;
238
+ align-items: center;
239
+ justify-content: center;
240
+ width: 24px !important;
241
+ height: 24px !important;
242
+ min-width: 24px !important;
243
+ min-height: 24px !important;
244
+ padding: 0 !important;
245
+ border-radius: 4px;
246
+ }
247
+
248
+ .actions :global(svg) {
249
+ width: 16px;
250
+ height: 16px;
251
+ }
252
+
253
+ .previewCard:hover .actions,
254
+ .previewCard:has(.leftButton:hover) .actions,
255
+ .selected .actions {
256
+ opacity: 1;
257
+ }
@@ -0,0 +1,112 @@
1
+ import React, { useMemo, useState } from 'react'
2
+ import { Flex, Text } from '@radix-ui/themes'
3
+ import { PreviewCard } from './PreviewCard'
4
+ import { MoreButton } from '../MoreButton/MoreButton'
5
+
6
+ const SAMPLE_AUDIO = {
7
+ zuri: 'https://storage.googleapis.com/moises-api-assets/voice/demo_zuri.m4a',
8
+ amy: 'https://storage.googleapis.com/moises-api-assets/voice/demo_amy.m4a',
9
+ rosa: 'https://storage.googleapis.com/moises-api-assets/voice/demo_rosa.m4a',
10
+ aisha: 'https://storage.googleapis.com/moises-api-assets/voice/demo_aisha.m4a',
11
+ }
12
+
13
+ const PREVIEW_ITEMS = [
14
+ { id: 'zuri', label: 'Zuri', audio: SAMPLE_AUDIO.zuri },
15
+ { id: 'amy', label: 'Amy', audio: SAMPLE_AUDIO.amy },
16
+ { id: 'rosa', label: 'Rosa', audio: SAMPLE_AUDIO.rosa },
17
+ { id: 'aisha', label: 'Aisha', audio: SAMPLE_AUDIO.aisha },
18
+ ]
19
+
20
+ export default {
21
+ title: 'Components/PreviewCard',
22
+ component: PreviewCard,
23
+ parameters: {
24
+ layout: 'centered',
25
+ docs: {
26
+ description: {
27
+ component: `
28
+ Preview card for tone previews with waveform, play/pause, selection, and continuous audio across multiple cards via \`wavegroup\`.
29
+ `,
30
+ },
31
+ },
32
+ },
33
+ decorators: [
34
+ (Story) => (
35
+ <Flex direction="column" gap="2" width="344px">
36
+ <Story />
37
+ </Flex>
38
+ ),
39
+ ],
40
+ }
41
+
42
+ export const Default = {
43
+ args: {
44
+ audio: SAMPLE_AUDIO.zuri,
45
+ },
46
+ }
47
+
48
+ export const Selected = {
49
+ args: {
50
+ audio: SAMPLE_AUDIO.amy,
51
+ selected: true,
52
+ },
53
+ }
54
+
55
+ export const Loading = {
56
+ args: {
57
+ audio: SAMPLE_AUDIO.rosa,
58
+ loading: true,
59
+ },
60
+ }
61
+
62
+ export const WithActions = {
63
+ args: {
64
+ audio: SAMPLE_AUDIO.zuri,
65
+ selected: true,
66
+ actions: <MoreButton aria-label="More options" />,
67
+ },
68
+ }
69
+
70
+ function PreviewCardGroupStory() {
71
+ const [selectedId, setSelectedId] = useState('zuri')
72
+ const [wavegroupState, setWavegroupState] = useState({
73
+ lastPlayed: null,
74
+ active: null,
75
+ isPlaying: false,
76
+ id: 'preview-card-group',
77
+ })
78
+
79
+ const wavegroup = useMemo(
80
+ () => ({
81
+ state: wavegroupState,
82
+ setter: setWavegroupState,
83
+ }),
84
+ [wavegroupState],
85
+ )
86
+
87
+ return (
88
+ <Flex direction="column" gap="3" width="344px">
89
+ <Text size="1" color="gray">
90
+ Click the card (outside waveform/play) to select. Play on another card
91
+ continues from the same position.
92
+ </Text>
93
+ {PREVIEW_ITEMS.map((item) => (
94
+ <PreviewCard
95
+ key={item.id}
96
+ audio={item.audio}
97
+ selected={selectedId === item.id}
98
+ wavegroup={wavegroup}
99
+ onSelect={() => setSelectedId(item.id)}
100
+ actions={<MoreButton aria-label={`More options for ${item.label}`} />}
101
+ />
102
+ ))}
103
+ </Flex>
104
+ )
105
+ }
106
+
107
+ export const Group = {
108
+ render: () => <PreviewCardGroupStory />,
109
+ parameters: {
110
+ layout: 'padded',
111
+ },
112
+ }
package/src/index.jsx CHANGED
@@ -148,6 +148,7 @@ export { TrackHeader } from './components/TrackHeader/TrackHeader'
148
148
  export { useForm } from './components/useForm/useForm'
149
149
  export { VoiceConversionForm } from './components/VoiceConversionForm/VoiceConversionForm'
150
150
  export { Waveform } from './components/VoiceConversionForm/Waveform/Waveform'
151
+ export { PreviewCard } from './components/PreviewCard/PreviewCard'
151
152
  export { SpecialDialog } from './components/SpecialDialog/SpecialDialog'
152
153
 
153
154
  export { VerticalSegmentControl } from './components/VerticalSegmentControl/VerticalSegmentControl'