260703_01_FST: First Commit
This commit is contained in:
43
registry/components/multi-select/use-responsive-settings.ts
Normal file
43
registry/components/multi-select/use-responsive-settings.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
'use client'
|
||||
|
||||
import * as React from 'react'
|
||||
import type { MultiSelectProps, MultiSelectResponsiveSettings } from './types'
|
||||
|
||||
export function useResponsiveSettings(
|
||||
responsive: MultiSelectProps['responsive'],
|
||||
defaultMaxCount: number,
|
||||
): Required<MultiSelectResponsiveSettings> {
|
||||
const [screen, setScreen] = React.useState<'mobile' | 'tablet' | 'desktop'>('desktop')
|
||||
|
||||
React.useEffect(() => {
|
||||
const update = () => {
|
||||
if (window.innerWidth < 640) setScreen('mobile')
|
||||
else if (window.innerWidth < 1024) setScreen('tablet')
|
||||
else setScreen('desktop')
|
||||
}
|
||||
|
||||
update()
|
||||
window.addEventListener('resize', update)
|
||||
return () => window.removeEventListener('resize', update)
|
||||
}, [])
|
||||
|
||||
if (!responsive) {
|
||||
return { maxCount: defaultMaxCount, hideIcons: false, compactMode: false }
|
||||
}
|
||||
|
||||
if (responsive === true) {
|
||||
const defaults = {
|
||||
mobile: { maxCount: 2, hideIcons: false, compactMode: true },
|
||||
tablet: { maxCount: 4, hideIcons: false, compactMode: false },
|
||||
desktop: { maxCount: 6, hideIcons: false, compactMode: false },
|
||||
}
|
||||
return defaults[screen]
|
||||
}
|
||||
|
||||
const current = responsive[screen]
|
||||
return {
|
||||
maxCount: current?.maxCount ?? defaultMaxCount,
|
||||
hideIcons: current?.hideIcons ?? false,
|
||||
compactMode: current?.compactMode ?? false,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user