@irisui/utils 0.0.0-stage → 0.1.1
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 +21 -0
- package/README.md +55 -2
- package/dist/index.cjs +97 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +25 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +69 -0
- package/dist/index.js.map +1 -0
- package/package.json +51 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 theteqlab
|
|
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
CHANGED
|
@@ -1,3 +1,56 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @irisui/utils
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Shared utilities and React hooks for
|
|
4
|
+
[iris-ui](https://github.com/theteqlab/iris-ui).
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npm install @irisui/utils
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
`react` (18 or 19) is a peer dependency.
|
|
13
|
+
|
|
14
|
+
## API
|
|
15
|
+
|
|
16
|
+
### `cn(...inputs)`
|
|
17
|
+
|
|
18
|
+
Merges class names with [clsx](https://github.com/lukeed/clsx) and resolves
|
|
19
|
+
conflicting Tailwind utilities with
|
|
20
|
+
[tailwind-merge](https://github.com/dcastil/tailwind-merge) (last one wins).
|
|
21
|
+
It knows the custom theme keys of `@irisui/tokens/tailwind.css`, so `text-h3`
|
|
22
|
+
is treated as a font size, not a text color.
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { cn } from "@irisui/utils";
|
|
26
|
+
|
|
27
|
+
cn("rounded-card p-4", isActive && "bg-glass-strong", className);
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### `useControllableState({ value, defaultValue, onChange })`
|
|
31
|
+
|
|
32
|
+
State that can be controlled (`value` + `onChange`) or uncontrolled
|
|
33
|
+
(`defaultValue`), following the Radix convention. Returns `[value, setValue]`;
|
|
34
|
+
`setValue` accepts a value or an updater function.
|
|
35
|
+
|
|
36
|
+
```tsx
|
|
37
|
+
import { useControllableState } from "@irisui/utils";
|
|
38
|
+
|
|
39
|
+
function Toggle({ pressed, defaultPressed = false, onPressedChange }: ToggleProps) {
|
|
40
|
+
const [isPressed, setPressed] = useControllableState({
|
|
41
|
+
value: pressed,
|
|
42
|
+
defaultValue: defaultPressed,
|
|
43
|
+
onChange: onPressedChange,
|
|
44
|
+
});
|
|
45
|
+
return <button aria-pressed={isPressed} onClick={() => setPressed((p) => !p)} />;
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### `useReducedMotion()`
|
|
50
|
+
|
|
51
|
+
Returns `true` when the user asked the system for less motion
|
|
52
|
+
(`prefers-reduced-motion: reduce`). Always `false` on the server.
|
|
53
|
+
|
|
54
|
+
## License
|
|
55
|
+
|
|
56
|
+
[MIT](./LICENSE)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var index_exports = {};
|
|
23
|
+
__export(index_exports, {
|
|
24
|
+
cn: () => cn,
|
|
25
|
+
useControllableState: () => useControllableState,
|
|
26
|
+
useReducedMotion: () => useReducedMotion
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
|
+
|
|
30
|
+
// src/cn.ts
|
|
31
|
+
var import_clsx = require("clsx");
|
|
32
|
+
var import_tailwind_merge = require("tailwind-merge");
|
|
33
|
+
var twMerge = (0, import_tailwind_merge.extendTailwindMerge)({
|
|
34
|
+
extend: {
|
|
35
|
+
theme: {
|
|
36
|
+
text: ["xs", "sm", "md", "lg", "h1", "h2", "h3"],
|
|
37
|
+
"font-weight": ["regular", "medium", "bold"],
|
|
38
|
+
tracking: ["display"],
|
|
39
|
+
radius: ["pill", "field", "check", "card", "panel"],
|
|
40
|
+
blur: ["glass", "small"],
|
|
41
|
+
shadow: ["glass", "rim", "holo"],
|
|
42
|
+
animate: ["holo"]
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
function cn(...inputs) {
|
|
47
|
+
return twMerge((0, import_clsx.clsx)(inputs));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// src/use-reduced-motion.ts
|
|
51
|
+
var import_react = require("react");
|
|
52
|
+
var QUERY = "(prefers-reduced-motion: reduce)";
|
|
53
|
+
function subscribe(onChange) {
|
|
54
|
+
const media = window.matchMedia(QUERY);
|
|
55
|
+
media.addEventListener("change", onChange);
|
|
56
|
+
return () => media.removeEventListener("change", onChange);
|
|
57
|
+
}
|
|
58
|
+
var getSnapshot = () => window.matchMedia(QUERY).matches;
|
|
59
|
+
var getServerSnapshot = () => false;
|
|
60
|
+
function useReducedMotion() {
|
|
61
|
+
return (0, import_react.useSyncExternalStore)(subscribe, getSnapshot, getServerSnapshot);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// src/use-controllable-state.ts
|
|
65
|
+
var import_react2 = require("react");
|
|
66
|
+
function useControllableState({
|
|
67
|
+
value,
|
|
68
|
+
defaultValue,
|
|
69
|
+
onChange
|
|
70
|
+
}) {
|
|
71
|
+
const [uncontrolled, setUncontrolled] = (0, import_react2.useState)(defaultValue);
|
|
72
|
+
const isControlled = value !== void 0;
|
|
73
|
+
const current = isControlled ? value : uncontrolled;
|
|
74
|
+
const onChangeRef = (0, import_react2.useRef)(onChange);
|
|
75
|
+
(0, import_react2.useEffect)(() => {
|
|
76
|
+
onChangeRef.current = onChange;
|
|
77
|
+
});
|
|
78
|
+
const currentRef = (0, import_react2.useRef)(current);
|
|
79
|
+
currentRef.current = current;
|
|
80
|
+
const setValue = (0, import_react2.useCallback)(
|
|
81
|
+
(next) => {
|
|
82
|
+
const resolved = typeof next === "function" ? next(currentRef.current) : next;
|
|
83
|
+
if (Object.is(resolved, currentRef.current)) return;
|
|
84
|
+
if (!isControlled) setUncontrolled(resolved);
|
|
85
|
+
onChangeRef.current?.(resolved);
|
|
86
|
+
},
|
|
87
|
+
[isControlled]
|
|
88
|
+
);
|
|
89
|
+
return [current, setValue];
|
|
90
|
+
}
|
|
91
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
92
|
+
0 && (module.exports = {
|
|
93
|
+
cn,
|
|
94
|
+
useControllableState,
|
|
95
|
+
useReducedMotion
|
|
96
|
+
});
|
|
97
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/cn.ts","../src/use-reduced-motion.ts","../src/use-controllable-state.ts"],"sourcesContent":["export { cn, type ClassValue } from \"./cn\";\nexport { useReducedMotion } from \"./use-reduced-motion\";\nexport {\n useControllableState,\n type UseControllableStateParams,\n} from \"./use-controllable-state\";\n","import { clsx, type ClassValue } from \"clsx\";\nimport { extendTailwindMerge } from \"tailwind-merge\";\n\n/**\n * tailwind-merge only knows Tailwind's default scale. The custom theme keys\n * declared in `@irisui/tokens/tailwind.css` are registered here so that, for\n * instance, `text-h3` is seen as a font size and not as a text color.\n */\nconst twMerge = extendTailwindMerge({\n extend: {\n theme: {\n text: [\"xs\", \"sm\", \"md\", \"lg\", \"h1\", \"h2\", \"h3\"],\n \"font-weight\": [\"regular\", \"medium\", \"bold\"],\n tracking: [\"display\"],\n radius: [\"pill\", \"field\", \"check\", \"card\", \"panel\"],\n blur: [\"glass\", \"small\"],\n shadow: [\"glass\", \"rim\", \"holo\"],\n animate: [\"holo\"],\n },\n },\n});\n\n/** Merges class names, resolving conflicting Tailwind utilities (last one wins). */\nexport function cn(...inputs: ClassValue[]): string {\n return twMerge(clsx(inputs));\n}\n\nexport type { ClassValue };\n","import { useSyncExternalStore } from \"react\";\n\nconst QUERY = \"(prefers-reduced-motion: reduce)\";\n\nfunction subscribe(onChange: () => void): () => void {\n const media = window.matchMedia(QUERY);\n media.addEventListener(\"change\", onChange);\n return () => media.removeEventListener(\"change\", onChange);\n}\n\nconst getSnapshot = (): boolean => window.matchMedia(QUERY).matches;\nconst getServerSnapshot = (): boolean => false;\n\n/** `true` when the user asked the system for less motion. Always `false` on the server. */\nexport function useReducedMotion(): boolean {\n return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n}\n","import { useCallback, useEffect, useRef, useState } from \"react\";\n\nexport interface UseControllableStateParams<T> {\n /** Controlled value. When defined, the component is controlled. */\n value?: T | undefined;\n /** Initial value when uncontrolled. */\n defaultValue: T;\n /** Called with the next value, in both controlled and uncontrolled modes. */\n onChange?: ((value: T) => void) | undefined;\n}\n\ntype SetStateFn<T> = (next: T | ((prev: T) => T)) => void;\n\n/**\n * State that can be either controlled (`value` + `onChange`) or uncontrolled\n * (`defaultValue`), following the Radix / shadcn convention.\n */\nexport function useControllableState<T>({\n value,\n defaultValue,\n onChange,\n}: UseControllableStateParams<T>): [T, SetStateFn<T>] {\n const [uncontrolled, setUncontrolled] = useState<T>(defaultValue);\n const isControlled = value !== undefined;\n const current = isControlled ? value : uncontrolled;\n\n const onChangeRef = useRef(onChange);\n useEffect(() => {\n onChangeRef.current = onChange;\n });\n\n const currentRef = useRef(current);\n currentRef.current = current;\n\n const setValue = useCallback<SetStateFn<T>>(\n (next) => {\n const resolved =\n typeof next === \"function\" ? (next as (prev: T) => T)(currentRef.current) : next;\n if (Object.is(resolved, currentRef.current)) return;\n if (!isControlled) setUncontrolled(resolved);\n onChangeRef.current?.(resolved);\n },\n [isControlled],\n );\n\n return [current, setValue];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAAsC;AACtC,4BAAoC;AAOpC,IAAM,cAAU,2CAAoB;AAAA,EAClC,QAAQ;AAAA,IACN,OAAO;AAAA,MACL,MAAM,CAAC,MAAM,MAAM,MAAM,MAAM,MAAM,MAAM,IAAI;AAAA,MAC/C,eAAe,CAAC,WAAW,UAAU,MAAM;AAAA,MAC3C,UAAU,CAAC,SAAS;AAAA,MACpB,QAAQ,CAAC,QAAQ,SAAS,SAAS,QAAQ,OAAO;AAAA,MAClD,MAAM,CAAC,SAAS,OAAO;AAAA,MACvB,QAAQ,CAAC,SAAS,OAAO,MAAM;AAAA,MAC/B,SAAS,CAAC,MAAM;AAAA,IAClB;AAAA,EACF;AACF,CAAC;AAGM,SAAS,MAAM,QAA8B;AAClD,SAAO,YAAQ,kBAAK,MAAM,CAAC;AAC7B;;;ACzBA,mBAAqC;AAErC,IAAM,QAAQ;AAEd,SAAS,UAAU,UAAkC;AACnD,QAAM,QAAQ,OAAO,WAAW,KAAK;AACrC,QAAM,iBAAiB,UAAU,QAAQ;AACzC,SAAO,MAAM,MAAM,oBAAoB,UAAU,QAAQ;AAC3D;AAEA,IAAM,cAAc,MAAe,OAAO,WAAW,KAAK,EAAE;AAC5D,IAAM,oBAAoB,MAAe;AAGlC,SAAS,mBAA4B;AAC1C,aAAO,mCAAqB,WAAW,aAAa,iBAAiB;AACvE;;;AChBA,IAAAA,gBAAyD;AAiBlD,SAAS,qBAAwB;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AACF,GAAsD;AACpD,QAAM,CAAC,cAAc,eAAe,QAAI,wBAAY,YAAY;AAChE,QAAM,eAAe,UAAU;AAC/B,QAAM,UAAU,eAAe,QAAQ;AAEvC,QAAM,kBAAc,sBAAO,QAAQ;AACnC,+BAAU,MAAM;AACd,gBAAY,UAAU;AAAA,EACxB,CAAC;AAED,QAAM,iBAAa,sBAAO,OAAO;AACjC,aAAW,UAAU;AAErB,QAAM,eAAW;AAAA,IACf,CAAC,SAAS;AACR,YAAM,WACJ,OAAO,SAAS,aAAc,KAAwB,WAAW,OAAO,IAAI;AAC9E,UAAI,OAAO,GAAG,UAAU,WAAW,OAAO,EAAG;AAC7C,UAAI,CAAC,aAAc,iBAAgB,QAAQ;AAC3C,kBAAY,UAAU,QAAQ;AAAA,IAChC;AAAA,IACA,CAAC,YAAY;AAAA,EACf;AAEA,SAAO,CAAC,SAAS,QAAQ;AAC3B;","names":["import_react"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ClassValue } from 'clsx';
|
|
2
|
+
export { ClassValue } from 'clsx';
|
|
3
|
+
|
|
4
|
+
/** Merges class names, resolving conflicting Tailwind utilities (last one wins). */
|
|
5
|
+
declare function cn(...inputs: ClassValue[]): string;
|
|
6
|
+
|
|
7
|
+
/** `true` when the user asked the system for less motion. Always `false` on the server. */
|
|
8
|
+
declare function useReducedMotion(): boolean;
|
|
9
|
+
|
|
10
|
+
interface UseControllableStateParams<T> {
|
|
11
|
+
/** Controlled value. When defined, the component is controlled. */
|
|
12
|
+
value?: T | undefined;
|
|
13
|
+
/** Initial value when uncontrolled. */
|
|
14
|
+
defaultValue: T;
|
|
15
|
+
/** Called with the next value, in both controlled and uncontrolled modes. */
|
|
16
|
+
onChange?: ((value: T) => void) | undefined;
|
|
17
|
+
}
|
|
18
|
+
type SetStateFn<T> = (next: T | ((prev: T) => T)) => void;
|
|
19
|
+
/**
|
|
20
|
+
* State that can be either controlled (`value` + `onChange`) or uncontrolled
|
|
21
|
+
* (`defaultValue`), following the Radix / shadcn convention.
|
|
22
|
+
*/
|
|
23
|
+
declare function useControllableState<T>({ value, defaultValue, onChange, }: UseControllableStateParams<T>): [T, SetStateFn<T>];
|
|
24
|
+
|
|
25
|
+
export { type UseControllableStateParams, cn, useControllableState, useReducedMotion };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ClassValue } from 'clsx';
|
|
2
|
+
export { ClassValue } from 'clsx';
|
|
3
|
+
|
|
4
|
+
/** Merges class names, resolving conflicting Tailwind utilities (last one wins). */
|
|
5
|
+
declare function cn(...inputs: ClassValue[]): string;
|
|
6
|
+
|
|
7
|
+
/** `true` when the user asked the system for less motion. Always `false` on the server. */
|
|
8
|
+
declare function useReducedMotion(): boolean;
|
|
9
|
+
|
|
10
|
+
interface UseControllableStateParams<T> {
|
|
11
|
+
/** Controlled value. When defined, the component is controlled. */
|
|
12
|
+
value?: T | undefined;
|
|
13
|
+
/** Initial value when uncontrolled. */
|
|
14
|
+
defaultValue: T;
|
|
15
|
+
/** Called with the next value, in both controlled and uncontrolled modes. */
|
|
16
|
+
onChange?: ((value: T) => void) | undefined;
|
|
17
|
+
}
|
|
18
|
+
type SetStateFn<T> = (next: T | ((prev: T) => T)) => void;
|
|
19
|
+
/**
|
|
20
|
+
* State that can be either controlled (`value` + `onChange`) or uncontrolled
|
|
21
|
+
* (`defaultValue`), following the Radix / shadcn convention.
|
|
22
|
+
*/
|
|
23
|
+
declare function useControllableState<T>({ value, defaultValue, onChange, }: UseControllableStateParams<T>): [T, SetStateFn<T>];
|
|
24
|
+
|
|
25
|
+
export { type UseControllableStateParams, cn, useControllableState, useReducedMotion };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/cn.ts
|
|
4
|
+
import { clsx } from "clsx";
|
|
5
|
+
import { extendTailwindMerge } from "tailwind-merge";
|
|
6
|
+
var twMerge = extendTailwindMerge({
|
|
7
|
+
extend: {
|
|
8
|
+
theme: {
|
|
9
|
+
text: ["xs", "sm", "md", "lg", "h1", "h2", "h3"],
|
|
10
|
+
"font-weight": ["regular", "medium", "bold"],
|
|
11
|
+
tracking: ["display"],
|
|
12
|
+
radius: ["pill", "field", "check", "card", "panel"],
|
|
13
|
+
blur: ["glass", "small"],
|
|
14
|
+
shadow: ["glass", "rim", "holo"],
|
|
15
|
+
animate: ["holo"]
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
function cn(...inputs) {
|
|
20
|
+
return twMerge(clsx(inputs));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/use-reduced-motion.ts
|
|
24
|
+
import { useSyncExternalStore } from "react";
|
|
25
|
+
var QUERY = "(prefers-reduced-motion: reduce)";
|
|
26
|
+
function subscribe(onChange) {
|
|
27
|
+
const media = window.matchMedia(QUERY);
|
|
28
|
+
media.addEventListener("change", onChange);
|
|
29
|
+
return () => media.removeEventListener("change", onChange);
|
|
30
|
+
}
|
|
31
|
+
var getSnapshot = () => window.matchMedia(QUERY).matches;
|
|
32
|
+
var getServerSnapshot = () => false;
|
|
33
|
+
function useReducedMotion() {
|
|
34
|
+
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/use-controllable-state.ts
|
|
38
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
39
|
+
function useControllableState({
|
|
40
|
+
value,
|
|
41
|
+
defaultValue,
|
|
42
|
+
onChange
|
|
43
|
+
}) {
|
|
44
|
+
const [uncontrolled, setUncontrolled] = useState(defaultValue);
|
|
45
|
+
const isControlled = value !== void 0;
|
|
46
|
+
const current = isControlled ? value : uncontrolled;
|
|
47
|
+
const onChangeRef = useRef(onChange);
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
onChangeRef.current = onChange;
|
|
50
|
+
});
|
|
51
|
+
const currentRef = useRef(current);
|
|
52
|
+
currentRef.current = current;
|
|
53
|
+
const setValue = useCallback(
|
|
54
|
+
(next) => {
|
|
55
|
+
const resolved = typeof next === "function" ? next(currentRef.current) : next;
|
|
56
|
+
if (Object.is(resolved, currentRef.current)) return;
|
|
57
|
+
if (!isControlled) setUncontrolled(resolved);
|
|
58
|
+
onChangeRef.current?.(resolved);
|
|
59
|
+
},
|
|
60
|
+
[isControlled]
|
|
61
|
+
);
|
|
62
|
+
return [current, setValue];
|
|
63
|
+
}
|
|
64
|
+
export {
|
|
65
|
+
cn,
|
|
66
|
+
useControllableState,
|
|
67
|
+
useReducedMotion
|
|
68
|
+
};
|
|
69
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/cn.ts","../src/use-reduced-motion.ts","../src/use-controllable-state.ts"],"sourcesContent":["import { clsx, type ClassValue } from \"clsx\";\nimport { extendTailwindMerge } from \"tailwind-merge\";\n\n/**\n * tailwind-merge only knows Tailwind's default scale. The custom theme keys\n * declared in `@irisui/tokens/tailwind.css` are registered here so that, for\n * instance, `text-h3` is seen as a font size and not as a text color.\n */\nconst twMerge = extendTailwindMerge({\n extend: {\n theme: {\n text: [\"xs\", \"sm\", \"md\", \"lg\", \"h1\", \"h2\", \"h3\"],\n \"font-weight\": [\"regular\", \"medium\", \"bold\"],\n tracking: [\"display\"],\n radius: [\"pill\", \"field\", \"check\", \"card\", \"panel\"],\n blur: [\"glass\", \"small\"],\n shadow: [\"glass\", \"rim\", \"holo\"],\n animate: [\"holo\"],\n },\n },\n});\n\n/** Merges class names, resolving conflicting Tailwind utilities (last one wins). */\nexport function cn(...inputs: ClassValue[]): string {\n return twMerge(clsx(inputs));\n}\n\nexport type { ClassValue };\n","import { useSyncExternalStore } from \"react\";\n\nconst QUERY = \"(prefers-reduced-motion: reduce)\";\n\nfunction subscribe(onChange: () => void): () => void {\n const media = window.matchMedia(QUERY);\n media.addEventListener(\"change\", onChange);\n return () => media.removeEventListener(\"change\", onChange);\n}\n\nconst getSnapshot = (): boolean => window.matchMedia(QUERY).matches;\nconst getServerSnapshot = (): boolean => false;\n\n/** `true` when the user asked the system for less motion. Always `false` on the server. */\nexport function useReducedMotion(): boolean {\n return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n}\n","import { useCallback, useEffect, useRef, useState } from \"react\";\n\nexport interface UseControllableStateParams<T> {\n /** Controlled value. When defined, the component is controlled. */\n value?: T | undefined;\n /** Initial value when uncontrolled. */\n defaultValue: T;\n /** Called with the next value, in both controlled and uncontrolled modes. */\n onChange?: ((value: T) => void) | undefined;\n}\n\ntype SetStateFn<T> = (next: T | ((prev: T) => T)) => void;\n\n/**\n * State that can be either controlled (`value` + `onChange`) or uncontrolled\n * (`defaultValue`), following the Radix / shadcn convention.\n */\nexport function useControllableState<T>({\n value,\n defaultValue,\n onChange,\n}: UseControllableStateParams<T>): [T, SetStateFn<T>] {\n const [uncontrolled, setUncontrolled] = useState<T>(defaultValue);\n const isControlled = value !== undefined;\n const current = isControlled ? value : uncontrolled;\n\n const onChangeRef = useRef(onChange);\n useEffect(() => {\n onChangeRef.current = onChange;\n });\n\n const currentRef = useRef(current);\n currentRef.current = current;\n\n const setValue = useCallback<SetStateFn<T>>(\n (next) => {\n const resolved =\n typeof next === \"function\" ? (next as (prev: T) => T)(currentRef.current) : next;\n if (Object.is(resolved, currentRef.current)) return;\n if (!isControlled) setUncontrolled(resolved);\n onChangeRef.current?.(resolved);\n },\n [isControlled],\n );\n\n return [current, setValue];\n}\n"],"mappings":";;;AAAA,SAAS,YAA6B;AACtC,SAAS,2BAA2B;AAOpC,IAAM,UAAU,oBAAoB;AAAA,EAClC,QAAQ;AAAA,IACN,OAAO;AAAA,MACL,MAAM,CAAC,MAAM,MAAM,MAAM,MAAM,MAAM,MAAM,IAAI;AAAA,MAC/C,eAAe,CAAC,WAAW,UAAU,MAAM;AAAA,MAC3C,UAAU,CAAC,SAAS;AAAA,MACpB,QAAQ,CAAC,QAAQ,SAAS,SAAS,QAAQ,OAAO;AAAA,MAClD,MAAM,CAAC,SAAS,OAAO;AAAA,MACvB,QAAQ,CAAC,SAAS,OAAO,MAAM;AAAA,MAC/B,SAAS,CAAC,MAAM;AAAA,IAClB;AAAA,EACF;AACF,CAAC;AAGM,SAAS,MAAM,QAA8B;AAClD,SAAO,QAAQ,KAAK,MAAM,CAAC;AAC7B;;;ACzBA,SAAS,4BAA4B;AAErC,IAAM,QAAQ;AAEd,SAAS,UAAU,UAAkC;AACnD,QAAM,QAAQ,OAAO,WAAW,KAAK;AACrC,QAAM,iBAAiB,UAAU,QAAQ;AACzC,SAAO,MAAM,MAAM,oBAAoB,UAAU,QAAQ;AAC3D;AAEA,IAAM,cAAc,MAAe,OAAO,WAAW,KAAK,EAAE;AAC5D,IAAM,oBAAoB,MAAe;AAGlC,SAAS,mBAA4B;AAC1C,SAAO,qBAAqB,WAAW,aAAa,iBAAiB;AACvE;;;AChBA,SAAS,aAAa,WAAW,QAAQ,gBAAgB;AAiBlD,SAAS,qBAAwB;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AACF,GAAsD;AACpD,QAAM,CAAC,cAAc,eAAe,IAAI,SAAY,YAAY;AAChE,QAAM,eAAe,UAAU;AAC/B,QAAM,UAAU,eAAe,QAAQ;AAEvC,QAAM,cAAc,OAAO,QAAQ;AACnC,YAAU,MAAM;AACd,gBAAY,UAAU;AAAA,EACxB,CAAC;AAED,QAAM,aAAa,OAAO,OAAO;AACjC,aAAW,UAAU;AAErB,QAAM,WAAW;AAAA,IACf,CAAC,SAAS;AACR,YAAM,WACJ,OAAO,SAAS,aAAc,KAAwB,WAAW,OAAO,IAAI;AAC9E,UAAI,OAAO,GAAG,UAAU,WAAW,OAAO,EAAG;AAC7C,UAAI,CAAC,aAAc,iBAAgB,QAAQ;AAC3C,kBAAY,UAAU,QAAQ;AAAA,IAChC;AAAA,IACA,CAAC,YAAY;AAAA,EACf;AAEA,SAAO,CAAC,SAAS,QAAQ;AAC3B;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@irisui/utils",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Shared utilities and hooks for the iris-ui library.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"main": "./dist/index.cjs",
|
|
12
|
+
"module": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
},
|
|
20
|
+
"require": {
|
|
21
|
+
"types": "./dist/index.d.cts",
|
|
22
|
+
"default": "./dist/index.cjs"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"clsx": "2.1.1",
|
|
28
|
+
"tailwind-merge": "3.7.0"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"react": "^18.2.0 || ^19.0.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/react": "19.3.0",
|
|
35
|
+
"react": "19.3.0"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "git+https://github.com/theteqlab/iris-ui.git",
|
|
43
|
+
"directory": "packages/utils"
|
|
44
|
+
},
|
|
45
|
+
"homepage": "https://github.com/theteqlab/iris-ui#readme",
|
|
46
|
+
"bugs": {
|
|
47
|
+
"url": "https://github.com/theteqlab/iris-ui/issues"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "tsup",
|
|
51
|
+
"dev": "tsup --watch",
|
|
52
|
+
"typecheck": "tsc -p tsconfig.json"
|
|
53
|
+
}
|
|
6
54
|
}
|