@journ/lift 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 +21 -0
- package/README.md +41 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/prefers-reduced-motion.d.ts +3 -0
- package/dist/prefers-reduced-motion.js +6 -0
- package/dist/required-context.d.ts +8 -0
- package/dist/required-context.js +17 -0
- package/dist/use-controllable-state.d.ts +7 -0
- package/dist/use-controllable-state.js +17 -0
- package/dist/use-debounced-value.d.ts +3 -0
- package/dist/use-debounced-value.js +14 -0
- package/dist/use-draft.d.ts +20 -0
- package/dist/use-draft.js +21 -0
- package/package.json +70 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Journ
|
|
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,41 @@
|
|
|
1
|
+
# @journ/lift
|
|
2
|
+
|
|
3
|
+
React hooks for state and timing, built on fitter-happier. No DOM access, so they work in PWAs, React Native and Wails desktop apps alike.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @journ/lift react
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import { useControllableState, useDebouncedValue, useDraft } from '@journ/lift'
|
|
15
|
+
|
|
16
|
+
function Toggle(props: { pressed?: boolean; defaultPressed?: boolean; onPressedChange?: (pressed: boolean) => void }) {
|
|
17
|
+
const [pressed, setPressed] = useControllableState(
|
|
18
|
+
props.pressed,
|
|
19
|
+
props.defaultPressed ?? false,
|
|
20
|
+
props.onPressedChange,
|
|
21
|
+
)
|
|
22
|
+
return (
|
|
23
|
+
<button type='button' aria-pressed={pressed ?? false} onClick={() => setPressed(!pressed)}>
|
|
24
|
+
Toggle
|
|
25
|
+
</button>
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function Search({ query }: { query: string }) {
|
|
30
|
+
const debouncedQuery = useDebouncedValue(query, 300)
|
|
31
|
+
return <p>{debouncedQuery}</p>
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
| Entry | Exports |
|
|
36
|
+
| ------------------------------------ | ---------------------------------------------------------------------- |
|
|
37
|
+
| `@journ/lift/use-controllable-state` | `useControllableState(value, defaultValue, onChange)` |
|
|
38
|
+
| `@journ/lift/use-debounced-value` | `useDebouncedValue(value, delay)` |
|
|
39
|
+
| `@journ/lift/use-draft` | `useDraft`: text typed freely that becomes a value on commit |
|
|
40
|
+
| `@journ/lift/required-context` | `createRequiredContext(owner)`: a context whose hook throws outside it |
|
|
41
|
+
| `@journ/lift/prefers-reduced-motion` | `prefersReducedMotion()` |
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { prefersReducedMotion } from "./prefers-reduced-motion.js";
|
|
2
|
+
import { createRequiredContext } from "./required-context.js";
|
|
3
|
+
import { useControllableState } from "./use-controllable-state.js";
|
|
4
|
+
import { useDebouncedValue } from "./use-debounced-value.js";
|
|
5
|
+
import { Draft, UseDraftOptions, useDraft } from "./use-draft.js";
|
|
6
|
+
export { type Draft, type UseDraftOptions, createRequiredContext, prefersReducedMotion, useControllableState, useDebouncedValue, useDraft };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { prefersReducedMotion } from "./prefers-reduced-motion.js";
|
|
2
|
+
import { createRequiredContext } from "./required-context.js";
|
|
3
|
+
import { useControllableState } from "./use-controllable-state.js";
|
|
4
|
+
import { useDebouncedValue } from "./use-debounced-value.js";
|
|
5
|
+
import { useDraft } from "./use-draft.js";
|
|
6
|
+
export { createRequiredContext, prefersReducedMotion, useControllableState, useDebouncedValue, useDraft };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Context } from "react";
|
|
2
|
+
//#region src/required-context.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* A context that parts must be rendered inside of. The hook throws a clear error
|
|
5
|
+
* naming the owner, so a part used on its own fails loudly instead of rendering wrong.
|
|
6
|
+
*/
|
|
7
|
+
export declare function createRequiredContext<T>(owner: string): readonly [Context<T | null>, () => T];
|
|
8
|
+
//#endregion
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createContext, use } from "react";
|
|
2
|
+
//#region src/required-context.ts
|
|
3
|
+
/**
|
|
4
|
+
* A context that parts must be rendered inside of. The hook throws a clear error
|
|
5
|
+
* naming the owner, so a part used on its own fails loudly instead of rendering wrong.
|
|
6
|
+
*/
|
|
7
|
+
function createRequiredContext(owner) {
|
|
8
|
+
const RequiredContext = createContext(null);
|
|
9
|
+
const useRequired = () => {
|
|
10
|
+
const value = use(RequiredContext);
|
|
11
|
+
if (value === null) throw new Error(`Must be rendered inside ${owner}`);
|
|
12
|
+
return value;
|
|
13
|
+
};
|
|
14
|
+
return [RequiredContext, useRequired];
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
export { createRequiredContext };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
//#region src/use-controllable-state.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* State that the caller may own. `value` undefined means uncontrolled: the hook
|
|
4
|
+
* keeps the state and still reports every change through `onChange`.
|
|
5
|
+
*/
|
|
6
|
+
export declare function useControllableState<T>(value: T | null | undefined, defaultValue: T | null, onChange?: (value: T) => void): readonly [T | null, (next: T) => void];
|
|
7
|
+
//#endregion
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useCallback, useState } from "react";
|
|
2
|
+
//#region src/use-controllable-state.ts
|
|
3
|
+
/**
|
|
4
|
+
* State that the caller may own. `value` undefined means uncontrolled: the hook
|
|
5
|
+
* keeps the state and still reports every change through `onChange`.
|
|
6
|
+
*/
|
|
7
|
+
function useControllableState(value, defaultValue, onChange) {
|
|
8
|
+
const [uncontrolled, setUncontrolled] = useState(defaultValue);
|
|
9
|
+
const controlled = value !== void 0;
|
|
10
|
+
const setValue = useCallback((next) => {
|
|
11
|
+
if (!controlled) setUncontrolled(next);
|
|
12
|
+
onChange?.(next);
|
|
13
|
+
}, [controlled, onChange]);
|
|
14
|
+
return [controlled ? value : uncontrolled, setValue];
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
export { useControllableState };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
import { debounce } from "@journ/fitter-happier";
|
|
3
|
+
//#region src/use-debounced-value.ts
|
|
4
|
+
function useDebouncedValue(value, delay) {
|
|
5
|
+
const [debounced, setDebounced] = useState(value);
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
const update = debounce(setDebounced, delay);
|
|
8
|
+
update(value);
|
|
9
|
+
return update.cancel;
|
|
10
|
+
}, [value, delay]);
|
|
11
|
+
return debounced;
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
export { useDebouncedValue };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/use-draft.d.ts
|
|
2
|
+
export interface UseDraftOptions<T> {
|
|
3
|
+
value: T;
|
|
4
|
+
format: (value: T) => string;
|
|
5
|
+
/** Return undefined to reject the text, which keeps the old value. */
|
|
6
|
+
parse: (text: string) => T | undefined;
|
|
7
|
+
onCommit: (value: T) => void;
|
|
8
|
+
}
|
|
9
|
+
export interface Draft {
|
|
10
|
+
text: string;
|
|
11
|
+
edit: (text: string) => void;
|
|
12
|
+
discard: () => void;
|
|
13
|
+
commit: () => void;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Text that is typed freely and only turns into a value on commit. While not
|
|
17
|
+
* editing it shows `format(value)`.
|
|
18
|
+
*/
|
|
19
|
+
export declare function useDraft<T>({ value, format, parse, onCommit }: UseDraftOptions<T>): Draft;
|
|
20
|
+
//#endregion
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
//#region src/use-draft.ts
|
|
3
|
+
/**
|
|
4
|
+
* Text that is typed freely and only turns into a value on commit. While not
|
|
5
|
+
* editing it shows `format(value)`.
|
|
6
|
+
*/
|
|
7
|
+
function useDraft({ value, format, parse, onCommit }) {
|
|
8
|
+
const [draft, setDraft] = useState(null);
|
|
9
|
+
return {
|
|
10
|
+
text: draft ?? format(value),
|
|
11
|
+
edit: setDraft,
|
|
12
|
+
discard: () => setDraft(null),
|
|
13
|
+
commit: () => {
|
|
14
|
+
const parsed = parse(draft ?? "");
|
|
15
|
+
if (parsed !== void 0) onCommit(parsed);
|
|
16
|
+
setDraft(null);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
export { useDraft };
|
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@journ/lift",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React hooks for state and timing, built on fitter-happier. No DOM access, so they work in PWAs, React Native and Wails desktop apps alike.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/thommorais/kid-a.git",
|
|
9
|
+
"directory": "packages/lift"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"type": "module",
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"@journ/source": "./src/index.ts",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"default": "./dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./prefers-reduced-motion": {
|
|
23
|
+
"@journ/source": "./src/prefers-reduced-motion.ts",
|
|
24
|
+
"types": "./dist/prefers-reduced-motion.d.ts",
|
|
25
|
+
"default": "./dist/prefers-reduced-motion.js"
|
|
26
|
+
},
|
|
27
|
+
"./required-context": {
|
|
28
|
+
"@journ/source": "./src/required-context.ts",
|
|
29
|
+
"types": "./dist/required-context.d.ts",
|
|
30
|
+
"default": "./dist/required-context.js"
|
|
31
|
+
},
|
|
32
|
+
"./use-controllable-state": {
|
|
33
|
+
"@journ/source": "./src/use-controllable-state.ts",
|
|
34
|
+
"types": "./dist/use-controllable-state.d.ts",
|
|
35
|
+
"default": "./dist/use-controllable-state.js"
|
|
36
|
+
},
|
|
37
|
+
"./use-debounced-value": {
|
|
38
|
+
"@journ/source": "./src/use-debounced-value.ts",
|
|
39
|
+
"types": "./dist/use-debounced-value.d.ts",
|
|
40
|
+
"default": "./dist/use-debounced-value.js"
|
|
41
|
+
},
|
|
42
|
+
"./use-draft": {
|
|
43
|
+
"@journ/source": "./src/use-draft.ts",
|
|
44
|
+
"types": "./dist/use-draft.d.ts",
|
|
45
|
+
"default": "./dist/use-draft.js"
|
|
46
|
+
},
|
|
47
|
+
"./package.json": "./package.json"
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public"
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@journ/fitter-happier": "0.1.0"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"react": "^19.0.0"
|
|
57
|
+
},
|
|
58
|
+
"nx": {
|
|
59
|
+
"tags": [
|
|
60
|
+
"scope:hooks"
|
|
61
|
+
]
|
|
62
|
+
},
|
|
63
|
+
"scripts": {
|
|
64
|
+
"build": "tsdown",
|
|
65
|
+
"test": "vitest run",
|
|
66
|
+
"typecheck": "tsc -b",
|
|
67
|
+
"lint": "oxlint",
|
|
68
|
+
"format:check": "oxfmt --check"
|
|
69
|
+
}
|
|
70
|
+
}
|