44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
'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,
|
|
}
|
|
}
|