@scalar/use-hooks 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-present Scalar
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.
package/README.md ADDED
@@ -0,0 +1,14 @@
1
+ # Scalar Use Hooks
2
+
3
+ A collection of utility hooks for Scalar.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm add @scalar/use-hooks
9
+ ```
10
+
11
+ ## Available Hooks
12
+
13
+ - [useClipboard](./src/useClipboard/README.md)
14
+ - [useColorMode](./src/useColorMode/README.md)
@@ -0,0 +1,2 @@
1
+ export * from './useClipboard.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/useClipboard/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA"}
@@ -0,0 +1,4 @@
1
+ import { useClipboard as e } from "./useClipboard.js";
2
+ export {
3
+ e as useClipboard
4
+ };
@@ -0,0 +1,7 @@
1
+ export type UseClipboardOptions = {
2
+ /**
3
+ * A function that will be called when the text is copied to the clipboard
4
+ */
5
+ notify?: (message: string) => void;
6
+ };
7
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/useClipboard/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,mBAAmB,GAAG;IAChC;;OAEG;IACH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAA;CACnC,CAAA"}
@@ -0,0 +1,8 @@
1
+ import type { UseClipboardOptions } from './types';
2
+ /**
3
+ * A hook for interacting with the clipboard
4
+ */
5
+ export declare function useClipboard(opts?: UseClipboardOptions): {
6
+ copyToClipboard: (value: string) => Promise<void>;
7
+ };
8
+ //# sourceMappingURL=useClipboard.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useClipboard.d.ts","sourceRoot":"","sources":["../../src/useClipboard/useClipboard.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAA;AAElD;;GAEG;AACH,wBAAgB,YAAY,CAAC,IAAI,GAAE,mBAAwB;6BAInB,MAAM;EAY7C"}
@@ -0,0 +1,15 @@
1
+ import { useToasts as c } from "@scalar/use-toasts";
2
+ function p(t = {}) {
3
+ const { notify: r = (o) => e(o, "info") } = t, { toast: e } = c();
4
+ async function a(o) {
5
+ try {
6
+ await navigator.clipboard.writeText(o), r("Copied to the clipboard");
7
+ } catch (i) {
8
+ console.error(i.message), r("Failed to copy to clipboard");
9
+ }
10
+ }
11
+ return { copyToClipboard: a };
12
+ }
13
+ export {
14
+ p as useClipboard
15
+ };
@@ -0,0 +1,3 @@
1
+ export * from './useColorMode.js';
2
+ export * from './types';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/useColorMode/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA;AAC9B,cAAc,SAAS,CAAA"}
@@ -0,0 +1,4 @@
1
+ import { useColorMode as r } from "./useColorMode.js";
2
+ export {
3
+ r as useColorMode
4
+ };
@@ -0,0 +1,10 @@
1
+ /** Possible color modes */
2
+ export type ColorMode = 'light' | 'dark' | 'system';
3
+ /** Options for the useColorMode hook */
4
+ export type UseColorModeOptions = {
5
+ /** The initial color mode to use */
6
+ initialColorMode?: ColorMode;
7
+ /** Override the color mode */
8
+ overrideColorMode?: ColorMode;
9
+ };
10
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/useColorMode/types.ts"],"names":[],"mappings":"AAAA,2BAA2B;AAC3B,MAAM,MAAM,SAAS,GAAG,OAAO,GAAG,MAAM,GAAG,QAAQ,CAAA;AAEnD,wCAAwC;AACxC,MAAM,MAAM,mBAAmB,GAAG;IAChC,oCAAoC;IACpC,gBAAgB,CAAC,EAAE,SAAS,CAAA;IAC5B,8BAA8B;IAC9B,iBAAiB,CAAC,EAAE,SAAS,CAAA;CAC9B,CAAA"}
@@ -0,0 +1,11 @@
1
+ import type { ColorMode, UseColorModeOptions } from './types';
2
+ /**
3
+ * A composable hook that provides color mode (dark/light) functionality.
4
+ */
5
+ export declare function useColorMode(opts?: UseColorModeOptions): {
6
+ colorMode: import("vue").ComputedRef<ColorMode>;
7
+ toggleColorMode: () => void;
8
+ setColorMode: (value: ColorMode) => void;
9
+ getSystemModePreference: () => ColorMode;
10
+ };
11
+ //# sourceMappingURL=useColorMode.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useColorMode.d.ts","sourceRoot":"","sources":["../../src/useColorMode/useColorMode.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAA;AAS7D;;GAEG;AACH,wBAAgB,YAAY,CAAC,IAAI,GAAE,mBAAwB;;;0BAgB5B,SAAS;mCAOF,SAAS;EAuD9C"}
@@ -0,0 +1,43 @@
1
+ import { ref as u, watch as f, onMounted as M, onUnmounted as h, computed as w } from "vue";
2
+ import { z as y } from "zod";
3
+ const o = u("dark"), v = y.enum(["dark", "light", "system"]).optional().catch(void 0);
4
+ function k(a = {}) {
5
+ var r;
6
+ const { initialColorMode: c = "system", overrideColorMode: i } = a;
7
+ function s() {
8
+ var t;
9
+ const e = o.value === "system" ? d() : o.value;
10
+ o.value = e === "dark" ? "light" : "dark", !(typeof window > "u") && ((t = window == null ? void 0 : window.localStorage) == null || t.setItem("colorMode", o.value));
11
+ }
12
+ function l(e) {
13
+ var t;
14
+ o.value = e, !(typeof window > "u") && ((t = window == null ? void 0 : window.localStorage) == null || t.setItem("colorMode", o.value));
15
+ }
16
+ function d() {
17
+ var e;
18
+ return typeof (window == null ? void 0 : window.matchMedia) != "function" || (e = window == null ? void 0 : window.matchMedia("(prefers-color-scheme: dark)")) != null && e.matches ? "dark" : "light";
19
+ }
20
+ function n(e) {
21
+ if (typeof document > "u") return;
22
+ (i ?? (e === "system" ? d() : e)) === "dark" ? (document.body.classList.add("dark-mode"), document.body.classList.remove("light-mode")) : (document.body.classList.add("light-mode"), document.body.classList.remove("dark-mode"));
23
+ }
24
+ const m = v.parse(
25
+ (r = window == null ? void 0 : window.localStorage) == null ? void 0 : r.getItem("colorMode")
26
+ );
27
+ return o.value = m ?? c, f(o, n, { immediate: !0 }), M(() => {
28
+ if (typeof (window == null ? void 0 : window.matchMedia) == "function") {
29
+ const e = window.matchMedia("(prefers-color-scheme: dark)"), t = () => o.value === "system" && n("system");
30
+ e.addEventListener("change", t), h(() => {
31
+ e.removeEventListener("change", t);
32
+ });
33
+ }
34
+ }), {
35
+ colorMode: w(() => o.value),
36
+ toggleColorMode: s,
37
+ setColorMode: l,
38
+ getSystemModePreference: d
39
+ };
40
+ }
41
+ export {
42
+ k as useColorMode
43
+ };
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@scalar/use-hooks",
3
+ "description": "Utility hooks for Scalar",
4
+ "license": "MIT",
5
+ "author": "Scalar (https://github.com/scalar)",
6
+ "homepage": "https://github.com/scalar/scalar",
7
+ "bugs": "https://github.com/scalar/scalar/issues/new/choose",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/scalar/scalar.git",
11
+ "directory": "packages/use-hooks"
12
+ },
13
+ "version": "0.1.0",
14
+ "engines": {
15
+ "node": ">=18"
16
+ },
17
+ "type": "module",
18
+ "main": "./dist/index.cjs",
19
+ "types": "./dist/index.d.ts",
20
+ "exports": {
21
+ "./useColorMode": {
22
+ "import": "./dist/useColorMode/index.js",
23
+ "types": "./dist/useColorMode/index.d.ts",
24
+ "default": "./dist/useColorMode/index.js"
25
+ },
26
+ "./useClipboard": {
27
+ "import": "./dist/useClipboard/index.js",
28
+ "types": "./dist/useClipboard/index.d.ts",
29
+ "default": "./dist/useClipboard/index.js"
30
+ }
31
+ },
32
+ "files": [
33
+ "dist"
34
+ ],
35
+ "module": "./dist/index.js",
36
+ "dependencies": {
37
+ "vue": "^3.5.12",
38
+ "zod": "^3.23.8",
39
+ "@scalar/use-toasts": "0.7.7"
40
+ },
41
+ "devDependencies": {
42
+ "@vitejs/plugin-vue": "^5.0.4",
43
+ "vite": "^5.4.9",
44
+ "zod-to-ts": "^1.2.0",
45
+ "@scalar/build-tooling": "0.1.12"
46
+ },
47
+ "scripts": {
48
+ "build": "scalar-build-vite",
49
+ "lint:check": "eslint .",
50
+ "lint:fix": "eslint . --fix",
51
+ "test": "vitest",
52
+ "types:build": "scalar-types-build-vue",
53
+ "types:check": "scalar-types-check-vue"
54
+ }
55
+ }