better-preact-use-state 1.0.3 → 1.0.5

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 ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md CHANGED
@@ -1,10 +1,10 @@
1
- ## better-preact-use-state
1
+ # better-preact-use-state
2
2
 
3
3
  A utility that improves Preact's `useState` to be safer and more convenient. Provides both `useState` (enhanced) and a dedicated `useBoolState` hook.
4
4
 
5
5
  ## Overview
6
6
 
7
- Preact's `useState` can cause unintended behavior when you want to hold a function as a state. `better-preact-use-state` solves this problem and is a utility designed for safer state management. It is designed as a drop-in replacement for Preact's `useState` hook, with added functionality. `better-preact-use-state` also provides a simplified hook, `useBoolState`, specifically for boolean state.
7
+ Preact's `useState` hands back a single setter that accepts either the next value or an updater function, so the two cannot be told apart by type. `better-preact-use-state` is a utility designed for safer state management: its `setState` accepts only the next value, and it also hands back `updateState` and `resetState`. It is designed as a drop-in replacement for Preact's `useState` hook, with added functionality. `better-preact-use-state` also provides a simplified hook, `useBoolState`, specifically for boolean state.
8
8
 
9
9
  ## Installation
10
10
 
@@ -30,27 +30,34 @@ pnpm add better-preact-use-state
30
30
 
31
31
  ```tsx
32
32
  import { useState } from 'better-preact-use-state';
33
+ import type * as Preact from 'preact';
34
+ import { useCallback } from 'preact/hooks';
33
35
 
34
- const MyComponent: FunctionalComponent = () => {
35
- const [name, setName] = useState('John Doe');
36
+ export const MyComponent = (): Preact.JSX.Element => {
37
+ const [userName, setUserName] = useState('John Doe');
36
38
 
37
- const [count, setCount, { updateState: updateCount }] = useState(0);
39
+ const [count, , { updateState: updateCount }] = useState(0);
38
40
 
39
- const onNameChange = useCallback((event) => {
40
- setName(event.target.value);
41
- }, []);
41
+ const onNameInput: Preact.InputEventHandler<HTMLInputElement> = useCallback(
42
+ (ev) => {
43
+ setUserName(ev.currentTarget.value);
44
+ },
45
+ [setUserName],
46
+ );
42
47
 
43
48
  const incrementCount = useCallback(() => {
44
49
  updateCount((x) => x + 1);
45
- }, []);
50
+ }, [updateCount]);
46
51
 
47
52
  return (
48
53
  <div>
49
54
  <p>{`Count: ${count}`}</p>
50
- <button onClick={incrementCount}>{'Increment'}</button>
55
+ <button type={'button'} onClick={incrementCount}>
56
+ {'Increment'}
57
+ </button>
51
58
 
52
- <p>{`Name: ${name}`}</p>
53
- <input type='text' value={name} onChange={onNameChange} />
59
+ <p>{`Name: ${userName}`}</p>
60
+ <input type={'text'} value={userName} onInput={onNameInput} />
54
61
  </div>
55
62
  );
56
63
  };
@@ -62,18 +69,25 @@ const MyComponent: FunctionalComponent = () => {
62
69
 
63
70
  ```tsx
64
71
  import { useBoolState } from 'better-preact-use-state';
72
+ import type * as Preact from 'preact';
65
73
 
66
- const MyComponent = () => {
67
- const [isOpen, { setTrue: open, setFalse: close, toggleState }] =
74
+ export const MyComponent = (): Preact.JSX.Element => {
75
+ const [isOpen, { setTrue: openPanel, setFalse: closePanel, toggleState }] =
68
76
  useBoolState(false);
69
77
 
70
78
  return (
71
79
  <div>
72
80
  <p>{`Is Open: ${isOpen ? 'Yes' : 'No'}`}</p>
73
- <button onClick={open}>{'Open'}</button>
74
- <button onClick={close}>{'Close'}</button>
75
- <button onClick={toggleState}>{'Toggle'}</button>
81
+ <button type={'button'} onClick={openPanel}>
82
+ {'Open'}
83
+ </button>
84
+ <button type={'button'} onClick={closePanel}>
85
+ {'Close'}
86
+ </button>
76
87
  {/* Toggles the boolean value */}
88
+ <button type={'button'} onClick={toggleState}>
89
+ {'Toggle'}
90
+ </button>
77
91
  </div>
78
92
  );
79
93
  };
@@ -83,110 +97,112 @@ const MyComponent = () => {
83
97
 
84
98
  ### `useState`
85
99
 
86
- ```typescript
87
- const [state, setState, { updateState, resetState }] = useState<T>(initialState: T);
100
+ ```ts
101
+ const [state, setState, { updateState, resetState }] = useState(initialState);
88
102
  ```
89
103
 
90
- - `state`: The current state.
91
- - `setState`: Function to update the state.
92
- - `updateState`: Function to update the state based on the current state.
93
- - `resetState`: Function to reset the state to the initial value.
104
+ - `state`: The current state.
105
+ - `setState`: Function to update the state.
106
+ - `updateState`: Function to update the state based on the current state.
107
+ - `resetState`: Function to reset the state to the initial value.
94
108
 
95
109
  #### `updateState`
96
110
 
97
- ```typescript
98
- updateState(updateFn: (v: T) => T): void;
111
+ ```ts
112
+ updateState: (updateFn: (v: T) => T) => void;
99
113
  ```
100
114
 
101
115
  Updates the state by passing a function `updateFn` that takes the current state as an argument and returns the new state.
102
116
 
103
117
  #### `resetState`
104
118
 
105
- ```typescript
106
- resetState(): void;
119
+ ```ts
120
+ resetState: () => void;
107
121
  ```
108
122
 
109
- Resets the state to the initial value.
123
+ Resets the state to the value it was created with: the first render's `initialState`, as `useState` reads it, whatever is passed later. Its identity never changes.
110
124
 
111
125
  ### `useBoolState`
112
126
 
113
- ```typescript
114
- const [state, { setState, setTrue, setFalse, resetState, toggleState, updateState }] = useBoolState(initialState: boolean);
127
+ ```ts
128
+ const [
129
+ state,
130
+ { setState, setTrue, setFalse, resetState, toggleState, updateState },
131
+ ] = useBoolState(initialState);
115
132
  ```
116
133
 
117
- - `state`: The current boolean value.
118
- - `setState`: Function to update the boolean value directly.
119
- - `setTrue`: Function to set the state to `true`.
120
- - `setFalse`: Function to set the state to `false`.
121
- - `resetState`: Function to reset the state to the initial value.
122
- - `toggleState`: Function to toggle the boolean value (true to false, false to true).
123
- - `updateState`: Function to update the state based on the current state.
134
+ - `state`: The current boolean value.
135
+ - `setState`: Function to update the boolean value directly.
136
+ - `setTrue`: Function to set the state to `true`.
137
+ - `setFalse`: Function to set the state to `false`.
138
+ - `resetState`: Function to reset the state to the initial value.
139
+ - `toggleState`: Function to toggle the boolean value (true to false, false to true).
140
+ - `updateState`: Function to update the state based on the current state.
124
141
 
125
142
  #### `setState`
126
143
 
127
- ```typescript
128
- setState(next: boolean): void;
144
+ ```ts
145
+ setState: (next: boolean) => void;
129
146
  ```
130
147
 
131
148
  Sets the state to the provided boolean value.
132
149
 
133
150
  #### `setTrue`
134
151
 
135
- ```typescript
136
- setTrue(): void;
152
+ ```ts
153
+ setTrue: () => void;
137
154
  ```
138
155
 
139
156
  Sets the state to `true`.
140
157
 
141
158
  #### `setFalse`
142
159
 
143
- ```typescript
144
- setFalse(): void;
160
+ ```ts
161
+ setFalse: () => void;
145
162
  ```
146
163
 
147
164
  Sets the state to `false`.
148
165
 
149
166
  #### `resetState`
150
167
 
151
- ```typescript
152
- resetState(): void;
168
+ ```ts
169
+ resetState: () => void;
153
170
  ```
154
171
 
155
- Resets the state to the initial value.
172
+ Resets the state to the value it was created with: the first render's `initialState`, as `useState` reads it, whatever is passed later. Its identity never changes.
156
173
 
157
174
  #### `toggleState`
158
175
 
159
- ```typescript
160
- toggleState(): void;
176
+ ```ts
177
+ toggleState: () => void;
161
178
  ```
162
179
 
163
180
  Toggles the current boolean value.
164
181
 
165
182
  #### `updateState`
166
183
 
167
- ```typescript
168
- updateState(updateFn: (v: boolean) => boolean): void;
184
+ ```ts
185
+ updateState: (updateFn: (v: boolean) => boolean) => void;
169
186
  ```
170
187
 
171
188
  Updates the state by passing a function `updateFn` that takes the current state as an argument and returns the new state. This is useful for updates that depend on the current state.
172
189
 
173
190
  ## Benefits of this library
174
191
 
175
- With Preact's standard `useState`, attempting to store a function as state can lead to unexpected behavior (the function being interpreted as an updater function). `better-preact-use-state` solves this issue and, by providing `updateState`, allows functions to be safely held as state. Also, `resetState` lets you easily return the state to its initial value. `useBoolState` simplifies boolean state management in Preact, offering several advantages over directly using useState for booleans.
176
-
177
- ### Example Use Case
192
+ With Preact's standard `useState`, `setState` accepts either the next value or an updater function, and nothing in the types tells the two apart. `better-preact-use-state` separates them: `setState` is typed to accept only the next value, and an update that depends on the current value goes through `updateState` explicitly. `resetState` lets you easily return the state to its initial value. `useBoolState` simplifies boolean state management in Preact, offering several advantages over directly using `useState` for booleans.
178
193
 
179
- Useful when you want to hold a function as state.
194
+ ### Holding a function as state
180
195
 
181
- ```typescript
182
- const [fn, setFn, { updateState: updateFn }] = useState(initialFn); // Holds a function as state
196
+ The initial value and the argument of `setState` are passed to Preact's `useState` as they are, so a function given to either is still called as a lazy initializer or an updater, just as with Preact's own hook. To hold a function as state, wrap it in an object:
183
197
 
184
- // In Preact's standard useState, `setFn(nextFn)` interprets nextFn as an update function.
198
+ ```ts
199
+ const [{ fn }, setFn] = useState({ fn: initialFn });
185
200
 
186
- // If you want to update the state with an update function, you can use updateState instead.
187
- updateFn((currentFn) => nextFn);
201
+ const onReplace = useCallback((): void => {
202
+ setFn({ fn: nextFn });
203
+ }, [setFn, nextFn]);
188
204
  ```
189
205
 
190
206
  ## License
191
207
 
192
- MIT
208
+ Apache-2.0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.mts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,iBAAiB,CAAC"}
@@ -1,2 +1,3 @@
1
1
  export * from './use-bool-state.mjs';
2
2
  export * from './use-state.mjs';
3
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.mts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,iBAAiB,CAAC"}
@@ -1,4 +1,4 @@
1
- export declare const useBoolState: (initialState: boolean) => [state: boolean, Readonly<{
1
+ export declare const useBoolState: (initialState: boolean) => readonly [state: boolean, setters: Readonly<{
2
2
  setState: (next: boolean) => void;
3
3
  setTrue: () => void;
4
4
  setFalse: () => void;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-bool-state.d.mts","sourceRoot":"","sources":["../src/use-bool-state.mts"],"names":[],"mappings":"AAGA,eAAO,MAAM,YAAY,iBACT,OAAO,KACpB,SAAS,CACV,KAAK,EAAE,OAAO,EACd,OAAO,EAAE,QAAQ,CAAC;IAChB,QAAQ,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,CAAC;IAClC,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,QAAQ,EAAE,MAAM,IAAI,CAAC;IACrB,UAAU,EAAE,MAAM,IAAI,CAAC;IACvB,WAAW,EAAE,MAAM,IAAI,CAAC;IACxB,WAAW,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC,EAAE,OAAO,KAAK,OAAO,KAAK,IAAI,CAAC;CAC1D,CAAC,CAuBH,CAAC"}
@@ -0,0 +1,22 @@
1
+ import * as Preact from 'preact/hooks';
2
+ import { useState } from './use-state.mjs';
3
+ export const useBoolState = (initialState) => {
4
+
5
+
6
+
7
+ const [state, setState, { resetState, updateState }] = useState(initialState);
8
+ const setTrue = Preact.useCallback(() => {
9
+ setState(true);
10
+ }, [setState]);
11
+ const setFalse = Preact.useCallback(() => {
12
+ setState(false);
13
+ }, [setState]);
14
+ const toggleState = Preact.useCallback(() => {
15
+ updateState((b) => !b);
16
+ }, [updateState]);
17
+ return [
18
+ state,
19
+ { setState, setTrue, setFalse, resetState, toggleState, updateState },
20
+ ];
21
+ };
22
+ //# sourceMappingURL=use-bool-state.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-bool-state.mjs","sourceRoot":"","sources":["../src/use-bool-state.mts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAE3C,MAAM,CAAC,MAAM,YAAY,GAAG,CAC1B,YAAqB,EAWrB,EAAE;IACF,6EAA6E;IAC7E,2EAA2E;IAC3E,iCAAiC;IACjC,MAAM,CAAC,KAAK,EAAE,QAAQ,EAAE,EAAE,UAAU,EAAE,WAAW,EAAE,CAAC,GAAG,QAAQ,CAAC,YAAY,CAAC,CAAC;IAE9E,MAAM,OAAO,GAAG,MAAM,CAAC,WAAW,CAAC,GAAG,EAAE;QACtC,QAAQ,CAAC,IAAI,CAAC,CAAC;IACjB,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEf,MAAM,QAAQ,GAAG,MAAM,CAAC,WAAW,CAAC,GAAG,EAAE;QACvC,QAAQ,CAAC,KAAK,CAAC,CAAC;IAClB,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEf,MAAM,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC,GAAG,EAAE;QAC1C,WAAW,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IACzB,CAAC,EAAE,CAAC,WAAW,CAAC,CAAC,CAAC;IAElB,OAAO;QACL,KAAK;QACL,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,WAAW,EAAE,WAAW,EAAE;KACtE,CAAC;AACJ,CAAC,CAAC"}
@@ -1,4 +1,4 @@
1
- export declare const useState: <T>(initialState: T) => readonly [state: T, setState: (next: T) => void, Readonly<{
1
+ export declare const useState: <T>(initialState: T) => readonly [state: T, setState: (next: T) => void, setters: Readonly<{
2
2
  updateState: (updateFn: (v: T) => T) => void;
3
3
  resetState: () => void;
4
4
  }>];
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-state.d.mts","sourceRoot":"","sources":["../src/use-state.mts"],"names":[],"mappings":"AAEA,eAAO,MAAM,QAAQ,GAAI,CAAC,gBACV,CAAC,KACd,SAAS,CACV,KAAK,EAAE,CAAC,EACR,QAAQ,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,EAC3B,OAAO,EAAE,QAAQ,CAAC;IAChB,WAAW,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,KAAK,IAAI,CAAC;IAC7C,UAAU,EAAE,MAAM,IAAI,CAAC;CACxB,CAAC,CAkBH,CAAC"}
@@ -1,16 +1,16 @@
1
1
  import * as Preact from 'preact/hooks';
2
-
3
- // eslint-disable-next-line @typescript-eslint/no-restricted-imports
4
- const useState = (initialState) => {
2
+ export const useState = (initialState) => {
5
3
  const [state, setState] = Preact.useState(initialState);
4
+
5
+
6
+
7
+ const [initial] = Preact.useState(initialState);
6
8
  const resetState = Preact.useCallback(() => {
7
- setState(() => initialState);
8
- }, [initialState]);
9
+ setState(() => initial);
10
+ }, [initial]);
9
11
  const updateState = Preact.useCallback((updateFn) => {
10
12
  setState(updateFn);
11
13
  }, []);
12
14
  return [state, setState, { updateState, resetState }];
13
15
  };
14
-
15
- export { useState };
16
- //# sourceMappingURL=use-state.mjs.map
16
+ //# sourceMappingURL=use-state.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-state.mjs","sourceRoot":"","sources":["../src/use-state.mts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,cAAc,CAAC;AAEvC,MAAM,CAAC,MAAM,QAAQ,GAAG,CACtB,YAAe,EAQf,EAAE;IACF,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAI,YAAY,CAAC,CAAC;IAE3D,6EAA6E;IAC7E,0EAA0E;IAC1E,uEAAuE;IACvE,MAAM,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAI,YAAY,CAAC,CAAC;IAEnD,MAAM,UAAU,GAAG,MAAM,CAAC,WAAW,CAAC,GAAG,EAAE;QACzC,QAAQ,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,CAAC;IAC1B,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IAEd,MAAM,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC,QAAqB,EAAE,EAAE;QAC/D,QAAQ,CAAC,QAAQ,CAAC,CAAC;IACrB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,CAAC,KAAK,EAAE,QAAQ,EAAE,EAAE,WAAW,EAAE,UAAU,EAAE,CAAC,CAAC;AACxD,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,62 +1,85 @@
1
1
  {
2
2
  "name": "better-preact-use-state",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "private": false,
5
- "license": "MIT",
5
+ "description": "useState that also hands back the setters you would otherwise write by hand: updateState, resetState, and setTrue / setFalse / toggleState for booleans.",
6
+ "keywords": [
7
+ "preact",
8
+ "hooks",
9
+ "usestate",
10
+ "typescript"
11
+ ],
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/noshiro-pf/mono.git",
15
+ "directory": "libs/better-preact-use-state"
16
+ },
17
+ "license": "Apache-2.0",
6
18
  "author": "noshiro-pf <noshiro.pf@gmail.com>",
7
19
  "sideEffects": false,
8
20
  "type": "module",
9
21
  "exports": {
10
22
  ".": {
11
- "import": {
12
- "types": "./esm/index.d.mts",
13
- "default": "./esm/index.mjs"
14
- }
23
+ "types": "./dist/index.d.mts",
24
+ "import": "./dist/index.mjs"
15
25
  }
16
26
  },
27
+ "module": "./dist/index.mjs",
28
+ "types": "./dist/index.d.mts",
17
29
  "files": [
18
30
  "src",
19
- "esm"
31
+ "dist",
32
+ "README.md",
33
+ "LICENSE"
20
34
  ],
21
- "scripts": {
22
- "build": "wireit",
23
- "clean": "run-p clean:**",
24
- "clean:build": "rimraf esm",
25
- "clean:wireit": "rimraf .wireit/**",
26
- "fmt": "yarn zz:prettier .",
27
- "gi": "bash ../../../scripts/bash/index_ts_generator.sh ./src --min-depth 0 --ext .mts",
28
- "lint": "yarn zz:eslint:src",
29
- "lint:fix": "yarn zz:eslint:src --fix",
30
- "pub": "yarn zz:publish",
31
- "test": "yarn zz:vitest run",
32
- "testw": "yarn zz:vitest watch",
33
- "tsc": "yarn type-check",
34
- "tscw": "tsc --noEmit --watch",
35
- "type-check": "tsc --noEmit",
36
- "zz:eslint": "ESLINT_USE_FLAT_CONFIG=true TIMING=1 eslint",
37
- "zz:eslint:print-config": "yarn zz:eslint --print-config src/index.mts",
38
- "zz:eslint:src": "yarn zz:eslint --config eslint.config.js './src/**/*'",
39
- "zz:prettier": "prettier --cache --cache-strategy content --ignore-path ../../../.prettierignore --write",
40
- "zz:publish": "yarn publish --no-git-tag-version --access=public",
41
- "zz:vitest": "vitest --config ./configs/vitest.config.ts"
42
- },
43
35
  "devDependencies": {
44
- "@noshiro/eslint-configs": "*",
45
- "@noshiro/mono-utils": "*",
46
- "preact": "^10.23.2"
36
+ "@testing-library/preact": "3.2.4",
37
+ "@vitest/browser-playwright": "5.0.5",
38
+ "eslint": "10.13.0",
39
+ "eslint-config-typed": "7.14.0",
40
+ "eslint-plugin-ts-data-forge": "0.13.1",
41
+ "eslint-plugin-ts-type-forge": "0.8.26",
42
+ "npm-run-all2": "9.0.4",
43
+ "playwright": "1.64.1",
44
+ "preact": "10.29.8",
45
+ "prettier": "3.9.10",
46
+ "prettier-plugin-organize-imports": "4.3.0",
47
+ "prettier-plugin-packagejson": "3.0.2",
48
+ "ts-data-forge": "15.0.1",
49
+ "ts-repo-utils": "11.0.1",
50
+ "tsx": "4.23.15"
47
51
  },
48
52
  "peerDependencies": {
49
- "preact": ">=10"
53
+ "preact": ">=10.0.0"
50
54
  },
51
- "wireit": {
52
- "pre-build": {
53
- "command": "run-s clean:build gi type-check"
54
- },
55
- "build": {
56
- "dependencies": [
57
- "pre-build"
58
- ],
59
- "command": "rollup --config ./configs/rollup.config.ts --configPlugin typescript --configImportAttributesKey with"
60
- }
55
+ "engines": {
56
+ "node": ">=22.23.4",
57
+ "pnpm": ">=9.0.0"
58
+ },
59
+ "publishConfig": {
60
+ "access": "public",
61
+ "provenance": true
62
+ },
63
+ "scripts": {
64
+ "build": "tsx --tsconfig ../../tools/configs/tsconfig.tsx.json ./scripts/cmd/build.mts",
65
+ "check:ext": "tsx --tsconfig ../../tools/configs/tsconfig.tsx.json ./scripts/cmd/check-ext.mts",
66
+ "check:lint": "node ../../tools/scripts/cmd/assert-deps-built.mts && oxlint . && eslint .",
67
+ "check:test": "node ../../tools/scripts/cmd/assert-deps-built.mts && pnpm run z:vitest:node run",
68
+ "check:test:browser": "node ../../tools/scripts/cmd/assert-deps-built.mts && pnpm run z:vitest --project='Browser' run",
69
+ "check:types": "node ../../tools/scripts/cmd/assert-deps-built.mts && node ../../node_modules/typescript-native/bin/tsc --noEmit",
70
+ "doc": "run-s gen:readme",
71
+ "fix:fmt": "tsx --tsconfig ../../tools/configs/tsconfig.tsx.json ../../libs/ts-repo-utils/src/cmd/format-uncommitted.mts --cwd .",
72
+ "fix:fmt:diff": "tsx --tsconfig ../../tools/configs/tsconfig.tsx.json ../../libs/ts-repo-utils/src/cmd/format-diff-from.mts origin/main --cwd .",
73
+ "fix:fmt:full": "tsx --tsconfig ../../tools/configs/tsconfig.tsx.json ../../tools/scripts/cmd/format-package.mts",
74
+ "fix:lint": "node ../../tools/scripts/cmd/assert-deps-built.mts && oxlint . --fix && eslint . --fix",
75
+ "fmt": "pnpm run fix:fmt",
76
+ "gen": "run-s gen:index gen:readme",
77
+ "gen:index": "run-s gen:index:src fix:fmt",
78
+ "gen:index:src": "tsx --tsconfig ../../tools/configs/tsconfig.tsx.json ../../libs/ts-repo-utils/src/cmd/gen-index-ts.mts ./src --index-ext .mts --export-ext .mjs --target-ext .mts",
79
+ "gen:readme": "tsx --tsconfig ../../tools/configs/tsconfig.tsx.json ./scripts/cmd/embed-examples.mts",
80
+ "test": "pnpm run check:test",
81
+ "watch:types": "node ../../node_modules/typescript-native/bin/tsc --noEmit --watch -p ./tsconfig.json",
82
+ "z:vitest": "vitest --config ./configs/vitest.config.mts",
83
+ "z:vitest:node": "pnpm run z:vitest --project='Node.js'"
61
84
  }
62
- }
85
+ }
package/src/index.mts CHANGED
@@ -1 +1,2 @@
1
- export * from './hooks/index.mjs';
1
+ export * from './use-bool-state.mjs';
2
+ export * from './use-state.mjs';
@@ -0,0 +1,38 @@
1
+ import * as Preact from 'preact/hooks';
2
+ import { useState } from './use-state.mjs';
3
+
4
+ export const useBoolState = (
5
+ initialState: boolean,
6
+ ): readonly [
7
+ state: boolean,
8
+ setters: Readonly<{
9
+ setState: (next: boolean) => void;
10
+ setTrue: () => void;
11
+ setFalse: () => void;
12
+ resetState: () => void;
13
+ toggleState: () => void;
14
+ updateState: (updateFn: (v: boolean) => boolean) => void;
15
+ }>,
16
+ ] => {
17
+ // No explicit type argument: `initialState` gives it, and the React Compiler
18
+ // rule reads `useState<boolean>(…)` on a custom hook as a reference to the
19
+ // hook rather than a call to it.
20
+ const [state, setState, { resetState, updateState }] = useState(initialState);
21
+
22
+ const setTrue = Preact.useCallback(() => {
23
+ setState(true);
24
+ }, [setState]);
25
+
26
+ const setFalse = Preact.useCallback(() => {
27
+ setState(false);
28
+ }, [setState]);
29
+
30
+ const toggleState = Preact.useCallback(() => {
31
+ updateState((b) => !b);
32
+ }, [updateState]);
33
+
34
+ return [
35
+ state,
36
+ { setState, setTrue, setFalse, resetState, toggleState, updateState },
37
+ ];
38
+ };
@@ -1,4 +1,3 @@
1
- // eslint-disable-next-line @typescript-eslint/no-restricted-imports
2
1
  import * as Preact from 'preact/hooks';
3
2
 
4
3
  export const useState = <T,>(
@@ -6,16 +5,21 @@ export const useState = <T,>(
6
5
  ): readonly [
7
6
  state: T,
8
7
  setState: (next: T) => void,
9
- Readonly<{
8
+ setters: Readonly<{
10
9
  updateState: (updateFn: (v: T) => T) => void;
11
10
  resetState: () => void;
12
11
  }>,
13
12
  ] => {
14
13
  const [state, setState] = Preact.useState<T>(initialState);
15
14
 
15
+ // The value the state was created with, read once as `useState` reads it, so
16
+ // that `resetState` neither follows a later argument nor changes identity
17
+ // when one is passed (an object literal is a new one on every render).
18
+ const [initial] = Preact.useState<T>(initialState);
19
+
16
20
  const resetState = Preact.useCallback(() => {
17
- setState(() => initialState);
18
- }, [initialState]);
21
+ setState(() => initial);
22
+ }, [initial]);
19
23
 
20
24
  const updateState = Preact.useCallback((updateFn: (v: T) => T) => {
21
25
  setState(updateFn);
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/hooks/index.mts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,iBAAiB,CAAC"}
@@ -1,3 +0,0 @@
1
- export { useBoolState } from './use-bool-state.mjs';
2
- export { useState } from './use-state.mjs';
3
- //# sourceMappingURL=index.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"use-bool-state.d.mts","sourceRoot":"","sources":["../../src/hooks/use-bool-state.mts"],"names":[],"mappings":"AAGA,eAAO,MAAM,YAAY,iBACT,OAAO,KACpB,CACD,KAAK,EAAE,OAAO,EACd,QAAQ,CAAC;IACP,QAAQ,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,CAAC;IAClC,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,QAAQ,EAAE,MAAM,IAAI,CAAC;IACrB,UAAU,EAAE,MAAM,IAAI,CAAC;IACvB,WAAW,EAAE,MAAM,IAAI,CAAC;IACxB,WAAW,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC,EAAE,OAAO,KAAK,OAAO,KAAK,IAAI,CAAC;CAC1D,CAAC,CA+BH,CAAC"}
@@ -1,32 +0,0 @@
1
- import { useCallback } from 'preact/hooks';
2
- import { useState } from './use-state.mjs';
3
-
4
- const useBoolState = (initialState) => {
5
- const [state, setState, { resetState, updateState }] = useState(initialState);
6
- const setTrue = useCallback(() => {
7
- setState(true);
8
- // eslint-disable-next-line react-hooks/exhaustive-deps
9
- }, []);
10
- const setFalse = useCallback(() => {
11
- setState(false);
12
- // eslint-disable-next-line react-hooks/exhaustive-deps
13
- }, []);
14
- const toggleState = useCallback(() => {
15
- updateState((b) => !b);
16
- // eslint-disable-next-line react-hooks/exhaustive-deps
17
- }, []);
18
- return [
19
- state,
20
- {
21
- setState,
22
- updateState,
23
- setTrue,
24
- setFalse,
25
- toggleState,
26
- resetState,
27
- },
28
- ];
29
- };
30
-
31
- export { useBoolState };
32
- //# sourceMappingURL=use-bool-state.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"use-bool-state.mjs","sources":["../../src/hooks/use-bool-state.mts"],"sourcesContent":[null],"names":[],"mappings":";;;AAGa,MAAA,YAAY,GAAG,CAC1B,YAAqB,KAWnB;AACF,IAAA,MAAM,CAAC,KAAK,EAAE,QAAQ,EAAE,EAAE,UAAU,EAAE,WAAW,EAAE,CAAC,GAClD,QAAQ,CAAU,YAAY,CAAC;AAEjC,IAAA,MAAM,OAAO,GAAG,WAAW,CAAC,MAAK;QAC/B,QAAQ,CAAC,IAAI,CAAC;;KAEf,EAAE,EAAE,CAAC;AAEN,IAAA,MAAM,QAAQ,GAAG,WAAW,CAAC,MAAK;QAChC,QAAQ,CAAC,KAAK,CAAC;;KAEhB,EAAE,EAAE,CAAC;AAEN,IAAA,MAAM,WAAW,GAAG,WAAW,CAAC,MAAK;QACnC,WAAW,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;;KAEvB,EAAE,EAAE,CAAC;IAEN,OAAO;QACL,KAAK;AACL,QAAA;YACE,QAAQ;YACR,WAAW;YACX,OAAO;YACP,QAAQ;YACR,WAAW;YACX,UAAU;AACX,SAAA;KACF;AACH;;;;"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"use-state.d.mts","sourceRoot":"","sources":["../../src/hooks/use-state.mts"],"names":[],"mappings":"AAGA,eAAO,MAAM,QAAQ,GAAI,CAAC,gBACV,CAAC,KACd,SAAS,CACV,KAAK,EAAE,CAAC,EACR,QAAQ,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,EAC3B,QAAQ,CAAC;IACP,WAAW,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,KAAK,IAAI,CAAC;IAC7C,UAAU,EAAE,MAAM,IAAI,CAAC;CACxB,CAAC,CAaH,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"use-state.mjs","sources":["../../src/hooks/use-state.mts"],"sourcesContent":[null],"names":[],"mappings":";;AAAA;AAGa,MAAA,QAAQ,GAAG,CACtB,YAAe,KAQb;AACF,IAAA,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAI,YAAY,CAAC;AAE1D,IAAA,MAAM,UAAU,GAAG,MAAM,CAAC,WAAW,CAAC,MAAK;AACzC,QAAA,QAAQ,CAAC,MAAM,YAAY,CAAC;AAC9B,KAAC,EAAE,CAAC,YAAY,CAAC,CAAC;IAElB,MAAM,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC,QAAqB,KAAI;QAC/D,QAAQ,CAAC,QAAQ,CAAC;KACnB,EAAE,EAAE,CAAC;IAEN,OAAO,CAAC,KAAK,EAAE,QAAQ,EAAE,EAAE,WAAW,EAAE,UAAU,EAAE,CAAC;AACvD;;;;"}
package/esm/index.d.mts DELETED
@@ -1,2 +0,0 @@
1
- export * from './hooks/index.mjs';
2
- //# sourceMappingURL=index.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.mts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC"}
package/esm/index.mjs DELETED
@@ -1,3 +0,0 @@
1
- export { useBoolState } from './hooks/use-bool-state.mjs';
2
- export { useState } from './hooks/use-state.mjs';
3
- //# sourceMappingURL=index.mjs.map
package/esm/index.mjs.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
@@ -1,46 +0,0 @@
1
- import { useCallback } from 'preact/hooks';
2
- import { useState } from './use-state.mjs';
3
-
4
- export const useBoolState = (
5
- initialState: boolean,
6
- ): [
7
- state: boolean,
8
- Readonly<{
9
- setState: (next: boolean) => void;
10
- setTrue: () => void;
11
- setFalse: () => void;
12
- resetState: () => void;
13
- toggleState: () => void;
14
- updateState: (updateFn: (v: boolean) => boolean) => void;
15
- }>,
16
- ] => {
17
- const [state, setState, { resetState, updateState }] =
18
- useState<boolean>(initialState);
19
-
20
- const setTrue = useCallback(() => {
21
- setState(true);
22
- // eslint-disable-next-line react-hooks/exhaustive-deps
23
- }, []);
24
-
25
- const setFalse = useCallback(() => {
26
- setState(false);
27
- // eslint-disable-next-line react-hooks/exhaustive-deps
28
- }, []);
29
-
30
- const toggleState = useCallback(() => {
31
- updateState((b) => !b);
32
- // eslint-disable-next-line react-hooks/exhaustive-deps
33
- }, []);
34
-
35
- return [
36
- state,
37
- {
38
- setState,
39
- updateState,
40
- setTrue,
41
- setFalse,
42
- toggleState,
43
- resetState,
44
- },
45
- ];
46
- };
File without changes