|
| 1 | +import { useMemo } from 'react' |
| 2 | + |
| 3 | +export interface ControlsData { |
| 4 | + itemsCount: number |
| 5 | + frameWidth: number |
| 6 | + gap: number |
| 7 | + containerWidth: number |
| 8 | +} |
| 9 | + |
| 10 | +interface ControlsProps { |
| 11 | + data: ControlsData |
| 12 | + onChange: (data: ControlsData) => void |
| 13 | +} |
| 14 | + |
| 15 | +export type AspectRatio = [number, number] |
| 16 | + |
| 17 | +const aspectRatios: AspectRatio[] = [ |
| 18 | + [1, 1], |
| 19 | + [2, 3], |
| 20 | + [3, 2] |
| 21 | +] |
| 22 | + |
| 23 | +function getRandomAspectRatio(prevAspectRatio?: AspectRatio) { |
| 24 | + const newAspectRatio = |
| 25 | + aspectRatios[Math.floor(Math.random() * aspectRatios.length)] |
| 26 | + |
| 27 | + if (newAspectRatio === prevAspectRatio) { |
| 28 | + return getRandomAspectRatio(prevAspectRatio) |
| 29 | + } |
| 30 | + |
| 31 | + return newAspectRatio |
| 32 | +} |
| 33 | + |
| 34 | +function getRandomColor() { |
| 35 | + return `hsl(${Math.floor(Math.random() * 360)}, 100%, 75%)` |
| 36 | +} |
| 37 | + |
| 38 | +export const defaultControlsData: ControlsData = { |
| 39 | + itemsCount: 35, |
| 40 | + frameWidth: 200, |
| 41 | + gap: 10, |
| 42 | + containerWidth: 100 |
| 43 | +} |
| 44 | + |
| 45 | +export function useItems(itemsCount: number) { |
| 46 | + return useMemo(() => { |
| 47 | + let aspectRatio: AspectRatio |
| 48 | + |
| 49 | + return Array.from({ |
| 50 | + length: itemsCount |
| 51 | + }, () => { |
| 52 | + aspectRatio = getRandomAspectRatio(aspectRatio) |
| 53 | + |
| 54 | + const backgroundColor = getRandomColor() |
| 55 | + |
| 56 | + return { |
| 57 | + width: aspectRatio[0], |
| 58 | + height: aspectRatio[1], |
| 59 | + backgroundColor |
| 60 | + } |
| 61 | + }) |
| 62 | + }, [itemsCount]) |
| 63 | +} |
| 64 | + |
| 65 | +export function Controls({ data, onChange }: ControlsProps) { |
| 66 | + const handleChange = (key: keyof ControlsData, value: number) => { |
| 67 | + onChange({ ...data, [key]: value }) |
| 68 | + } |
| 69 | + |
| 70 | + return ( |
| 71 | + <div className='controls'> |
| 72 | + <div className='control-group'> |
| 73 | + <label htmlFor='itemsCount'>Items Count: {data.itemsCount}</label> |
| 74 | + <input |
| 75 | + id='itemsCount' |
| 76 | + type='range' |
| 77 | + min='3' |
| 78 | + max='100' |
| 79 | + value={data.itemsCount} |
| 80 | + onChange={(e) => handleChange('itemsCount', Number(e.target.value))} |
| 81 | + /> |
| 82 | + </div> |
| 83 | + |
| 84 | + <div className='control-group'> |
| 85 | + <label htmlFor='frameWidth'>Frame Width: {data.frameWidth}px</label> |
| 86 | + <input |
| 87 | + id='frameWidth' |
| 88 | + type='range' |
| 89 | + min='50' |
| 90 | + max='300' |
| 91 | + value={data.frameWidth} |
| 92 | + onChange={(e) => handleChange('frameWidth', Number(e.target.value))} |
| 93 | + /> |
| 94 | + </div> |
| 95 | + |
| 96 | + <div className='control-group'> |
| 97 | + <label htmlFor='gap'>Gap: {data.gap}px</label> |
| 98 | + <input |
| 99 | + id='gap' |
| 100 | + type='range' |
| 101 | + min='0' |
| 102 | + max='40' |
| 103 | + value={data.gap} |
| 104 | + onChange={(e) => handleChange('gap', Number(e.target.value))} |
| 105 | + /> |
| 106 | + </div> |
| 107 | + |
| 108 | + <div className='control-group'> |
| 109 | + <label htmlFor='containerWidth'>Container Width: {data.containerWidth}%</label> |
| 110 | + <input |
| 111 | + id='containerWidth' |
| 112 | + type='range' |
| 113 | + min='0' |
| 114 | + max='100' |
| 115 | + value={data.containerWidth} |
| 116 | + onChange={(e) => handleChange('containerWidth', Number(e.target.value))} |
| 117 | + /> |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + ) |
| 121 | +} |
0 commit comments