@mado/hooks 0.3.3 → 0.4.0

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @mado/hooks
2
2
 
3
+ ## 0.4.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 6b13875: Add useMediaQuery hook
8
+
3
9
  ## 0.3.3
4
10
 
5
11
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -4,6 +4,7 @@ var useDebounce_cjs = require('./useDebounce.cjs');
4
4
  var useEvent_cjs = require('./useEvent.cjs');
5
5
  var useIsMounted_cjs = require('./useIsMounted.cjs');
6
6
  var useIsomorphicLayoutEffect_cjs = require('./useIsomorphicLayoutEffect.cjs');
7
+ var useMediaQuery_cjs = require('./useMediaQuery.cjs');
7
8
  var useOnline_cjs = require('./useOnline.cjs');
8
9
  var useRerender_cjs = require('./useRerender.cjs');
9
10
  var useVisibilityState_cjs = require('./useVisibilityState.cjs');
@@ -34,6 +35,12 @@ Object.keys(useIsomorphicLayoutEffect_cjs).forEach(function (k) {
34
35
  get: function () { return useIsomorphicLayoutEffect_cjs[k]; }
35
36
  });
36
37
  });
38
+ Object.keys(useMediaQuery_cjs).forEach(function (k) {
39
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
40
+ enumerable: true,
41
+ get: function () { return useMediaQuery_cjs[k]; }
42
+ });
43
+ });
37
44
  Object.keys(useOnline_cjs).forEach(function (k) {
38
45
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
39
46
  enumerable: true,
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc","sourcesContent":["export * from './useDebounce';\nexport * from './useEvent';\nexport * from './useIsMounted';\nexport * from './useIsomorphicLayoutEffect';\nexport * from './useOnline';\nexport * from './useRerender';\nexport * from './useVisibilityState';\n"]}
1
+ {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc","sourcesContent":["export * from './useDebounce';\nexport * from './useEvent';\nexport * from './useIsMounted';\nexport * from './useIsomorphicLayoutEffect';\nexport * from './useMediaQuery';\nexport * from './useOnline';\nexport * from './useRerender';\nexport * from './useVisibilityState';\n"]}
package/dist/index.d.cts CHANGED
@@ -2,6 +2,7 @@ export { useDebounce } from './useDebounce.cjs';
2
2
  export { useEvent } from './useEvent.cjs';
3
3
  export { useIsMounted } from './useIsMounted.cjs';
4
4
  export { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect.cjs';
5
+ export { useMediaQuery } from './useMediaQuery.cjs';
5
6
  export { useOnline } from './useOnline.cjs';
6
7
  export { useRerender } from './useRerender.cjs';
7
8
  export { useVisibilityState } from './useVisibilityState.cjs';
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@ export { useDebounce } from './useDebounce.js';
2
2
  export { useEvent } from './useEvent.js';
3
3
  export { useIsMounted } from './useIsMounted.js';
4
4
  export { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect.js';
5
+ export { useMediaQuery } from './useMediaQuery.js';
5
6
  export { useOnline } from './useOnline.js';
6
7
  export { useRerender } from './useRerender.js';
7
8
  export { useVisibilityState } from './useVisibilityState.js';
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@ export * from './useDebounce.js';
2
2
  export * from './useEvent.js';
3
3
  export * from './useIsMounted.js';
4
4
  export * from './useIsomorphicLayoutEffect.js';
5
+ export * from './useMediaQuery.js';
5
6
  export * from './useOnline.js';
6
7
  export * from './useRerender.js';
7
8
  export * from './useVisibilityState.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc","sourcesContent":["export * from './useDebounce';\nexport * from './useEvent';\nexport * from './useIsMounted';\nexport * from './useIsomorphicLayoutEffect';\nexport * from './useOnline';\nexport * from './useRerender';\nexport * from './useVisibilityState';\n"]}
1
+ {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc","sourcesContent":["export * from './useDebounce';\nexport * from './useEvent';\nexport * from './useIsMounted';\nexport * from './useIsomorphicLayoutEffect';\nexport * from './useMediaQuery';\nexport * from './useOnline';\nexport * from './useRerender';\nexport * from './useVisibilityState';\n"]}
@@ -0,0 +1,36 @@
1
+ 'use strict';
2
+
3
+ var react = require('react');
4
+ var index_js = require('use-sync-external-store/shim/index.js');
5
+
6
+ // src/useMediaQuery.ts
7
+ function useMediaQuery(query, defaultMatches = false) {
8
+ const [subscribe, getSnapshot, getServerSnapshot] = react.useMemo(() => {
9
+ const matchMedia = typeof window !== "undefined" && window.matchMedia;
10
+ const getDefaultSnapshot = () => {
11
+ return defaultMatches;
12
+ };
13
+ if (!matchMedia) {
14
+ return [() => () => {
15
+ }, getDefaultSnapshot, getDefaultSnapshot];
16
+ }
17
+ const mediaQueryList = matchMedia(query);
18
+ return [
19
+ (onStoreChange) => {
20
+ mediaQueryList.addListener(onStoreChange);
21
+ return () => {
22
+ mediaQueryList.removeListener(onStoreChange);
23
+ };
24
+ },
25
+ () => {
26
+ return mediaQueryList.matches;
27
+ },
28
+ getDefaultSnapshot
29
+ ];
30
+ }, [defaultMatches, query]);
31
+ return index_js.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
32
+ }
33
+
34
+ exports.useMediaQuery = useMediaQuery;
35
+ //# sourceMappingURL=out.js.map
36
+ //# sourceMappingURL=useMediaQuery.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/useMediaQuery.ts"],"names":[],"mappings":";AAAA,SAAS,eAAe;AAExB,SAAS,4BAA4B;AAE9B,SAAS,cAAc,OAAe,iBAAiB,OAAgB;AAC5E,QAAM,CAAC,WAAW,aAAa,iBAAiB,IAAI,QAAQ,MAAM;AAChE,UAAM,aAAa,OAAO,WAAW,eAAe,OAAO;AAE3D,UAAM,qBAAqB,MAAM;AAC/B,aAAO;AAAA,IACT;AAEA,QAAI,CAAC,YAAY;AACf,aAAO,CAAC,MAAM,MAAM;AAAA,MAAC,GAAG,oBAAoB,kBAAkB;AAAA,IAChE;AAEA,UAAM,iBAAiB,WAAW,KAAK;AAEvC,WAAO;AAAA,MACL,CAAC,kBAAgC;AAC/B,uBAAe,YAAY,aAAa;AAExC,eAAO,MAAM;AACX,yBAAe,eAAe,aAAa;AAAA,QAC7C;AAAA,MACF;AAAA,MACA,MAAM;AACJ,eAAO,eAAe;AAAA,MACxB;AAAA,MACA;AAAA,IACF;AAAA,EACF,GAAG,CAAC,gBAAgB,KAAK,CAAC;AAE1B,SAAO,qBAAqB,WAAW,aAAa,iBAAiB;AACvE","sourcesContent":["import { useMemo } from 'react';\n// eslint-disable-next-line import/extensions\nimport { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';\n\nexport function useMediaQuery(query: string, defaultMatches = false): boolean {\n const [subscribe, getSnapshot, getServerSnapshot] = useMemo(() => {\n const matchMedia = typeof window !== 'undefined' && window.matchMedia;\n\n const getDefaultSnapshot = () => {\n return defaultMatches;\n };\n\n if (!matchMedia) {\n return [() => () => {}, getDefaultSnapshot, getDefaultSnapshot];\n }\n\n const mediaQueryList = matchMedia(query);\n\n return [\n (onStoreChange: VoidFunction) => {\n mediaQueryList.addListener(onStoreChange);\n\n return () => {\n mediaQueryList.removeListener(onStoreChange);\n };\n },\n () => {\n return mediaQueryList.matches;\n },\n getDefaultSnapshot,\n ];\n }, [defaultMatches, query]);\n\n return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n}\n"]}
@@ -0,0 +1,3 @@
1
+ declare function useMediaQuery(query: string, defaultMatches?: boolean): boolean;
2
+
3
+ export { useMediaQuery };
@@ -0,0 +1,3 @@
1
+ declare function useMediaQuery(query: string, defaultMatches?: boolean): boolean;
2
+
3
+ export { useMediaQuery };
@@ -0,0 +1,34 @@
1
+ import { useMemo } from 'react';
2
+ import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';
3
+
4
+ // src/useMediaQuery.ts
5
+ function useMediaQuery(query, defaultMatches = false) {
6
+ const [subscribe, getSnapshot, getServerSnapshot] = useMemo(() => {
7
+ const matchMedia = typeof window !== "undefined" && window.matchMedia;
8
+ const getDefaultSnapshot = () => {
9
+ return defaultMatches;
10
+ };
11
+ if (!matchMedia) {
12
+ return [() => () => {
13
+ }, getDefaultSnapshot, getDefaultSnapshot];
14
+ }
15
+ const mediaQueryList = matchMedia(query);
16
+ return [
17
+ (onStoreChange) => {
18
+ mediaQueryList.addListener(onStoreChange);
19
+ return () => {
20
+ mediaQueryList.removeListener(onStoreChange);
21
+ };
22
+ },
23
+ () => {
24
+ return mediaQueryList.matches;
25
+ },
26
+ getDefaultSnapshot
27
+ ];
28
+ }, [defaultMatches, query]);
29
+ return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
30
+ }
31
+
32
+ export { useMediaQuery };
33
+ //# sourceMappingURL=out.js.map
34
+ //# sourceMappingURL=useMediaQuery.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/useMediaQuery.ts"],"names":[],"mappings":";AAAA,SAAS,eAAe;AAExB,SAAS,4BAA4B;AAE9B,SAAS,cAAc,OAAe,iBAAiB,OAAgB;AAC5E,QAAM,CAAC,WAAW,aAAa,iBAAiB,IAAI,QAAQ,MAAM;AAChE,UAAM,aAAa,OAAO,WAAW,eAAe,OAAO;AAE3D,UAAM,qBAAqB,MAAM;AAC/B,aAAO;AAAA,IACT;AAEA,QAAI,CAAC,YAAY;AACf,aAAO,CAAC,MAAM,MAAM;AAAA,MAAC,GAAG,oBAAoB,kBAAkB;AAAA,IAChE;AAEA,UAAM,iBAAiB,WAAW,KAAK;AAEvC,WAAO;AAAA,MACL,CAAC,kBAAgC;AAC/B,uBAAe,YAAY,aAAa;AAExC,eAAO,MAAM;AACX,yBAAe,eAAe,aAAa;AAAA,QAC7C;AAAA,MACF;AAAA,MACA,MAAM;AACJ,eAAO,eAAe;AAAA,MACxB;AAAA,MACA;AAAA,IACF;AAAA,EACF,GAAG,CAAC,gBAAgB,KAAK,CAAC;AAE1B,SAAO,qBAAqB,WAAW,aAAa,iBAAiB;AACvE","sourcesContent":["import { useMemo } from 'react';\n// eslint-disable-next-line import/extensions\nimport { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';\n\nexport function useMediaQuery(query: string, defaultMatches = false): boolean {\n const [subscribe, getSnapshot, getServerSnapshot] = useMemo(() => {\n const matchMedia = typeof window !== 'undefined' && window.matchMedia;\n\n const getDefaultSnapshot = () => {\n return defaultMatches;\n };\n\n if (!matchMedia) {\n return [() => () => {}, getDefaultSnapshot, getDefaultSnapshot];\n }\n\n const mediaQueryList = matchMedia(query);\n\n return [\n (onStoreChange: VoidFunction) => {\n mediaQueryList.addListener(onStoreChange);\n\n return () => {\n mediaQueryList.removeListener(onStoreChange);\n };\n },\n () => {\n return mediaQueryList.matches;\n },\n getDefaultSnapshot,\n ];\n }, [defaultMatches, query]);\n\n return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n}\n"]}
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var shim = require('use-sync-external-store/shim');
3
+ var index_js = require('use-sync-external-store/shim/index.js');
4
4
 
5
5
  // src/useOnline.ts
6
6
  function subscribe(onStoreChange) {
@@ -22,7 +22,7 @@ function getServerSnapshot() {
22
22
  return true;
23
23
  }
24
24
  function useOnline() {
25
- return shim.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
25
+ return index_js.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
26
26
  }
27
27
  /* istanbul ignore next -- @preserve */
28
28
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useOnline.ts"],"names":[],"mappings":";AAAA,SAAS,4BAA4B;AAErC,SAAS,UAAU,eAA2C;AAC5D,SAAO,iBAAiB,UAAU,eAAe;AAAA,IAC/C,SAAS;AAAA,EACX,CAAC;AACD,SAAO,iBAAiB,WAAW,eAAe;AAAA,IAChD,SAAS;AAAA,EACX,CAAC;AAED,SAAO,MAAM;AACX,WAAO,oBAAoB,UAAU,aAAa;AAClD,WAAO,oBAAoB,WAAW,aAAa;AAAA,EACrD;AACF;AAEA,SAAS,cAAuB;AAC9B,SAAO,UAAU;AACnB;AAGA,SAAS,oBAA6B;AACpC,SAAO;AACT;AAEO,SAAS,YAAqB;AACnC,SAAO,qBAAqB,WAAW,aAAa,iBAAiB;AACvE","sourcesContent":["import { useSyncExternalStore } from 'use-sync-external-store/shim';\n\nfunction subscribe(onStoreChange: VoidFunction): VoidFunction {\n window.addEventListener('online', onStoreChange, {\n passive: true,\n });\n window.addEventListener('offline', onStoreChange, {\n passive: true,\n });\n\n return () => {\n window.removeEventListener('online', onStoreChange);\n window.removeEventListener('offline', onStoreChange);\n };\n}\n\nfunction getSnapshot(): boolean {\n return navigator.onLine;\n}\n\n/* istanbul ignore next -- @preserve */\nfunction getServerSnapshot(): boolean {\n return true;\n}\n\nexport function useOnline(): boolean {\n return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n}\n"]}
1
+ {"version":3,"sources":["../src/useOnline.ts"],"names":[],"mappings":";AACA,SAAS,4BAA4B;AAErC,SAAS,UAAU,eAA2C;AAC5D,SAAO,iBAAiB,UAAU,eAAe;AAAA,IAC/C,SAAS;AAAA,EACX,CAAC;AACD,SAAO,iBAAiB,WAAW,eAAe;AAAA,IAChD,SAAS;AAAA,EACX,CAAC;AAED,SAAO,MAAM;AACX,WAAO,oBAAoB,UAAU,aAAa;AAClD,WAAO,oBAAoB,WAAW,aAAa;AAAA,EACrD;AACF;AAEA,SAAS,cAAuB;AAC9B,SAAO,UAAU;AACnB;AAGA,SAAS,oBAA6B;AACpC,SAAO;AACT;AAEO,SAAS,YAAqB;AACnC,SAAO,qBAAqB,WAAW,aAAa,iBAAiB;AACvE","sourcesContent":["// eslint-disable-next-line import/extensions\nimport { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';\n\nfunction subscribe(onStoreChange: VoidFunction): VoidFunction {\n window.addEventListener('online', onStoreChange, {\n passive: true,\n });\n window.addEventListener('offline', onStoreChange, {\n passive: true,\n });\n\n return () => {\n window.removeEventListener('online', onStoreChange);\n window.removeEventListener('offline', onStoreChange);\n };\n}\n\nfunction getSnapshot(): boolean {\n return navigator.onLine;\n}\n\n/* istanbul ignore next -- @preserve */\nfunction getServerSnapshot(): boolean {\n return true;\n}\n\nexport function useOnline(): boolean {\n return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n}\n"]}
package/dist/useOnline.js CHANGED
@@ -1,4 +1,4 @@
1
- import { useSyncExternalStore } from 'use-sync-external-store/shim';
1
+ import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';
2
2
 
3
3
  // src/useOnline.ts
4
4
  function subscribe(onStoreChange) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useOnline.ts"],"names":[],"mappings":";AAAA,SAAS,4BAA4B;AAErC,SAAS,UAAU,eAA2C;AAC5D,SAAO,iBAAiB,UAAU,eAAe;AAAA,IAC/C,SAAS;AAAA,EACX,CAAC;AACD,SAAO,iBAAiB,WAAW,eAAe;AAAA,IAChD,SAAS;AAAA,EACX,CAAC;AAED,SAAO,MAAM;AACX,WAAO,oBAAoB,UAAU,aAAa;AAClD,WAAO,oBAAoB,WAAW,aAAa;AAAA,EACrD;AACF;AAEA,SAAS,cAAuB;AAC9B,SAAO,UAAU;AACnB;AAGA,SAAS,oBAA6B;AACpC,SAAO;AACT;AAEO,SAAS,YAAqB;AACnC,SAAO,qBAAqB,WAAW,aAAa,iBAAiB;AACvE","sourcesContent":["import { useSyncExternalStore } from 'use-sync-external-store/shim';\n\nfunction subscribe(onStoreChange: VoidFunction): VoidFunction {\n window.addEventListener('online', onStoreChange, {\n passive: true,\n });\n window.addEventListener('offline', onStoreChange, {\n passive: true,\n });\n\n return () => {\n window.removeEventListener('online', onStoreChange);\n window.removeEventListener('offline', onStoreChange);\n };\n}\n\nfunction getSnapshot(): boolean {\n return navigator.onLine;\n}\n\n/* istanbul ignore next -- @preserve */\nfunction getServerSnapshot(): boolean {\n return true;\n}\n\nexport function useOnline(): boolean {\n return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n}\n"]}
1
+ {"version":3,"sources":["../src/useOnline.ts"],"names":[],"mappings":";AACA,SAAS,4BAA4B;AAErC,SAAS,UAAU,eAA2C;AAC5D,SAAO,iBAAiB,UAAU,eAAe;AAAA,IAC/C,SAAS;AAAA,EACX,CAAC;AACD,SAAO,iBAAiB,WAAW,eAAe;AAAA,IAChD,SAAS;AAAA,EACX,CAAC;AAED,SAAO,MAAM;AACX,WAAO,oBAAoB,UAAU,aAAa;AAClD,WAAO,oBAAoB,WAAW,aAAa;AAAA,EACrD;AACF;AAEA,SAAS,cAAuB;AAC9B,SAAO,UAAU;AACnB;AAGA,SAAS,oBAA6B;AACpC,SAAO;AACT;AAEO,SAAS,YAAqB;AACnC,SAAO,qBAAqB,WAAW,aAAa,iBAAiB;AACvE","sourcesContent":["// eslint-disable-next-line import/extensions\nimport { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';\n\nfunction subscribe(onStoreChange: VoidFunction): VoidFunction {\n window.addEventListener('online', onStoreChange, {\n passive: true,\n });\n window.addEventListener('offline', onStoreChange, {\n passive: true,\n });\n\n return () => {\n window.removeEventListener('online', onStoreChange);\n window.removeEventListener('offline', onStoreChange);\n };\n}\n\nfunction getSnapshot(): boolean {\n return navigator.onLine;\n}\n\n/* istanbul ignore next -- @preserve */\nfunction getServerSnapshot(): boolean {\n return true;\n}\n\nexport function useOnline(): boolean {\n return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n}\n"]}
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var shim = require('use-sync-external-store/shim');
3
+ var index_js = require('use-sync-external-store/shim/index.js');
4
4
 
5
5
  // src/useVisibilityState.ts
6
6
  function subscribe(onStoreChange) {
@@ -18,7 +18,7 @@ function getServerSnapshot() {
18
18
  return "visible";
19
19
  }
20
20
  function useVisibilityState() {
21
- return shim.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
21
+ return index_js.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
22
22
  }
23
23
  /* istanbul ignore next -- @preserve */
24
24
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useVisibilityState.ts"],"names":[],"mappings":";AAAA,SAAS,4BAA4B;AAErC,SAAS,UAAU,eAA2C;AAC5D,WAAS,iBAAiB,oBAAoB,eAAe;AAAA,IAC3D,SAAS;AAAA,EACX,CAAC;AAED,SAAO,MAAM;AACX,aAAS,oBAAoB,oBAAoB,aAAa;AAAA,EAChE;AACF;AAEA,SAAS,cAAuC;AAC9C,SAAO,SAAS;AAClB;AAGA,SAAS,oBAA6C;AACpD,SAAO;AACT;AAEO,SAAS,qBAA8C;AAC5D,SAAO,qBAAqB,WAAW,aAAa,iBAAiB;AACvE","sourcesContent":["import { useSyncExternalStore } from 'use-sync-external-store/shim';\n\nfunction subscribe(onStoreChange: VoidFunction): VoidFunction {\n document.addEventListener('visibilitychange', onStoreChange, {\n passive: true,\n });\n\n return () => {\n document.removeEventListener('visibilitychange', onStoreChange);\n };\n}\n\nfunction getSnapshot(): DocumentVisibilityState {\n return document.visibilityState;\n}\n\n/* istanbul ignore next -- @preserve */\nfunction getServerSnapshot(): DocumentVisibilityState {\n return 'visible';\n}\n\nexport function useVisibilityState(): DocumentVisibilityState {\n return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n}\n"]}
1
+ {"version":3,"sources":["../src/useVisibilityState.ts"],"names":[],"mappings":";AACA,SAAS,4BAA4B;AAErC,SAAS,UAAU,eAA2C;AAC5D,WAAS,iBAAiB,oBAAoB,eAAe;AAAA,IAC3D,SAAS;AAAA,EACX,CAAC;AAED,SAAO,MAAM;AACX,aAAS,oBAAoB,oBAAoB,aAAa;AAAA,EAChE;AACF;AAEA,SAAS,cAAuC;AAC9C,SAAO,SAAS;AAClB;AAGA,SAAS,oBAA6C;AACpD,SAAO;AACT;AAEO,SAAS,qBAA8C;AAC5D,SAAO,qBAAqB,WAAW,aAAa,iBAAiB;AACvE","sourcesContent":["// eslint-disable-next-line import/extensions\nimport { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';\n\nfunction subscribe(onStoreChange: VoidFunction): VoidFunction {\n document.addEventListener('visibilitychange', onStoreChange, {\n passive: true,\n });\n\n return () => {\n document.removeEventListener('visibilitychange', onStoreChange);\n };\n}\n\nfunction getSnapshot(): DocumentVisibilityState {\n return document.visibilityState;\n}\n\n/* istanbul ignore next -- @preserve */\nfunction getServerSnapshot(): DocumentVisibilityState {\n return 'visible';\n}\n\nexport function useVisibilityState(): DocumentVisibilityState {\n return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n}\n"]}
@@ -1,4 +1,4 @@
1
- import { useSyncExternalStore } from 'use-sync-external-store/shim';
1
+ import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';
2
2
 
3
3
  // src/useVisibilityState.ts
4
4
  function subscribe(onStoreChange) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useVisibilityState.ts"],"names":[],"mappings":";AAAA,SAAS,4BAA4B;AAErC,SAAS,UAAU,eAA2C;AAC5D,WAAS,iBAAiB,oBAAoB,eAAe;AAAA,IAC3D,SAAS;AAAA,EACX,CAAC;AAED,SAAO,MAAM;AACX,aAAS,oBAAoB,oBAAoB,aAAa;AAAA,EAChE;AACF;AAEA,SAAS,cAAuC;AAC9C,SAAO,SAAS;AAClB;AAGA,SAAS,oBAA6C;AACpD,SAAO;AACT;AAEO,SAAS,qBAA8C;AAC5D,SAAO,qBAAqB,WAAW,aAAa,iBAAiB;AACvE","sourcesContent":["import { useSyncExternalStore } from 'use-sync-external-store/shim';\n\nfunction subscribe(onStoreChange: VoidFunction): VoidFunction {\n document.addEventListener('visibilitychange', onStoreChange, {\n passive: true,\n });\n\n return () => {\n document.removeEventListener('visibilitychange', onStoreChange);\n };\n}\n\nfunction getSnapshot(): DocumentVisibilityState {\n return document.visibilityState;\n}\n\n/* istanbul ignore next -- @preserve */\nfunction getServerSnapshot(): DocumentVisibilityState {\n return 'visible';\n}\n\nexport function useVisibilityState(): DocumentVisibilityState {\n return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n}\n"]}
1
+ {"version":3,"sources":["../src/useVisibilityState.ts"],"names":[],"mappings":";AACA,SAAS,4BAA4B;AAErC,SAAS,UAAU,eAA2C;AAC5D,WAAS,iBAAiB,oBAAoB,eAAe;AAAA,IAC3D,SAAS;AAAA,EACX,CAAC;AAED,SAAO,MAAM;AACX,aAAS,oBAAoB,oBAAoB,aAAa;AAAA,EAChE;AACF;AAEA,SAAS,cAAuC;AAC9C,SAAO,SAAS;AAClB;AAGA,SAAS,oBAA6C;AACpD,SAAO;AACT;AAEO,SAAS,qBAA8C;AAC5D,SAAO,qBAAqB,WAAW,aAAa,iBAAiB;AACvE","sourcesContent":["// eslint-disable-next-line import/extensions\nimport { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';\n\nfunction subscribe(onStoreChange: VoidFunction): VoidFunction {\n document.addEventListener('visibilitychange', onStoreChange, {\n passive: true,\n });\n\n return () => {\n document.removeEventListener('visibilitychange', onStoreChange);\n };\n}\n\nfunction getSnapshot(): DocumentVisibilityState {\n return document.visibilityState;\n}\n\n/* istanbul ignore next -- @preserve */\nfunction getServerSnapshot(): DocumentVisibilityState {\n return 'visible';\n}\n\nexport function useVisibilityState(): DocumentVisibilityState {\n return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mado/hooks",
3
- "version": "0.3.3",
3
+ "version": "0.4.0",
4
4
  "license": "MIT",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@ export * from './useDebounce';
2
2
  export * from './useEvent';
3
3
  export * from './useIsMounted';
4
4
  export * from './useIsomorphicLayoutEffect';
5
+ export * from './useMediaQuery';
5
6
  export * from './useOnline';
6
7
  export * from './useRerender';
7
8
  export * from './useVisibilityState';
@@ -0,0 +1,35 @@
1
+ import { useMemo } from 'react';
2
+ // eslint-disable-next-line import/extensions
3
+ import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';
4
+
5
+ export function useMediaQuery(query: string, defaultMatches = false): boolean {
6
+ const [subscribe, getSnapshot, getServerSnapshot] = useMemo(() => {
7
+ const matchMedia = typeof window !== 'undefined' && window.matchMedia;
8
+
9
+ const getDefaultSnapshot = () => {
10
+ return defaultMatches;
11
+ };
12
+
13
+ if (!matchMedia) {
14
+ return [() => () => {}, getDefaultSnapshot, getDefaultSnapshot];
15
+ }
16
+
17
+ const mediaQueryList = matchMedia(query);
18
+
19
+ return [
20
+ (onStoreChange: VoidFunction) => {
21
+ mediaQueryList.addListener(onStoreChange);
22
+
23
+ return () => {
24
+ mediaQueryList.removeListener(onStoreChange);
25
+ };
26
+ },
27
+ () => {
28
+ return mediaQueryList.matches;
29
+ },
30
+ getDefaultSnapshot,
31
+ ];
32
+ }, [defaultMatches, query]);
33
+
34
+ return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
35
+ }
package/src/useOnline.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { useSyncExternalStore } from 'use-sync-external-store/shim';
1
+ // eslint-disable-next-line import/extensions
2
+ import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';
2
3
 
3
4
  function subscribe(onStoreChange: VoidFunction): VoidFunction {
4
5
  window.addEventListener('online', onStoreChange, {
@@ -1,4 +1,5 @@
1
- import { useSyncExternalStore } from 'use-sync-external-store/shim';
1
+ // eslint-disable-next-line import/extensions
2
+ import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';
2
3
 
3
4
  function subscribe(onStoreChange: VoidFunction): VoidFunction {
4
5
  document.addEventListener('visibilitychange', onStoreChange, {