source: src/hooks/use-responsive.ts@ dc3406b

main
Last change on this file since dc3406b was 5d6f37a, checked in by Naum Shapkarovski <naumshapkarovski@…>, 7 weeks ago

add customer

  • Property mode set to 100644
File size: 1.4 KB
Line 
1// @mui
2import { useTheme, Breakpoint } from '@mui/material/styles';
3import useMediaQuery from '@mui/material/useMediaQuery';
4
5// ----------------------------------------------------------------------
6
7type ReturnType = boolean;
8
9export type Query = 'up' | 'down' | 'between' | 'only';
10
11export type Value = Breakpoint | number;
12
13export function useResponsive(query: Query, start?: Value, end?: Value): ReturnType {
14 const theme = useTheme();
15
16 const mediaUp = useMediaQuery(theme.breakpoints.up(start as Value));
17
18 const mediaDown = useMediaQuery(theme.breakpoints.down(start as Value));
19
20 const mediaBetween = useMediaQuery(theme.breakpoints.between(start as Value, end as Value));
21
22 const mediaOnly = useMediaQuery(theme.breakpoints.only(start as Breakpoint));
23
24 if (query === 'up') {
25 return mediaUp;
26 }
27
28 if (query === 'down') {
29 return mediaDown;
30 }
31
32 if (query === 'between') {
33 return mediaBetween;
34 }
35
36 return mediaOnly;
37}
38
39// ----------------------------------------------------------------------
40
41type BreakpointOrNull = Breakpoint | null;
42
43export function useWidth() {
44 const theme = useTheme();
45
46 const keys = [...theme.breakpoints.keys].reverse();
47
48 return (
49 keys.reduce((output: BreakpointOrNull, key: Breakpoint) => {
50 // eslint-disable-next-line react-hooks/rules-of-hooks
51 const matches = useMediaQuery(theme.breakpoints.up(key));
52
53 return !output && matches ? key : output;
54 }, null) || 'xs'
55 );
56}
Note: See TracBrowser for help on using the repository browser.