nexa-device 0.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.
@@ -0,0 +1,23 @@
1
+ export declare function useVibration(): {
2
+ supported: boolean;
3
+ vibrate(pattern?: number | number[]): void;
4
+ stop(): void;
5
+ };
6
+ export declare function useNetwork(): {
7
+ type: import("nexa-reactivity").Signal<string>;
8
+ isOnline: import("nexa-reactivity").Signal<boolean>;
9
+ destroy(): void;
10
+ };
11
+ export declare function useOrientation(): {
12
+ isLandscape: import("nexa-reactivity").Signal<boolean>;
13
+ destroy(): void;
14
+ };
15
+ export declare function useClipboard(): {
16
+ copied: import("nexa-reactivity").Signal<boolean>;
17
+ copy(text: string): Promise<void>;
18
+ read(): Promise<string>;
19
+ };
20
+ export declare function useShare(): {
21
+ supported: boolean;
22
+ share(data: ShareData): Promise<boolean>;
23
+ };
package/dist/device.js ADDED
@@ -0,0 +1,84 @@
1
+ import { signal } from 'nexa-reactivity';
2
+ export function useVibration() {
3
+ const supported = 'vibrate' in navigator;
4
+ return {
5
+ supported,
6
+ vibrate(pattern = 200) {
7
+ if (supported)
8
+ navigator.vibrate(pattern);
9
+ },
10
+ stop() {
11
+ if (supported)
12
+ navigator.vibrate(0);
13
+ }
14
+ };
15
+ }
16
+ export function useNetwork() {
17
+ const connection = navigator.connection ?? navigator.mozConnection ?? navigator.webkitConnection;
18
+ const type = signal(connection?.effectiveType ?? 'unknown');
19
+ const isOnline = signal(navigator.onLine);
20
+ const updateType = () => { type.value = connection?.effectiveType ?? 'unknown'; };
21
+ const updateOnline = () => { isOnline.value = true; };
22
+ const updateOffline = () => { isOnline.value = false; };
23
+ connection?.addEventListener('change', updateType);
24
+ window.addEventListener('online', updateOnline);
25
+ window.addEventListener('offline', updateOffline);
26
+ return {
27
+ type,
28
+ isOnline,
29
+ destroy() {
30
+ connection?.removeEventListener('change', updateType);
31
+ window.removeEventListener('online', updateOnline);
32
+ window.removeEventListener('offline', updateOffline);
33
+ }
34
+ };
35
+ }
36
+ export function useOrientation() {
37
+ const isLandscape = signal(window.innerWidth > window.innerHeight);
38
+ const update = () => {
39
+ isLandscape.value = window.innerWidth > window.innerHeight;
40
+ };
41
+ window.addEventListener('resize', update);
42
+ window.addEventListener('orientationchange', update);
43
+ return {
44
+ isLandscape,
45
+ destroy() {
46
+ window.removeEventListener('resize', update);
47
+ window.removeEventListener('orientationchange', update);
48
+ }
49
+ };
50
+ }
51
+ export function useClipboard() {
52
+ const copied = signal(false);
53
+ let timer = null;
54
+ return {
55
+ copied,
56
+ async copy(text) {
57
+ await navigator.clipboard.writeText(text);
58
+ copied.value = true;
59
+ if (timer)
60
+ clearTimeout(timer);
61
+ timer = setTimeout(() => { copied.value = false; }, 2000);
62
+ },
63
+ async read() {
64
+ return navigator.clipboard.readText();
65
+ }
66
+ };
67
+ }
68
+ export function useShare() {
69
+ const supported = 'share' in navigator;
70
+ return {
71
+ supported,
72
+ async share(data) {
73
+ if (!supported)
74
+ return false;
75
+ try {
76
+ await navigator.share(data);
77
+ return true;
78
+ }
79
+ catch {
80
+ return false;
81
+ }
82
+ }
83
+ };
84
+ }
@@ -0,0 +1,3 @@
1
+ export { useGeolocation } from './useGeolocation.js';
2
+ export { useVibration, useNetwork, useOrientation, useClipboard, useShare } from './device.js';
3
+ export type { GeolocationState } from './useGeolocation.js';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { useGeolocation } from './useGeolocation.js';
2
+ export { useVibration, useNetwork, useOrientation, useClipboard, useShare } from './device.js';
@@ -0,0 +1,10 @@
1
+ import { signal } from 'nexa-reactivity';
2
+ export interface GeolocationState {
3
+ latitude: ReturnType<typeof signal<number | null>>;
4
+ longitude: ReturnType<typeof signal<number | null>>;
5
+ accuracy: ReturnType<typeof signal<number | null>>;
6
+ error: ReturnType<typeof signal<string | null>>;
7
+ loading: ReturnType<typeof signal<boolean>>;
8
+ stop: () => void;
9
+ }
10
+ export declare function useGeolocation(options?: PositionOptions): GeolocationState;
@@ -0,0 +1,36 @@
1
+ import { signal } from 'nexa-reactivity';
2
+ export function useGeolocation(options) {
3
+ const latitude = signal(null);
4
+ const longitude = signal(null);
5
+ const accuracy = signal(null);
6
+ const error = signal(null);
7
+ const loading = signal(true);
8
+ let watchId = null;
9
+ if ('geolocation' in navigator) {
10
+ watchId = navigator.geolocation.watchPosition((pos) => {
11
+ latitude.value = pos.coords.latitude;
12
+ longitude.value = pos.coords.longitude;
13
+ accuracy.value = pos.coords.accuracy;
14
+ error.value = null;
15
+ loading.value = false;
16
+ }, (err) => {
17
+ error.value = err.message;
18
+ loading.value = false;
19
+ }, options);
20
+ }
21
+ else {
22
+ error.value = 'Geolocation not supported';
23
+ loading.value = false;
24
+ }
25
+ return {
26
+ latitude,
27
+ longitude,
28
+ accuracy,
29
+ error,
30
+ loading,
31
+ stop() {
32
+ if (watchId !== null)
33
+ navigator.geolocation.clearWatch(watchId);
34
+ }
35
+ };
36
+ }
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "nexa-device",
3
+ "version": "0.7.0",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js",
12
+ "default": "./dist/index.js"
13
+ }
14
+ },
15
+ "dependencies": {
16
+ "nexa-reactivity": "0.7.0"
17
+ },
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "publishConfig": {
22
+ "access": "public"
23
+ },
24
+ "scripts": {
25
+ "build": "tsc",
26
+ "clean": "rm -rf dist"
27
+ }
28
+ }