UNPKG

@mantine/core

Version:

React components library focused on usability, accessibility and developer experience

1 lines 2.44 kB
{"version":3,"file":"use-matches.mjs","names":[],"sources":["../../../../src/core/MantineProvider/use-matches/use-matches.ts"],"sourcesContent":["import { useMediaQuery, UseMediaQueryOptions } from '@mantine/hooks';\nimport { useMantineTheme } from '../MantineThemeProvider';\nimport { MantineBreakpoint } from '../theme.types';\n\ntype UseMatchesInput<T> = Partial<Record<MantineBreakpoint, T>>;\n\nconst BREAKPOINTS = ['xs', 'sm', 'md', 'lg', 'xl'];\n\nfunction getFirstMatchingValue<T>(\n value: UseMatchesInput<T>,\n biggestMatch: MantineBreakpoint | undefined\n): T {\n if (!biggestMatch) {\n return value.base!;\n }\n\n let index = BREAKPOINTS.indexOf(biggestMatch);\n\n while (index >= 0) {\n if (BREAKPOINTS[index] in value) {\n return value[BREAKPOINTS[index]]!;\n }\n index -= 1;\n }\n\n return value.base!;\n}\n\nfunction getFirstMatchingBreakpoint(matches: (boolean | undefined)[]) {\n return matches.findLastIndex((v) => v);\n}\n\nexport function useMatches<T>(payload: UseMatchesInput<T>, options?: UseMediaQueryOptions) {\n const theme = useMantineTheme();\n const xsMatches = useMediaQuery(`(min-width: ${theme.breakpoints.xs})`, false, options);\n const smMatches = useMediaQuery(`(min-width: ${theme.breakpoints.sm})`, false, options);\n const mdMatches = useMediaQuery(`(min-width: ${theme.breakpoints.md})`, false, options);\n const lgMatches = useMediaQuery(`(min-width: ${theme.breakpoints.lg})`, false, options);\n const xlMatches = useMediaQuery(`(min-width: ${theme.breakpoints.xl})`, false, options);\n\n const breakpoints = [xsMatches, smMatches, mdMatches, lgMatches, xlMatches];\n const firstMatchingBreakpointIndex = getFirstMatchingBreakpoint(breakpoints);\n return getFirstMatchingValue(payload, BREAKPOINTS[firstMatchingBreakpointIndex]);\n}\n"],"mappings":";;;;AAMA,MAAM,cAAc;CAAC;CAAM;CAAM;CAAM;CAAM;CAAK;AAElD,SAAS,sBACP,OACA,cACG;AACH,KAAI,CAAC,aACH,QAAO,MAAM;CAGf,IAAI,QAAQ,YAAY,QAAQ,aAAa;AAE7C,QAAO,SAAS,GAAG;AACjB,MAAI,YAAY,UAAU,MACxB,QAAO,MAAM,YAAY;AAE3B,WAAS;;AAGX,QAAO,MAAM;;AAGf,SAAS,2BAA2B,SAAkC;AACpE,QAAO,QAAQ,eAAe,MAAM,EAAE;;AAGxC,SAAgB,WAAc,SAA6B,SAAgC;CACzF,MAAM,QAAQ,iBAAiB;AAS/B,QAAO,sBAAsB,SAAS,YADD,2BADjB;EANF,cAAc,eAAe,MAAM,YAAY,GAAG,IAAI,OAAO,QAAQ;EACrE,cAAc,eAAe,MAAM,YAAY,GAAG,IAAI,OAAO,QAAQ;EACrE,cAAc,eAAe,MAAM,YAAY,GAAG,IAAI,OAAO,QAAQ;EACrE,cAAc,eAAe,MAAM,YAAY,GAAG,IAAI,OAAO,QAAQ;EACrE,cAAc,eAAe,MAAM,YAAY,GAAG,IAAI,OAAO,QAAQ;EAEZ,CACC,EACI"}