@usefy/use-toggle 0.0.16 → 0.0.18
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/README.md +48 -39
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -80,7 +80,7 @@ This package requires React 18 or 19:
|
|
|
80
80
|
## Quick Start
|
|
81
81
|
|
|
82
82
|
```tsx
|
|
83
|
-
import { useToggle } from
|
|
83
|
+
import { useToggle } from "@usefy/use-toggle";
|
|
84
84
|
|
|
85
85
|
function Modal() {
|
|
86
86
|
const { value: isOpen, toggle, setTrue, setFalse } = useToggle(false);
|
|
@@ -109,18 +109,18 @@ A hook that manages boolean state with toggle, setTrue, setFalse, and setValue f
|
|
|
109
109
|
|
|
110
110
|
#### Parameters
|
|
111
111
|
|
|
112
|
-
| Parameter
|
|
113
|
-
|
|
112
|
+
| Parameter | Type | Default | Description |
|
|
113
|
+
| -------------- | --------- | ------- | ------------------------- |
|
|
114
114
|
| `initialValue` | `boolean` | `false` | The initial boolean value |
|
|
115
115
|
|
|
116
116
|
#### Returns `UseToggleReturn`
|
|
117
117
|
|
|
118
|
-
| Property
|
|
119
|
-
|
|
120
|
-
| `value`
|
|
121
|
-
| `toggle`
|
|
122
|
-
| `setTrue`
|
|
123
|
-
| `setFalse` | `() => void`
|
|
118
|
+
| Property | Type | Description |
|
|
119
|
+
| ---------- | -------------------------- | ------------------------------------ |
|
|
120
|
+
| `value` | `boolean` | The current boolean state |
|
|
121
|
+
| `toggle` | `() => void` | Toggles the value (true ↔ false) |
|
|
122
|
+
| `setTrue` | `() => void` | Sets the value to `true` |
|
|
123
|
+
| `setFalse` | `() => void` | Sets the value to `false` |
|
|
124
124
|
| `setValue` | `(value: boolean) => void` | Sets the value to a specific boolean |
|
|
125
125
|
|
|
126
126
|
---
|
|
@@ -130,7 +130,7 @@ A hook that manages boolean state with toggle, setTrue, setFalse, and setValue f
|
|
|
130
130
|
### Modal/Dialog
|
|
131
131
|
|
|
132
132
|
```tsx
|
|
133
|
-
import { useToggle } from
|
|
133
|
+
import { useToggle } from "@usefy/use-toggle";
|
|
134
134
|
|
|
135
135
|
function ConfirmDialog() {
|
|
136
136
|
const { value: isOpen, setTrue: open, setFalse: close } = useToggle(false);
|
|
@@ -145,7 +145,12 @@ function ConfirmDialog() {
|
|
|
145
145
|
<p>Are you sure you want to delete this item?</p>
|
|
146
146
|
<div className="dialog-actions">
|
|
147
147
|
<button onClick={close}>Cancel</button>
|
|
148
|
-
<button
|
|
148
|
+
<button
|
|
149
|
+
onClick={() => {
|
|
150
|
+
deleteItem();
|
|
151
|
+
close();
|
|
152
|
+
}}
|
|
153
|
+
>
|
|
149
154
|
Delete
|
|
150
155
|
</button>
|
|
151
156
|
</div>
|
|
@@ -160,7 +165,7 @@ function ConfirmDialog() {
|
|
|
160
165
|
### Accordion
|
|
161
166
|
|
|
162
167
|
```tsx
|
|
163
|
-
import { useToggle } from
|
|
168
|
+
import { useToggle } from "@usefy/use-toggle";
|
|
164
169
|
|
|
165
170
|
function AccordionItem({ title, content }: AccordionItemProps) {
|
|
166
171
|
const { value: isExpanded, toggle } = useToggle(false);
|
|
@@ -173,11 +178,9 @@ function AccordionItem({ title, content }: AccordionItemProps) {
|
|
|
173
178
|
aria-expanded={isExpanded}
|
|
174
179
|
>
|
|
175
180
|
{title}
|
|
176
|
-
<span className={`icon ${isExpanded ?
|
|
181
|
+
<span className={`icon ${isExpanded ? "rotate" : ""}`}>▼</span>
|
|
177
182
|
</button>
|
|
178
|
-
{isExpanded &&
|
|
179
|
-
<div className="accordion-content">{content}</div>
|
|
180
|
-
)}
|
|
183
|
+
{isExpanded && <div className="accordion-content">{content}</div>}
|
|
181
184
|
</div>
|
|
182
185
|
);
|
|
183
186
|
}
|
|
@@ -186,23 +189,23 @@ function AccordionItem({ title, content }: AccordionItemProps) {
|
|
|
186
189
|
### Dark Mode Toggle
|
|
187
190
|
|
|
188
191
|
```tsx
|
|
189
|
-
import { useToggle } from
|
|
192
|
+
import { useToggle } from "@usefy/use-toggle";
|
|
190
193
|
|
|
191
194
|
function ThemeToggle() {
|
|
192
195
|
const { value: isDark, toggle, setValue } = useToggle(false);
|
|
193
196
|
|
|
194
197
|
// Sync with system preference
|
|
195
198
|
useEffect(() => {
|
|
196
|
-
const mediaQuery = window.matchMedia(
|
|
199
|
+
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
|
|
197
200
|
setValue(mediaQuery.matches);
|
|
198
201
|
}, [setValue]);
|
|
199
202
|
|
|
200
203
|
return (
|
|
201
204
|
<button
|
|
202
205
|
onClick={toggle}
|
|
203
|
-
aria-label={`Switch to ${isDark ?
|
|
206
|
+
aria-label={`Switch to ${isDark ? "light" : "dark"} mode`}
|
|
204
207
|
>
|
|
205
|
-
{isDark ?
|
|
208
|
+
{isDark ? "🌙" : "☀️"}
|
|
206
209
|
</button>
|
|
207
210
|
);
|
|
208
211
|
}
|
|
@@ -211,7 +214,7 @@ function ThemeToggle() {
|
|
|
211
214
|
### Dropdown Menu
|
|
212
215
|
|
|
213
216
|
```tsx
|
|
214
|
-
import { useToggle } from
|
|
217
|
+
import { useToggle } from "@usefy/use-toggle";
|
|
215
218
|
|
|
216
219
|
function Dropdown({ items }: DropdownProps) {
|
|
217
220
|
const { value: isOpen, toggle, setFalse: close } = useToggle(false);
|
|
@@ -225,7 +228,12 @@ function Dropdown({ items }: DropdownProps) {
|
|
|
225
228
|
<ul className="dropdown-menu">
|
|
226
229
|
{items.map((item) => (
|
|
227
230
|
<li key={item.id}>
|
|
228
|
-
<button
|
|
231
|
+
<button
|
|
232
|
+
onClick={() => {
|
|
233
|
+
item.onClick();
|
|
234
|
+
close();
|
|
235
|
+
}}
|
|
236
|
+
>
|
|
229
237
|
{item.label}
|
|
230
238
|
</button>
|
|
231
239
|
</li>
|
|
@@ -240,7 +248,7 @@ function Dropdown({ items }: DropdownProps) {
|
|
|
240
248
|
### Controlled from Props
|
|
241
249
|
|
|
242
250
|
```tsx
|
|
243
|
-
import { useToggle } from
|
|
251
|
+
import { useToggle } from "@usefy/use-toggle";
|
|
244
252
|
|
|
245
253
|
function ControlledSwitch({ defaultChecked, onChange }: SwitchProps) {
|
|
246
254
|
const { value, toggle } = useToggle(defaultChecked);
|
|
@@ -255,7 +263,7 @@ function ControlledSwitch({ defaultChecked, onChange }: SwitchProps) {
|
|
|
255
263
|
role="switch"
|
|
256
264
|
aria-checked={value}
|
|
257
265
|
onClick={handleToggle}
|
|
258
|
-
className={`switch ${value ?
|
|
266
|
+
className={`switch ${value ? "on" : "off"}`}
|
|
259
267
|
>
|
|
260
268
|
<span className="switch-thumb" />
|
|
261
269
|
</button>
|
|
@@ -270,10 +278,11 @@ function ControlledSwitch({ defaultChecked, onChange }: SwitchProps) {
|
|
|
270
278
|
This hook is written in TypeScript and exports the `UseToggleReturn` interface.
|
|
271
279
|
|
|
272
280
|
```tsx
|
|
273
|
-
import { useToggle, type UseToggleReturn } from
|
|
281
|
+
import { useToggle, type UseToggleReturn } from "@usefy/use-toggle";
|
|
274
282
|
|
|
275
283
|
// Return type is fully typed
|
|
276
|
-
const { value, toggle, setTrue, setFalse, setValue }: UseToggleReturn =
|
|
284
|
+
const { value, toggle, setTrue, setFalse, setValue }: UseToggleReturn =
|
|
285
|
+
useToggle(false);
|
|
277
286
|
|
|
278
287
|
// value: boolean
|
|
279
288
|
// toggle: () => void
|
|
@@ -305,12 +314,12 @@ This package maintains comprehensive test coverage to ensure reliability and sta
|
|
|
305
314
|
|
|
306
315
|
### Test Coverage
|
|
307
316
|
|
|
308
|
-
| Category
|
|
309
|
-
|
|
317
|
+
| Category | Coverage |
|
|
318
|
+
| ---------- | ------------ |
|
|
310
319
|
| Statements | 100% (11/11) |
|
|
311
|
-
| Branches
|
|
312
|
-
| Functions
|
|
313
|
-
| Lines
|
|
320
|
+
| Branches | 100% (1/1) |
|
|
321
|
+
| Functions | 100% (6/6) |
|
|
322
|
+
| Lines | 100% (10/10) |
|
|
314
323
|
|
|
315
324
|
### Test Categories
|
|
316
325
|
|
|
@@ -373,14 +382,14 @@ pnpm test --coverage
|
|
|
373
382
|
|
|
374
383
|
Explore other hooks in the **@usefy** collection:
|
|
375
384
|
|
|
376
|
-
| Package
|
|
377
|
-
|
|
378
|
-
| [@usefy/use-counter](https://www.npmjs.com/package/@usefy/use-counter)
|
|
379
|
-
| [@usefy/use-debounce](https://www.npmjs.com/package/@usefy/use-debounce)
|
|
380
|
-
| [@usefy/use-debounce-callback](https://www.npmjs.com/package/@usefy/use-debounce-callback) | Debounced callbacks
|
|
381
|
-
| [@usefy/use-throttle](https://www.npmjs.com/package/@usefy/use-throttle)
|
|
382
|
-
| [@usefy/use-throttle-callback](https://www.npmjs.com/package/@usefy/use-throttle-callback) | Throttled callbacks
|
|
383
|
-
| [@usefy/use-click-any-where](https://www.npmjs.com/package/@usefy/use-click-any-where)
|
|
385
|
+
| Package | Description |
|
|
386
|
+
| ------------------------------------------------------------------------------------------ | ------------------------ |
|
|
387
|
+
| [@usefy/use-counter](https://www.npmjs.com/package/@usefy/use-counter) | Counter state management |
|
|
388
|
+
| [@usefy/use-debounce](https://www.npmjs.com/package/@usefy/use-debounce) | Value debouncing |
|
|
389
|
+
| [@usefy/use-debounce-callback](https://www.npmjs.com/package/@usefy/use-debounce-callback) | Debounced callbacks |
|
|
390
|
+
| [@usefy/use-throttle](https://www.npmjs.com/package/@usefy/use-throttle) | Value throttling |
|
|
391
|
+
| [@usefy/use-throttle-callback](https://www.npmjs.com/package/@usefy/use-throttle-callback) | Throttled callbacks |
|
|
392
|
+
| [@usefy/use-click-any-where](https://www.npmjs.com/package/@usefy/use-click-any-where) | Global click detection |
|
|
384
393
|
|
|
385
394
|
---
|
|
386
395
|
|