ferst-core 0.16.0 → 0.17.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/NOTICE +63 -31
- package/bin/ferst-modules.mjs +17 -2
- package/components/BlockRenderer.astro +131 -129
- package/components/MembersAccount.astro +370 -0
- package/components/SiteHeader.astro +23 -1
- package/components/islands/BookMonth.tsx +95 -0
- package/components/islands/MembersAccount.tsx +392 -0
- package/components/recipes/Book.astro +1147 -926
- package/components/recipes/MembersContent.astro +176 -0
- package/content/blocks.ts +25 -6
- package/content/cookies.ts +14 -0
- package/content/recipe-questions.ts +81 -80
- package/examples/recipes.ts +1 -0
- package/integration.mjs +319 -253
- package/layouts/Base.astro +1 -0
- package/lib/book.ts +416 -378
- package/lib/members-account.ts +276 -0
- package/lib/members.ts +115 -0
- package/package.json +80 -72
- package/routes/account.astro +16 -0
- package/routes/cookie-policy.astro +28 -28
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* MembersAccount — the members' account page's content (`/account`, the Ferst Members module):
|
|
4
|
+
* signing in with an emailed code, and the member's own account. The page's title is built; the
|
|
5
|
+
* rest is a React island (components/islands/MembersAccount.tsx, docs/BLOCK-ARCHITECTURE.md §8),
|
|
6
|
+
* drawn in the browser only, since it all depends on who is visiting. The island brings no look of
|
|
7
|
+
* its own: this file styles it from the site's tokens, in light and dark, and with the site's field
|
|
8
|
+
* treatment (`data-field` on <html>).
|
|
9
|
+
*
|
|
10
|
+
* Used by the injected route (routes/account.astro) and the showcase.
|
|
11
|
+
*/
|
|
12
|
+
import MembersAccountIsland from './islands/MembersAccount.tsx';
|
|
13
|
+
import { ACCOUNT_TEXT } from '../lib/members-account';
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
<section class="m-account" aria-labelledby="m-account-title">
|
|
17
|
+
<h1 id="m-account-title" class="m-account__title">{ACCOUNT_TEXT.title}</h1>
|
|
18
|
+
<MembersAccountIsland client:only="react">
|
|
19
|
+
<p slot="fallback" class="m-account__wait">{ACCOUNT_TEXT.loading}</p>
|
|
20
|
+
</MembersAccountIsland>
|
|
21
|
+
<noscript><p class="m-account__notice">{ACCOUNT_TEXT.noScript}</p></noscript>
|
|
22
|
+
</section>
|
|
23
|
+
|
|
24
|
+
<style is:global>
|
|
25
|
+
/* The island's markup isn't the build's, so these rules are global, every one under .m-account. */
|
|
26
|
+
.m-account {
|
|
27
|
+
display: flex;
|
|
28
|
+
flex-direction: column;
|
|
29
|
+
gap: 1.25rem;
|
|
30
|
+
max-width: 34rem;
|
|
31
|
+
margin: 0 auto;
|
|
32
|
+
padding: clamp(2rem, 6vw, 4rem) 1rem;
|
|
33
|
+
color: var(--fg);
|
|
34
|
+
box-sizing: border-box;
|
|
35
|
+
}
|
|
36
|
+
.m-account__title {
|
|
37
|
+
margin: 0;
|
|
38
|
+
font-family: var(--font-heading);
|
|
39
|
+
font-size: clamp(1.75rem, 4vw, 2.25rem);
|
|
40
|
+
line-height: 1.2;
|
|
41
|
+
color: var(--fg);
|
|
42
|
+
}
|
|
43
|
+
.m-account__app,
|
|
44
|
+
.m-account__card {
|
|
45
|
+
display: flex;
|
|
46
|
+
flex-direction: column;
|
|
47
|
+
gap: 1.1rem;
|
|
48
|
+
}
|
|
49
|
+
.m-account__card {
|
|
50
|
+
padding: clamp(1.25rem, 4vw, 2rem);
|
|
51
|
+
border: var(--card-border, 1px solid var(--border));
|
|
52
|
+
border-radius: var(--radius);
|
|
53
|
+
background: var(--bg-surface);
|
|
54
|
+
box-shadow: var(--shadow-sm);
|
|
55
|
+
}
|
|
56
|
+
.m-account__step,
|
|
57
|
+
.m-account__part-title {
|
|
58
|
+
margin: 0;
|
|
59
|
+
font-family: var(--font-heading);
|
|
60
|
+
font-size: 1.25rem;
|
|
61
|
+
line-height: 1.3;
|
|
62
|
+
color: var(--fg);
|
|
63
|
+
overflow-wrap: anywhere;
|
|
64
|
+
}
|
|
65
|
+
.m-account__part-title {
|
|
66
|
+
font-size: 1.05rem;
|
|
67
|
+
}
|
|
68
|
+
.m-account__step:focus {
|
|
69
|
+
outline: none;
|
|
70
|
+
}
|
|
71
|
+
.m-account__step:focus-visible {
|
|
72
|
+
outline: 2px solid var(--gold);
|
|
73
|
+
outline-offset: 4px;
|
|
74
|
+
}
|
|
75
|
+
.m-account__intro,
|
|
76
|
+
.m-account__status,
|
|
77
|
+
.m-account__wait,
|
|
78
|
+
.m-account__back {
|
|
79
|
+
margin: 0;
|
|
80
|
+
line-height: 1.6;
|
|
81
|
+
}
|
|
82
|
+
.m-account__intro,
|
|
83
|
+
.m-account__wait {
|
|
84
|
+
color: var(--muted);
|
|
85
|
+
}
|
|
86
|
+
.m-account__status {
|
|
87
|
+
font-weight: 600;
|
|
88
|
+
}
|
|
89
|
+
.m-account__welcome {
|
|
90
|
+
margin: 0;
|
|
91
|
+
padding: 1rem 1.1rem;
|
|
92
|
+
border-inline-start: 4px solid var(--gold);
|
|
93
|
+
border-radius: var(--radius-sm);
|
|
94
|
+
background: var(--bg-section);
|
|
95
|
+
line-height: 1.6;
|
|
96
|
+
white-space: pre-line;
|
|
97
|
+
}
|
|
98
|
+
.m-account a {
|
|
99
|
+
color: var(--accent);
|
|
100
|
+
text-decoration: underline;
|
|
101
|
+
text-underline-offset: 0.15em;
|
|
102
|
+
}
|
|
103
|
+
.m-account a:focus-visible {
|
|
104
|
+
outline: 2px solid var(--gold);
|
|
105
|
+
outline-offset: 2px;
|
|
106
|
+
border-radius: 2px;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/* What just happened: told once, in the site's colours. */
|
|
110
|
+
.m-account__notice {
|
|
111
|
+
margin: 0;
|
|
112
|
+
padding: 0.85rem 1rem;
|
|
113
|
+
border: 1px solid var(--border);
|
|
114
|
+
border-radius: var(--radius-sm);
|
|
115
|
+
background: var(--bg-section);
|
|
116
|
+
color: var(--fg);
|
|
117
|
+
line-height: 1.5;
|
|
118
|
+
}
|
|
119
|
+
.m-account__notice--done {
|
|
120
|
+
border-color: var(--gold);
|
|
121
|
+
background: var(--gold-light);
|
|
122
|
+
}
|
|
123
|
+
.m-account__notice--error {
|
|
124
|
+
border-color: var(--fg);
|
|
125
|
+
border-inline-start-width: 4px;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/* Fields, as every form on the site has them (the ContactForm's treatment). */
|
|
129
|
+
.m-account__form {
|
|
130
|
+
display: flex;
|
|
131
|
+
flex-direction: column;
|
|
132
|
+
gap: 1rem;
|
|
133
|
+
}
|
|
134
|
+
.m-account__form--row {
|
|
135
|
+
flex-flow: row wrap;
|
|
136
|
+
align-items: flex-end;
|
|
137
|
+
}
|
|
138
|
+
.m-account__form--row .m-account__field {
|
|
139
|
+
flex: 1 1 14rem;
|
|
140
|
+
}
|
|
141
|
+
.m-account__field {
|
|
142
|
+
display: flex;
|
|
143
|
+
flex-direction: column;
|
|
144
|
+
gap: 0.4rem;
|
|
145
|
+
}
|
|
146
|
+
.m-account__label {
|
|
147
|
+
font-family: var(--font-body);
|
|
148
|
+
font-size: 0.9375rem;
|
|
149
|
+
font-weight: 600;
|
|
150
|
+
color: var(--fg);
|
|
151
|
+
}
|
|
152
|
+
.m-account__hint {
|
|
153
|
+
font-size: 0.875rem;
|
|
154
|
+
color: var(--muted);
|
|
155
|
+
line-height: 1.5;
|
|
156
|
+
}
|
|
157
|
+
.m-account__error {
|
|
158
|
+
margin: 0;
|
|
159
|
+
font-size: 0.9rem;
|
|
160
|
+
font-weight: 600;
|
|
161
|
+
color: var(--fg);
|
|
162
|
+
line-height: 1.5;
|
|
163
|
+
}
|
|
164
|
+
.m-account__error::before {
|
|
165
|
+
content: '! ';
|
|
166
|
+
color: var(--gold);
|
|
167
|
+
}
|
|
168
|
+
.m-account__input {
|
|
169
|
+
font: inherit;
|
|
170
|
+
font-size: 1rem;
|
|
171
|
+
color: var(--fg);
|
|
172
|
+
background: var(--field-bg, var(--bg-surface));
|
|
173
|
+
border: var(--field-border, 1px solid var(--border));
|
|
174
|
+
border-radius: var(--field-radius, var(--radius-sm));
|
|
175
|
+
padding: 0.7rem 0.85rem;
|
|
176
|
+
width: 100%;
|
|
177
|
+
box-sizing: border-box;
|
|
178
|
+
transition: border-color 0.15s ease, box-shadow 0.15s ease;
|
|
179
|
+
}
|
|
180
|
+
.m-account__input--code {
|
|
181
|
+
max-width: 14rem;
|
|
182
|
+
font-family: var(--font-mono);
|
|
183
|
+
font-size: 1.35rem;
|
|
184
|
+
letter-spacing: 0.3em;
|
|
185
|
+
font-variant-numeric: tabular-nums;
|
|
186
|
+
}
|
|
187
|
+
.m-account__input:focus {
|
|
188
|
+
outline: none;
|
|
189
|
+
border-color: var(--gold);
|
|
190
|
+
box-shadow: 0 0 0 3px var(--gold-light);
|
|
191
|
+
}
|
|
192
|
+
.m-account__input[aria-invalid='true'] {
|
|
193
|
+
border-color: var(--fg);
|
|
194
|
+
border-width: 2px;
|
|
195
|
+
}
|
|
196
|
+
html[data-field='filled'] .m-account__input {
|
|
197
|
+
background: var(--bg-section);
|
|
198
|
+
border-color: transparent;
|
|
199
|
+
}
|
|
200
|
+
html[data-field='underline'] .m-account__input {
|
|
201
|
+
background: transparent;
|
|
202
|
+
border: none;
|
|
203
|
+
border-bottom: 1.5px solid var(--border);
|
|
204
|
+
border-radius: 0;
|
|
205
|
+
padding-inline: 0;
|
|
206
|
+
}
|
|
207
|
+
html[data-field='underline'] .m-account__input:focus {
|
|
208
|
+
border-bottom-color: var(--gold);
|
|
209
|
+
box-shadow: none;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/* The terms box: React Aria's tick box (a real, visually hidden checkbox), drawn here. */
|
|
213
|
+
.m-account__agree {
|
|
214
|
+
display: flex;
|
|
215
|
+
flex-direction: column;
|
|
216
|
+
gap: 0.4rem;
|
|
217
|
+
}
|
|
218
|
+
.m-account__check {
|
|
219
|
+
display: inline-flex;
|
|
220
|
+
align-items: flex-start;
|
|
221
|
+
gap: 0.65rem;
|
|
222
|
+
line-height: 1.5;
|
|
223
|
+
cursor: pointer;
|
|
224
|
+
}
|
|
225
|
+
.m-account__box {
|
|
226
|
+
flex: none;
|
|
227
|
+
display: grid;
|
|
228
|
+
place-items: center;
|
|
229
|
+
width: 1.25rem;
|
|
230
|
+
height: 1.25rem;
|
|
231
|
+
margin-top: 0.1rem;
|
|
232
|
+
border: 2px solid var(--fg);
|
|
233
|
+
border-radius: calc(var(--radius-sm) * 0.5);
|
|
234
|
+
background: var(--bg-surface);
|
|
235
|
+
box-sizing: border-box;
|
|
236
|
+
}
|
|
237
|
+
.m-account__box svg {
|
|
238
|
+
width: 0.9rem;
|
|
239
|
+
height: 0.9rem;
|
|
240
|
+
fill: none;
|
|
241
|
+
stroke: var(--on-brand);
|
|
242
|
+
stroke-width: 3;
|
|
243
|
+
stroke-linecap: round;
|
|
244
|
+
stroke-linejoin: round;
|
|
245
|
+
visibility: hidden;
|
|
246
|
+
}
|
|
247
|
+
.m-account__check[data-selected] .m-account__box {
|
|
248
|
+
background: var(--gold);
|
|
249
|
+
border-color: var(--gold);
|
|
250
|
+
}
|
|
251
|
+
.m-account__check[data-selected] .m-account__box svg {
|
|
252
|
+
visibility: visible;
|
|
253
|
+
}
|
|
254
|
+
.m-account__check[data-focus-visible] .m-account__box {
|
|
255
|
+
outline: 2px solid var(--gold);
|
|
256
|
+
outline-offset: 2px;
|
|
257
|
+
}
|
|
258
|
+
.m-account__check[data-invalid] .m-account__box {
|
|
259
|
+
border-width: 3px;
|
|
260
|
+
}
|
|
261
|
+
.m-account__links {
|
|
262
|
+
display: flex;
|
|
263
|
+
flex-wrap: wrap;
|
|
264
|
+
gap: 0.25rem 1rem;
|
|
265
|
+
margin: 0 0 0 1.9rem;
|
|
266
|
+
font-size: 0.9rem;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/* Buttons: the main one filled in the site's accent; the others quieter. */
|
|
270
|
+
.m-account__row {
|
|
271
|
+
display: flex;
|
|
272
|
+
flex-wrap: wrap;
|
|
273
|
+
align-items: center;
|
|
274
|
+
gap: 0.75rem 1.25rem;
|
|
275
|
+
}
|
|
276
|
+
.m-account__go,
|
|
277
|
+
.m-account__plain,
|
|
278
|
+
.m-account__danger {
|
|
279
|
+
align-self: flex-start;
|
|
280
|
+
min-height: 44px;
|
|
281
|
+
padding: 0.6rem 1.25rem;
|
|
282
|
+
font: inherit;
|
|
283
|
+
font-family: var(--font-heading);
|
|
284
|
+
font-weight: 600;
|
|
285
|
+
border-radius: var(--radius-sm);
|
|
286
|
+
cursor: pointer;
|
|
287
|
+
box-sizing: border-box;
|
|
288
|
+
touch-action: manipulation;
|
|
289
|
+
}
|
|
290
|
+
.m-account__go {
|
|
291
|
+
border: 2px solid var(--gold);
|
|
292
|
+
background: var(--gold);
|
|
293
|
+
color: var(--on-brand);
|
|
294
|
+
}
|
|
295
|
+
.m-account__go[data-hovered] {
|
|
296
|
+
background: color-mix(in srgb, var(--gold) 85%, #000);
|
|
297
|
+
border-color: color-mix(in srgb, var(--gold) 85%, #000);
|
|
298
|
+
}
|
|
299
|
+
.m-account__plain {
|
|
300
|
+
border: 2px solid var(--fg);
|
|
301
|
+
background: transparent;
|
|
302
|
+
color: var(--fg);
|
|
303
|
+
}
|
|
304
|
+
.m-account__plain[data-hovered] {
|
|
305
|
+
border-color: var(--gold);
|
|
306
|
+
color: var(--gold);
|
|
307
|
+
}
|
|
308
|
+
.m-account__danger {
|
|
309
|
+
border: 2px solid var(--fg);
|
|
310
|
+
background: var(--fg);
|
|
311
|
+
color: var(--bg-surface);
|
|
312
|
+
}
|
|
313
|
+
.m-account__link {
|
|
314
|
+
padding: 0.25rem 0;
|
|
315
|
+
border: 0;
|
|
316
|
+
background: none;
|
|
317
|
+
color: var(--fg);
|
|
318
|
+
font: inherit;
|
|
319
|
+
text-decoration: underline;
|
|
320
|
+
text-underline-offset: 0.15em;
|
|
321
|
+
cursor: pointer;
|
|
322
|
+
}
|
|
323
|
+
.m-account__link--danger {
|
|
324
|
+
color: var(--muted);
|
|
325
|
+
}
|
|
326
|
+
.m-account__go[data-focus-visible],
|
|
327
|
+
.m-account__plain[data-focus-visible],
|
|
328
|
+
.m-account__danger[data-focus-visible],
|
|
329
|
+
.m-account__link[data-focus-visible] {
|
|
330
|
+
outline: 2px solid var(--gold);
|
|
331
|
+
outline-offset: 2px;
|
|
332
|
+
}
|
|
333
|
+
.m-account [data-disabled],
|
|
334
|
+
.m-account [data-pending] {
|
|
335
|
+
opacity: 0.55;
|
|
336
|
+
cursor: default;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
.m-account__part {
|
|
340
|
+
padding-top: 1.25rem;
|
|
341
|
+
border-top: 1px solid var(--border);
|
|
342
|
+
}
|
|
343
|
+
section.m-account__part {
|
|
344
|
+
display: flex;
|
|
345
|
+
flex-direction: column;
|
|
346
|
+
gap: 0.85rem;
|
|
347
|
+
}
|
|
348
|
+
.m-account__delete {
|
|
349
|
+
padding: 1rem 1.1rem;
|
|
350
|
+
border: 1px solid var(--border);
|
|
351
|
+
border-radius: var(--radius-sm);
|
|
352
|
+
background: var(--bg-section);
|
|
353
|
+
}
|
|
354
|
+
.m-account__sr {
|
|
355
|
+
position: absolute;
|
|
356
|
+
width: 1px;
|
|
357
|
+
height: 1px;
|
|
358
|
+
margin: -1px;
|
|
359
|
+
padding: 0;
|
|
360
|
+
overflow: hidden;
|
|
361
|
+
clip: rect(0 0 0 0);
|
|
362
|
+
white-space: nowrap;
|
|
363
|
+
border: 0;
|
|
364
|
+
}
|
|
365
|
+
@media (prefers-reduced-motion: reduce) {
|
|
366
|
+
.m-account__input {
|
|
367
|
+
transition: none;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
</style>
|
|
@@ -28,9 +28,12 @@ interface Props {
|
|
|
28
28
|
dropdown?: NavGroup;
|
|
29
29
|
/** The call-to-action button (navbarSettings.cta); null hides it. */
|
|
30
30
|
cta?: { label: string; href: string } | null;
|
|
31
|
+
/** The members' account link (the site has the Ferst Members module): "Account" in the build,
|
|
32
|
+
* then "Sign in" or the member's first name, from a small script (lib/members). */
|
|
33
|
+
account?: boolean;
|
|
31
34
|
}
|
|
32
35
|
|
|
33
|
-
const { path, headerClass, primaryLogoLight, primaryLogoDark, logoHeader = {}, brandAlt, primaryNav, dropdown, cta = { label: 'Contact', href: '/contact/' } } = Astro.props;
|
|
36
|
+
const { path, headerClass, primaryLogoLight, primaryLogoDark, logoHeader = {}, brandAlt, primaryNav, dropdown, cta = { label: 'Contact', href: '/contact/' }, account = false } = Astro.props;
|
|
34
37
|
// The logo's own size (read from the site's files at build), so its box keeps the image's
|
|
35
38
|
// shape before it loads; and the header's attributes for the logo's size and placement.
|
|
36
39
|
const logoSize = imageSizeOf(primaryLogoLight);
|
|
@@ -148,6 +151,9 @@ const primaryNavLongestMatch = Math.max(-1, ...primaryNavMatchLengths);
|
|
|
148
151
|
</a>
|
|
149
152
|
);
|
|
150
153
|
})}
|
|
154
|
+
{account && (
|
|
155
|
+
<a href="/account" class:list={['nav-link', 'nav-account', { active: path.startsWith('/account') }]} data-members-account>Account</a>
|
|
156
|
+
)}
|
|
151
157
|
<div class="nav-mobile-theme">
|
|
152
158
|
{cta && (
|
|
153
159
|
<Button
|
|
@@ -1032,6 +1038,22 @@ const primaryNavLongestMatch = Math.max(-1, ...primaryNavMatchLengths);
|
|
|
1032
1038
|
}
|
|
1033
1039
|
</style>
|
|
1034
1040
|
|
|
1041
|
+
<script>
|
|
1042
|
+
// The members' account link, on a site with Members: "Sign in", or the member's first name
|
|
1043
|
+
// (lib/members). Without the link it does nothing.
|
|
1044
|
+
import { MEMBERS_ME, accountLinkText, readMembersAnswer } from '../lib/members';
|
|
1045
|
+
const accountLink = document.querySelector<HTMLAnchorElement>('[data-members-account]');
|
|
1046
|
+
if (accountLink) {
|
|
1047
|
+
fetch(MEMBERS_ME, { credentials: 'same-origin', headers: { Accept: 'application/json' } })
|
|
1048
|
+
.then(readMembersAnswer)
|
|
1049
|
+
.then((answer) => {
|
|
1050
|
+
const text = accountLinkText(answer);
|
|
1051
|
+
if (text && accountLink.textContent !== text) accountLink.textContent = text;
|
|
1052
|
+
})
|
|
1053
|
+
.catch(() => { /* the build's "Account" stands */ });
|
|
1054
|
+
}
|
|
1055
|
+
</script>
|
|
1056
|
+
|
|
1035
1057
|
<script>
|
|
1036
1058
|
// ── Hamburger (mobile) ──
|
|
1037
1059
|
const toggle = document.querySelector('.nav-toggle') as HTMLButtonElement;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Book block's month (look: calendar; myai4-webdev #239): a React island (docs/BLOCK-ARCHITECTURE.md §8).
|
|
3
|
+
* A month grid from React Aria Components, the days with free times marked and named so to screen
|
|
4
|
+
* readers, earlier and later months by arrows or the keyboard. A view only: the block's own script reads
|
|
5
|
+
* the free times and runs the journey (hold, details, confirmation), as for its other looks.
|
|
6
|
+
*
|
|
7
|
+
* It talks to the block through the element the recipe places it in (`[data-book-month]`):
|
|
8
|
+
* - in: `host.bookMonth` (London dates: today, the last day bookings are taken, the month shown, its
|
|
9
|
+
* free days, the chosen day), and a `book:month-state` event whenever that changes;
|
|
10
|
+
* - out: `book:day` (a day chosen, 'YYYY-MM-DD') and `book:month` (another month shown, 'YYYY-MM').
|
|
11
|
+
* Dates are calendar dates (London's, worked out by the block), so a visitor's own time zone never
|
|
12
|
+
* moves a day. Styled wholly by the block's CSS from the site's tokens; nothing of its own.
|
|
13
|
+
*/
|
|
14
|
+
import { useContext, useEffect, useRef, useState } from 'react';
|
|
15
|
+
import { Button, Calendar, CalendarGrid, CalendarGridBody, CalendarGridHeader, CalendarHeaderCell, CalendarStateContext, Heading, I18nProvider } from 'react-aria-components';
|
|
16
|
+
import { useCalendarCell } from 'react-aria';
|
|
17
|
+
import { isSameMonth, parseDate, type CalendarDate } from '@internationalized/date';
|
|
18
|
+
|
|
19
|
+
export interface MonthState { today: string; max: string; month: string; free: string[]; value: string | null }
|
|
20
|
+
type Host = HTMLElement & { bookMonth?: MonthState };
|
|
21
|
+
|
|
22
|
+
/** One day: a button in the grid; a day with free times says so, one with none can't be chosen. */
|
|
23
|
+
function Day({ date, free, today }: { date: CalendarDate; free: Set<string>; today: string }) {
|
|
24
|
+
const state = useContext(CalendarStateContext)!;
|
|
25
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
26
|
+
const outside = !isSameMonth(date, state.visibleRange.start);
|
|
27
|
+
const { cellProps, buttonProps, isSelected, isFocused, formattedDate } = useCalendarCell({ date, isOutsideMonth: outside }, state, ref);
|
|
28
|
+
if (outside) return <td {...cellProps} />;
|
|
29
|
+
const day = date.toString();
|
|
30
|
+
const isFree = free.has(day);
|
|
31
|
+
const label = `${buttonProps['aria-label'] ?? day}${isFree ? ', times free' : ''}`;
|
|
32
|
+
const cls = ['b-book__cell', isFree && 'is-free', isSelected && 'is-selected', day === today && 'is-today', isFocused && 'is-focused'].filter(Boolean).join(' ');
|
|
33
|
+
return (
|
|
34
|
+
<td {...cellProps}>
|
|
35
|
+
<div {...buttonProps} aria-label={label} ref={ref} className={cls}>{formattedDate}</div>
|
|
36
|
+
</td>
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default function BookMonth() {
|
|
41
|
+
const root = useRef<HTMLDivElement>(null);
|
|
42
|
+
const [s, setS] = useState<MonthState | null>(null);
|
|
43
|
+
const [focus, setFocus] = useState<CalendarDate | null>(null);
|
|
44
|
+
const host = () => root.current?.closest<Host>('[data-book-month]') ?? null;
|
|
45
|
+
const tell = (name: string, detail: string) => host()?.dispatchEvent(new CustomEvent(name, { detail, bubbles: true }));
|
|
46
|
+
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
const h = host();
|
|
49
|
+
if (!h) return;
|
|
50
|
+
const read = () => setS(h.bookMonth ? { ...h.bookMonth } : null);
|
|
51
|
+
read();
|
|
52
|
+
h.addEventListener('book:month-state', read);
|
|
53
|
+
return () => h.removeEventListener('book:month-state', read);
|
|
54
|
+
}, []);
|
|
55
|
+
|
|
56
|
+
// The block moved the month (a first month with nothing free looks on to the next): follow it.
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
if (s && (!focus || focus.toString().slice(0, 7) !== s.month)) setFocus(parseDate(s.value && s.value.startsWith(s.month) ? s.value : `${s.month}-01`));
|
|
59
|
+
}, [s?.month, s?.value]);
|
|
60
|
+
|
|
61
|
+
if (!s || !focus) return <div ref={root} className="b-book__month-wait" />;
|
|
62
|
+
const free = new Set(s.free);
|
|
63
|
+
return (
|
|
64
|
+
<div ref={root}>
|
|
65
|
+
<I18nProvider locale="en-GB">
|
|
66
|
+
<Calendar
|
|
67
|
+
className="b-book__calendar"
|
|
68
|
+
aria-label="Choose a day"
|
|
69
|
+
firstDayOfWeek="mon"
|
|
70
|
+
minValue={parseDate(s.today)}
|
|
71
|
+
maxValue={parseDate(s.max)}
|
|
72
|
+
value={s.value ? parseDate(s.value) : null}
|
|
73
|
+
onChange={(d) => tell('book:day', d.toString())}
|
|
74
|
+
focusedValue={focus}
|
|
75
|
+
onFocusChange={(d) => {
|
|
76
|
+
setFocus(d);
|
|
77
|
+
const m = d.toString().slice(0, 7);
|
|
78
|
+
if (m !== s.month) tell('book:month', m);
|
|
79
|
+
}}
|
|
80
|
+
isDateUnavailable={(d) => !free.has(d.toString())}
|
|
81
|
+
>
|
|
82
|
+
<header className="b-book__month-head">
|
|
83
|
+
<Button slot="previous" className="b-book__month-nav" aria-label="Earlier month"><span aria-hidden="true">‹</span></Button>
|
|
84
|
+
<Heading className="b-book__month-title" />
|
|
85
|
+
<Button slot="next" className="b-book__month-nav" aria-label="Later month"><span aria-hidden="true">›</span></Button>
|
|
86
|
+
</header>
|
|
87
|
+
<CalendarGrid className="b-book__grid" weekdayStyle="short">
|
|
88
|
+
<CalendarGridHeader>{(day) => <CalendarHeaderCell className="b-book__weekday">{day}</CalendarHeaderCell>}</CalendarGridHeader>
|
|
89
|
+
<CalendarGridBody>{(date) => <Day date={date} free={free} today={s.today} />}</CalendarGridBody>
|
|
90
|
+
</CalendarGrid>
|
|
91
|
+
</Calendar>
|
|
92
|
+
</I18nProvider>
|
|
93
|
+
</div>
|
|
94
|
+
);
|
|
95
|
+
}
|