@praxisjs/composables 0.1.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 ADDED
@@ -0,0 +1,13 @@
1
+ # @praxisjs/composables
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - aaf7dab: Initial beta release
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [aaf7dab]
12
+ - @praxisjs/core@0.1.0
13
+ - @praxisjs/shared@0.1.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026-present Mateus Martins
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,13 @@
1
+ import type { Computed } from "@praxisjs/shared";
2
+ export declare function useMediaQuery(query: string): Computed<boolean>;
3
+ export declare function useColorScheme(): {
4
+ isDark: Computed<boolean>;
5
+ isLight: Computed<boolean>;
6
+ };
7
+ export declare function useMouse(): {
8
+ x: Computed<number>;
9
+ y: Computed<number>;
10
+ };
11
+ export declare function useKeyCombo(combo: string): Computed<boolean>;
12
+ export declare function useIdle(timeout?: number): Computed<boolean>;
13
+ //# sourceMappingURL=browser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAEjD,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,CAO9D;AAED,wBAAgB,cAAc;;;EAI7B;AAED,wBAAgB,QAAQ;;;EAQvB;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,CAe5D;AAED,wBAAgB,OAAO,CAAC,OAAO,SAAS,GAAG,QAAQ,CAAC,OAAO,CAAC,CAa3D"}
@@ -0,0 +1,48 @@
1
+ import { signal, computed } from "@praxisjs/core";
2
+ export function useMediaQuery(query) {
3
+ const mql = window.matchMedia(query);
4
+ const matches = signal(mql.matches);
5
+ mql.addEventListener("change", (e) => { matches.set(e.matches); });
6
+ return computed(() => matches());
7
+ }
8
+ export function useColorScheme() {
9
+ const isDark = useMediaQuery("(prefers-color-scheme: dark)");
10
+ const isLight = computed(() => !isDark());
11
+ return { isDark, isLight };
12
+ }
13
+ export function useMouse() {
14
+ const x = signal(0);
15
+ const y = signal(0);
16
+ window.addEventListener("mousemove", (e) => {
17
+ x.set(e.clientX);
18
+ y.set(e.clientY);
19
+ });
20
+ return { x: computed(() => x()), y: computed(() => y()) };
21
+ }
22
+ export function useKeyCombo(combo) {
23
+ const parts = combo.toLowerCase().split("+");
24
+ const pressed = signal(false);
25
+ window.addEventListener("keydown", (e) => {
26
+ const ctrl = parts.includes("ctrl") ? e.ctrlKey : true;
27
+ const shift = parts.includes("shift") ? e.shiftKey : true;
28
+ const alt = parts.includes("alt") ? e.altKey : true;
29
+ const key = parts.find((p) => !["ctrl", "shift", "alt", "meta"].includes(p));
30
+ if (ctrl && shift && alt && (!key || e.key.toLowerCase() === key))
31
+ pressed.set(true);
32
+ });
33
+ window.addEventListener("keyup", () => { pressed.set(false); });
34
+ return computed(() => pressed());
35
+ }
36
+ export function useIdle(timeout = 60_000) {
37
+ const idle = signal(false);
38
+ let timer;
39
+ const reset = () => {
40
+ idle.set(false);
41
+ clearTimeout(timer);
42
+ timer = setTimeout(() => { idle.set(true); }, timeout);
43
+ };
44
+ ["mousemove", "keydown", "click", "scroll", "touchstart"].forEach((e) => { window.addEventListener(e, reset, { passive: true }); });
45
+ reset();
46
+ return computed(() => idle());
47
+ }
48
+ //# sourceMappingURL=browser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser.js","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAGlD,MAAM,UAAU,aAAa,CAAC,KAAa;IACzC,MAAM,GAAG,GAAG,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;IACrC,MAAM,OAAO,GAAG,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACpC,GAAG,CAAC,gBAAgB,CAAC,QAAQ,EAAE,CAAC,CAAsB,EAAE,EAAE,GACtD,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAC5B,CAAC;IACF,OAAO,QAAQ,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC;AACnC,CAAC;AAED,MAAM,UAAU,cAAc;IAC5B,MAAM,MAAM,GAAG,aAAa,CAAC,8BAA8B,CAAC,CAAC;IAC7D,MAAM,OAAO,GAAG,QAAQ,CAAC,GAAG,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;IAC1C,OAAO,EAAE,MAAM,EAAE,OAAO,E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,CAAC,CAAC;AAChC,CAAC"}
package/dist/dom.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ import type { Computed } from "@praxisjs/shared";
2
+ export declare function createRef(): {
3
+ current: HTMLElement | null;
4
+ };
5
+ export declare function useElementSize(ref: {
6
+ current: HTMLElement | null;
7
+ }): {
8
+ width: Computed<number>;
9
+ height: Computed<number>;
10
+ stop: () => void;
11
+ };
12
+ export declare function useWindowSize(): {
13
+ width: Computed<number>;
14
+ height: Computed<number>;
15
+ };
16
+ export declare function useScrollPosition(target?: HTMLElement | Window): {
17
+ x: Computed<number>;
18
+ y: Computed<number>;
19
+ };
20
+ export declare function useIntersection(ref: {
21
+ current: HTMLElement | null;
22
+ }, options?: IntersectionObserverInit): Computed<boolean>;
23
+ export declare function useFocus(ref: {
24
+ current: HTMLElement | null;
25
+ }): Computed<boolean>;
26
+ //# sourceMappingURL=dom.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dom.d.ts","sourceRoot":"","sources":["../src/dom.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAEjD,wBAAgB,SAAS,IAAI;IAAE,OAAO,EAAE,WAAW,GAAG,IAAI,CAAA;CAAE,CAE3D;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE;IAAE,OAAO,EAAE,WAAW,GAAG,IAAI,CAAA;CAAE;;;;EAmBlE;AAED,wBAAgB,aAAa;;;EAQ5B;AAED,wBAAgB,iBAAiB,CAAC,MAAM,GAAE,WAAW,GAAG,MAAe;;;EAYtE;AAED,wBAAgB,eAAe,CAC7B,GAAG,EAAE;IAAE,OAAO,EAAE,WAAW,GAAG,IAAI,CAAA;CAAE,EACpC,OAAO,CAAC,EAAE,wBAAwB,GACjC,QAAQ,CAAC,OAAO,CAAC,CAWnB;AAED,wBAAgB,QAAQ,CAAC,GAAG,EAAE;IAC5B,OAAO,EAAE,WAAW,GAAG,IAAI,CAAC;CAC7B,GAAG,QAAQ,CAAC,OAAO,CAAC,CAepB"}
package/dist/dom.js ADDED
@@ -0,0 +1,70 @@
1
+ import { signal, computed, effect } from "@praxisjs/core";
2
+ export function createRef() {
3
+ return { current: null };
4
+ }
5
+ export function useElementSize(ref) {
6
+ const width = signal(0);
7
+ const height = signal(0);
8
+ const observer = new ResizeObserver(([entry]) => {
9
+ width.set(entry.contentRect.width);
10
+ height.set(entry.contentRect.height);
11
+ });
12
+ effect(() => {
13
+ if (ref.current) {
14
+ observer.observe(ref.current);
15
+ width.set(ref.current.offsetWidth);
16
+ height.set(ref.current.offsetHeight);
17
+ }
18
+ });
19
+ return {
20
+ width: computed(() => width()),
21
+ height: computed(() => height()),
22
+ stop: () => { observer.disconnect(); },
23
+ };
24
+ }
25
+ export function useWindowSize() {
26
+ const width = signal(window.innerWidth);
27
+ const height = signal(window.innerHeight);
28
+ window.addEventListener("resize", () => {
29
+ width.set(window.innerWidth);
30
+ height.set(window.innerHeight);
31
+ });
32
+ return { width: computed(() => width()), height: computed(() => height()) };
33
+ }
34
+ export function useScrollPosition(target = window) {
35
+ const x = signal(0);
36
+ const y = signal(0);
37
+ target.addEventListener("scroll", () => {
38
+ x.set(target === window ? window.scrollX : target.scrollLeft);
39
+ y.set(target === window ? window.scrollY : target.scrollTop);
40
+ });
41
+ return { x: computed(() => x()), y: computed(() => y()) };
42
+ }
43
+ export function useIntersection(ref, options) {
44
+ const visible = signal(false);
45
+ const observer = new IntersectionObserver(([entry]) => { visible.set(entry.isIntersecting); }, options);
46
+ effect(() => {
47
+ if (ref.current)
48
+ observer.observe(ref.current);
49
+ return () => { observer.disconnect(); };
50
+ });
51
+ return computed(() => visible());
52
+ }
53
+ export function useFocus(ref) {
54
+ const focused = signal(false);
55
+ effect(() => {
56
+ const el = ref.current;
57
+ if (!el)
58
+ return;
59
+ const onFocus = () => { focused.set(true); };
60
+ const onBlur = () => { focused.set(false); };
61
+ el.addEventListener("focus", onFocus);
62
+ el.addEventListener("blur", onBlur);
63
+ return () => {
64
+ el.removeEventListener("focus", onFocus);
65
+ el.removeEventListener("blur", onBlur);
66
+ };
67
+ });
68
+ return computed(() => focused());
69
+ }
70
+ //# sourceMappingURL=dom.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dom.js","sourceRoot":"","sources":["../src/dom.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAG1D,MAAM,UAAU,SAAS;IACvB,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;AAC3B,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,GAAoC;IACjE,MAAM,KAAK,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;IACxB,MAAM,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;IACzB,MAAM,QAAQ,GAAG,IAAI,cAAc,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,EAAE;QAC9C,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;QACnC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;IACvC,CAAC,CAAC,CAAC;IACH,MAAM,CAAC,GAAG,EAAE;QACV,IAAI,GAAG,CAAC,OAAO,EAAE,CAAC;YAChB,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;YAC9B,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;YACnC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;QACvC,CAAC;IACH,CAAC,CAAC,CAAC;IACH,OAAO;QACL,KAAK,EAAE,QAAQ,CAAC,GAAG,EAAE,CAAC,KAAK,EAAE,CAAC;QAC9B,MAAM,EAAE,QAAQ,CAAC,GAAG,EAAE,CAAC,MAAM,EAAE,CAAC;QAChC,IAAI,EAAE,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AAO,EAAE,OAAO,CAAC,CAAC;YACzC,EAAE,CAAC,mBAAmB,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACzC,CAAC,CAAC;IACJ,CAAC,CAAC,CAAC;IACH,OAAO,QAAQ,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC;AACnC,CAAC"}
@@ -0,0 +1,5 @@
1
+ export { createRef, useElementSize, useWindowSize, useScrollPosition, useIntersection, useFocus, } from "./dom";
2
+ export { useMediaQuery, useColorScheme, useMouse, useKeyCombo, useIdle, } from "./browser";
3
+ export { useClipboard, useGeolocation, useTimeAgo, usePagination, } from "./utilities";
4
+ export type { PaginationOptions } from "./utilities";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,cAAc,EACd,aAAa,EACb,iBAAiB,EACjB,eAAe,EACf,QAAQ,GACT,MAAM,OAAO,CAAC;AACf,OAAO,EACL,aAAa,EACb,cAAc,EACd,QAAQ,EACR,WAAW,EACX,OAAO,GACR,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,YAAY,EACZ,cAAc,EACd,UAAU,EACV,aAAa,GACd,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { createRef, useElementSize, useWindowSize, useScrollPosition, useIntersection, useFocus, } from "./dom";
2
+ export { useMediaQuery, useColorScheme, useMouse, useKeyCombo, useIdle, } from "./browser";
3
+ export { useClipboard, useGeolocation, useTimeAgo, usePagination, } from "./utilities";
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,cAAc,EACd,aAAa,EACb,iBAAiB,EACjB,eAAe,EACf,QAAQ,GACT,MAAM,OAAO,CAAC;AACf,OAAO,EACL,aAAa,EACb,cAAc,EACd,QAAQ,EACR,WAAW,EACX,OAAO,GACR,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,YAAY,EACZ,cAAc,EACd,UAAU,EACV,aAAa,GACd,MAAM,aAAa,CAAC"}
@@ -0,0 +1,33 @@
1
+ import type { Signal, Computed } from "@praxisjs/shared";
2
+ export declare function useClipboard(resetDelay?: number): {
3
+ copy: (text: string) => Promise<void>;
4
+ copied: Computed<boolean>;
5
+ content: Computed<string>;
6
+ };
7
+ export declare function useGeolocation(options?: PositionOptions): {
8
+ lat: Computed<number | null>;
9
+ lng: Computed<number | null>;
10
+ error: Computed<GeolocationPositionError | null>;
11
+ loading: Computed<boolean>;
12
+ };
13
+ export declare function useTimeAgo(source: Signal<Date | number> | Computed<Date | number> | (() => Date | number), locale?: string): Computed<string>;
14
+ export interface PaginationOptions {
15
+ total: number;
16
+ pageSize: number;
17
+ initial?: number;
18
+ }
19
+ export declare function usePagination(options: PaginationOptions | (() => PaginationOptions)): {
20
+ page: Computed<number>;
21
+ totalPages: Computed<number>;
22
+ offset: Computed<number>;
23
+ hasNext: Computed<boolean>;
24
+ hasPrev: Computed<boolean>;
25
+ pageSize: Computed<number>;
26
+ pages: Computed<number[]>;
27
+ next(): void;
28
+ prev(): void;
29
+ goTo(p: number): void;
30
+ first(): void;
31
+ last(): void;
32
+ };
33
+ //# sourceMappingURL=utilities.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utilities.d.ts","sourceRoot":"","sources":["../src/utilities.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAEzD,wBAAgB,YAAY,CAAC,UAAU,SAAO;iBAGlB,MAAM,KAAG,OAAO,CAAC,IAAI,CAAC;;;EAejD;AAED,wBAAgB,cAAc,CAAC,OAAO,CAAC,EAAE,eAAe;;;;;EAuBvD;AAED,wBAAgB,UAAU,CACxB,MAAM,EACF,MAAM,CAAC,IAAI,GAAG,MAAM,CAAC,GACrB,QAAQ,CAAC,IAAI,GAAG,MAAM,CAAC,GACvB,CAAC,MAAM,IAAI,GAAG,MAAM,CAAC,EACzB,MAAM,SAAU,GACf,QAAQ,CAAC,MAAM,CAAC,CAkBlB;AAED,MAAM,WAAW,iBAAiB;IAChC,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,wBAAgB,aAAa,CAC3B,OAAO,EAAE,iBAAiB,GAAG,CAAC,MAAM,iBAAiB,CAAC;;;;;;;;;;YA2B5C,MAAM;;;EAUjB"}
@@ -0,0 +1,97 @@
1
+ import { signal, computed } from "@praxisjs/core";
2
+ export function useClipboard(resetDelay = 2000) {
3
+ const copied = signal(false);
4
+ const content = signal("");
5
+ async function copy(text) {
6
+ try {
7
+ await navigator.clipboard.writeText(text);
8
+ content.set(text);
9
+ copied.set(true);
10
+ setTimeout(() => { copied.set(false); }, resetDelay);
11
+ }
12
+ catch {
13
+ console.warn("[useClipboard] Falha ao copiar");
14
+ }
15
+ }
16
+ return {
17
+ copy,
18
+ copied: computed(() => copied()),
19
+ content: computed(() => content()),
20
+ };
21
+ }
22
+ export function useGeolocation(options) {
23
+ const lat = signal(null);
24
+ const lng = signal(null);
25
+ const error = signal(null);
26
+ const loading = signal(true);
27
+ navigator.geolocation.getCurrentPosition((pos) => {
28
+ lat.set(pos.coords.latitude);
29
+ lng.set(pos.coords.longitude);
30
+ loading.set(false);
31
+ }, (err) => {
32
+ error.set(err);
33
+ loading.set(false);
34
+ }, options);
35
+ return {
36
+ lat: computed(() => lat()),
37
+ lng: computed(() => lng()),
38
+ error: computed(() => error()),
39
+ loading: computed(() => loading()),
40
+ };
41
+ }
42
+ export function useTimeAgo(source, locale = "pt-BR") {
43
+ const read = typeof source === "function" ? source : source;
44
+ const tick = signal(Date.now());
45
+ setInterval(() => { tick.set(Date.now()); }, 60_000);
46
+ const rtf = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
47
+ return computed(() => {
48
+ void tick();
49
+ const diff = new Date(read()).getTime() - Date.now();
50
+ const abs = Math.abs(diff);
51
+ if (abs < 60_000)
52
+ return rtf.format(Math.round(diff / 1000), "second");
53
+ if (abs < 3_600_000)
54
+ return rtf.format(Math.round(diff / 60_000), "minute");
55
+ if (abs < 86_400_000)
56
+ return rtf.format(Math.round(diff / 3_600_000), "hour");
57
+ if (abs < 2_592_000_000)
58
+ return rtf.format(Math.round(diff / 86_400_000), "day");
59
+ return rtf.format(Math.round(diff / 2_592_000_000), "month");
60
+ });
61
+ }
62
+ export function usePagination(options) {
63
+ const getOpts = typeof options === "function" ? options : () => options;
64
+ const _page = signal(getOpts().initial ?? 1);
65
+ const totalPages = computed(() => Math.ceil(getOpts().total / getOpts().pageSize));
66
+ const page = computed(() => Math.min(_page(), totalPages()));
67
+ const offset = computed(() => (page() - 1) * getOpts().pageSize);
68
+ const hasNext = computed(() => page() < totalPages());
69
+ const hasPrev = computed(() => page() > 1);
70
+ return {
71
+ page,
72
+ totalPages,
73
+ offset,
74
+ hasNext,
75
+ hasPrev,
76
+ pageSize: computed(() => getOpts().pageSize),
77
+ pages: computed(() => Array.from({ length: totalPages() }, (_, i) => i + 1)),
78
+ next() {
79
+ if (hasNext())
80
+ _page.update((p) => p + 1);
81
+ },
82
+ prev() {
83
+ if (hasPrev())
84
+ _page.update((p) => p - 1);
85
+ },
86
+ goTo(p) {
87
+ _page.set(Math.max(1, Math.min(p, totalPages())));
88
+ },
89
+ first() {
90
+ _page.set(1);
91
+ },
92
+ last() {
93
+ _page.set(totalPages());
94
+ },
95
+ };
96
+ }
97
+ //# sourceMappingURL=utilities.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utilities.js","sourceRoot":"","sources":["../src/utilities.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,QAAQ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AAC;QACf,CAAC;QACD,IAAI;YACF,KAAK,CAAC,GAAG,CAAC,UAAU,EAAE,CAAC,CAAC;QAC1B,CAAC;KACF,CAAC;AACJ,CAAC"}
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@praxisjs/composables",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.js",
10
+ "types": "./dist/index.d.ts"
11
+ }
12
+ },
13
+ "devDependencies": {
14
+ "typescript": "^5.9.3"
15
+ },
16
+ "dependencies": {
17
+ "@praxisjs/core": "0.1.0",
18
+ "@praxisjs/shared": "0.1.0"
19
+ },
20
+ "scripts": {
21
+ "build": "tsc",
22
+ "dev": "tsc --watch"
23
+ }
24
+ }
package/src/browser.ts ADDED
@@ -0,0 +1,59 @@
1
+ import { signal, computed } from "@praxisjs/core";
2
+ import type { Computed } from "@praxisjs/shared";
3
+
4
+ export function useMediaQuery(query: string): Computed<boolean> {
5
+ const mql = window.matchMedia(query);
6
+ const matches = signal(mql.matches);
7
+ mql.addEventListener("change", (e: MediaQueryListEvent) =>
8
+ { matches.set(e.matches); },
9
+ );
10
+ return computed(() => matches());
11
+ }
12
+
13
+ export function useColorScheme() {
14
+ const isDark = useMediaQuery("(prefers-color-scheme: dark)");
15
+ const isLight = computed(() => !isDark());
16
+ return { isDark, isLight };
17
+ }
18
+
19
+ export function useMouse() {
20
+ const x = signal(0);
21
+ const y = signal(0);
22
+ window.addEventListener("mousemove", (e) => {
23
+ x.set(e.clientX);
24
+ y.set(e.clientY);
25
+ });
26
+ return { x: computed(() => x()), y: computed(() => y()) };
27
+ }
28
+
29
+ export function useKeyCombo(combo: string): Computed<boolean> {
30
+ const parts = combo.toLowerCase().split("+");
31
+ const pressed = signal(false);
32
+ window.addEventListener("keydown", (e: KeyboardEvent) => {
33
+ const ctrl = parts.includes("ctrl") ? e.ctrlKey : true;
34
+ const shift = parts.includes("shift") ? e.shiftKey : true;
35
+ const alt = parts.includes("alt") ? e.altKey : true;
36
+ const key = parts.find(
37
+ (p) => !["ctrl", "shift", "alt", "meta"].includes(p),
38
+ );
39
+ if (ctrl && shift && alt && (!key || e.key.toLowerCase() === key))
40
+ pressed.set(true);
41
+ });
42
+ window.addEventListener("keyup", () => { pressed.set(false); });
43
+ return computed(() => pressed());
44
+ }
45
+
46
+ export function useIdle(timeout = 60_000): Computed<boolean> {
47
+ const idle = signal(false);
48
+ let timer: ReturnType<typeof setTimeout>;
49
+ const reset = () => {
50
+ idle.set(false);
51
+ clearTimeout(timer);
52
+ timer = setTimeout(() => { idle.set(true); }, timeout);
53
+ };
54
+ ["mousemove", "keydown", "click", "scroll", "touchstart"].forEach((e) =>
55
+ { window.addEventListener(e, reset, { passive: true }); },
56
+ );
57
+ reset();
58
+ return computed(() => idle());
59
+ }
package/src/dom.ts ADDED
@@ -0,0 +1,86 @@
1
+ import { signal, computed, effect } from "@praxisjs/core";
2
+ import type { Computed } from "@praxisjs/shared";
3
+
4
+ export function createRef(): { current: HTMLElement | null } {
5
+ return { current: null };
6
+ }
7
+
8
+ export function useElementSize(ref: { current: HTMLElement | null }) {
9
+ const width = signal(0);
10
+ const height = signal(0);
11
+ const observer = new ResizeObserver(([entry]) => {
12
+ width.set(entry.contentRect.width);
13
+ height.set(entry.contentRect.height);
14
+ });
15
+ effect(() => {
16
+ if (ref.current) {
17
+ observer.observe(ref.current);
18
+ width.set(ref.current.offsetWidth);
19
+ height.set(ref.current.offsetHeight);
20
+ }
21
+ });
22
+ return {
23
+ width: computed(() => width()),
24
+ height: computed(() => height()),
25
+ stop: () => { observer.disconnect(); },
26
+ };
27
+ }
28
+
29
+ export function useWindowSize() {
30
+ const width = signal(window.innerWidth);
31
+ const height = signal(window.innerHeight);
32
+ window.addEventListener("resize", () => {
33
+ width.set(window.innerWidth);
34
+ height.set(window.innerHeight);
35
+ });
36
+ return { width: computed(() => width()), height: computed(() => height()) };
37
+ }
38
+
39
+ export function useScrollPosition(target: HTMLElement | Window = window) {
40
+ const x = signal(0);
41
+ const y = signal(0);
42
+ target.addEventListener("scroll", () => {
43
+ x.set(
44
+ target === window ? window.scrollX : (target as HTMLElement).scrollLeft,
45
+ );
46
+ y.set(
47
+ target === window ? window.scrollY : (target as HTMLElement).scrollTop,
48
+ );
49
+ });
50
+ return { x: computed(() => x()), y: computed(() => y()) };
51
+ }
52
+
53
+ export function useIntersection(
54
+ ref: { current: HTMLElement | null },
55
+ options?: IntersectionObserverInit,
56
+ ): Computed<boolean> {
57
+ const visible = signal(false);
58
+ const observer = new IntersectionObserver(
59
+ ([entry]) => { visible.set(entry.isIntersecting); },
60
+ options,
61
+ );
62
+ effect(() => {
63
+ if (ref.current) observer.observe(ref.current);
64
+ return () => { observer.disconnect(); };
65
+ });
66
+ return computed(() => visible());
67
+ }
68
+
69
+ export function useFocus(ref: {
70
+ current: HTMLElement | null;
71
+ }): Computed<boolean> {
72
+ const focused = signal(false);
73
+ effect(() => {
74
+ const el = ref.current;
75
+ if (!el) return;
76
+ const onFocus = () => { focused.set(true); };
77
+ const onBlur = () => { focused.set(false); };
78
+ el.addEventListener("focus", onFocus);
79
+ el.addEventListener("blur", onBlur);
80
+ return () => {
81
+ el.removeEventListener("focus", onFocus);
82
+ el.removeEventListener("blur", onBlur);
83
+ };
84
+ });
85
+ return computed(() => focused());
86
+ }
package/src/index.ts ADDED
@@ -0,0 +1,22 @@
1
+ export {
2
+ createRef,
3
+ useElementSize,
4
+ useWindowSize,
5
+ useScrollPosition,
6
+ useIntersection,
7
+ useFocus,
8
+ } from "./dom";
9
+ export {
10
+ useMediaQuery,
11
+ useColorScheme,
12
+ useMouse,
13
+ useKeyCombo,
14
+ useIdle,
15
+ } from "./browser";
16
+ export {
17
+ useClipboard,
18
+ useGeolocation,
19
+ useTimeAgo,
20
+ usePagination,
21
+ } from "./utilities";
22
+ export type { PaginationOptions } from "./utilities";
@@ -0,0 +1,119 @@
1
+ import { signal, computed } from "@praxisjs/core";
2
+ import type { Signal, Computed } from "@praxisjs/shared";
3
+
4
+ export function useClipboard(resetDelay = 2000) {
5
+ const copied = signal(false);
6
+ const content = signal("");
7
+ async function copy(text: string): Promise<void> {
8
+ try {
9
+ await navigator.clipboard.writeText(text);
10
+ content.set(text);
11
+ copied.set(true);
12
+ setTimeout(() => { copied.set(false); }, resetDelay);
13
+ } catch {
14
+ console.warn("[useClipboard] Falha ao copiar");
15
+ }
16
+ }
17
+ return {
18
+ copy,
19
+ copied: computed(() => copied()),
20
+ content: computed(() => content()),
21
+ };
22
+ }
23
+
24
+ export function useGeolocation(options?: PositionOptions) {
25
+ const lat = signal<number | null>(null);
26
+ const lng = signal<number | null>(null);
27
+ const error = signal<GeolocationPositionError | null>(null);
28
+ const loading = signal(true);
29
+ navigator.geolocation.getCurrentPosition(
30
+ (pos) => {
31
+ lat.set(pos.coords.latitude);
32
+ lng.set(pos.coords.longitude);
33
+ loading.set(false);
34
+ },
35
+ (err) => {
36
+ error.set(err);
37
+ loading.set(false);
38
+ },
39
+ options,
40
+ );
41
+ return {
42
+ lat: computed(() => lat()),
43
+ lng: computed(() => lng()),
44
+ error: computed(() => error()),
45
+ loading: computed(() => loading()),
46
+ };
47
+ }
48
+
49
+ export function useTimeAgo(
50
+ source:
51
+ | Signal<Date | number>
52
+ | Computed<Date | number>
53
+ | (() => Date | number),
54
+ locale = "pt-BR",
55
+ ): Computed<string> {
56
+ const read = typeof source === "function" ? source : source;
57
+ const tick = signal(Date.now());
58
+ setInterval(() => { tick.set(Date.now()); }, 60_000);
59
+ const rtf = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
60
+
61
+ return computed(() => {
62
+ void tick();
63
+ const diff = new Date(read() as Date).getTime() - Date.now();
64
+ const abs = Math.abs(diff);
65
+ if (abs < 60_000) return rtf.format(Math.round(diff / 1000), "second");
66
+ if (abs < 3_600_000) return rtf.format(Math.round(diff / 60_000), "minute");
67
+ if (abs < 86_400_000)
68
+ return rtf.format(Math.round(diff / 3_600_000), "hour");
69
+ if (abs < 2_592_000_000)
70
+ return rtf.format(Math.round(diff / 86_400_000), "day");
71
+ return rtf.format(Math.round(diff / 2_592_000_000), "month");
72
+ });
73
+ }
74
+
75
+ export interface PaginationOptions {
76
+ total: number;
77
+ pageSize: number;
78
+ initial?: number;
79
+ }
80
+
81
+ export function usePagination(
82
+ options: PaginationOptions | (() => PaginationOptions),
83
+ ) {
84
+ const getOpts = typeof options === "function" ? options : () => options;
85
+ const _page = signal(getOpts().initial ?? 1);
86
+ const totalPages = computed(() =>
87
+ Math.ceil(getOpts().total / getOpts().pageSize),
88
+ );
89
+ const page = computed(() => Math.min(_page(), totalPages()));
90
+ const offset = computed(() => (page() - 1) * getOpts().pageSize);
91
+ const hasNext = computed(() => page() < totalPages());
92
+ const hasPrev = computed(() => page() > 1);
93
+ return {
94
+ page,
95
+ totalPages,
96
+ offset,
97
+ hasNext,
98
+ hasPrev,
99
+ pageSize: computed(() => getOpts().pageSize),
100
+ pages: computed(() =>
101
+ Array.from({ length: totalPages() }, (_, i) => i + 1),
102
+ ),
103
+ next() {
104
+ if (hasNext()) _page.update((p) => p + 1);
105
+ },
106
+ prev() {
107
+ if (hasPrev()) _page.update((p) => p - 1);
108
+ },
109
+ goTo(p: number) {
110
+ _page.set(Math.max(1, Math.min(p, totalPages())));
111
+ },
112
+ first() {
113
+ _page.set(1);
114
+ },
115
+ last() {
116
+ _page.set(totalPages());
117
+ },
118
+ };
119
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./src"
6
+ },
7
+ "include": ["src"]
8
+ }