Dashboard sipadu mbip
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

useBreakpoint.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import useMediaQuery from './useMediaQuery';
  2. import { useMemo } from 'react';
  3. /**
  4. * Create a responsive hook we a set of breakpoint names and widths.
  5. * You can use any valid css units as well as a numbers (for pixels).
  6. *
  7. * **NOTE:** The object key order is important! it's assumed to be in order from smallest to largest
  8. *
  9. * ```ts
  10. * const useBreakpoint = createBreakpointHook({
  11. * xs: 0,
  12. * sm: 576,
  13. * md: 768,
  14. * lg: 992,
  15. * xl: 1200,
  16. * })
  17. * ```
  18. *
  19. * **Watch out!** using string values will sometimes construct media queries using css `calc()` which
  20. * is NOT supported in media queries by all browsers at the moment. use numbers for
  21. * the widest range of browser support.
  22. *
  23. * @param breakpointValues A object hash of names to breakpoint dimensions
  24. */
  25. export function createBreakpointHook(breakpointValues) {
  26. var names = Object.keys(breakpointValues);
  27. function and(query, next) {
  28. if (query === next) {
  29. return next;
  30. }
  31. return query ? query + " and " + next : next;
  32. }
  33. function getNext(breakpoint) {
  34. return names[Math.min(names.indexOf(breakpoint) + 1, names.length - 1)];
  35. }
  36. function getMaxQuery(breakpoint) {
  37. var next = getNext(breakpoint);
  38. var value = breakpointValues[next];
  39. if (typeof value === 'number') value = value - 0.2 + "px";else value = "calc(" + value + " - 0.2px)";
  40. return "(max-width: " + value + ")";
  41. }
  42. function getMinQuery(breakpoint) {
  43. var value = breakpointValues[breakpoint];
  44. if (typeof value === 'number') {
  45. value = value + "px";
  46. }
  47. return "(min-width: " + value + ")";
  48. }
  49. /**
  50. * Match a set of breakpoints
  51. *
  52. * ```tsx
  53. * const MidSizeOnly = () => {
  54. * const isMid = useBreakpoint({ lg: 'down', sm: 'up' });
  55. *
  56. * if (isMid) return <div>On a Reasonable sized Screen!</div>
  57. * return null;
  58. * }
  59. * ```
  60. * @param breakpointMap An object map of breakpoints and directions, queries are constructed using "and" to join
  61. * breakpoints together
  62. */
  63. function useBreakpoint(breakpointOrMap, direction) {
  64. if (direction === void 0) {
  65. direction = true;
  66. }
  67. var breakpointMap;
  68. if (typeof breakpointOrMap === 'object') {
  69. breakpointMap = breakpointOrMap;
  70. } else {
  71. var _breakpointMap;
  72. breakpointMap = (_breakpointMap = {}, _breakpointMap[breakpointOrMap] = direction, _breakpointMap);
  73. }
  74. var query = useMemo(function () {
  75. return Object.entries(breakpointMap).reduce(function (query, _ref) {
  76. var key = _ref[0],
  77. direction = _ref[1];
  78. if (direction === 'up' || direction === true) {
  79. query = and(query, getMinQuery(key));
  80. }
  81. if (direction === 'down' || direction === true) {
  82. query = and(query, getMaxQuery(key));
  83. }
  84. return query;
  85. }, '');
  86. }, [JSON.stringify(breakpointMap)]);
  87. return useMediaQuery(query);
  88. }
  89. return useBreakpoint;
  90. }
  91. var useBreakpoint = createBreakpointHook({
  92. xs: 0,
  93. sm: 576,
  94. md: 768,
  95. lg: 992,
  96. xl: 1200
  97. });
  98. export default useBreakpoint;