Dashboard sipadu mbip
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

useBreakpoint.js 3.3KB

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