@koine/react 1.0.85 → 1.0.86

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,169 @@
1
+ import { useState, useEffect, useMemo } from "react";
2
+ import { isBrowser, isUndefined, getMediaQueryWidthResolvers, } from "@koine/utils";
3
+ export function useMediaQueryWidthCreator(customBreakpoints) {
4
+ var queryResolvers = getMediaQueryWidthResolvers(customBreakpoints);
5
+ return function useMediaQueryWidth(media) {
6
+ var definition = media.substring(1);
7
+ var _a = definition.split("-"), rule = _a[0], ruleBreakpoint = _a[1];
8
+ if (isUndefined(ruleBreakpoint)) {
9
+ ruleBreakpoint = rule;
10
+ }
11
+ if (isUndefined(rule)) {
12
+ rule = "min";
13
+ }
14
+ // with the hook creator approach these breakpoint types cannot be deduced
15
+ // const [br1, br2] = ruleBreakpoint.split("-") as Split<
16
+ // typeof ruleBreakpoint,
17
+ // "-"
18
+ // >;
19
+ var _b = ruleBreakpoint.split("_"), br1 = _b[0], br2 = _b[1];
20
+ var query = queryResolvers[rule](br1, br2);
21
+ var mq = useMemo(function () { return (isBrowser ? window.matchMedia(query) : { matches: false }); }, [query]);
22
+ var _c = useState(mq.matches), matches = _c[0], setMatches = _c[1];
23
+ useEffect(function () {
24
+ var mq = window.matchMedia(query);
25
+ var handleChange = function (event) {
26
+ setMatches(event.matches);
27
+ };
28
+ setMatches(mq.matches);
29
+ // Safari < 14 can't use addEventListener on a MediaQueryList
30
+ // https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList#Browser_compatibility
31
+ if (!mq.addEventListener) {
32
+ // Update the state whenever the media query match state changes
33
+ mq.addListener(handleChange);
34
+ // Clean up on unmount and if the query changes
35
+ return function () {
36
+ mq.removeListener(handleChange);
37
+ };
38
+ }
39
+ mq.addEventListener("change", handleChange);
40
+ return function () {
41
+ mq.removeEventListener("change", handleChange);
42
+ };
43
+ }, [query]);
44
+ return matches;
45
+ };
46
+ }
47
+ export default useMediaQueryWidthCreator;
48
+ //// without creator it would be:
49
+ //// ---------------------------------------------------------------------------
50
+ // import { useState, useEffect, useMemo } from "react";
51
+ // import { isBrowser, type Split } from "@koine/utils";
52
+ // import { breakpoints as themeBreakpoints } from "@/config/theme/breakpoints";
53
+ // type Breakpoint = "xs" | "sm" | "md" | "lg" | "xl";
54
+ // type Breakpoints = Record<Breakpoint, number>;
55
+ // type MediaQuery =
56
+ // | `max:${Breakpoint}`
57
+ // | `min:${Breakpoint}`
58
+ // | `down:${Breakpoint}`
59
+ // | `up:${Breakpoint}`
60
+ // | `between:${Breakpoint}-${Breakpoint}`
61
+ // | `only:${Breakpoint}`;
62
+ // const breakpoints: Breakpoints = {
63
+ // xs: 0,
64
+ // ...themeBreakpoints,
65
+ // };
66
+ // const sortedBreakpointsNames = (
67
+ // Object.keys(breakpoints).map((key) => {
68
+ // const br = key as keyof typeof breakpoints;
69
+ // return [br, breakpoints[br]];
70
+ // }) as [Breakpoint, number][]
71
+ // )
72
+ // .sort((a, b) => a[1] - b[1])
73
+ // .map((item) => item[0]);
74
+ // const getNextBreakpoint = (breakpoint: Breakpoint) => {
75
+ // const index = sortedBreakpointsNames.indexOf(breakpoint);
76
+ // return sortedBreakpointsNames[index + 1];
77
+ // };
78
+ // /**
79
+ // * It behaves the same as `(min-width: ${value}px)`
80
+ // * where value is the given breakpoint value.
81
+ // * For ease of use this can be used both as a function `min("md")` and as an
82
+ // * object literal `min.md`.
83
+ // */
84
+ // const min = (br: Breakpoint) => `(min-width: ${breakpoints[br]}px)`;
85
+ // /**
86
+ // * It behaves the same as `(max-width: ${value}px)`
87
+ // * where value is the given breakpoint value.
88
+ // * For ease of use this can be used both as a function `max("md")` and as an
89
+ // * object literal `max.md`.
90
+ // */
91
+ // const max = (br: Breakpoint) => `(max-width: ${breakpoints[br] - 0.02}px)`;
92
+ // /**
93
+ // * It behaves the same as `min`
94
+ // * @inheritdoc {max}
95
+ // */
96
+ // const up = min;
97
+ // /**
98
+ // * It behaves similarly to `max` but you will use the "next" breakpoint,
99
+ // * specifying CSS that will apply from the given breakpoint and down.
100
+ // */
101
+ // const down = (br: Breakpoint) => {
102
+ // const brNext = getNextBreakpoint(br);
103
+ // // TODO: if br does not exists otherwise throw Error
104
+ // return brNext && `(max-width: ${breakpoints[brNext] - 0.02}px)`;
105
+ // };
106
+ // /**
107
+ // * Media query between the two given breakpoints
108
+ // */
109
+ // const between = (br1: Breakpoint, br2?: Breakpoint) => {
110
+ // return br2
111
+ // ? `(min-width: ${breakpoints[br1]}px) and (max-width: ${
112
+ // breakpoints[br2] - 0.02
113
+ // }px)`
114
+ // : min(br1);
115
+ // };
116
+ // /**
117
+ // * Media query to apply from the given breakpoint until the next, just for its
118
+ // * full range
119
+ // */
120
+ // const only = (br: Breakpoint) => {
121
+ // const brNext = getNextBreakpoint(br);
122
+ // return brNext ? between(br, brNext) : min(br);
123
+ // };
124
+ // const queryResolvers = {
125
+ // max,
126
+ // min,
127
+ // down,
128
+ // up,
129
+ // between,
130
+ // only,
131
+ // };
132
+ // export function useMqWidth(media: MediaQuery) {
133
+ // const [rule = "min", ruleBreakpoint] = media.split(":") as Split<
134
+ // MediaQuery,
135
+ // ":"
136
+ // >;
137
+ // const [br1, br2] = ruleBreakpoint.split("-") as Split<
138
+ // typeof ruleBreakpoint,
139
+ // "-"
140
+ // >;
141
+ // const query = queryResolvers[rule](br1, br2);
142
+ // const mq = useMemo(
143
+ // () => (isBrowser ? window.matchMedia(query) : { matches: false }),
144
+ // [query]
145
+ // );
146
+ // const [matches, setMatches] = useState(mq.matches);
147
+ // useEffect(() => {
148
+ // const mq = window.matchMedia(query);
149
+ // const handleChange = (event: MediaQueryListEvent) => {
150
+ // setMatches(event.matches);
151
+ // };
152
+ // setMatches(mq.matches);
153
+ // // Safari < 14 can't use addEventListener on a MediaQueryList
154
+ // // https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList#Browser_compatibility
155
+ // if (!mq.addEventListener) {
156
+ // // Update the state whenever the media query match state changes
157
+ // mq.addListener(handleChange);
158
+ // // Clean up on unmount and if the query changes
159
+ // return () => {
160
+ // mq.removeListener(handleChange);
161
+ // };
162
+ // }
163
+ // mq.addEventListener("change", handleChange);
164
+ // return () => {
165
+ // mq.removeEventListener("change", handleChange);
166
+ // };
167
+ // }, [query]);
168
+ // return matches;
169
+ // }
@@ -8,9 +8,9 @@ tslib_1.__exportStar(require("./useFirstMountState"), exports);
8
8
  tslib_1.__exportStar(require("./useFocus"), exports);
9
9
  tslib_1.__exportStar(require("./useId"), exports);
10
10
  tslib_1.__exportStar(require("./useIsomorphicLayoutEffect"), exports);
11
+ tslib_1.__exportStar(require("./useMediaQueryWidthCreator"), exports);
11
12
  tslib_1.__exportStar(require("./useMount"), exports);
12
13
  tslib_1.__exportStar(require("./useMountedState"), exports);
13
- tslib_1.__exportStar(require("./useMqWidthCreator"), exports);
14
14
  tslib_1.__exportStar(require("./usePrevious"), exports);
15
15
  tslib_1.__exportStar(require("./useScrollPosition"), exports);
16
16
  // export * from "./useScrollTo";
@@ -1,84 +1,28 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useMqWidthCreator = void 0;
4
- var tslib_1 = require("tslib");
3
+ exports.useMediaQueryWidthCreator = void 0;
5
4
  var react_1 = require("react");
6
5
  var utils_1 = require("@koine/utils");
7
- function useMqWidthCreator(customBreakpoints) {
8
- var breakpoints = tslib_1.__assign({ xs: 0 }, customBreakpoints);
9
- var sortedBreakpointsNames = Object.keys(breakpoints).map(function (key) {
10
- var br = key;
11
- return [br, breakpoints[br]];
12
- })
13
- .sort(function (a, b) { return a[1] - b[1]; })
14
- .map(function (item) { return item[0]; });
15
- var getNextBreakpoint = function (breakpoint) {
16
- var index = sortedBreakpointsNames.indexOf(breakpoint);
17
- return sortedBreakpointsNames[index + 1];
18
- };
19
- /**
20
- * It behaves the same as `(min-width: ${value}px)`
21
- * where value is the given breakpoint value.
22
- * For ease of use this can be used both as a function `min("md")` and as an
23
- * object literal `min.md`.
24
- */
25
- var min = function (br) { return "(min-width: ".concat(breakpoints[br], "px)"); };
26
- /**
27
- * It behaves the same as `(max-width: ${value}px)`
28
- * where value is the given breakpoint value.
29
- * For ease of use this can be used both as a function `max("md")` and as an
30
- * object literal `max.md`.
31
- */
32
- var max = function (br) { return "(max-width: ".concat(breakpoints[br] - 0.02, "px)"); };
33
- /**
34
- * It behaves the same as `min`
35
- * @inheritdoc {max}
36
- */
37
- var up = min;
38
- /**
39
- * It behaves similarly to `max` but you will use the "next" breakpoint,
40
- * specifying CSS that will apply from the given breakpoint and down.
41
- */
42
- var down = function (br) {
43
- var brNext = getNextBreakpoint(br);
44
- // TODO: if br does not exists otherwise throw Error
45
- return brNext && "(max-width: ".concat(breakpoints[brNext] - 0.02, "px)");
46
- };
47
- /**
48
- * Media query between the two given breakpoints
49
- */
50
- var between = function (br1, br2) {
51
- return br2
52
- ? "(min-width: ".concat(breakpoints[br1], "px) and (max-width: ").concat(breakpoints[br2] - 0.02, "px)")
53
- : min(br1);
54
- };
55
- /**
56
- * Media query to apply from the given breakpoint until the next, just for its
57
- * full range
58
- */
59
- var only = function (br) {
60
- var brNext = getNextBreakpoint(br);
61
- return brNext ? between(br, brNext) : min(br);
62
- };
63
- var queryResolvers = {
64
- max: max,
65
- min: min,
66
- down: down,
67
- up: up,
68
- between: between,
69
- only: only,
70
- };
71
- return function useMqWidth(media) {
72
- var _a = media.split(":"), _b = _a[0], rule = _b === void 0 ? "min" : _b, ruleBreakpoint = _a[1];
6
+ function useMediaQueryWidthCreator(customBreakpoints) {
7
+ var queryResolvers = (0, utils_1.getMediaQueryWidthResolvers)(customBreakpoints);
8
+ return function useMediaQueryWidth(media) {
9
+ var definition = media.substring(1);
10
+ var _a = definition.split("-"), rule = _a[0], ruleBreakpoint = _a[1];
11
+ if ((0, utils_1.isUndefined)(ruleBreakpoint)) {
12
+ ruleBreakpoint = rule;
13
+ }
14
+ if ((0, utils_1.isUndefined)(rule)) {
15
+ rule = "min";
16
+ }
73
17
  // with the hook creator approach these breakpoint types cannot be deduced
74
18
  // const [br1, br2] = ruleBreakpoint.split("-") as Split<
75
19
  // typeof ruleBreakpoint,
76
20
  // "-"
77
21
  // >;
78
- var _c = ruleBreakpoint.split("-"), br1 = _c[0], br2 = _c[1];
22
+ var _b = ruleBreakpoint.split("_"), br1 = _b[0], br2 = _b[1];
79
23
  var query = queryResolvers[rule](br1, br2);
80
24
  var mq = (0, react_1.useMemo)(function () { return (utils_1.isBrowser ? window.matchMedia(query) : { matches: false }); }, [query]);
81
- var _d = (0, react_1.useState)(mq.matches), matches = _d[0], setMatches = _d[1];
25
+ var _c = (0, react_1.useState)(mq.matches), matches = _c[0], setMatches = _c[1];
82
26
  (0, react_1.useEffect)(function () {
83
27
  var mq = window.matchMedia(query);
84
28
  var handleChange = function (event) {
@@ -103,8 +47,8 @@ function useMqWidthCreator(customBreakpoints) {
103
47
  return matches;
104
48
  };
105
49
  }
106
- exports.useMqWidthCreator = useMqWidthCreator;
107
- exports.default = useMqWidthCreator;
50
+ exports.useMediaQueryWidthCreator = useMediaQueryWidthCreator;
51
+ exports.default = useMediaQueryWidthCreator;
108
52
  //// without creator it would be:
109
53
  //// ---------------------------------------------------------------------------
110
54
  // import { useState, useEffect, useMemo } from "react";
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "framer-motion": "^7.2.1",
9
9
  "react": "^18.2.0",
10
10
  "@mui/base": "^5.0.0-alpha.95",
11
- "@koine/utils": "1.0.85",
11
+ "@koine/utils": "1.0.86",
12
12
  "ts-debounce": "^4.0.0",
13
13
  "date-fns-tz": "^1.3.7",
14
14
  "react-icons": "^4.4.0",
@@ -22,7 +22,7 @@
22
22
  "tslib": "^2.4.0"
23
23
  },
24
24
  "peerDependencies": {},
25
- "version": "1.0.85",
25
+ "version": "1.0.86",
26
26
  "module": "./index.js",
27
27
  "types": "./index.d.ts"
28
28
  }