@bquery/bquery 1.6.0 → 1.7.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/README.md +716 -586
- package/dist/a11y/announce.d.ts +43 -0
- package/dist/a11y/announce.d.ts.map +1 -0
- package/dist/a11y/audit.d.ts +42 -0
- package/dist/a11y/audit.d.ts.map +1 -0
- package/dist/a11y/index.d.ts +53 -0
- package/dist/a11y/index.d.ts.map +1 -0
- package/dist/a11y/media-preferences.d.ts +77 -0
- package/dist/a11y/media-preferences.d.ts.map +1 -0
- package/dist/a11y/roving-tab-index.d.ts +38 -0
- package/dist/a11y/roving-tab-index.d.ts.map +1 -0
- package/dist/a11y/skip-link.d.ts +37 -0
- package/dist/a11y/skip-link.d.ts.map +1 -0
- package/dist/a11y/trap-focus.d.ts +49 -0
- package/dist/a11y/trap-focus.d.ts.map +1 -0
- package/dist/a11y/types.d.ts +152 -0
- package/dist/a11y/types.d.ts.map +1 -0
- package/dist/a11y-C5QOVvRn.js +421 -0
- package/dist/a11y-C5QOVvRn.js.map +1 -0
- package/dist/a11y.es.mjs +14 -0
- package/dist/component/component.d.ts.map +1 -1
- package/dist/component/html.d.ts.map +1 -1
- package/dist/component/index.d.ts +2 -1
- package/dist/component/index.d.ts.map +1 -1
- package/dist/component/library.d.ts.map +1 -1
- package/dist/component/scope.d.ts +138 -0
- package/dist/component/scope.d.ts.map +1 -0
- package/dist/component/types.d.ts +53 -1
- package/dist/component/types.d.ts.map +1 -1
- package/dist/component-CuuTijA6.js +684 -0
- package/dist/component-CuuTijA6.js.map +1 -0
- package/dist/component.es.mjs +9 -6
- package/dist/{config-DRmZZno3.js → config-BW35FKuA.js} +4 -4
- package/dist/{config-DRmZZno3.js.map → config-BW35FKuA.js.map} +1 -1
- package/dist/constraints-3lV9yyBw.js +100 -0
- package/dist/constraints-3lV9yyBw.js.map +1 -0
- package/dist/core/collection.d.ts +48 -0
- package/dist/core/collection.d.ts.map +1 -1
- package/dist/core/element.d.ts +92 -0
- package/dist/core/element.d.ts.map +1 -1
- package/dist/core/env.d.ts +18 -0
- package/dist/core/env.d.ts.map +1 -0
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/shared.d.ts +8 -0
- package/dist/core/shared.d.ts.map +1 -1
- package/dist/core/utils/index.d.ts +52 -41
- package/dist/core/utils/index.d.ts.map +1 -1
- package/dist/core-Cjl7GUu8.js +717 -0
- package/dist/core-Cjl7GUu8.js.map +1 -0
- package/dist/core-DnlyjbF2.js +112 -0
- package/dist/core-DnlyjbF2.js.map +1 -0
- package/dist/core.es.mjs +45 -44
- package/dist/custom-directives-7wAShnnd.js +9 -0
- package/dist/custom-directives-7wAShnnd.js.map +1 -0
- package/dist/devtools/devtools.d.ts +212 -0
- package/dist/devtools/devtools.d.ts.map +1 -0
- package/dist/devtools/index.d.ts +20 -0
- package/dist/devtools/index.d.ts.map +1 -0
- package/dist/devtools/types.d.ts +69 -0
- package/dist/devtools/types.d.ts.map +1 -0
- package/dist/devtools-D2fQLhDN.js +122 -0
- package/dist/devtools-D2fQLhDN.js.map +1 -0
- package/dist/devtools.es.mjs +19 -0
- package/dist/dnd/draggable.d.ts +51 -0
- package/dist/dnd/draggable.d.ts.map +1 -0
- package/dist/dnd/droppable.d.ts +38 -0
- package/dist/dnd/droppable.d.ts.map +1 -0
- package/dist/dnd/index.d.ts +47 -0
- package/dist/dnd/index.d.ts.map +1 -0
- package/dist/dnd/sortable.d.ts +43 -0
- package/dist/dnd/sortable.d.ts.map +1 -0
- package/dist/dnd/types.d.ts +250 -0
- package/dist/dnd/types.d.ts.map +1 -0
- package/dist/dnd-B8EgyzaI.js +244 -0
- package/dist/dnd-B8EgyzaI.js.map +1 -0
- package/dist/dnd.es.mjs +6 -0
- package/dist/env-NeVmr4Gf.js +19 -0
- package/dist/env-NeVmr4Gf.js.map +1 -0
- package/dist/forms/create-form.d.ts +49 -0
- package/dist/forms/create-form.d.ts.map +1 -0
- package/dist/forms/index.d.ts +39 -0
- package/dist/forms/index.d.ts.map +1 -0
- package/dist/forms/types.d.ts +139 -0
- package/dist/forms/types.d.ts.map +1 -0
- package/dist/forms/validators.d.ts +179 -0
- package/dist/forms/validators.d.ts.map +1 -0
- package/dist/forms-C3yovgH9.js +141 -0
- package/dist/forms-C3yovgH9.js.map +1 -0
- package/dist/forms.es.mjs +14 -0
- package/dist/full.d.ts +35 -7
- package/dist/full.d.ts.map +1 -1
- package/dist/full.es.mjs +182 -91
- package/dist/full.iife.js +47 -31
- package/dist/full.iife.js.map +1 -1
- package/dist/full.umd.js +47 -31
- package/dist/full.umd.js.map +1 -1
- package/dist/i18n/formatting.d.ts +40 -0
- package/dist/i18n/formatting.d.ts.map +1 -0
- package/dist/i18n/i18n.d.ts +48 -0
- package/dist/i18n/i18n.d.ts.map +1 -0
- package/dist/i18n/index.d.ts +57 -0
- package/dist/i18n/index.d.ts.map +1 -0
- package/dist/i18n/translate.d.ts +83 -0
- package/dist/i18n/translate.d.ts.map +1 -0
- package/dist/i18n/types.d.ts +156 -0
- package/dist/i18n/types.d.ts.map +1 -0
- package/dist/i18n-BnnhTFOS.js +89 -0
- package/dist/i18n-BnnhTFOS.js.map +1 -0
- package/dist/i18n.es.mjs +6 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.es.mjs +227 -136
- package/dist/media/battery.d.ts +35 -0
- package/dist/media/battery.d.ts.map +1 -0
- package/dist/media/breakpoints.d.ts +51 -0
- package/dist/media/breakpoints.d.ts.map +1 -0
- package/dist/media/clipboard.d.ts +30 -0
- package/dist/media/clipboard.d.ts.map +1 -0
- package/dist/media/device-sensors.d.ts +54 -0
- package/dist/media/device-sensors.d.ts.map +1 -0
- package/dist/media/geolocation.d.ts +38 -0
- package/dist/media/geolocation.d.ts.map +1 -0
- package/dist/media/index.d.ts +42 -0
- package/dist/media/index.d.ts.map +1 -0
- package/dist/media/media-query.d.ts +36 -0
- package/dist/media/media-query.d.ts.map +1 -0
- package/dist/media/network.d.ts +35 -0
- package/dist/media/network.d.ts.map +1 -0
- package/dist/media/types.d.ts +173 -0
- package/dist/media/types.d.ts.map +1 -0
- package/dist/media/viewport.d.ts +32 -0
- package/dist/media/viewport.d.ts.map +1 -0
- package/dist/media-Di2Ta22s.js +340 -0
- package/dist/media-Di2Ta22s.js.map +1 -0
- package/dist/media.es.mjs +12 -0
- package/dist/motion/index.d.ts +7 -3
- package/dist/motion/index.d.ts.map +1 -1
- package/dist/motion/morph.d.ts +27 -0
- package/dist/motion/morph.d.ts.map +1 -0
- package/dist/motion/parallax.d.ts +30 -0
- package/dist/motion/parallax.d.ts.map +1 -0
- package/dist/motion/reduced-motion.d.ts +36 -3
- package/dist/motion/reduced-motion.d.ts.map +1 -1
- package/dist/motion/types.d.ts +58 -0
- package/dist/motion/types.d.ts.map +1 -1
- package/dist/motion/typewriter.d.ts +31 -0
- package/dist/motion/typewriter.d.ts.map +1 -0
- package/dist/motion-qPj_TYGv.js +530 -0
- package/dist/motion-qPj_TYGv.js.map +1 -0
- package/dist/motion.es.mjs +27 -23
- package/dist/{view-C70lA3vf.js → mount-SM07RUa6.js} +166 -160
- package/dist/mount-SM07RUa6.js.map +1 -0
- package/dist/{object-qGpWr6-J.js → object-BCk-1c8T.js} +5 -4
- package/dist/{object-qGpWr6-J.js.map → object-BCk-1c8T.js.map} +1 -1
- package/dist/{platform-Dr9b6fsq.js → platform-CPbCprb6.js} +21 -22
- package/dist/{platform-Dr9b6fsq.js.map → platform-CPbCprb6.js.map} +1 -1
- package/dist/platform.es.mjs +2 -2
- package/dist/plugin/index.d.ts +22 -0
- package/dist/plugin/index.d.ts.map +1 -0
- package/dist/plugin/registry.d.ts +108 -0
- package/dist/plugin/registry.d.ts.map +1 -0
- package/dist/plugin/types.d.ts +110 -0
- package/dist/plugin/types.d.ts.map +1 -0
- package/dist/plugin-cPoOHFLY.js +64 -0
- package/dist/plugin-cPoOHFLY.js.map +1 -0
- package/dist/plugin.es.mjs +9 -0
- package/dist/reactive/computed.d.ts +7 -0
- package/dist/reactive/computed.d.ts.map +1 -1
- package/dist/reactive-Cfv0RK6x.js +233 -0
- package/dist/reactive-Cfv0RK6x.js.map +1 -0
- package/dist/reactive.es.mjs +19 -20
- package/dist/registry-CWf368tT.js +26 -0
- package/dist/registry-CWf368tT.js.map +1 -0
- package/dist/router/bq-link.d.ts +112 -0
- package/dist/router/bq-link.d.ts.map +1 -0
- package/dist/router/constraints.d.ts +9 -0
- package/dist/router/constraints.d.ts.map +1 -0
- package/dist/router/index.d.ts +14 -6
- package/dist/router/index.d.ts.map +1 -1
- package/dist/router/match.d.ts +0 -1
- package/dist/router/match.d.ts.map +1 -1
- package/dist/router/path-pattern.d.ts +14 -0
- package/dist/router/path-pattern.d.ts.map +1 -0
- package/dist/router/query.d.ts.map +1 -1
- package/dist/router/router.d.ts +3 -1
- package/dist/router/router.d.ts.map +1 -1
- package/dist/router/types.d.ts +48 -4
- package/dist/router/types.d.ts.map +1 -1
- package/dist/router/use-route.d.ts +50 -0
- package/dist/router/use-route.d.ts.map +1 -0
- package/dist/router/utils.d.ts +3 -0
- package/dist/router/utils.d.ts.map +1 -1
- package/dist/router-BrthaP_z.js +473 -0
- package/dist/router-BrthaP_z.js.map +1 -0
- package/dist/router.es.mjs +13 -10
- package/dist/{sanitize-Bs2dkMby.js → sanitize-B1V4JswB.js} +2 -1
- package/dist/{sanitize-Bs2dkMby.js.map → sanitize-B1V4JswB.js.map} +1 -1
- package/dist/security/index.d.ts +2 -2
- package/dist/security/index.d.ts.map +1 -1
- package/dist/security.es.mjs +1 -1
- package/dist/ssr/hydrate.d.ts +65 -0
- package/dist/ssr/hydrate.d.ts.map +1 -0
- package/dist/ssr/index.d.ts +59 -0
- package/dist/ssr/index.d.ts.map +1 -0
- package/dist/ssr/render.d.ts +62 -0
- package/dist/ssr/render.d.ts.map +1 -0
- package/dist/ssr/serialize.d.ts +118 -0
- package/dist/ssr/serialize.d.ts.map +1 -0
- package/dist/ssr/types.d.ts +70 -0
- package/dist/ssr/types.d.ts.map +1 -0
- package/dist/ssr-B2qd_WBB.js +248 -0
- package/dist/ssr-B2qd_WBB.js.map +1 -0
- package/dist/ssr.es.mjs +9 -0
- package/dist/store/create-store.d.ts.map +1 -1
- package/dist/store/index.d.ts +1 -1
- package/dist/store/index.d.ts.map +1 -1
- package/dist/store/persisted.d.ts +38 -4
- package/dist/store/persisted.d.ts.map +1 -1
- package/dist/store/types.d.ts +138 -1
- package/dist/store/types.d.ts.map +1 -1
- package/dist/store/utils.d.ts +2 -2
- package/dist/store/utils.d.ts.map +1 -1
- package/dist/store-DWpyH6p5.js +338 -0
- package/dist/store-DWpyH6p5.js.map +1 -0
- package/dist/store.es.mjs +11 -10
- package/dist/storybook/index.d.ts.map +1 -1
- package/dist/storybook.es.mjs +1 -1
- package/dist/storybook.es.mjs.map +1 -1
- package/dist/testing/index.d.ts +23 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/testing.d.ts +156 -0
- package/dist/testing/testing.d.ts.map +1 -0
- package/dist/testing/types.d.ts +134 -0
- package/dist/testing/types.d.ts.map +1 -0
- package/dist/testing-CsqjNUyy.js +224 -0
- package/dist/testing-CsqjNUyy.js.map +1 -0
- package/dist/testing.es.mjs +9 -0
- package/dist/type-guards-Do9DWgNp.js +44 -0
- package/dist/type-guards-Do9DWgNp.js.map +1 -0
- package/dist/untrack-DJVQQ2WM.js +33 -0
- package/dist/untrack-DJVQQ2WM.js.map +1 -0
- package/dist/view/custom-directives.d.ts +20 -0
- package/dist/view/custom-directives.d.ts.map +1 -0
- package/dist/view/evaluate.d.ts.map +1 -1
- package/dist/view/process.d.ts.map +1 -1
- package/dist/view.es.mjs +9 -9
- package/package.json +177 -141
- package/src/a11y/announce.ts +131 -0
- package/src/a11y/audit.ts +314 -0
- package/src/a11y/index.ts +68 -0
- package/src/a11y/media-preferences.ts +255 -0
- package/src/a11y/roving-tab-index.ts +164 -0
- package/src/a11y/skip-link.ts +255 -0
- package/src/a11y/trap-focus.ts +184 -0
- package/src/a11y/types.ts +183 -0
- package/src/component/component.ts +104 -29
- package/src/component/html.ts +5 -5
- package/src/component/index.ts +2 -0
- package/src/component/library.ts +26 -2
- package/src/component/scope.ts +212 -0
- package/src/component/types.ts +94 -40
- package/src/core/collection.ts +707 -628
- package/src/core/element.ts +981 -774
- package/src/core/env.ts +60 -0
- package/src/core/index.ts +49 -48
- package/src/core/shared.ts +62 -13
- package/src/core/utils/index.ts +148 -83
- package/src/devtools/devtools.ts +410 -0
- package/src/devtools/index.ts +48 -0
- package/src/devtools/types.ts +104 -0
- package/src/dnd/draggable.ts +296 -0
- package/src/dnd/droppable.ts +228 -0
- package/src/dnd/index.ts +62 -0
- package/src/dnd/sortable.ts +307 -0
- package/src/dnd/types.ts +293 -0
- package/src/forms/create-form.ts +278 -0
- package/src/forms/index.ts +65 -0
- package/src/forms/types.ts +154 -0
- package/src/forms/validators.ts +265 -0
- package/src/full.ts +253 -2
- package/src/i18n/formatting.ts +67 -0
- package/src/i18n/i18n.ts +200 -0
- package/src/i18n/index.ts +67 -0
- package/src/i18n/translate.ts +182 -0
- package/src/i18n/types.ts +171 -0
- package/src/index.ts +108 -36
- package/src/media/battery.ts +116 -0
- package/src/media/breakpoints.ts +131 -0
- package/src/media/clipboard.ts +80 -0
- package/src/media/device-sensors.ts +158 -0
- package/src/media/geolocation.ts +119 -0
- package/src/media/index.ts +76 -0
- package/src/media/media-query.ts +92 -0
- package/src/media/network.ts +115 -0
- package/src/media/types.ts +177 -0
- package/src/media/viewport.ts +84 -0
- package/src/motion/index.ts +57 -48
- package/src/motion/morph.ts +151 -0
- package/src/motion/parallax.ts +120 -0
- package/src/motion/reduced-motion.ts +66 -17
- package/src/motion/types.ts +271 -208
- package/src/motion/typewriter.ts +164 -0
- package/src/plugin/index.ts +37 -0
- package/src/plugin/registry.ts +269 -0
- package/src/plugin/types.ts +137 -0
- package/src/reactive/computed.ts +130 -92
- package/src/router/bq-link.ts +279 -0
- package/src/router/constraints.ts +201 -0
- package/src/router/index.ts +49 -41
- package/src/router/match.ts +312 -106
- package/src/router/path-pattern.ts +52 -0
- package/src/router/query.ts +38 -35
- package/src/router/router.ts +402 -211
- package/src/router/types.ts +139 -93
- package/src/router/use-route.ts +68 -0
- package/src/router/utils.ts +157 -116
- package/src/security/index.ts +2 -7
- package/src/security/sanitize.ts +70 -70
- package/src/security/trusted-html.ts +71 -71
- package/src/ssr/hydrate.ts +82 -0
- package/src/ssr/index.ts +70 -0
- package/src/ssr/render.ts +508 -0
- package/src/ssr/serialize.ts +296 -0
- package/src/ssr/types.ts +81 -0
- package/src/store/create-store.ts +467 -329
- package/src/store/define-store.ts +49 -49
- package/src/store/index.ts +27 -22
- package/src/store/mapping.ts +74 -74
- package/src/store/persisted.ts +206 -19
- package/src/store/types.ts +157 -2
- package/src/store/utils.ts +135 -141
- package/src/store/watch.ts +53 -53
- package/src/storybook/index.ts +2 -1
- package/src/testing/index.ts +42 -0
- package/src/testing/testing.ts +593 -0
- package/src/testing/types.ts +170 -0
- package/src/view/custom-directives.ts +30 -0
- package/src/view/evaluate.ts +292 -290
- package/src/view/process.ts +108 -92
- package/dist/component-BEQgt5hl.js +0 -600
- package/dist/component-BEQgt5hl.js.map +0 -1
- package/dist/core-BGQJVw0-.js +0 -35
- package/dist/core-BGQJVw0-.js.map +0 -1
- package/dist/core-CCEabVHl.js +0 -648
- package/dist/core-CCEabVHl.js.map +0 -1
- package/dist/effect-AFRW_Plg.js +0 -84
- package/dist/effect-AFRW_Plg.js.map +0 -1
- package/dist/motion-D9TcHxOF.js +0 -415
- package/dist/motion-D9TcHxOF.js.map +0 -1
- package/dist/reactive-DSkct0dO.js +0 -254
- package/dist/reactive-DSkct0dO.js.map +0 -1
- package/dist/router-CbDhl8rS.js +0 -188
- package/dist/router-CbDhl8rS.js.map +0 -1
- package/dist/store-BwDvI45q.js +0 -263
- package/dist/store-BwDvI45q.js.map +0 -1
- package/dist/untrack-B0rVscTc.js +0 -7
- package/dist/untrack-B0rVscTc.js.map +0 -1
- package/dist/view-C70lA3vf.js.map +0 -1
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reactive geolocation.
|
|
3
|
+
*
|
|
4
|
+
* Provides a reactive signal tracking the device's geographic position
|
|
5
|
+
* via the Geolocation API.
|
|
6
|
+
*
|
|
7
|
+
* @module bquery/media
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { signal, readonly } from '../reactive/index';
|
|
11
|
+
import type { GeolocationOptions, GeolocationSignal, GeolocationState } from './types';
|
|
12
|
+
|
|
13
|
+
/** Default geolocation state. */
|
|
14
|
+
const DEFAULT_GEO_STATE: GeolocationState = {
|
|
15
|
+
supported: false,
|
|
16
|
+
loading: false,
|
|
17
|
+
latitude: null,
|
|
18
|
+
longitude: null,
|
|
19
|
+
accuracy: null,
|
|
20
|
+
altitude: null,
|
|
21
|
+
altitudeAccuracy: null,
|
|
22
|
+
heading: null,
|
|
23
|
+
speed: null,
|
|
24
|
+
timestamp: null,
|
|
25
|
+
error: null,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Returns a reactive signal tracking the device's geographic position.
|
|
30
|
+
*
|
|
31
|
+
* Uses the Geolocation API (`navigator.geolocation`) where available.
|
|
32
|
+
* Can operate in one-shot mode (default) or continuous watch mode.
|
|
33
|
+
*
|
|
34
|
+
* @param options - Configuration for the geolocation request
|
|
35
|
+
* @returns A readonly reactive signal with position data and loading/error state,
|
|
36
|
+
* plus a `destroy()` method to stop an active watcher
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* import { useGeolocation } from '@bquery/bquery/media';
|
|
41
|
+
* import { effect } from '@bquery/bquery/reactive';
|
|
42
|
+
*
|
|
43
|
+
* // One-shot position
|
|
44
|
+
* const geo = useGeolocation();
|
|
45
|
+
* effect(() => {
|
|
46
|
+
* if (geo.value.loading) return console.log('Getting position...');
|
|
47
|
+
* if (geo.value.error) return console.error(geo.value.error);
|
|
48
|
+
* console.log(`Lat: ${geo.value.latitude}, Lng: ${geo.value.longitude}`);
|
|
49
|
+
* });
|
|
50
|
+
*
|
|
51
|
+
* // Continuous watch with high accuracy
|
|
52
|
+
* const geoWatch = useGeolocation({ watch: true, enableHighAccuracy: true });
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export const useGeolocation = (options: GeolocationOptions = {}): GeolocationSignal => {
|
|
56
|
+
const { enableHighAccuracy = false, maximumAge = 0, timeout = Infinity, watch = false } = options;
|
|
57
|
+
|
|
58
|
+
const s = signal<GeolocationState>({ ...DEFAULT_GEO_STATE });
|
|
59
|
+
|
|
60
|
+
let destroyWatcher: (() => void) | undefined;
|
|
61
|
+
|
|
62
|
+
if (typeof navigator !== 'undefined' && 'geolocation' in navigator) {
|
|
63
|
+
s.value = { ...DEFAULT_GEO_STATE, supported: true, loading: true };
|
|
64
|
+
|
|
65
|
+
const posOptions: PositionOptions = {
|
|
66
|
+
enableHighAccuracy,
|
|
67
|
+
maximumAge,
|
|
68
|
+
timeout: timeout === Infinity ? undefined : timeout,
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const onSuccess = (pos: GeolocationPosition): void => {
|
|
72
|
+
s.value = {
|
|
73
|
+
supported: true,
|
|
74
|
+
loading: false,
|
|
75
|
+
latitude: pos.coords.latitude,
|
|
76
|
+
longitude: pos.coords.longitude,
|
|
77
|
+
accuracy: pos.coords.accuracy,
|
|
78
|
+
altitude: pos.coords.altitude,
|
|
79
|
+
altitudeAccuracy: pos.coords.altitudeAccuracy,
|
|
80
|
+
heading: pos.coords.heading,
|
|
81
|
+
speed: pos.coords.speed,
|
|
82
|
+
timestamp: pos.timestamp,
|
|
83
|
+
error: null,
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const onError = (err: GeolocationPositionError): void => {
|
|
88
|
+
s.value = {
|
|
89
|
+
...s.value,
|
|
90
|
+
loading: false,
|
|
91
|
+
error: err.message,
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
if (watch) {
|
|
96
|
+
const watchId = navigator.geolocation.watchPosition(onSuccess, onError, posOptions);
|
|
97
|
+
destroyWatcher = () => {
|
|
98
|
+
navigator.geolocation.clearWatch(watchId);
|
|
99
|
+
};
|
|
100
|
+
} else {
|
|
101
|
+
navigator.geolocation.getCurrentPosition(onSuccess, onError, posOptions);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const ro = readonly(s) as GeolocationSignal;
|
|
106
|
+
let destroyed = false;
|
|
107
|
+
Object.defineProperty(ro, 'destroy', {
|
|
108
|
+
enumerable: false,
|
|
109
|
+
configurable: true,
|
|
110
|
+
value(): void {
|
|
111
|
+
if (destroyed) return;
|
|
112
|
+
destroyed = true;
|
|
113
|
+
destroyWatcher?.();
|
|
114
|
+
s.dispose();
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
return ro;
|
|
119
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* bQuery Media module — reactive browser and device API signals.
|
|
3
|
+
*
|
|
4
|
+
* Provides reactive wrappers around browser media queries, viewport,
|
|
5
|
+
* network status, battery, geolocation, device sensors, and clipboard.
|
|
6
|
+
*
|
|
7
|
+
* @module bquery/media
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { mediaQuery, breakpoints, useViewport, useNetworkStatus, clipboard } from '@bquery/bquery/media';
|
|
12
|
+
* import { effect } from '@bquery/bquery/reactive';
|
|
13
|
+
*
|
|
14
|
+
* // Reactive media query
|
|
15
|
+
* const isDark = mediaQuery('(prefers-color-scheme: dark)');
|
|
16
|
+
*
|
|
17
|
+
* // Named breakpoints
|
|
18
|
+
* const bp = breakpoints({ sm: 640, md: 768, lg: 1024, xl: 1280 });
|
|
19
|
+
*
|
|
20
|
+
* // Viewport tracking
|
|
21
|
+
* const viewport = useViewport();
|
|
22
|
+
* effect(() => console.log(viewport.value.width));
|
|
23
|
+
*
|
|
24
|
+
* // Network status
|
|
25
|
+
* const net = useNetworkStatus();
|
|
26
|
+
* effect(() => console.log('Online:', net.value.online));
|
|
27
|
+
*
|
|
28
|
+
* // Clipboard
|
|
29
|
+
* await clipboard.write('Hello!');
|
|
30
|
+
* const text = await clipboard.read();
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
// Media query
|
|
35
|
+
export { mediaQuery } from './media-query';
|
|
36
|
+
|
|
37
|
+
// Breakpoints
|
|
38
|
+
export { breakpoints } from './breakpoints';
|
|
39
|
+
|
|
40
|
+
// Viewport
|
|
41
|
+
export { useViewport } from './viewport';
|
|
42
|
+
|
|
43
|
+
// Network
|
|
44
|
+
export { useNetworkStatus } from './network';
|
|
45
|
+
|
|
46
|
+
// Battery
|
|
47
|
+
export { useBattery } from './battery';
|
|
48
|
+
|
|
49
|
+
// Geolocation
|
|
50
|
+
export { useGeolocation } from './geolocation';
|
|
51
|
+
|
|
52
|
+
// Device sensors
|
|
53
|
+
export { useDeviceMotion, useDeviceOrientation } from './device-sensors';
|
|
54
|
+
|
|
55
|
+
// Clipboard
|
|
56
|
+
export { clipboard } from './clipboard';
|
|
57
|
+
|
|
58
|
+
// Types
|
|
59
|
+
export type {
|
|
60
|
+
BatterySignal,
|
|
61
|
+
BatteryState,
|
|
62
|
+
BreakpointMap,
|
|
63
|
+
ClipboardAPI,
|
|
64
|
+
DeviceMotionSignal,
|
|
65
|
+
DeviceMotionState,
|
|
66
|
+
DeviceOrientationSignal,
|
|
67
|
+
DeviceOrientationState,
|
|
68
|
+
GeolocationOptions,
|
|
69
|
+
GeolocationSignal,
|
|
70
|
+
GeolocationState,
|
|
71
|
+
MediaSignalHandle,
|
|
72
|
+
NetworkSignal,
|
|
73
|
+
NetworkState,
|
|
74
|
+
ViewportSignal,
|
|
75
|
+
ViewportState,
|
|
76
|
+
} from './types';
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reactive media query matching.
|
|
3
|
+
*
|
|
4
|
+
* Returns a reactive boolean signal that tracks whether a CSS media query matches.
|
|
5
|
+
*
|
|
6
|
+
* @module bquery/media
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { readonly, signal } from '../reactive/index';
|
|
10
|
+
import type { MediaSignalHandle } from './types';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Creates a reactive signal that tracks whether a CSS media query matches.
|
|
14
|
+
*
|
|
15
|
+
* Uses `window.matchMedia()` under the hood and automatically updates
|
|
16
|
+
* when the match state changes (e.g., on window resize, device orientation change).
|
|
17
|
+
*
|
|
18
|
+
* @param query - A valid CSS media query string (e.g., `'(min-width: 768px)'`)
|
|
19
|
+
* @returns A readonly reactive signal that is `true` when the query matches,
|
|
20
|
+
* plus a `destroy()` method to remove the media query listener
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { mediaQuery } from '@bquery/bquery/media';
|
|
25
|
+
* import { effect } from '@bquery/bquery/reactive';
|
|
26
|
+
*
|
|
27
|
+
* const isDark = mediaQuery('(prefers-color-scheme: dark)');
|
|
28
|
+
* effect(() => {
|
|
29
|
+
* document.body.classList.toggle('dark', isDark.value);
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* const isWide = mediaQuery('(min-width: 1024px)');
|
|
33
|
+
* effect(() => {
|
|
34
|
+
* console.log('Wide screen:', isWide.value);
|
|
35
|
+
* });
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export const mediaQuery = (query: string): MediaSignalHandle<boolean> => {
|
|
39
|
+
const s = signal(false);
|
|
40
|
+
let cleanup: (() => void) | undefined;
|
|
41
|
+
|
|
42
|
+
if (typeof window !== 'undefined' && typeof window.matchMedia === 'function') {
|
|
43
|
+
try {
|
|
44
|
+
const mql = window.matchMedia(query);
|
|
45
|
+
s.value = mql.matches;
|
|
46
|
+
|
|
47
|
+
const handler = (e: MediaQueryListEvent): void => {
|
|
48
|
+
s.value = e.matches;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
if (typeof mql.addEventListener === 'function') {
|
|
52
|
+
mql.addEventListener('change', handler);
|
|
53
|
+
cleanup = () => {
|
|
54
|
+
mql.removeEventListener('change', handler);
|
|
55
|
+
};
|
|
56
|
+
} else if (
|
|
57
|
+
typeof (
|
|
58
|
+
mql as MediaQueryList & {
|
|
59
|
+
addListener?: (listener: (event: MediaQueryListEvent) => void) => void;
|
|
60
|
+
removeListener?: (listener: (event: MediaQueryListEvent) => void) => void;
|
|
61
|
+
}
|
|
62
|
+
).addListener === 'function'
|
|
63
|
+
) {
|
|
64
|
+
const legacyMql = mql as MediaQueryList & {
|
|
65
|
+
addListener: (listener: (event: MediaQueryListEvent) => void) => void;
|
|
66
|
+
removeListener: (listener: (event: MediaQueryListEvent) => void) => void;
|
|
67
|
+
};
|
|
68
|
+
legacyMql.addListener(handler);
|
|
69
|
+
cleanup = () => {
|
|
70
|
+
legacyMql.removeListener(handler);
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
} catch {
|
|
74
|
+
// matchMedia may throw in non-browser environments
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const ro = readonly(s) as MediaSignalHandle<boolean>;
|
|
79
|
+
let destroyed = false;
|
|
80
|
+
Object.defineProperty(ro, 'destroy', {
|
|
81
|
+
enumerable: false,
|
|
82
|
+
configurable: true,
|
|
83
|
+
value(): void {
|
|
84
|
+
if (destroyed) return;
|
|
85
|
+
destroyed = true;
|
|
86
|
+
cleanup?.();
|
|
87
|
+
s.dispose();
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
return ro;
|
|
92
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reactive network status.
|
|
3
|
+
*
|
|
4
|
+
* Provides a reactive signal tracking the browser's network connectivity
|
|
5
|
+
* and connection quality via the Network Information API.
|
|
6
|
+
*
|
|
7
|
+
* @module bquery/media
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { readonly, signal } from '../reactive/index';
|
|
11
|
+
import type { NetworkSignal, NetworkState } from './types';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Navigator connection interface for the Network Information API.
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
interface NavigatorConnection extends EventTarget {
|
|
18
|
+
effectiveType?: string;
|
|
19
|
+
downlink?: number;
|
|
20
|
+
rtt?: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Extended Navigator with connection property.
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
interface NavigatorWithConnection extends Navigator {
|
|
28
|
+
connection?: NavigatorConnection;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Reads current network state from browser APIs.
|
|
33
|
+
* @internal
|
|
34
|
+
*/
|
|
35
|
+
const getNetworkState = (): NetworkState => {
|
|
36
|
+
const online =
|
|
37
|
+
typeof navigator !== 'undefined' && navigator.onLine !== undefined ? navigator.onLine : true;
|
|
38
|
+
|
|
39
|
+
const nav = typeof navigator !== 'undefined' ? (navigator as NavigatorWithConnection) : undefined;
|
|
40
|
+
const conn = nav?.connection;
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
online,
|
|
44
|
+
effectiveType: conn?.effectiveType ?? 'unknown',
|
|
45
|
+
downlink: conn?.downlink ?? 0,
|
|
46
|
+
rtt: conn?.rtt ?? 0,
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Returns a reactive signal tracking network connectivity and quality.
|
|
52
|
+
*
|
|
53
|
+
* Tracks whether the browser is online/offline and, where supported,
|
|
54
|
+
* the effective connection type, downlink speed, and round-trip time
|
|
55
|
+
* via the Network Information API.
|
|
56
|
+
*
|
|
57
|
+
* @returns A readonly reactive signal with `{ online, effectiveType, downlink, rtt }`
|
|
58
|
+
* and a `destroy()` method to remove attached listeners
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* import { useNetworkStatus } from '@bquery/bquery/media';
|
|
63
|
+
* import { effect } from '@bquery/bquery/reactive';
|
|
64
|
+
*
|
|
65
|
+
* const net = useNetworkStatus();
|
|
66
|
+
* effect(() => {
|
|
67
|
+
* if (!net.value.online) {
|
|
68
|
+
* console.warn('You are offline!');
|
|
69
|
+
* }
|
|
70
|
+
* console.log(`Connection: ${net.value.effectiveType}, RTT: ${net.value.rtt}ms`);
|
|
71
|
+
* });
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export const useNetworkStatus = (): NetworkSignal => {
|
|
75
|
+
const s = signal<NetworkState>(getNetworkState());
|
|
76
|
+
let cleanup: (() => void) | undefined;
|
|
77
|
+
|
|
78
|
+
if (typeof window !== 'undefined') {
|
|
79
|
+
const update = (): void => {
|
|
80
|
+
s.value = getNetworkState();
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
window.addEventListener('online', update);
|
|
84
|
+
window.addEventListener('offline', update);
|
|
85
|
+
|
|
86
|
+
const nav =
|
|
87
|
+
typeof navigator !== 'undefined' ? (navigator as NavigatorWithConnection) : undefined;
|
|
88
|
+
if (nav?.connection && typeof nav.connection.addEventListener === 'function') {
|
|
89
|
+
nav.connection.addEventListener('change', update);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
cleanup = () => {
|
|
93
|
+
window.removeEventListener('online', update);
|
|
94
|
+
window.removeEventListener('offline', update);
|
|
95
|
+
if (nav?.connection && typeof nav.connection.removeEventListener === 'function') {
|
|
96
|
+
nav.connection.removeEventListener('change', update);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const ro = readonly(s) as NetworkSignal;
|
|
102
|
+
let destroyed = false;
|
|
103
|
+
Object.defineProperty(ro, 'destroy', {
|
|
104
|
+
value(): void {
|
|
105
|
+
if (destroyed) return;
|
|
106
|
+
destroyed = true;
|
|
107
|
+
cleanup?.();
|
|
108
|
+
s.dispose();
|
|
109
|
+
},
|
|
110
|
+
enumerable: false,
|
|
111
|
+
configurable: true,
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
return ro;
|
|
115
|
+
};
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for the bQuery media module.
|
|
3
|
+
*
|
|
4
|
+
* @module bquery/media
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { ReadonlySignal } from '../reactive/index';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Readonly media signal with an explicit cleanup hook.
|
|
11
|
+
*
|
|
12
|
+
* Some media utilities subscribe to browser APIs such as resize events or
|
|
13
|
+
* geolocation watchers. Call `destroy()` when the signal is no longer needed
|
|
14
|
+
* to release those underlying subscriptions.
|
|
15
|
+
*/
|
|
16
|
+
export interface MediaSignalHandle<T> extends ReadonlySignal<T> {
|
|
17
|
+
/** Releases any underlying browser listeners or observers. Safe to call multiple times. */
|
|
18
|
+
destroy(): void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Viewport information returned by {@link useViewport}.
|
|
23
|
+
*/
|
|
24
|
+
export interface ViewportState {
|
|
25
|
+
/** Current viewport width in pixels. */
|
|
26
|
+
width: number;
|
|
27
|
+
/** Current viewport height in pixels. */
|
|
28
|
+
height: number;
|
|
29
|
+
/** Current orientation: `'portrait'` or `'landscape'`. */
|
|
30
|
+
orientation: 'portrait' | 'landscape';
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Viewport signal handle returned by {@link useViewport}.
|
|
35
|
+
*/
|
|
36
|
+
export type ViewportSignal = MediaSignalHandle<ViewportState>;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Network connection information returned by {@link useNetworkStatus}.
|
|
40
|
+
*/
|
|
41
|
+
export interface NetworkState {
|
|
42
|
+
/** Whether the browser is online. */
|
|
43
|
+
online: boolean;
|
|
44
|
+
/** Effective connection type (e.g., `'4g'`, `'3g'`, `'2g'`, `'slow-2g'`). */
|
|
45
|
+
effectiveType: string;
|
|
46
|
+
/** Estimated downlink speed in megabits per second. */
|
|
47
|
+
downlink: number;
|
|
48
|
+
/** Estimated round-trip time in milliseconds. */
|
|
49
|
+
rtt: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Network status signal handle returned by {@link useNetworkStatus}.
|
|
54
|
+
*/
|
|
55
|
+
export type NetworkSignal = MediaSignalHandle<NetworkState>;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Battery status information returned by {@link useBattery}.
|
|
59
|
+
*/
|
|
60
|
+
export interface BatteryState {
|
|
61
|
+
/** Whether the battery API is supported. */
|
|
62
|
+
supported: boolean;
|
|
63
|
+
/** Whether the device is charging. */
|
|
64
|
+
charging: boolean;
|
|
65
|
+
/** Time in seconds until the battery is fully charged, or `Infinity`. */
|
|
66
|
+
chargingTime: number;
|
|
67
|
+
/** Time in seconds until the battery is discharged, or `Infinity`. */
|
|
68
|
+
dischargingTime: number;
|
|
69
|
+
/** Battery level as a value between 0 and 1. */
|
|
70
|
+
level: number;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Battery signal handle returned by {@link useBattery}.
|
|
75
|
+
*/
|
|
76
|
+
export type BatterySignal = MediaSignalHandle<BatteryState>;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Geolocation state returned by {@link useGeolocation}.
|
|
80
|
+
*/
|
|
81
|
+
export interface GeolocationState {
|
|
82
|
+
/** Whether the geolocation API is supported. */
|
|
83
|
+
supported: boolean;
|
|
84
|
+
/** Whether position data is being loaded. */
|
|
85
|
+
loading: boolean;
|
|
86
|
+
/** Current latitude, or `null` if unavailable. */
|
|
87
|
+
latitude: number | null;
|
|
88
|
+
/** Current longitude, or `null` if unavailable. */
|
|
89
|
+
longitude: number | null;
|
|
90
|
+
/** Position accuracy in meters, or `null`. */
|
|
91
|
+
accuracy: number | null;
|
|
92
|
+
/** Altitude in meters, or `null`. */
|
|
93
|
+
altitude: number | null;
|
|
94
|
+
/** Altitude accuracy in meters, or `null`. */
|
|
95
|
+
altitudeAccuracy: number | null;
|
|
96
|
+
/** Heading in degrees, or `null`. */
|
|
97
|
+
heading: number | null;
|
|
98
|
+
/** Speed in meters per second, or `null`. */
|
|
99
|
+
speed: number | null;
|
|
100
|
+
/** Timestamp of the position, or `null`. */
|
|
101
|
+
timestamp: number | null;
|
|
102
|
+
/** Error message, or `null` if no error. */
|
|
103
|
+
error: string | null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Geolocation signal handle returned by {@link useGeolocation}.
|
|
108
|
+
*/
|
|
109
|
+
export type GeolocationSignal = MediaSignalHandle<GeolocationState>;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Options for {@link useGeolocation}.
|
|
113
|
+
*/
|
|
114
|
+
export interface GeolocationOptions {
|
|
115
|
+
/** Whether to enable high-accuracy mode. */
|
|
116
|
+
enableHighAccuracy?: boolean;
|
|
117
|
+
/** Maximum age of cached position in milliseconds. */
|
|
118
|
+
maximumAge?: number;
|
|
119
|
+
/** Timeout for position requests in milliseconds. */
|
|
120
|
+
timeout?: number;
|
|
121
|
+
/** Whether to watch for continuous position updates. */
|
|
122
|
+
watch?: boolean;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Device motion state returned by {@link useDeviceMotion}.
|
|
127
|
+
*/
|
|
128
|
+
export interface DeviceMotionState {
|
|
129
|
+
/** Acceleration including gravity (x, y, z). */
|
|
130
|
+
acceleration: { x: number | null; y: number | null; z: number | null };
|
|
131
|
+
/** Acceleration including gravity along each axis. */
|
|
132
|
+
accelerationIncludingGravity: { x: number | null; y: number | null; z: number | null };
|
|
133
|
+
/** Rotation rate around each axis in degrees per second. */
|
|
134
|
+
rotationRate: { alpha: number | null; beta: number | null; gamma: number | null };
|
|
135
|
+
/** Interval in milliseconds between data updates. */
|
|
136
|
+
interval: number;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Device motion signal handle returned by {@link useDeviceMotion}.
|
|
141
|
+
*/
|
|
142
|
+
export type DeviceMotionSignal = MediaSignalHandle<DeviceMotionState>;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Device orientation state returned by {@link useDeviceOrientation}.
|
|
146
|
+
*/
|
|
147
|
+
export interface DeviceOrientationState {
|
|
148
|
+
/** Rotation around the z-axis (0–360). */
|
|
149
|
+
alpha: number | null;
|
|
150
|
+
/** Rotation around the x-axis (−180–180). */
|
|
151
|
+
beta: number | null;
|
|
152
|
+
/** Rotation around the y-axis (−90–90). */
|
|
153
|
+
gamma: number | null;
|
|
154
|
+
/** Whether the orientation is absolute. */
|
|
155
|
+
absolute: boolean;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Device orientation signal handle returned by {@link useDeviceOrientation}.
|
|
160
|
+
*/
|
|
161
|
+
export type DeviceOrientationSignal = MediaSignalHandle<DeviceOrientationState>;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Breakpoint definition map.
|
|
165
|
+
* Keys are breakpoint names, values are minimum widths in pixels.
|
|
166
|
+
*/
|
|
167
|
+
export type BreakpointMap = Record<string, number>;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Clipboard API wrapper.
|
|
171
|
+
*/
|
|
172
|
+
export interface ClipboardAPI {
|
|
173
|
+
/** Read text from the clipboard. */
|
|
174
|
+
read: () => Promise<string>;
|
|
175
|
+
/** Write text to the clipboard. */
|
|
176
|
+
write: (text: string) => Promise<void>;
|
|
177
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reactive viewport dimensions.
|
|
3
|
+
*
|
|
4
|
+
* Provides a reactive signal tracking the browser viewport's
|
|
5
|
+
* width, height, and orientation.
|
|
6
|
+
*
|
|
7
|
+
* @module bquery/media
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { readonly, signal } from '../reactive/index';
|
|
11
|
+
import type { ViewportSignal, ViewportState } from './types';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Computes orientation from width and height.
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
const getOrientation = (w: number, h: number): 'portrait' | 'landscape' =>
|
|
18
|
+
h >= w ? 'portrait' : 'landscape';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Returns a reactive signal tracking the current viewport dimensions and orientation.
|
|
22
|
+
*
|
|
23
|
+
* Updates automatically when the window is resized. Uses `window.innerWidth`
|
|
24
|
+
* and `window.innerHeight` under the hood.
|
|
25
|
+
*
|
|
26
|
+
* @returns A readonly reactive signal with `{ width, height, orientation }`
|
|
27
|
+
* and a `destroy()` method to remove the resize listener
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { useViewport } from '@bquery/bquery/media';
|
|
32
|
+
* import { effect } from '@bquery/bquery/reactive';
|
|
33
|
+
*
|
|
34
|
+
* const viewport = useViewport();
|
|
35
|
+
* effect(() => {
|
|
36
|
+
* console.log(`Viewport: ${viewport.value.width}x${viewport.value.height}`);
|
|
37
|
+
* console.log(`Orientation: ${viewport.value.orientation}`);
|
|
38
|
+
* });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export const useViewport = (): ViewportSignal => {
|
|
42
|
+
const initial: ViewportState = {
|
|
43
|
+
width: typeof window !== 'undefined' ? window.innerWidth : 0,
|
|
44
|
+
height: typeof window !== 'undefined' ? window.innerHeight : 0,
|
|
45
|
+
orientation:
|
|
46
|
+
typeof window !== 'undefined'
|
|
47
|
+
? getOrientation(window.innerWidth, window.innerHeight)
|
|
48
|
+
: 'portrait',
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const s = signal<ViewportState>(initial);
|
|
52
|
+
|
|
53
|
+
let cleanup: (() => void) | undefined;
|
|
54
|
+
|
|
55
|
+
if (typeof window !== 'undefined') {
|
|
56
|
+
const handler = (): void => {
|
|
57
|
+
s.value = {
|
|
58
|
+
width: window.innerWidth,
|
|
59
|
+
height: window.innerHeight,
|
|
60
|
+
orientation: getOrientation(window.innerWidth, window.innerHeight),
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
window.addEventListener('resize', handler);
|
|
65
|
+
cleanup = () => {
|
|
66
|
+
window.removeEventListener('resize', handler);
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const ro = readonly(s) as ViewportSignal;
|
|
71
|
+
let destroyed = false;
|
|
72
|
+
Object.defineProperty(ro, 'destroy', {
|
|
73
|
+
enumerable: false,
|
|
74
|
+
configurable: true,
|
|
75
|
+
value: (): void => {
|
|
76
|
+
if (destroyed) return;
|
|
77
|
+
destroyed = true;
|
|
78
|
+
cleanup?.();
|
|
79
|
+
s.dispose();
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
return ro;
|
|
84
|
+
};
|