-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse-breakpoints.tsx
33 lines (28 loc) · 1.05 KB
/
use-breakpoints.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { useMediaQuery } from 'react-responsive';
import resolveConfig from 'tailwindcss/resolveConfig';
import { Config, ScreensConfig } from 'tailwindcss/types/config';
import tailwindConfig from '@/tailwind.config';
const fullConfig = resolveConfig(tailwindConfig as unknown as Config);
const breakpoints = fullConfig?.theme?.screens || {
xs: '480px',
sm: '640px',
md: '768px',
lg: '1024px',
xl: '1280px'
};
export function useBreakpoint<K extends string>(breakpointKey: K) {
const breakpointValue =
breakpoints[breakpointKey as keyof typeof breakpoints];
const bool = useMediaQuery({
query: `(max-width: ${breakpointValue})`
});
const capitalizedKey =
breakpointKey[0].toUpperCase() + breakpointKey.substring(1);
type KeyAbove = `isAbove${Capitalize<K>}`;
type KeyBelow = `isBelow${Capitalize<K>}`;
return {
[breakpointKey]: Number(String(breakpointValue).replace(/[^0-9]/g, '')),
[`isAbove${capitalizedKey}`]: !bool,
[`isBelow${capitalizedKey}`]: bool
} as Record<K, number> & Record<KeyAbove | KeyBelow, boolean>;
}