nextemos 3.6.3 → 3.6.5

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.
@@ -1,3 +1,3 @@
1
1
  export { default as useFetch } from './useFetch';
2
2
  export { default as useLocalStorage } from './useLocalStorage';
3
- export { default as useWindowSize } from './useWindowSize';
3
+ export { default as useMediaQuery } from './useMediaQuery';
@@ -3,11 +3,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.useWindowSize = exports.useLocalStorage = exports.useFetch = void 0;
6
+ exports.useMediaQuery = exports.useLocalStorage = exports.useFetch = void 0;
7
7
  /// hooks
8
8
  var useFetch_1 = require("./useFetch");
9
9
  Object.defineProperty(exports, "useFetch", { enumerable: true, get: function () { return __importDefault(useFetch_1).default; } });
10
10
  var useLocalStorage_1 = require("./useLocalStorage");
11
11
  Object.defineProperty(exports, "useLocalStorage", { enumerable: true, get: function () { return __importDefault(useLocalStorage_1).default; } });
12
- var useWindowSize_1 = require("./useWindowSize");
13
- Object.defineProperty(exports, "useWindowSize", { enumerable: true, get: function () { return __importDefault(useWindowSize_1).default; } });
12
+ var useMediaQuery_1 = require("./useMediaQuery");
13
+ Object.defineProperty(exports, "useMediaQuery", { enumerable: true, get: function () { return __importDefault(useMediaQuery_1).default; } });
@@ -0,0 +1,37 @@
1
+ interface IWindowSize {
2
+ width: number;
3
+ height: number;
4
+ }
5
+ /**
6
+ * useMediaQuery Hook
7
+ *
8
+ * @description
9
+ * A custom React hook that listens for the window size and checks if it matches a specified media query.
10
+ *
11
+ * This hook is particularly useful in responsive design scenarios where you need to render different components
12
+ * or apply different styles based on the screen size (e.g., mobile vs desktop).
13
+ *
14
+ * @param {Object} options - The options for the hook.
15
+ * @param {string} options.query - The media query string to be matched (e.g., '(max-width: 960px)').
16
+ * If not provided, it defaults to a common mobile screen width.
17
+ *
18
+ * @returns {Object} An object containing:
19
+ * - `width` (number): The current width of the window.
20
+ * - `height` (number): The current height of the window.
21
+ * - `isMobile` (boolean): Whether the screen matches the provided media query (typically for mobile screens).
22
+ *
23
+ * @example
24
+ * const { width, height, isMobile } = useMediaQuery({ query: '(max-width: 768px)' });
25
+ *
26
+ * if (isMobile) {
27
+ * // Render mobile-specific component or apply mobile-specific styles
28
+ * } else {
29
+ * // Render desktop-specific component or apply desktop-specific styles
30
+ * }
31
+ */
32
+ declare const useMediaQuery: ({ query }?: {
33
+ query?: string;
34
+ }) => IWindowSize & {
35
+ isMobile: boolean;
36
+ };
37
+ export default useMediaQuery;
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ /**
5
+ * useMediaQuery Hook
6
+ *
7
+ * @description
8
+ * A custom React hook that listens for the window size and checks if it matches a specified media query.
9
+ *
10
+ * This hook is particularly useful in responsive design scenarios where you need to render different components
11
+ * or apply different styles based on the screen size (e.g., mobile vs desktop).
12
+ *
13
+ * @param {Object} options - The options for the hook.
14
+ * @param {string} options.query - The media query string to be matched (e.g., '(max-width: 960px)').
15
+ * If not provided, it defaults to a common mobile screen width.
16
+ *
17
+ * @returns {Object} An object containing:
18
+ * - `width` (number): The current width of the window.
19
+ * - `height` (number): The current height of the window.
20
+ * - `isMobile` (boolean): Whether the screen matches the provided media query (typically for mobile screens).
21
+ *
22
+ * @example
23
+ * const { width, height, isMobile } = useMediaQuery({ query: '(max-width: 768px)' });
24
+ *
25
+ * if (isMobile) {
26
+ * // Render mobile-specific component or apply mobile-specific styles
27
+ * } else {
28
+ * // Render desktop-specific component or apply desktop-specific styles
29
+ * }
30
+ */
31
+ const useMediaQuery = ({ query = process.env.NEXT_PUBLIC_MAX_MATCHES || '(max-width: 960px)' } = {}) => {
32
+ const [windowSize, setWindowSize] = (0, react_1.useState)({ width: 0, height: 0 });
33
+ const [isMobile, setIsMobile] = (0, react_1.useState)(false);
34
+ /**
35
+ * handleResize
36
+ *
37
+ * @description
38
+ * Updates the window size state when the window is resized.
39
+ */
40
+ const handleResize = () => {
41
+ const { innerWidth, innerHeight } = window;
42
+ setWindowSize({ width: innerWidth, height: innerHeight });
43
+ };
44
+ /**
45
+ * handleChange
46
+ *
47
+ * @description
48
+ * Updates the isMobile state when the media query status changes.
49
+ */
50
+ const handleChange = (event) => {
51
+ setIsMobile(event.matches);
52
+ };
53
+ (0, react_1.useEffect)(() => {
54
+ if (typeof window !== 'undefined') {
55
+ // Initialize window size and media query match
56
+ handleResize();
57
+ const mediaQuery = window.matchMedia(query);
58
+ setIsMobile(mediaQuery.matches);
59
+ // Add event listeners
60
+ mediaQuery.addEventListener('change', handleChange);
61
+ window.addEventListener('resize', handleResize);
62
+ // Cleanup event listeners on component unmount
63
+ return () => {
64
+ mediaQuery.removeEventListener('change', handleChange);
65
+ window.removeEventListener('resize', handleResize);
66
+ };
67
+ }
68
+ }, [query]);
69
+ return Object.assign(Object.assign({}, windowSize), { isMobile });
70
+ };
71
+ exports.default = useMediaQuery;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nextemos",
3
- "version": "3.6.3",
3
+ "version": "3.6.5",
4
4
  "description": "For helpers and hooks used in NextJS projects",
5
5
  "main": "dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -1,14 +0,0 @@
1
- interface IWindowSize {
2
- width: number;
3
- height: number;
4
- }
5
- /**
6
- * useWindowSize Hook
7
- *
8
- * @description
9
- * A custom React hook that listens for the window size and returns it.
10
- *
11
- * @returns {Object} An object containing the width and height of the window window.
12
- */
13
- declare const useWindowSize: () => IWindowSize;
14
- export default useWindowSize;
@@ -1,52 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const react_1 = require("react");
4
- /**
5
- * useWindowSize Hook
6
- *
7
- * @description
8
- * A custom React hook that listens for the window size and returns it.
9
- *
10
- * @returns {Object} An object containing the width and height of the window window.
11
- */
12
- const useWindowSize = () => {
13
- const [windowSize, setWindowSize] = (0, react_1.useState)({
14
- width: 0,
15
- height: 0,
16
- });
17
- /**
18
- * handleResize
19
- *
20
- * @description
21
- * A function to update the window size state when the window is resized.
22
- */
23
- const handleResize = () => {
24
- setWindowSize({
25
- width: window.innerWidth,
26
- height: window.innerHeight,
27
- });
28
- };
29
- /**
30
- * useEffect Hook
31
- *
32
- * @description
33
- * A React hook that is used for side-effects in functional components.
34
- *
35
- * @params {Function} () => {} A callback function that contains the side-effect logic.
36
- * @params {Array} [] An array of dependencies. When empty, it means the useEffect runs once after the initial render.
37
- */
38
- (0, react_1.useEffect)(() => {
39
- if (typeof window !== 'undefined') {
40
- setWindowSize({
41
- width: window.innerWidth,
42
- height: window.innerHeight,
43
- });
44
- window.addEventListener('resize', handleResize);
45
- return () => {
46
- window.removeEventListener('resize', handleResize);
47
- };
48
- }
49
- }, []);
50
- return windowSize;
51
- };
52
- exports.default = useWindowSize;