-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
/
Copy pathApp.tsx
230 lines (206 loc) · 5.7 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
import * as React from "react";
import {
Routes,
Route,
Outlet,
Link,
useLocation,
useNavigate,
useParams,
} from "react-router-dom";
import { Dialog } from "@reach/dialog";
import "@reach/dialog/styles.css";
import { IMAGES, getImageById } from "./images";
export default function App() {
let location = useLocation();
// The `backgroundLocation` state is the location that we were at when one of
// the gallery links was clicked. If it's there, use it as the location for
// the <Routes> so we show the gallery in the background, behind the modal.
let state = location.state as { backgroundLocation?: Location };
return (
<div>
<h1>Modal Example</h1>
<p>
This is an example of how to create a contextual modal navigation with
React Router where the navigation path the user takes determines if the
page is rendered in the modal or not (popularized by pinterest,
instagram, and others in the 2010s). This type of modal is typically
used as a kind of "detail" view to focus on a particular object in a
collection (like a pinterest board) while not taking you completely out
of context of the parent page. But, when the same URL is visited
directly (rather than from the collection page) it renders as it's own
full page instead of in a modal.
</p>
<p>
In this example, notice how the URL updates when the modal opens (if you
are viewing the example in StackBlitz you may need to open in a new
browser window). Even though the URL is updated to the specific item in
the modal, the background page is still showing behind it.
</p>
<p>
Next, copy and paste the URL to a new browser tab and notice that it
shows that specific item not in a modal, but directly on the page. This
is the view that someone would see if they clicked on a link that you
sent them when you had the modal open. They don't have the context you
did when you opened the modal, so they don't see it in the context of
the background page.
</p>
<Routes location={state?.backgroundLocation || location}>
<Route path="/" element={<Layout />}>
<Route index element={<Home />} />
<Route path="gallery" element={<Gallery />} />
<Route path="/img/:id" element={<ImageView />} />
<Route path="*" element={<NoMatch />} />
</Route>
</Routes>
{/* Show the modal when a `backgroundLocation` is set */}
{state?.backgroundLocation && (
<Routes>
<Route path="/img/:id" element={<Modal />} />
</Routes>
)}
</div>
);
}
function Layout() {
return (
<div>
<nav>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/gallery">Gallery</Link>
</li>
</ul>
</nav>
<hr />
<Outlet />
</div>
);
}
function Home() {
return (
<div>
<h2>Home</h2>
<h3>Featured Images</h3>
<ul>
<li>
<Link to="/img/1">Image 1</Link>
</li>
<li>
<Link to="/img/2">Image 2</Link>
</li>
</ul>
</div>
);
}
function Gallery() {
let location = useLocation();
return (
<div style={{ padding: "0 24px" }}>
<h2>Gallery</h2>
<div
style={{
display: "grid",
gridTemplateColumns: "repeat(auto-fit, minmax(200px, 1fr))",
gap: "24px",
}}
>
{IMAGES.map((image) => (
<Link
key={image.id}
to={`/img/${image.id}`}
// This is the trick! Set the `backgroundLocation` in location state
// so that when we open the modal we still see the current page in
// the background.
state={{ backgroundLocation: location }}
>
<img
width={200}
height={200}
style={{
width: "100%",
aspectRatio: "1 / 1",
height: "auto",
borderRadius: "8px",
}}
src={image.src}
alt={image.title}
/>
</Link>
))}
</div>
</div>
);
}
function ImageView() {
let { id } = useParams<"id">();
let image = getImageById(Number(id));
if (!image) return <div>Image not found</div>;
return (
<div>
<h1>{image.title}</h1>
<img width={400} height={400} src={image.src} alt="" />
</div>
);
}
function Modal() {
let navigate = useNavigate();
let { id } = useParams<"id">();
let image = getImageById(Number(id));
let buttonRef = React.useRef<HTMLButtonElement>(null);
function onDismiss() {
navigate(-1);
}
if (!image) return null;
return (
<Dialog
aria-labelledby="label"
onDismiss={onDismiss}
initialFocusRef={buttonRef}
>
<div
style={{
display: "grid",
justifyContent: "center",
padding: "8px 8px",
}}
>
<h1 id="label" style={{ margin: 0 }}>
{image.title}
</h1>
<img
style={{
margin: "16px 0",
borderRadius: "8px",
width: "100%",
height: "auto",
}}
width={400}
height={400}
src={image.src}
alt=""
/>
<button
style={{ display: "block" }}
ref={buttonRef}
onClick={onDismiss}
>
Close
</button>
</div>
</Dialog>
);
}
function NoMatch() {
return (
<div>
<h2>Nothing to see here!</h2>
<p>
<Link to="/">Go to the home page</Link>
</p>
</div>
);
}