@moises.ai/design-system 4.21.17 → 4.21.19

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.21.17",
3
+ "version": "4.21.19",
4
4
  "description": "Design System package based on @radix-ui/themes with custom defaults",
5
5
  "private": false,
6
6
  "type": "module",
@@ -1,4 +1,4 @@
1
- import { forwardRef, useMemo } from 'react'
1
+ import { forwardRef, useLayoutEffect, useMemo, useRef, useState } from 'react'
2
2
  import classNames from 'classnames'
3
3
  import { Text } from '../Text/Text'
4
4
  import { Tooltip } from '../Tooltip/Tooltip'
@@ -33,7 +33,7 @@ import styles from './InstrumentSelect.module.css'
33
33
  * @property {InstrumentSelectValue} [selectedValue] - Alias for `value` (backwards-compat with `InstrumentSelector`).
34
34
  * @property {(item: InstrumentSelectItem) => void} [onSelect] - Fires for every card click, including locked items. The parent decides whether to update `value`, open a paywall, etc.
35
35
  * @property {'horizontal' | 'vertical'} [orientation='horizontal'] - `horizontal` fills each row left to right, then wraps. `vertical` fills each column top to bottom, for `rows` rows, then starts the next column.
36
- * @property {number | { initial?: string | number, sm?: string | number, md?: string | number, lg?: string | number }} [columns=2] - Horizontal only. Maximum items per row. If the container cannot fit that many at the card's minimum width, the extras wrap onto the next row.
36
+ * @property {number | { initial?: string | number, sm?: string | number, md?: string | number, lg?: string | number }} [columns=2] - Horizontal only. Number of column tracks in the row. A short row stretches so its items split those tracks: 2 items in 4 columns means each item spans 2. If the container cannot fit that many tracks at the card's minimum width, the row uses fewer tracks and wraps.
37
37
  * @property {number} [rows] - Vertical only. How many rows to fill before starting another column. Defaults to one column (every item on its own row).
38
38
  * @property {'neutral' | 'cyan'} [variant='neutral'] - Visual variant. `neutral` is the default bordered card. `cyan` uses the accent-tinted background and selection border.
39
39
  * @property {'small' | 'medium' | 'large'} [size='small'] - Card size. `small` is the compact horizontal card. `medium` is the 64px vertical card (min 150px). `large` is the 160×80 vertical card.
@@ -70,6 +70,27 @@ const TRACK_MAX = {
70
70
  large: 160,
71
71
  }
72
72
 
73
+ function columnsThatFit(width, columns, min, gap) {
74
+ if (!width) return columns
75
+ const count = Math.floor((width + gap) / (min + gap))
76
+ return Math.max(1, Math.min(columns, count))
77
+ }
78
+
79
+ /**
80
+ * How many of `columnCount` tracks this item spans. A row with fewer items than
81
+ * tracks splits the tracks across those items (2 items in 4 columns span 2 each).
82
+ */
83
+ function itemColumnSpan(index, itemCount, columnCount) {
84
+ const rowStart = Math.floor(index / columnCount) * columnCount
85
+ const remaining = itemCount - rowStart
86
+ const inRow = Math.min(columnCount, remaining)
87
+ if (inRow >= columnCount) return 1
88
+ const base = Math.floor(columnCount / inRow)
89
+ const extra = columnCount % inRow
90
+ const indexInRow = index - rowStart
91
+ return base + (indexInRow >= inRow - extra ? 1 : 0)
92
+ }
93
+
73
94
  function resolveColumns(columns) {
74
95
  if (typeof columns === 'number' && Number.isFinite(columns)) return Math.max(1, columns)
75
96
  if (columns && typeof columns === 'object') {
@@ -87,13 +108,12 @@ function resolveColumns(columns) {
87
108
  *
88
109
  * Vertical fills `rows` rows top to bottom, then starts the next column.
89
110
  */
90
- function getGridStyle(orientation, size, columns, rows, itemCount, fit) {
111
+ function getGridStyle(orientation, size, columns, rows, itemCount, fit, trackCount) {
91
112
  const min = TRACK_MIN[size] ?? TRACK_MIN.small
92
113
  const fill = fit === 'fill' || size === 'small'
93
114
  const max = fill ? undefined : TRACK_MAX[size]
94
115
  const gap = gridGap(size)
95
116
  const gaps = Math.max(columns - 1, 0)
96
- const share = `calc((100% - ${gap}px * ${gaps}) / ${columns})`
97
117
 
98
118
  if (orientation === 'vertical') {
99
119
  const rowCount = Math.max(rows ?? itemCount, 1)
@@ -114,9 +134,10 @@ function getGridStyle(orientation, size, columns, rows, itemCount, fit) {
114
134
  return {
115
135
  display: 'grid',
116
136
  gap,
117
- gridTemplateColumns: `repeat(auto-fit, minmax(max(${min}px, ${share}), 1fr))`,
118
- justifyContent: 'stretch',
119
- width: '100%',
137
+ gridTemplateColumns: `repeat(${trackCount}, ${min}px)`,
138
+ justifyContent: 'start',
139
+ width: 'max-content',
140
+ maxWidth: '100%',
120
141
  }
121
142
  }
122
143
 
@@ -194,14 +215,32 @@ export const InstrumentSelect = forwardRef(function InstrumentSelect(
194
215
  ref,
195
216
  ) {
196
217
  const resolvedValue = value ?? selectedValue
218
+ const gridRef = useRef(null)
219
+ const [gridWidth, setGridWidth] = useState(0)
197
220
 
198
221
  const cols = useMemo(() => resolveColumns(columns), [columns])
199
222
 
200
223
  const fill = fit === 'fill' || size === 'small'
224
+ const minTrack = TRACK_MIN[size] ?? TRACK_MIN.small
225
+ const gap = gridGap(size)
226
+ const trackCount =
227
+ fill && orientation === 'horizontal'
228
+ ? columnsThatFit(gridWidth, cols, minTrack, gap)
229
+ : cols
230
+
231
+ useLayoutEffect(() => {
232
+ const element = gridRef.current
233
+ if (!element || !fill || orientation !== 'horizontal') return undefined
234
+ const update = () => setGridWidth(element.clientWidth)
235
+ update()
236
+ const observer = new ResizeObserver(update)
237
+ observer.observe(element)
238
+ return () => observer.disconnect()
239
+ }, [fill, orientation, cols, size, items.length])
201
240
 
202
241
  const gridStyle = useMemo(
203
- () => getGridStyle(orientation, size, cols, rows, items.length, fit),
204
- [orientation, size, cols, rows, items.length, fit],
242
+ () => getGridStyle(orientation, size, cols, rows, items.length, fit, trackCount),
243
+ [orientation, size, cols, rows, items.length, fit, trackCount],
205
244
  )
206
245
 
207
246
  return (
@@ -220,6 +259,7 @@ export const InstrumentSelect = forwardRef(function InstrumentSelect(
220
259
  )}
221
260
 
222
261
  <div
262
+ ref={gridRef}
223
263
  className={styles.grid}
224
264
  data-orientation={orientation}
225
265
  data-fit={fill ? 'fill' : 'fixed'}
@@ -227,11 +267,15 @@ export const InstrumentSelect = forwardRef(function InstrumentSelect(
227
267
  role="group"
228
268
  aria-label={title}
229
269
  >
230
- {items.map((item) => {
270
+ {items.map((item, index) => {
231
271
  const itemText = item.text ?? item.label ?? item.name
232
272
  const ItemIcon = item.Icon ?? item.icon
233
273
  const itemLocked = item.locked ?? item.isLocked ?? false
234
274
  const itemDisabled = item.disabled ?? false
275
+ const span =
276
+ fill && orientation === 'horizontal'
277
+ ? itemColumnSpan(index, items.length, trackCount)
278
+ : 1
235
279
 
236
280
  return (
237
281
  <InstrumentSelectCard
@@ -245,6 +289,7 @@ export const InstrumentSelect = forwardRef(function InstrumentSelect(
245
289
  disabled={itemDisabled}
246
290
  variant={variant}
247
291
  size={size}
292
+ style={span > 1 ? { gridColumn: `span ${span}` } : undefined}
248
293
  onClick={() => onSelect?.(item)}
249
294
  />
250
295
  )
@@ -252,6 +252,7 @@
252
252
  max-width: 100%;
253
253
  margin-right: 0;
254
254
  font-weight: 500;
255
+ line-height: 18px;
255
256
  }
256
257
 
257
258
  .card[data-size="large"] .cardText {
@@ -307,6 +307,48 @@ export const Large = {
307
307
  },
308
308
  }
309
309
 
310
+ export const FourColumns = {
311
+ render: () => {
312
+ const [basic, setBasic] = useState('vocals-instruments')
313
+ const [voice, setVoice] = useState('vocals')
314
+
315
+ return (
316
+ <div style={{ display: 'flex', flexDirection: 'column', gap: 24 }}>
317
+ <InstrumentSelect
318
+ title="2 itens, 4 columns"
319
+ variant="cyan"
320
+ size="large"
321
+ fit="fill"
322
+ columns={4}
323
+ items={[
324
+ { value: 'vocals-instruments', text: 'Vocals & Instruments', Icon: VocalIcon },
325
+ {
326
+ value: 'vocals-bass',
327
+ text: 'Vocals, Bass, Drums & Others',
328
+ Icon: VocalsBackgroundIcon,
329
+ },
330
+ ]}
331
+ value={basic}
332
+ onSelect={(item) => setBasic(item.value)}
333
+ />
334
+ <InstrumentSelect
335
+ title="4 itens, 5 columns"
336
+ variant="cyan"
337
+ size="large"
338
+ fit="fixed"
339
+ columns={5}
340
+ items={INSTRUMENTS.slice(0, 5)}
341
+ value={voice}
342
+ onSelect={(item) => setVoice(item.value)}
343
+ />
344
+ </div>
345
+ )
346
+ },
347
+ parameters: {
348
+ containerWidth: 880,
349
+ },
350
+ }
351
+
310
352
  export const MediumFill = {
311
353
  render: verticalStory(['vocals']),
312
354
  args: {