wasdf
This commit is contained in:
1 parent
efecc500c4
commit
cd45c5d98f
24 files changed
+689
-281
No files matched your search
@@ -1,4 +1,5 @@
|
||||
---
|
||||
import Toast from '../Toast.astro';
|
||||
import { t, type Lang } from '../../i18n/ui';
|
||||
interface Props { lang: Lang }
|
||||
const { lang } = Astro.props;
|
||||
@@ -18,6 +19,7 @@ const f = ui.contact.form;
|
||||
|
||||
<form
|
||||
class="contact__form"
|
||||
id="contact-form"
|
||||
action="/contact"
|
||||
method="post"
|
||||
lang={lang}
|
||||
@@ -55,21 +57,7 @@ const f = ui.contact.form;
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<!-- The form's answer, as a toast in the lower right. Framed like the menu, on night. -->
|
||||
<div
|
||||
class="toast hud-frame"
|
||||
data-toast
|
||||
data-label-sending={f.toast.sending}
|
||||
data-label-sent={f.toast.sent}
|
||||
data-label-failed={f.toast.failed}
|
||||
>
|
||||
<div class="toast__head">
|
||||
<p class="toast__label"><span data-toast-label></span><span class="toast__dots" aria-hidden="true"><span>.</span><span>.</span><span>.</span></span></p>
|
||||
<button class="toast__close" type="button" data-toast-close>{f.toast.close}</button>
|
||||
</div>
|
||||
<p class="toast__message" role="status" aria-live="polite" data-toast-message></p>
|
||||
<span class="toast__timer" aria-hidden="true" data-toast-timer></span>
|
||||
</div>
|
||||
<Toast for="contact-form" labels={{ ...f.toast }} />
|
||||
</section>
|
||||
|
||||
<style>
|
||||
@@ -118,105 +106,6 @@ const f = ui.contact.form;
|
||||
|
||||
.contact__form[aria-busy='true'] .button { opacity: 0.6; cursor: progress; }
|
||||
|
||||
/* Toast: a night panel fixed in the lower right. The menu's corner crosses frame it; their arms
|
||||
reach out onto the page, so they take the menu's line colour, not the night one. */
|
||||
.toast {
|
||||
--frame-color: var(--hud-line);
|
||||
position: fixed;
|
||||
right: var(--gutter);
|
||||
bottom: var(--gutter);
|
||||
z-index: 20;
|
||||
width: min(26rem, calc(100% - 2 * var(--gutter)));
|
||||
display: grid;
|
||||
gap: var(--space-3);
|
||||
padding: var(--space-4) var(--space-5) var(--space-5);
|
||||
background: var(--night);
|
||||
color: var(--ink-on-night);
|
||||
/* Closed: lowered and slightly small, grown from its corner. Opening pops it up; closing
|
||||
just sinks back, quicker and without the overshoot. */
|
||||
opacity: 0;
|
||||
visibility: hidden;
|
||||
transform: translateY(var(--space-5)) scale(0.94);
|
||||
transform-origin: bottom right;
|
||||
transition:
|
||||
opacity var(--dur-fast) var(--ease),
|
||||
transform var(--dur-fast) var(--ease),
|
||||
visibility 0s linear var(--dur-fast);
|
||||
}
|
||||
.toast[data-open] {
|
||||
opacity: 1;
|
||||
visibility: visible;
|
||||
transform: none;
|
||||
transition:
|
||||
opacity var(--dur-fast) var(--ease),
|
||||
transform var(--dur-slow) var(--ease-pop);
|
||||
}
|
||||
.toast__head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: var(--space-4);
|
||||
}
|
||||
.toast__label,
|
||||
.toast__close {
|
||||
font-family: var(--font-mono);
|
||||
font-size: var(--step-micro);
|
||||
font-weight: var(--weight-medium);
|
||||
line-height: 1;
|
||||
letter-spacing: var(--track-caps);
|
||||
text-transform: uppercase;
|
||||
}
|
||||
.toast__label { color: var(--ink-on-night); }
|
||||
/* While sending, three dots after the label light up one after another. */
|
||||
.toast__dots { display: none; }
|
||||
.toast[data-state='sending'] .toast__dots { display: inline; }
|
||||
.toast__dots span { animation: calc(4 * var(--dur-slow)) steps(1, end) infinite; }
|
||||
.toast__dots span:nth-child(1) { animation-name: toast-dot-1; }
|
||||
.toast__dots span:nth-child(2) { animation-name: toast-dot-2; }
|
||||
.toast__dots span:nth-child(3) { animation-name: toast-dot-3; }
|
||||
.toast__close {
|
||||
color: var(--ink-muted-on-night);
|
||||
padding: var(--space-2);
|
||||
margin: calc(-1 * var(--space-2));
|
||||
transition: color var(--dur-fast) var(--ease);
|
||||
}
|
||||
.toast__close:hover { color: var(--ink-on-night); }
|
||||
.toast__close:focus-visible { outline-color: var(--ink-on-night); }
|
||||
.toast__message {
|
||||
font-size: var(--step-small);
|
||||
line-height: var(--leading-small);
|
||||
color: var(--ink-muted-on-night);
|
||||
}
|
||||
.toast__message:empty { display: none; }
|
||||
/* A hairline counts down the time until the toast leaves, longer for an error, which has more
|
||||
to read. Pointer or focus pauses it. */
|
||||
.toast__timer {
|
||||
position: absolute;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
height: var(--hud-line-width);
|
||||
background: var(--ink-muted-on-night);
|
||||
transform-origin: left;
|
||||
transform: scaleX(0);
|
||||
}
|
||||
.toast[data-open][data-state='sent'] .toast__timer { animation: toast-timer var(--dur-toast) linear forwards; }
|
||||
.toast[data-open][data-state='failed'] .toast__timer { animation: toast-timer var(--dur-toast-error) linear forwards; }
|
||||
.toast:hover .toast__timer,
|
||||
.toast:focus-within .toast__timer { animation-play-state: paused; }
|
||||
@keyframes toast-timer { from { transform: scaleX(1); } to { transform: scaleX(0); } }
|
||||
/* One shared cycle in four beats: none, one, two, three dots. */
|
||||
@keyframes toast-dot-1 { 0% { opacity: 0; } 25%, 100% { opacity: 1; } }
|
||||
@keyframes toast-dot-2 { 0% { opacity: 0; } 50%, 100% { opacity: 1; } }
|
||||
@keyframes toast-dot-3 { 0% { opacity: 0; } 75%, 100% { opacity: 1; } }
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.toast, .toast[data-open] { transition: none; transform: none; }
|
||||
.toast__dots span { animation: none; }
|
||||
/* The timer still runs, since it dismisses the toast, but it is not drawn. */
|
||||
.toast__timer { visibility: hidden; }
|
||||
}
|
||||
|
||||
@media (min-width: 48rem) {
|
||||
.contact__text { grid-column: 1 / span 5; }
|
||||
.contact__form { grid-column: 7 / span 6; }
|
||||
@@ -228,87 +117,9 @@ const f = ui.contact.form;
|
||||
</style>
|
||||
|
||||
<script>
|
||||
// The form sends in place and answers in a toast. Every send carries the answer to a
|
||||
// proof-of-work challenge (ALTCHA), worked out in the background from the moment the visitor
|
||||
// first touches the form. Without scripting the server refuses the post and redirects to
|
||||
// /contact/error, which points to the email address.
|
||||
import { solveChallenge, type Challenge } from 'altcha-lib';
|
||||
import { deriveKey } from 'altcha-lib/algorithms/web/pbkdf2';
|
||||
|
||||
// Sends in place and answers in the toast; see scripts/send-form.ts. Without scripting the
|
||||
// server refuses the post and redirects to /contact/error, which points to the email address.
|
||||
import { sendInPlace } from '../../scripts/send-form';
|
||||
const form = document.querySelector<HTMLFormElement>('[data-contact]');
|
||||
const toast = document.querySelector<HTMLElement>('[data-toast]');
|
||||
if (form && toast) {
|
||||
const label = toast.querySelector<HTMLElement>('[data-toast-label]')!;
|
||||
const message = toast.querySelector<HTMLElement>('[data-toast-message]')!;
|
||||
const timer = toast.querySelector<HTMLElement>('[data-toast-timer]')!;
|
||||
const close = toast.querySelector<HTMLButtonElement>('[data-toast-close]')!;
|
||||
const submit = form.querySelector<HTMLButtonElement>('[type="submit"]')!;
|
||||
|
||||
// One challenge per message: fetched and solved once, spent by the send that carries it.
|
||||
let proof: Promise<string | null> | null = null;
|
||||
const solve = async (): Promise<string | null> => {
|
||||
try {
|
||||
const res = await fetch('/contact/challenge', { headers: { Accept: 'application/json' } });
|
||||
if (!res.ok) return null;
|
||||
const challenge: Challenge = await res.json();
|
||||
const solution = await solveChallenge({ challenge, deriveKey });
|
||||
return solution ? btoa(JSON.stringify({ challenge, solution })) : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
const prepare = () => (proof ??= solve());
|
||||
form.addEventListener('focusin', prepare);
|
||||
|
||||
const show = (state: 'sending' | 'sent' | 'failed', text: string) => {
|
||||
toast.dataset.state = state;
|
||||
toast.setAttribute('data-open', '');
|
||||
// Restart the countdown for a second message in a row.
|
||||
timer.style.animation = 'none';
|
||||
void timer.offsetWidth;
|
||||
timer.style.animation = '';
|
||||
label.textContent = toast.dataset[`label${state[0].toUpperCase()}${state.slice(1)}`] ?? '';
|
||||
// Set on the next frame, so the now visible live region announces it.
|
||||
requestAnimationFrame(() => { message.textContent = text; });
|
||||
};
|
||||
const hide = () => {
|
||||
if (toast.contains(document.activeElement)) submit.focus();
|
||||
toast.removeAttribute('data-open');
|
||||
};
|
||||
|
||||
timer.addEventListener('animationend', hide);
|
||||
close.addEventListener('click', hide);
|
||||
toast.addEventListener('keydown', (e) => { if (e.key === 'Escape') hide(); });
|
||||
|
||||
form.addEventListener('submit', async (event) => {
|
||||
event.preventDefault();
|
||||
if (form.getAttribute('aria-busy') === 'true') return;
|
||||
form.setAttribute('aria-busy', 'true');
|
||||
show('sending', '');
|
||||
let outcome = 'failed';
|
||||
const altcha = await prepare();
|
||||
proof = null;
|
||||
if (altcha) {
|
||||
const body = new URLSearchParams(new FormData(form) as unknown as Record<string, string>);
|
||||
body.set('altcha', altcha);
|
||||
try {
|
||||
const res = await fetch(form.action, {
|
||||
method: 'POST',
|
||||
headers: { Accept: 'application/json' },
|
||||
body,
|
||||
});
|
||||
outcome = (await res.json()).outcome ?? 'failed';
|
||||
} catch {
|
||||
outcome = 'failed';
|
||||
}
|
||||
}
|
||||
if (outcome === 'sent') {
|
||||
form.reset();
|
||||
show('sent', form.dataset.sent ?? '');
|
||||
} else {
|
||||
show('failed', (outcome === 'invalid' ? form.dataset.invalid : form.dataset.failed) ?? '');
|
||||
}
|
||||
form.removeAttribute('aria-busy');
|
||||
});
|
||||
}
|
||||
if (form) sendInPlace(form);
|
||||
</script>
|
||||
Reference in new issue
Block a user