/
app.tsx
236 lines (215 loc) · 6.04 KB
/
app.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import React from "react";
import type {
unstable_Blocker as Blocker,
unstable_BlockerFunction as BlockerFunction,
} from "react-router-dom";
import {
createBrowserRouter,
createRoutesFromElements,
Form,
json,
Link,
Outlet,
Route,
RouterProvider,
useBeforeUnload,
unstable_useBlocker as useBlocker,
useLocation,
useNavigate,
} from "react-router-dom";
let router = createBrowserRouter(
createRoutesFromElements(
<Route path="/" element={<Layout />}>
<Route index element={<h2>Index</h2>} />
<Route path="one" element={<h2>One</h2>} />
<Route path="two" element={<h2>Two</h2>} />
<Route
path="three"
action={() => json({ ok: true })}
element={
<>
<h2>Three</h2>
<ImportantFormWithBlocker />
</>
}
/>
<Route
path="four"
action={() => json({ ok: true })}
element={
<>
<h2>Four</h2>
<ImportantFormWithPrompt />
</>
}
/>
<Route path="five" element={<h2>Five</h2>} />
</Route>
)
);
if (import.meta.hot) {
import.meta.hot.dispose(() => router.dispose());
}
export default function App() {
return <RouterProvider router={router} />;
}
function Layout() {
let [historyIndex, setHistoryIndex] = React.useState(
window.history.state?.idx
);
let location = useLocation();
// Expose the underlying history index in the UI for debugging
React.useEffect(() => {
setHistoryIndex(window.history.state?.idx);
}, [location]);
// Give us meaningful document titles for popping back/forward more than 1 entry
React.useEffect(() => {
document.title = location.pathname;
}, [location]);
return (
<>
<h1>Navigation Blocking Example</h1>
<nav>
<Link to="/">Index</Link>
<Link to="/one">One</Link>
<Link to="/two">Two</Link>
<Link to="/three">Three (Form with blocker)</Link>
<Link to="/four">Four (Form with prompt)</Link>
<Link to="/five">Five</Link>
<a href="https://remix.run">External link to Remix Docs</a>
</nav>
<p>
Current location (index): {location.pathname} ({historyIndex})
</p>
<Outlet />
</>
);
}
// You can abstract `useBlocker` to use the browser's `window.confirm` dialog to
// determine whether or not the user should navigate within the current origin.
// `useBlocker` can also be used in conjunction with `useBeforeUnload` to
// prevent navigation away from the current origin.
// IMPORTANT: There are edge cases with this behavior in which React Router
// cannot reliably access the correct location in the history stack. In such
// cases the user may attempt to stay on the page but the app navigates anyway,
// or the app may stay on the correct page but the browser's history stack gets
// out of whack. You should test your own implementation thoroughly to make sure
// the tradeoffs are right for your users.
function usePrompt(
message: string | null | undefined | false,
{
beforeUnload,
}: {
beforeUnload?: boolean;
} = {}
) {
let blocker = useBlocker(
React.useCallback(
() => (typeof message === "string" ? !window.confirm(message) : false),
[message]
)
);
let prevState = React.useRef(blocker.state);
React.useEffect(() => {
if (blocker.state === "blocked") {
blocker.reset();
}
prevState.current = blocker.state;
}, [blocker]);
useBeforeUnload(
React.useCallback(
(event) => {
if (beforeUnload && typeof message === "string") {
event.preventDefault();
event.returnValue = message;
}
},
[message, beforeUnload]
),
{ capture: true }
);
}
function ImportantFormWithBlocker() {
let [value, setValue] = React.useState("");
let isBlocked = value !== "";
let blocker = useBlocker(isBlocked);
// Reset the blocker if the user cleans the form
React.useEffect(() => {
if (blocker.state === "blocked" && !isBlocked) {
blocker.reset();
}
}, [blocker, isBlocked]);
return (
<>
<p>
Is the form dirty?{" "}
{isBlocked ? (
<span style={{ color: "red" }}>Yes</span>
) : (
<span style={{ color: "green" }}>No</span>
)}
</p>
<Form method="post">
<label>
Enter some important data:
<input
name="data"
value={value}
onChange={(e) => setValue(e.target.value)}
/>
</label>
<button type="submit">Save</button>
</Form>
{blocker ? <ConfirmNavigation blocker={blocker} /> : null}
</>
);
}
function ConfirmNavigation({ blocker }: { blocker: Blocker }) {
if (blocker.state === "blocked") {
return (
<>
<p style={{ color: "red" }}>
Blocked the last navigation to {blocker.location.pathname}
</p>
<button onClick={() => blocker.proceed?.()}>Let me through</button>
<button onClick={() => blocker.reset?.()}>Keep me here</button>
</>
);
}
if (blocker.state === "proceeding") {
return (
<p style={{ color: "orange" }}>Proceeding through blocked navigation</p>
);
}
return <p style={{ color: "green" }}>Blocker is currently unblocked</p>;
}
function ImportantFormWithPrompt() {
let [value, setValue] = React.useState("");
let isBlocked = value !== "";
usePrompt(isBlocked && "Are you sure you want to leave there buddy?", {
beforeUnload: true,
});
return (
<>
<p>
Is the form dirty?{" "}
{isBlocked ? (
<span style={{ color: "red" }}>Yes</span>
) : (
<span style={{ color: "green" }}>No</span>
)}
</p>
<Form method="post">
<label>
Enter some important data:
<input
name="data"
value={value}
onChange={(e) => setValue(e.target.value)}
/>
</label>
<button type="submit">Save</button>
</Form>
</>
);
}