usethishook 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 +186 -0
- package/dist/index.cjs +857 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +252 -0
- package/dist/index.d.ts +252 -0
- package/dist/index.js +824 -0
- package/dist/index.js.map +1 -0
- package/package.json +72 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Senthil Kumar Thangavel
|
|
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,186 @@
|
|
|
1
|
+
# useThisHook
|
|
2
|
+
|
|
3
|
+
**useThisHook** is an open-source TypeScript library of named React hooks for everyday UI, forms, lists, overlays, and browser APIs. Import only what you need — the package is tree-shakeable.
|
|
4
|
+
|
|
5
|
+
The npm package name is lowercase: [`usethishook`](https://www.npmjs.com/package/usethishook).
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install usethishook
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
**Peer dependency:** React 18 or later (React 19 is supported).
|
|
12
|
+
|
|
13
|
+
## Why use it
|
|
14
|
+
|
|
15
|
+
- Drop the same hooks into Vite, Next.js, CRA, or Module Federation hosts.
|
|
16
|
+
- Named exports and generated `.d.ts` types — no default export.
|
|
17
|
+
- Promise-based overlays, wizards, confirm, and file pick instead of ad-hoc `useEffect` state machines.
|
|
18
|
+
- A playground with a live preview, plain-English description, API reference, and copy-paste example for every hook.
|
|
19
|
+
|
|
20
|
+
## Documentation playground
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install
|
|
24
|
+
npm run playground
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Open the URL Vite prints (usually `http://localhost:5173`).
|
|
28
|
+
|
|
29
|
+
- Home (`#/`): what the library is, install, and the hook index
|
|
30
|
+
- Hook pages (`#/useToggle`, `#/useConfirm`, …): description, live preview, API, example
|
|
31
|
+
|
|
32
|
+
See [`playground/README.md`](playground/README.md). Build a static site with `npm run playground:build` (output in `playground/dist`).
|
|
33
|
+
|
|
34
|
+
## Usage
|
|
35
|
+
|
|
36
|
+
```tsx
|
|
37
|
+
import { useToggle, useDebounce, useLocalStorage } from 'usethishook';
|
|
38
|
+
|
|
39
|
+
export const SearchBox = () => {
|
|
40
|
+
const { value: isOpen, toggle } = useToggle();
|
|
41
|
+
const [query, setQuery] = useLocalStorage('search', '');
|
|
42
|
+
const debouncedQuery = useDebounce(query, 300);
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<div>
|
|
46
|
+
<button type="button" onClick={toggle}>
|
|
47
|
+
{isOpen ? 'Hide' : 'Show'} search
|
|
48
|
+
</button>
|
|
49
|
+
{isOpen && (
|
|
50
|
+
<input value={query} onChange={(event) => setQuery(event.target.value)} />
|
|
51
|
+
)}
|
|
52
|
+
<p>Searching for: {debouncedQuery}</p>
|
|
53
|
+
</div>
|
|
54
|
+
);
|
|
55
|
+
};
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Confirm and overlay hooks return a `render()` function. Place it in the tree once:
|
|
59
|
+
|
|
60
|
+
```tsx
|
|
61
|
+
import { useConfirm } from 'usethishook';
|
|
62
|
+
|
|
63
|
+
export const DeleteButton = () => {
|
|
64
|
+
const { confirm, render } = useConfirm();
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<>
|
|
68
|
+
<button
|
|
69
|
+
type="button"
|
|
70
|
+
onClick={async () => {
|
|
71
|
+
if (await confirm({ title: 'Delete this row?', danger: true })) {
|
|
72
|
+
// delete
|
|
73
|
+
}
|
|
74
|
+
}}
|
|
75
|
+
>
|
|
76
|
+
Delete
|
|
77
|
+
</button>
|
|
78
|
+
{render()}
|
|
79
|
+
</>
|
|
80
|
+
);
|
|
81
|
+
};
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Hooks
|
|
85
|
+
|
|
86
|
+
Playground paths are hash routes on the local docs app (for example `#/useToggle`).
|
|
87
|
+
|
|
88
|
+
### State
|
|
89
|
+
|
|
90
|
+
| Hook | Purpose | Preview |
|
|
91
|
+
| --- | --- | --- |
|
|
92
|
+
| `useToggle` | Boolean with `toggle`, `setTrue`, `setFalse` | `#/useToggle` |
|
|
93
|
+
| `useCounter` | Increment, decrement, reset | `#/useCounter` |
|
|
94
|
+
| `useDisclosure` | Open / close / toggle for menus and dialogs | `#/useDisclosure` |
|
|
95
|
+
| `useDebounce` | Debounce a rapidly changing value | `#/useDebounce` |
|
|
96
|
+
| `usePrevious` | Previous render’s value | `#/usePrevious` |
|
|
97
|
+
| `useInterval` | Declarative `setInterval` (`null` pauses) | `#/useInterval` |
|
|
98
|
+
| `useCopyToClipboard` | Clipboard write + last copied text | `#/useCopyToClipboard` |
|
|
99
|
+
| `useLocalStorage` | JSON state persisted in `localStorage` | `#/useLocalStorage` |
|
|
100
|
+
| `useDocumentTitle` | Set `document.title` while mounted | `#/useDocumentTitle` |
|
|
101
|
+
|
|
102
|
+
### Browser
|
|
103
|
+
|
|
104
|
+
| Hook | Purpose | Preview |
|
|
105
|
+
| --- | --- | --- |
|
|
106
|
+
| `useOnlineStatus` | `navigator.onLine` plus online/offline events | `#/useOnlineStatus` |
|
|
107
|
+
| `useMediaQuery` | Subscribe to a CSS media query | `#/useMediaQuery` |
|
|
108
|
+
| `useWindowSize` | Viewport width and height | `#/useWindowSize` |
|
|
109
|
+
| `useOnClickOutside` | Handler when the user presses outside a ref | `#/useOnClickOutside` |
|
|
110
|
+
| `useOverlay` | Promise-based custom overlay | `#/useOverlay` |
|
|
111
|
+
| `useStepFlow` | Multi-step wizard that resolves when finished | `#/useStepFlow` |
|
|
112
|
+
| `useAsyncSelect` | Native file picker as a Promise | `#/useAsyncSelect` |
|
|
113
|
+
|
|
114
|
+
### App
|
|
115
|
+
|
|
116
|
+
| Hook | Purpose | Preview |
|
|
117
|
+
| --- | --- | --- |
|
|
118
|
+
| `useStableCallback` | Stable function identity, always-latest body | `#/useStableCallback` |
|
|
119
|
+
| `useOnChange` | Callback when a value changes, not on mount | `#/useOnChange` |
|
|
120
|
+
| `useResetState` | Local state that resets when a source key changes | `#/useResetState` |
|
|
121
|
+
| `useAsyncAction` | Pending / error / data around one async action | `#/useAsyncAction` |
|
|
122
|
+
| `useDebouncedCallback` | Debounce calling a function | `#/useDebouncedCallback` |
|
|
123
|
+
| `useFields` | Small form object, optional Zod-shaped schema | `#/useFields` |
|
|
124
|
+
| `useList` | Insert, update, remove, reorder by `id` | `#/useList` |
|
|
125
|
+
| `useSelection` | Single or multi select ids | `#/useSelection` |
|
|
126
|
+
| `useSearchState` | URL search params as React state | `#/useSearchState` |
|
|
127
|
+
| `useConfirm` | Await a yes/no dialog | `#/useConfirm` |
|
|
128
|
+
| `usePrompt` | Await a string from a dialog | `#/usePrompt` |
|
|
129
|
+
| `useControllableState` | Controlled and uncontrolled in one setter | `#/useControllableState` |
|
|
130
|
+
| `useUnsavedChanges` | Tab-close warning and in-app leave confirm | `#/useUnsavedChanges` |
|
|
131
|
+
| `useElementSize` | Element size via `ResizeObserver` | `#/useElementSize` |
|
|
132
|
+
| `useInView` | Element vs viewport via `IntersectionObserver` | `#/useInView` |
|
|
133
|
+
| `usePagination` | Page, offset, next/prev with clamping | `#/usePagination` |
|
|
134
|
+
|
|
135
|
+
## Scripts
|
|
136
|
+
|
|
137
|
+
| Command | What it does |
|
|
138
|
+
| --- | --- |
|
|
139
|
+
| `npm test` | Vitest (jsdom) |
|
|
140
|
+
| `npm run typecheck` | `tsc --noEmit` |
|
|
141
|
+
| `npm run build` | ESM + CJS + types via tsup (`dist/`) |
|
|
142
|
+
| `npm run playground` | Docs app |
|
|
143
|
+
| `npm run playground:build` | Static playground |
|
|
144
|
+
|
|
145
|
+
`prepublishOnly` runs typecheck, tests, and build.
|
|
146
|
+
|
|
147
|
+
## Local development
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
npm install
|
|
151
|
+
npm test
|
|
152
|
+
npm run build
|
|
153
|
+
npm run playground
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Guidelines for adding a hook: [`CONTRIBUTING.md`](CONTRIBUTING.md).
|
|
157
|
+
|
|
158
|
+
## Publish to npm
|
|
159
|
+
|
|
160
|
+
1. Push the public GitHub repository.
|
|
161
|
+
2. `npm login`
|
|
162
|
+
3. Confirm the name is free: `npm view usethishook`
|
|
163
|
+
4. `npm publish --access public`
|
|
164
|
+
|
|
165
|
+
Later releases: `npm version patch|minor|major`, then publish again.
|
|
166
|
+
|
|
167
|
+
## License
|
|
168
|
+
|
|
169
|
+
**useThisHook** is released under the [MIT License](LICENSE).
|
|
170
|
+
|
|
171
|
+
Copyright © 2026 **Senthil Kumar Thangavel**.
|
|
172
|
+
|
|
173
|
+
You may use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the software, provided the copyright notice and permission notice appear in all copies or substantial portions of the software.
|
|
174
|
+
|
|
175
|
+
The software is provided **“as is”**, without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages, or other liability arising from use of the software.
|
|
176
|
+
|
|
177
|
+
The full legal text is in [`LICENSE`](LICENSE).
|
|
178
|
+
|
|
179
|
+
## Author
|
|
180
|
+
|
|
181
|
+
**Senthil Kumar Thangavel**
|
|
182
|
+
|
|
183
|
+
- Website: [senthilkumar.mentorbridge.in](https://senthilkumar.mentorbridge.in)
|
|
184
|
+
- LinkedIn: [linkedin.com/in/senthilk979](https://www.linkedin.com/in/senthilk979)
|
|
185
|
+
|
|
186
|
+
Issues and ideas: [GitHub issues](https://github.com/senthilkumar979/usethishook/issues).
|