Skip to content

Commit 61305f7

Browse files
committed
feat: Added use-toggle.
1 parent 06a1d71 commit 61305f7

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ We want to achieve as 1:1 as possible with Mantine's original hooks. So you can
4040
- 🌳 Tree-shakable
4141
- 🖌️ TypeScript support
4242
- 🔵 For SolidJS
43-
- 📦 Zero-dependencies
43+
- 📦 Zero-dependencies (except Solid)
4444

4545
## Roadmap
4646

@@ -111,7 +111,7 @@ Based on the [@mantine/hooks](https://github.com/mantinedev/mantine/tree/master/
111111
- [ ] use-throttled-state
112112
- [ ] use-throttled-value
113113
- [ ] use-timeout
114-
- [ ] use-toggle
114+
- [x] use-toggle
115115
- [ ] use-uncontrolled
116116
- [ ] use-validated-state
117117
- [ ] use-viewport-size

dev/App.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ import type { Component } from 'solid-js';
22
import './app.css';
33

44
// Hooks
5-
import { useClickOutside, useHotkeys, useHover, useIdle, useNetwork, useOs } from '../src';
5+
import {
6+
useClickOutside,
7+
useHotkeys,
8+
useHover,
9+
useIdle,
10+
useNetwork,
11+
useOs,
12+
useToggle,
13+
} from '../src';
614

715
const App: Component = () => {
816
// let ref = useClickOutside(() => {
@@ -20,6 +28,8 @@ const App: Component = () => {
2028

2129
const networkStatus = useNetwork();
2230

31+
const [currentOption, toggle] = useToggle(['light', 'dark', 'system']);
32+
2333
return (
2434
<div class="">
2535
{/* <p class="bg-green-500" ref={ref}>
@@ -33,6 +43,8 @@ const App: Component = () => {
3343
<p>idle: {JSON.stringify(idle())}</p>
3444

3545
<p>networkStatus: {JSON.stringify(networkStatus())}</p>
46+
47+
<button onClick={() => toggle()}>Current Toggled: {JSON.stringify(currentOption())}</button>
3648
</div>
3749
);
3850
};

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './use-idle/use-idle';
55
export * from './use-mounted/use-mounted';
66
export * from './use-network/use-network';
77
export * from './use-os/use-os';
8+
export * from './use-toggle/use-toggle';

src/use-toggle/use-toggle.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { createMemo, createSignal } from 'solid-js';
2+
3+
/**
4+
* A hook that toggles between two or multiple values (by implementing a common state pattern).
5+
*/
6+
export function useToggle<T = boolean>(options: readonly T[] = [false, true] as any) {
7+
const [_options, _setOptions] = createSignal<typeof options>(options);
8+
9+
function toggle() {
10+
const value = _options()[0]!;
11+
const index = Math.abs(_options()!.indexOf(value));
12+
13+
_setOptions(
14+
_options()!
15+
.slice(index + 1)
16+
.concat(value),
17+
);
18+
}
19+
20+
const currentOption = createMemo(() => _options()[0]);
21+
22+
return [currentOption, toggle] as const;
23+
}

0 commit comments

Comments
 (0)