@idevconn/use-draft 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 +201 -0
- package/README.md +99 -0
- package/dist/index.cjs +93 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +53 -0
- package/dist/index.d.ts +53 -0
- package/dist/index.js +65 -0
- package/dist/index.js.map +1 -0
- package/package.json +74 -0
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 interface) to the interfaces
|
|
46
|
+
of, 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 2026 iDEVconn
|
|
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
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# @idevconn/use-draft
|
|
2
|
+
|
|
3
|
+
Global unsaved-changes blocker for [TanStack Router](https://tanstack.com/router) SPAs.
|
|
4
|
+
|
|
5
|
+
Aggregates dirty state from every mounted form via a [Zustand](https://zustand.docs.pmnd.rs/) store and blocks both:
|
|
6
|
+
|
|
7
|
+
- **In-app navigation** through `useBlocker` (renders your own confirm dialog).
|
|
8
|
+
- **Browser refresh / tab-close** through `beforeunload` (native dialog).
|
|
9
|
+
|
|
10
|
+
Any component on the page can subscribe to the aggregate `isDirty` state — useful for showing an upload-in-progress indicator outside the form tree.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @idevconn/use-draft
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Peer dependencies: `react >= 18`, `@tanstack/react-router >= 1.100`, `zustand >= 4`.
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
### Inside a form
|
|
23
|
+
|
|
24
|
+
```tsx
|
|
25
|
+
import { useDraft } from "@idevconn/use-draft";
|
|
26
|
+
import { useForm } from "react-hook-form";
|
|
27
|
+
|
|
28
|
+
function ProductForm() {
|
|
29
|
+
const form = useForm<ProductInput>();
|
|
30
|
+
const { showDialog, confirmLeave, cancelLeave } = useDraft(form.formState.isDirty);
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<>
|
|
34
|
+
<form onSubmit={...}>{/* ... */}</form>
|
|
35
|
+
{showDialog && (
|
|
36
|
+
<ConfirmDialog
|
|
37
|
+
message="You have unsaved changes. Leave anyway?"
|
|
38
|
+
onConfirm={confirmLeave}
|
|
39
|
+
onCancel={cancelLeave}
|
|
40
|
+
/>
|
|
41
|
+
)}
|
|
42
|
+
</>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### When the dirty flag changes inside the navigation handler
|
|
48
|
+
|
|
49
|
+
If you set the dirty state synchronously **right before** triggering navigation (e.g. on save), pass a **getter function**. `useBlocker` invokes the predicate lazily, so the getter sees the latest value before React has had a chance to re-render.
|
|
50
|
+
|
|
51
|
+
```tsx
|
|
52
|
+
const savedRef = useRef(false);
|
|
53
|
+
useDraft(() => form.formState.isDirty && !savedRef.current);
|
|
54
|
+
|
|
55
|
+
async function onSubmit(values: ProductInput) {
|
|
56
|
+
await mutation.mutateAsync(values);
|
|
57
|
+
savedRef.current = true; // synchronous
|
|
58
|
+
navigate({ to: "/products" }); // not blocked
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Reading the aggregate state
|
|
63
|
+
|
|
64
|
+
```tsx
|
|
65
|
+
import { useDraftStore } from "@idevconn/use-draft";
|
|
66
|
+
|
|
67
|
+
function UploadIndicator() {
|
|
68
|
+
const isDirty = useDraftStore((s) => s.isDirty);
|
|
69
|
+
return isDirty ? <span>Saving…</span> : null;
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## API
|
|
74
|
+
|
|
75
|
+
### `useDraft(isDirty)`
|
|
76
|
+
|
|
77
|
+
| Argument | Type | Description |
|
|
78
|
+
| --------- | ----------------------------- | -------------------------------------------------------------------- |
|
|
79
|
+
| `isDirty` | `boolean \| (() => boolean)` | Current dirty state, or a getter evaluated lazily on each navigation attempt. |
|
|
80
|
+
|
|
81
|
+
Returns `{ showDialog, confirmLeave, cancelLeave }`. `showDialog` is `true` while navigation is held; call `confirmLeave()` to release it or `cancelLeave()` to stay.
|
|
82
|
+
|
|
83
|
+
Side effects per mount:
|
|
84
|
+
|
|
85
|
+
- Registers a synthetic form ID (`useId`) in the global `useDraftStore`.
|
|
86
|
+
- Hooks `useBlocker({ shouldBlockFn, withResolver: true })`.
|
|
87
|
+
- Adds a `beforeunload` listener that vetoes the unload when the aggregate `isDirty` is true.
|
|
88
|
+
|
|
89
|
+
### `useDraftStore`
|
|
90
|
+
|
|
91
|
+
A Zustand store exposing `{ dirtyForms, isDirty, markDirty, markClean, reset }`. Read-only for most consumers; the hook owns writes.
|
|
92
|
+
|
|
93
|
+
## Why a global store?
|
|
94
|
+
|
|
95
|
+
Multiple independent forms on the same page (a product edit form and an inline avatar uploader, say) each need to block navigation. Tracking the state per-component means each one only knows about its own dirty flag — the avatar uploader can't block when only the product form is dirty. The global store aggregates: any one dirty → page-level block.
|
|
96
|
+
|
|
97
|
+
## License
|
|
98
|
+
|
|
99
|
+
Apache-2.0
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
useDraft: () => useDraft,
|
|
24
|
+
useDraftStore: () => useDraftStore
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// src/use-draft.ts
|
|
29
|
+
var import_react = require("react");
|
|
30
|
+
var import_react_router = require("@tanstack/react-router");
|
|
31
|
+
|
|
32
|
+
// src/store.ts
|
|
33
|
+
var import_zustand = require("zustand");
|
|
34
|
+
var useDraftStore = (0, import_zustand.create)()((set) => ({
|
|
35
|
+
dirtyForms: /* @__PURE__ */ new Set(),
|
|
36
|
+
isDirty: false,
|
|
37
|
+
markDirty: (formId) => set((state) => {
|
|
38
|
+
if (state.dirtyForms.has(formId)) return state;
|
|
39
|
+
const next = new Set(state.dirtyForms);
|
|
40
|
+
next.add(formId);
|
|
41
|
+
return { dirtyForms: next, isDirty: next.size > 0 };
|
|
42
|
+
}),
|
|
43
|
+
markClean: (formId) => set((state) => {
|
|
44
|
+
if (!state.dirtyForms.has(formId)) return state;
|
|
45
|
+
const next = new Set(state.dirtyForms);
|
|
46
|
+
next.delete(formId);
|
|
47
|
+
return { dirtyForms: next, isDirty: next.size > 0 };
|
|
48
|
+
}),
|
|
49
|
+
reset: () => set({ dirtyForms: /* @__PURE__ */ new Set(), isDirty: false })
|
|
50
|
+
}));
|
|
51
|
+
|
|
52
|
+
// src/use-draft.ts
|
|
53
|
+
function useDraft(isDirty) {
|
|
54
|
+
const formId = (0, import_react.useId)();
|
|
55
|
+
const markDirty = useDraftStore((s) => s.markDirty);
|
|
56
|
+
const markClean = useDraftStore((s) => s.markClean);
|
|
57
|
+
const isDirtyNow = typeof isDirty === "function" ? isDirty() : isDirty;
|
|
58
|
+
(0, import_react.useEffect)(() => {
|
|
59
|
+
if (isDirtyNow) {
|
|
60
|
+
markDirty(formId);
|
|
61
|
+
} else {
|
|
62
|
+
markClean(formId);
|
|
63
|
+
}
|
|
64
|
+
return () => markClean(formId);
|
|
65
|
+
}, [isDirtyNow, formId, markDirty, markClean]);
|
|
66
|
+
const shouldBlockFn = (0, import_react.useCallback)(() => {
|
|
67
|
+
return typeof isDirty === "function" ? isDirty() : isDirty;
|
|
68
|
+
}, [isDirty]);
|
|
69
|
+
const { status, proceed, reset } = (0, import_react_router.useBlocker)({
|
|
70
|
+
shouldBlockFn,
|
|
71
|
+
withResolver: true
|
|
72
|
+
});
|
|
73
|
+
(0, import_react.useEffect)(() => {
|
|
74
|
+
function handleBeforeUnload(e) {
|
|
75
|
+
if (useDraftStore.getState().isDirty) {
|
|
76
|
+
e.preventDefault();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
window.addEventListener("beforeunload", handleBeforeUnload);
|
|
80
|
+
return () => window.removeEventListener("beforeunload", handleBeforeUnload);
|
|
81
|
+
}, []);
|
|
82
|
+
return {
|
|
83
|
+
showDialog: status === "blocked",
|
|
84
|
+
confirmLeave: () => proceed?.(),
|
|
85
|
+
cancelLeave: () => reset?.()
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
89
|
+
0 && (module.exports = {
|
|
90
|
+
useDraft,
|
|
91
|
+
useDraftStore
|
|
92
|
+
});
|
|
93
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/use-draft.ts","../src/store.ts"],"sourcesContent":["export { useDraft } from \"./use-draft\";\nexport type { IsDirtyInput, UseDraftReturn } from \"./use-draft\";\nexport { useDraftStore } from \"./store\";\nexport type { DraftState } from \"./store\";\n","import { useCallback, useEffect, useId } from \"react\";\nimport { useBlocker } from \"@tanstack/react-router\";\nimport { useDraftStore } from \"./store\";\n\nexport interface UseDraftReturn {\n /** True when navigation is blocked and the consumer should render its confirm dialog. */\n showDialog: boolean;\n /** Proceed with the originally-attempted navigation. */\n confirmLeave: () => void;\n /** Stay on the current page, dismiss the dialog. */\n cancelLeave: () => void;\n}\n\nexport type IsDirtyInput = boolean | (() => boolean);\n\n/**\n * Track unsaved-changes state for the current form and block navigation\n * while it's dirty. The hook:\n *\n * 1. Reports this form's dirty state into the global draft store so other\n * components (e.g. an upload indicator outside the form tree) can read\n * the aggregate via `useDraftStore`.\n * 2. Hooks TanStack Router's `useBlocker` (with `withResolver: true`) so\n * in-app navigation pauses on a status === \"blocked\" condition,\n * letting the caller render a confirm dialog and call confirmLeave /\n * cancelLeave.\n * 3. Adds a `beforeunload` listener so browser refresh / tab-close\n * surfaces the native \"Leave site?\" dialog when ANY form is dirty.\n *\n * Pass a getter function (`() => isDirty`) rather than a boolean when the\n * dirty state changes inside the same event handler that triggers\n * navigation — `useBlocker` evaluates the predicate lazily, so a getter\n * reading from a `useRef` sees the synchronously-set value before React\n * has a chance to re-render.\n */\nexport function useDraft(isDirty: IsDirtyInput): UseDraftReturn {\n const formId = useId();\n const markDirty = useDraftStore((s) => s.markDirty);\n const markClean = useDraftStore((s) => s.markClean);\n\n const isDirtyNow = typeof isDirty === \"function\" ? isDirty() : isDirty;\n\n useEffect(() => {\n if (isDirtyNow) {\n markDirty(formId);\n } else {\n markClean(formId);\n }\n return () => markClean(formId);\n }, [isDirtyNow, formId, markDirty, markClean]);\n\n const shouldBlockFn = useCallback(() => {\n return typeof isDirty === \"function\" ? isDirty() : isDirty;\n }, [isDirty]);\n\n const { status, proceed, reset } = useBlocker({\n shouldBlockFn,\n withResolver: true,\n });\n\n useEffect(() => {\n function handleBeforeUnload(e: BeforeUnloadEvent) {\n if (useDraftStore.getState().isDirty) {\n e.preventDefault();\n }\n }\n window.addEventListener(\"beforeunload\", handleBeforeUnload);\n return () => window.removeEventListener(\"beforeunload\", handleBeforeUnload);\n }, []);\n\n return {\n showDialog: status === \"blocked\",\n confirmLeave: () => proceed?.(),\n cancelLeave: () => reset?.(),\n };\n}\n","import { create } from \"zustand\";\n\nexport interface DraftState {\n /** Set of form IDs currently flagged as dirty. */\n dirtyForms: Set<string>;\n /** True when at least one form is dirty. */\n isDirty: boolean;\n /** Flag the given form ID as dirty. Idempotent. */\n markDirty: (formId: string) => void;\n /** Clear the given form ID. Idempotent. */\n markClean: (formId: string) => void;\n /** Drop every dirty flag. Useful in tests / global logout. */\n reset: () => void;\n}\n\n/**\n * Global store of dirty form IDs. Any component can subscribe with\n * `useDraftStore((s) => s.isDirty)` to react to the aggregate state — e.g.\n * an upload-in-progress indicator that lives outside the form tree.\n */\nexport const useDraftStore = create<DraftState>()((set) => ({\n dirtyForms: new Set<string>(),\n isDirty: false,\n markDirty: (formId) =>\n set((state) => {\n if (state.dirtyForms.has(formId)) return state;\n const next = new Set(state.dirtyForms);\n next.add(formId);\n return { dirtyForms: next, isDirty: next.size > 0 };\n }),\n markClean: (formId) =>\n set((state) => {\n if (!state.dirtyForms.has(formId)) return state;\n const next = new Set(state.dirtyForms);\n next.delete(formId);\n return { dirtyForms: next, isDirty: next.size > 0 };\n }),\n reset: () => set({ dirtyForms: new Set<string>(), isDirty: false }),\n}));\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAA8C;AAC9C,0BAA2B;;;ACD3B,qBAAuB;AAoBhB,IAAM,oBAAgB,uBAAmB,EAAE,CAAC,SAAS;AAAA,EAC1D,YAAY,oBAAI,IAAY;AAAA,EAC5B,SAAS;AAAA,EACT,WAAW,CAAC,WACV,IAAI,CAAC,UAAU;AACb,QAAI,MAAM,WAAW,IAAI,MAAM,EAAG,QAAO;AACzC,UAAM,OAAO,IAAI,IAAI,MAAM,UAAU;AACrC,SAAK,IAAI,MAAM;AACf,WAAO,EAAE,YAAY,MAAM,SAAS,KAAK,OAAO,EAAE;AAAA,EACpD,CAAC;AAAA,EACH,WAAW,CAAC,WACV,IAAI,CAAC,UAAU;AACb,QAAI,CAAC,MAAM,WAAW,IAAI,MAAM,EAAG,QAAO;AAC1C,UAAM,OAAO,IAAI,IAAI,MAAM,UAAU;AACrC,SAAK,OAAO,MAAM;AAClB,WAAO,EAAE,YAAY,MAAM,SAAS,KAAK,OAAO,EAAE;AAAA,EACpD,CAAC;AAAA,EACH,OAAO,MAAM,IAAI,EAAE,YAAY,oBAAI,IAAY,GAAG,SAAS,MAAM,CAAC;AACpE,EAAE;;;ADHK,SAAS,SAAS,SAAuC;AAC9D,QAAM,aAAS,oBAAM;AACrB,QAAM,YAAY,cAAc,CAAC,MAAM,EAAE,SAAS;AAClD,QAAM,YAAY,cAAc,CAAC,MAAM,EAAE,SAAS;AAElD,QAAM,aAAa,OAAO,YAAY,aAAa,QAAQ,IAAI;AAE/D,8BAAU,MAAM;AACd,QAAI,YAAY;AACd,gBAAU,MAAM;AAAA,IAClB,OAAO;AACL,gBAAU,MAAM;AAAA,IAClB;AACA,WAAO,MAAM,UAAU,MAAM;AAAA,EAC/B,GAAG,CAAC,YAAY,QAAQ,WAAW,SAAS,CAAC;AAE7C,QAAM,oBAAgB,0BAAY,MAAM;AACtC,WAAO,OAAO,YAAY,aAAa,QAAQ,IAAI;AAAA,EACrD,GAAG,CAAC,OAAO,CAAC;AAEZ,QAAM,EAAE,QAAQ,SAAS,MAAM,QAAI,gCAAW;AAAA,IAC5C;AAAA,IACA,cAAc;AAAA,EAChB,CAAC;AAED,8BAAU,MAAM;AACd,aAAS,mBAAmB,GAAsB;AAChD,UAAI,cAAc,SAAS,EAAE,SAAS;AACpC,UAAE,eAAe;AAAA,MACnB;AAAA,IACF;AACA,WAAO,iBAAiB,gBAAgB,kBAAkB;AAC1D,WAAO,MAAM,OAAO,oBAAoB,gBAAgB,kBAAkB;AAAA,EAC5E,GAAG,CAAC,CAAC;AAEL,SAAO;AAAA,IACL,YAAY,WAAW;AAAA,IACvB,cAAc,MAAM,UAAU;AAAA,IAC9B,aAAa,MAAM,QAAQ;AAAA,EAC7B;AACF;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import * as zustand from 'zustand';
|
|
2
|
+
|
|
3
|
+
interface UseDraftReturn {
|
|
4
|
+
/** True when navigation is blocked and the consumer should render its confirm dialog. */
|
|
5
|
+
showDialog: boolean;
|
|
6
|
+
/** Proceed with the originally-attempted navigation. */
|
|
7
|
+
confirmLeave: () => void;
|
|
8
|
+
/** Stay on the current page, dismiss the dialog. */
|
|
9
|
+
cancelLeave: () => void;
|
|
10
|
+
}
|
|
11
|
+
type IsDirtyInput = boolean | (() => boolean);
|
|
12
|
+
/**
|
|
13
|
+
* Track unsaved-changes state for the current form and block navigation
|
|
14
|
+
* while it's dirty. The hook:
|
|
15
|
+
*
|
|
16
|
+
* 1. Reports this form's dirty state into the global draft store so other
|
|
17
|
+
* components (e.g. an upload indicator outside the form tree) can read
|
|
18
|
+
* the aggregate via `useDraftStore`.
|
|
19
|
+
* 2. Hooks TanStack Router's `useBlocker` (with `withResolver: true`) so
|
|
20
|
+
* in-app navigation pauses on a status === "blocked" condition,
|
|
21
|
+
* letting the caller render a confirm dialog and call confirmLeave /
|
|
22
|
+
* cancelLeave.
|
|
23
|
+
* 3. Adds a `beforeunload` listener so browser refresh / tab-close
|
|
24
|
+
* surfaces the native "Leave site?" dialog when ANY form is dirty.
|
|
25
|
+
*
|
|
26
|
+
* Pass a getter function (`() => isDirty`) rather than a boolean when the
|
|
27
|
+
* dirty state changes inside the same event handler that triggers
|
|
28
|
+
* navigation — `useBlocker` evaluates the predicate lazily, so a getter
|
|
29
|
+
* reading from a `useRef` sees the synchronously-set value before React
|
|
30
|
+
* has a chance to re-render.
|
|
31
|
+
*/
|
|
32
|
+
declare function useDraft(isDirty: IsDirtyInput): UseDraftReturn;
|
|
33
|
+
|
|
34
|
+
interface DraftState {
|
|
35
|
+
/** Set of form IDs currently flagged as dirty. */
|
|
36
|
+
dirtyForms: Set<string>;
|
|
37
|
+
/** True when at least one form is dirty. */
|
|
38
|
+
isDirty: boolean;
|
|
39
|
+
/** Flag the given form ID as dirty. Idempotent. */
|
|
40
|
+
markDirty: (formId: string) => void;
|
|
41
|
+
/** Clear the given form ID. Idempotent. */
|
|
42
|
+
markClean: (formId: string) => void;
|
|
43
|
+
/** Drop every dirty flag. Useful in tests / global logout. */
|
|
44
|
+
reset: () => void;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Global store of dirty form IDs. Any component can subscribe with
|
|
48
|
+
* `useDraftStore((s) => s.isDirty)` to react to the aggregate state — e.g.
|
|
49
|
+
* an upload-in-progress indicator that lives outside the form tree.
|
|
50
|
+
*/
|
|
51
|
+
declare const useDraftStore: zustand.UseBoundStore<zustand.StoreApi<DraftState>>;
|
|
52
|
+
|
|
53
|
+
export { type DraftState, type IsDirtyInput, type UseDraftReturn, useDraft, useDraftStore };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import * as zustand from 'zustand';
|
|
2
|
+
|
|
3
|
+
interface UseDraftReturn {
|
|
4
|
+
/** True when navigation is blocked and the consumer should render its confirm dialog. */
|
|
5
|
+
showDialog: boolean;
|
|
6
|
+
/** Proceed with the originally-attempted navigation. */
|
|
7
|
+
confirmLeave: () => void;
|
|
8
|
+
/** Stay on the current page, dismiss the dialog. */
|
|
9
|
+
cancelLeave: () => void;
|
|
10
|
+
}
|
|
11
|
+
type IsDirtyInput = boolean | (() => boolean);
|
|
12
|
+
/**
|
|
13
|
+
* Track unsaved-changes state for the current form and block navigation
|
|
14
|
+
* while it's dirty. The hook:
|
|
15
|
+
*
|
|
16
|
+
* 1. Reports this form's dirty state into the global draft store so other
|
|
17
|
+
* components (e.g. an upload indicator outside the form tree) can read
|
|
18
|
+
* the aggregate via `useDraftStore`.
|
|
19
|
+
* 2. Hooks TanStack Router's `useBlocker` (with `withResolver: true`) so
|
|
20
|
+
* in-app navigation pauses on a status === "blocked" condition,
|
|
21
|
+
* letting the caller render a confirm dialog and call confirmLeave /
|
|
22
|
+
* cancelLeave.
|
|
23
|
+
* 3. Adds a `beforeunload` listener so browser refresh / tab-close
|
|
24
|
+
* surfaces the native "Leave site?" dialog when ANY form is dirty.
|
|
25
|
+
*
|
|
26
|
+
* Pass a getter function (`() => isDirty`) rather than a boolean when the
|
|
27
|
+
* dirty state changes inside the same event handler that triggers
|
|
28
|
+
* navigation — `useBlocker` evaluates the predicate lazily, so a getter
|
|
29
|
+
* reading from a `useRef` sees the synchronously-set value before React
|
|
30
|
+
* has a chance to re-render.
|
|
31
|
+
*/
|
|
32
|
+
declare function useDraft(isDirty: IsDirtyInput): UseDraftReturn;
|
|
33
|
+
|
|
34
|
+
interface DraftState {
|
|
35
|
+
/** Set of form IDs currently flagged as dirty. */
|
|
36
|
+
dirtyForms: Set<string>;
|
|
37
|
+
/** True when at least one form is dirty. */
|
|
38
|
+
isDirty: boolean;
|
|
39
|
+
/** Flag the given form ID as dirty. Idempotent. */
|
|
40
|
+
markDirty: (formId: string) => void;
|
|
41
|
+
/** Clear the given form ID. Idempotent. */
|
|
42
|
+
markClean: (formId: string) => void;
|
|
43
|
+
/** Drop every dirty flag. Useful in tests / global logout. */
|
|
44
|
+
reset: () => void;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Global store of dirty form IDs. Any component can subscribe with
|
|
48
|
+
* `useDraftStore((s) => s.isDirty)` to react to the aggregate state — e.g.
|
|
49
|
+
* an upload-in-progress indicator that lives outside the form tree.
|
|
50
|
+
*/
|
|
51
|
+
declare const useDraftStore: zustand.UseBoundStore<zustand.StoreApi<DraftState>>;
|
|
52
|
+
|
|
53
|
+
export { type DraftState, type IsDirtyInput, type UseDraftReturn, useDraft, useDraftStore };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// src/use-draft.ts
|
|
2
|
+
import { useCallback, useEffect, useId } from "react";
|
|
3
|
+
import { useBlocker } from "@tanstack/react-router";
|
|
4
|
+
|
|
5
|
+
// src/store.ts
|
|
6
|
+
import { create } from "zustand";
|
|
7
|
+
var useDraftStore = create()((set) => ({
|
|
8
|
+
dirtyForms: /* @__PURE__ */ new Set(),
|
|
9
|
+
isDirty: false,
|
|
10
|
+
markDirty: (formId) => set((state) => {
|
|
11
|
+
if (state.dirtyForms.has(formId)) return state;
|
|
12
|
+
const next = new Set(state.dirtyForms);
|
|
13
|
+
next.add(formId);
|
|
14
|
+
return { dirtyForms: next, isDirty: next.size > 0 };
|
|
15
|
+
}),
|
|
16
|
+
markClean: (formId) => set((state) => {
|
|
17
|
+
if (!state.dirtyForms.has(formId)) return state;
|
|
18
|
+
const next = new Set(state.dirtyForms);
|
|
19
|
+
next.delete(formId);
|
|
20
|
+
return { dirtyForms: next, isDirty: next.size > 0 };
|
|
21
|
+
}),
|
|
22
|
+
reset: () => set({ dirtyForms: /* @__PURE__ */ new Set(), isDirty: false })
|
|
23
|
+
}));
|
|
24
|
+
|
|
25
|
+
// src/use-draft.ts
|
|
26
|
+
function useDraft(isDirty) {
|
|
27
|
+
const formId = useId();
|
|
28
|
+
const markDirty = useDraftStore((s) => s.markDirty);
|
|
29
|
+
const markClean = useDraftStore((s) => s.markClean);
|
|
30
|
+
const isDirtyNow = typeof isDirty === "function" ? isDirty() : isDirty;
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
if (isDirtyNow) {
|
|
33
|
+
markDirty(formId);
|
|
34
|
+
} else {
|
|
35
|
+
markClean(formId);
|
|
36
|
+
}
|
|
37
|
+
return () => markClean(formId);
|
|
38
|
+
}, [isDirtyNow, formId, markDirty, markClean]);
|
|
39
|
+
const shouldBlockFn = useCallback(() => {
|
|
40
|
+
return typeof isDirty === "function" ? isDirty() : isDirty;
|
|
41
|
+
}, [isDirty]);
|
|
42
|
+
const { status, proceed, reset } = useBlocker({
|
|
43
|
+
shouldBlockFn,
|
|
44
|
+
withResolver: true
|
|
45
|
+
});
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
function handleBeforeUnload(e) {
|
|
48
|
+
if (useDraftStore.getState().isDirty) {
|
|
49
|
+
e.preventDefault();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
window.addEventListener("beforeunload", handleBeforeUnload);
|
|
53
|
+
return () => window.removeEventListener("beforeunload", handleBeforeUnload);
|
|
54
|
+
}, []);
|
|
55
|
+
return {
|
|
56
|
+
showDialog: status === "blocked",
|
|
57
|
+
confirmLeave: () => proceed?.(),
|
|
58
|
+
cancelLeave: () => reset?.()
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export {
|
|
62
|
+
useDraft,
|
|
63
|
+
useDraftStore
|
|
64
|
+
};
|
|
65
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/use-draft.ts","../src/store.ts"],"sourcesContent":["import { useCallback, useEffect, useId } from \"react\";\nimport { useBlocker } from \"@tanstack/react-router\";\nimport { useDraftStore } from \"./store\";\n\nexport interface UseDraftReturn {\n /** True when navigation is blocked and the consumer should render its confirm dialog. */\n showDialog: boolean;\n /** Proceed with the originally-attempted navigation. */\n confirmLeave: () => void;\n /** Stay on the current page, dismiss the dialog. */\n cancelLeave: () => void;\n}\n\nexport type IsDirtyInput = boolean | (() => boolean);\n\n/**\n * Track unsaved-changes state for the current form and block navigation\n * while it's dirty. The hook:\n *\n * 1. Reports this form's dirty state into the global draft store so other\n * components (e.g. an upload indicator outside the form tree) can read\n * the aggregate via `useDraftStore`.\n * 2. Hooks TanStack Router's `useBlocker` (with `withResolver: true`) so\n * in-app navigation pauses on a status === \"blocked\" condition,\n * letting the caller render a confirm dialog and call confirmLeave /\n * cancelLeave.\n * 3. Adds a `beforeunload` listener so browser refresh / tab-close\n * surfaces the native \"Leave site?\" dialog when ANY form is dirty.\n *\n * Pass a getter function (`() => isDirty`) rather than a boolean when the\n * dirty state changes inside the same event handler that triggers\n * navigation — `useBlocker` evaluates the predicate lazily, so a getter\n * reading from a `useRef` sees the synchronously-set value before React\n * has a chance to re-render.\n */\nexport function useDraft(isDirty: IsDirtyInput): UseDraftReturn {\n const formId = useId();\n const markDirty = useDraftStore((s) => s.markDirty);\n const markClean = useDraftStore((s) => s.markClean);\n\n const isDirtyNow = typeof isDirty === \"function\" ? isDirty() : isDirty;\n\n useEffect(() => {\n if (isDirtyNow) {\n markDirty(formId);\n } else {\n markClean(formId);\n }\n return () => markClean(formId);\n }, [isDirtyNow, formId, markDirty, markClean]);\n\n const shouldBlockFn = useCallback(() => {\n return typeof isDirty === \"function\" ? isDirty() : isDirty;\n }, [isDirty]);\n\n const { status, proceed, reset } = useBlocker({\n shouldBlockFn,\n withResolver: true,\n });\n\n useEffect(() => {\n function handleBeforeUnload(e: BeforeUnloadEvent) {\n if (useDraftStore.getState().isDirty) {\n e.preventDefault();\n }\n }\n window.addEventListener(\"beforeunload\", handleBeforeUnload);\n return () => window.removeEventListener(\"beforeunload\", handleBeforeUnload);\n }, []);\n\n return {\n showDialog: status === \"blocked\",\n confirmLeave: () => proceed?.(),\n cancelLeave: () => reset?.(),\n };\n}\n","import { create } from \"zustand\";\n\nexport interface DraftState {\n /** Set of form IDs currently flagged as dirty. */\n dirtyForms: Set<string>;\n /** True when at least one form is dirty. */\n isDirty: boolean;\n /** Flag the given form ID as dirty. Idempotent. */\n markDirty: (formId: string) => void;\n /** Clear the given form ID. Idempotent. */\n markClean: (formId: string) => void;\n /** Drop every dirty flag. Useful in tests / global logout. */\n reset: () => void;\n}\n\n/**\n * Global store of dirty form IDs. Any component can subscribe with\n * `useDraftStore((s) => s.isDirty)` to react to the aggregate state — e.g.\n * an upload-in-progress indicator that lives outside the form tree.\n */\nexport const useDraftStore = create<DraftState>()((set) => ({\n dirtyForms: new Set<string>(),\n isDirty: false,\n markDirty: (formId) =>\n set((state) => {\n if (state.dirtyForms.has(formId)) return state;\n const next = new Set(state.dirtyForms);\n next.add(formId);\n return { dirtyForms: next, isDirty: next.size > 0 };\n }),\n markClean: (formId) =>\n set((state) => {\n if (!state.dirtyForms.has(formId)) return state;\n const next = new Set(state.dirtyForms);\n next.delete(formId);\n return { dirtyForms: next, isDirty: next.size > 0 };\n }),\n reset: () => set({ dirtyForms: new Set<string>(), isDirty: false }),\n}));\n"],"mappings":";AAAA,SAAS,aAAa,WAAW,aAAa;AAC9C,SAAS,kBAAkB;;;ACD3B,SAAS,cAAc;AAoBhB,IAAM,gBAAgB,OAAmB,EAAE,CAAC,SAAS;AAAA,EAC1D,YAAY,oBAAI,IAAY;AAAA,EAC5B,SAAS;AAAA,EACT,WAAW,CAAC,WACV,IAAI,CAAC,UAAU;AACb,QAAI,MAAM,WAAW,IAAI,MAAM,EAAG,QAAO;AACzC,UAAM,OAAO,IAAI,IAAI,MAAM,UAAU;AACrC,SAAK,IAAI,MAAM;AACf,WAAO,EAAE,YAAY,MAAM,SAAS,KAAK,OAAO,EAAE;AAAA,EACpD,CAAC;AAAA,EACH,WAAW,CAAC,WACV,IAAI,CAAC,UAAU;AACb,QAAI,CAAC,MAAM,WAAW,IAAI,MAAM,EAAG,QAAO;AAC1C,UAAM,OAAO,IAAI,IAAI,MAAM,UAAU;AACrC,SAAK,OAAO,MAAM;AAClB,WAAO,EAAE,YAAY,MAAM,SAAS,KAAK,OAAO,EAAE;AAAA,EACpD,CAAC;AAAA,EACH,OAAO,MAAM,IAAI,EAAE,YAAY,oBAAI,IAAY,GAAG,SAAS,MAAM,CAAC;AACpE,EAAE;;;ADHK,SAAS,SAAS,SAAuC;AAC9D,QAAM,SAAS,MAAM;AACrB,QAAM,YAAY,cAAc,CAAC,MAAM,EAAE,SAAS;AAClD,QAAM,YAAY,cAAc,CAAC,MAAM,EAAE,SAAS;AAElD,QAAM,aAAa,OAAO,YAAY,aAAa,QAAQ,IAAI;AAE/D,YAAU,MAAM;AACd,QAAI,YAAY;AACd,gBAAU,MAAM;AAAA,IAClB,OAAO;AACL,gBAAU,MAAM;AAAA,IAClB;AACA,WAAO,MAAM,UAAU,MAAM;AAAA,EAC/B,GAAG,CAAC,YAAY,QAAQ,WAAW,SAAS,CAAC;AAE7C,QAAM,gBAAgB,YAAY,MAAM;AACtC,WAAO,OAAO,YAAY,aAAa,QAAQ,IAAI;AAAA,EACrD,GAAG,CAAC,OAAO,CAAC;AAEZ,QAAM,EAAE,QAAQ,SAAS,MAAM,IAAI,WAAW;AAAA,IAC5C;AAAA,IACA,cAAc;AAAA,EAChB,CAAC;AAED,YAAU,MAAM;AACd,aAAS,mBAAmB,GAAsB;AAChD,UAAI,cAAc,SAAS,EAAE,SAAS;AACpC,UAAE,eAAe;AAAA,MACnB;AAAA,IACF;AACA,WAAO,iBAAiB,gBAAgB,kBAAkB;AAC1D,WAAO,MAAM,OAAO,oBAAoB,gBAAgB,kBAAkB;AAAA,EAC5E,GAAG,CAAC,CAAC;AAEL,SAAO;AAAA,IACL,YAAY,WAAW;AAAA,IACvB,cAAc,MAAM,UAAU;AAAA,IAC9B,aAAa,MAAM,QAAQ;AAAA,EAC7B;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@idevconn/use-draft",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Global unsaved-changes blocker for TanStack Router SPAs. Aggregates dirty state from every mounted form via a Zustand store and blocks both in-app navigation (router dialog) and browser refresh/close (native dialog).",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": "iDEVconn",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/iDEVconn/use-draft.git"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/iDEVconn/use-draft/issues"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/iDEVconn/use-draft#readme",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"react",
|
|
17
|
+
"tanstack-router",
|
|
18
|
+
"zustand",
|
|
19
|
+
"forms",
|
|
20
|
+
"unsaved-changes",
|
|
21
|
+
"navigation-blocker",
|
|
22
|
+
"dirty-state"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "./dist/index.cjs",
|
|
26
|
+
"module": "./dist/index.js",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"import": "./dist/index.js",
|
|
32
|
+
"require": "./dist/index.cjs"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist",
|
|
37
|
+
"README.md",
|
|
38
|
+
"LICENSE"
|
|
39
|
+
],
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsup",
|
|
42
|
+
"dev": "tsup --watch",
|
|
43
|
+
"test": "vitest run",
|
|
44
|
+
"test:watch": "vitest",
|
|
45
|
+
"lint": "eslint src",
|
|
46
|
+
"typecheck": "tsc --noEmit",
|
|
47
|
+
"prepublishOnly": "npm run typecheck && npm run test && npm run build"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"@tanstack/react-router": ">=1.100.0",
|
|
51
|
+
"react": ">=18.0.0",
|
|
52
|
+
"zustand": ">=4.0.0"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@changesets/cli": "^2.31.0",
|
|
56
|
+
"@tanstack/react-router": "^1.169.0",
|
|
57
|
+
"@testing-library/jest-dom": "^6.9.0",
|
|
58
|
+
"@testing-library/react": "^16.3.0",
|
|
59
|
+
"@types/react": "^19.0.0",
|
|
60
|
+
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
61
|
+
"@typescript-eslint/parser": "^8.0.0",
|
|
62
|
+
"eslint": "^9.0.0",
|
|
63
|
+
"jsdom": "^25.0.0",
|
|
64
|
+
"react": "^19.0.0",
|
|
65
|
+
"react-dom": "^19.0.0",
|
|
66
|
+
"tsup": "^8.3.0",
|
|
67
|
+
"typescript": "^5.5.0",
|
|
68
|
+
"vitest": "^4.1.7",
|
|
69
|
+
"zustand": "^5.0.0"
|
|
70
|
+
},
|
|
71
|
+
"publishConfig": {
|
|
72
|
+
"access": "public"
|
|
73
|
+
}
|
|
74
|
+
}
|