diff --git a/public/css/app.css b/public/css/app.css index 3bdc382..ff435ed 100644 --- a/public/css/app.css +++ b/public/css/app.css @@ -313,10 +313,10 @@ a.btn:active { align-items: center; gap: 28px; margin: 0; - padding: 4px 30px; - background: #fff; - border: 1px solid var(--hope-border-2); - border-radius: 50px; + padding: 0; + background: transparent; + border: 0; + border-radius: 0; box-shadow: none; font-size: 18px; line-height: 1; @@ -1597,6 +1597,101 @@ article.shift-card { height: 1.15rem; } +html:has(#signup-dialog[open]) { + overflow: hidden; +} + +#signup-dialog { + overflow: hidden; +} + +.signup-dialog-card { + width: min(36rem, 100%); + max-height: calc(100dvh - 2rem); +} + +.signup-dialog-close { + position: absolute; + top: 0.85rem; + right: 0.85rem; + z-index: 2; +} + +#signup-dialog-body { + box-sizing: border-box; + overflow: hidden; + border-radius: 1.75rem; + max-height: inherit; + /* Keep the scrollbar off the curved corners. */ + padding: 1.35rem 0.55rem 1.35rem 0; + background: var(--hope-blue-100); + box-shadow: var(--hope-shadow); +} + +.signup-dialog-panel { + display: grid; + width: 100%; + max-height: calc(100dvh - 2rem - 2.7rem); + overflow: auto; + overscroll-behavior: contain; + background: transparent; + scrollbar-width: thin; + scrollbar-color: rgb(34 34 34 / 0.45) transparent; +} + +.signup-dialog-panel::-webkit-scrollbar { + width: 10px; +} + +.signup-dialog-panel::-webkit-scrollbar-track { + background: transparent; +} + +.signup-dialog-panel::-webkit-scrollbar-thumb { + background: rgb(34 34 34 / 0.35); + border: 2px solid var(--hope-blue-100); + border-radius: 999px; +} + +.signup-dialog-summary { + display: grid; + gap: 0.15rem; + padding: 0 3.4rem 0.15rem 1.35rem; +} + +.signup-dialog-title { + margin: 0; + font-size: 1.6rem; + overflow-wrap: anywhere; +} + +.signup-dialog-summary .location, +.signup-dialog-summary .when, +.signup-dialog-summary .status, +.signup-dialog-summary .spots { + margin: 0; +} + +.signup-dialog-note { + margin: 0; + padding: 0.35rem 1.35rem 1.35rem; +} + +.event-dialog .signup-dialog-panel .signup-form { + max-width: none; + max-height: none; + margin: 0; + overflow: visible; + background: transparent; + box-shadow: none; + border-radius: 0; +} + +.event-dialog .signup-dialog-panel .signup-form h2 { + padding-right: 0; + font-size: 1.25rem; +} + .confirm-dialog { width: 100%; max-width: none; diff --git a/public/js/signup-dialog.js b/public/js/signup-dialog.js new file mode 100644 index 0000000..eee1140 --- /dev/null +++ b/public/js/signup-dialog.js @@ -0,0 +1,275 @@ +(function () { + var dialog = document.getElementById('signup-dialog'); + var body = document.getElementById('signup-dialog-body'); + if (!dialog || !body || typeof dialog.showModal !== 'function') return; + + var requestId = 0; + var widgetId = null; + var turnstileLoading = null; + var lastTrigger = null; + + document.querySelectorAll('a[data-signup]').forEach(function (link) { + link.setAttribute('aria-haspopup', 'dialog'); + link.setAttribute('aria-controls', 'signup-dialog'); + }); + + document.addEventListener('click', function (event) { + var link = event.target && event.target.closest ? event.target.closest('a[data-signup]') : null; + if (!link) return; + if (event.defaultPrevented || event.button !== 0) return; + if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return; + var href = link.getAttribute('href'); + if (!href) return; + event.preventDefault(); + openSignup(link, href); + }); + + var closeButton = dialog.querySelector('[data-signup-close]'); + if (closeButton) closeButton.addEventListener('click', function () { dialog.close(); }); + + dialog.addEventListener('click', function (event) { + if (event.target === dialog) dialog.close(); + }); + + dialog.addEventListener('close', function () { + requestId += 1; + clearWidget(); + dialog.classList.remove('is-modal'); + dialog.removeAttribute('aria-busy'); + if (lastTrigger && typeof lastTrigger.focus === 'function') lastTrigger.focus(); + }); + + function openSignup(link, href) { + lastTrigger = link; + var id = ++requestId; + showLoading(); + if (!dialog.open) { + dialog.showModal(); + dialog.classList.add('is-modal'); + } + fetch(href, { credentials: 'same-origin', cache: 'no-store', headers: { Accept: 'text/html' } }) + .then(function (response) { + return response.text(); + }) + .then(function (html) { + if (id !== requestId) return; + fill(html, href); + }) + .catch(function () { + if (id !== requestId) return; + showFailure(href); + }); + } + + function showLoading() { + clearWidget(); + dialog.setAttribute('aria-busy', 'true'); + var panel = panelWithTitle('Sign up'); + var note = document.createElement('p'); + note.className = 'signup-dialog-note'; + note.setAttribute('role', 'status'); + note.textContent = 'Loading the signup form.'; + panel.appendChild(note); + body.replaceChildren(panel); + } + + function showFailure(href) { + clearWidget(); + dialog.removeAttribute('aria-busy'); + var panel = panelWithTitle('Sign up'); + var note = document.createElement('p'); + note.className = 'signup-dialog-note'; + note.textContent = 'The signup form could not be loaded. '; + var link = document.createElement('a'); + link.href = href; + link.textContent = 'Open the shift page'; + note.appendChild(link); + panel.appendChild(note); + body.replaceChildren(panel); + if (closeButton) closeButton.focus(); + } + + function fill(html, href) { + var doc = new DOMParser().parseFromString(html, 'text/html'); + var main = doc.querySelector('main'); + if (!main) { + showFailure(href); + return; + } + var form = main.querySelector('form.signup-form'); + var heading = main.querySelector('h1'); + clearWidget(); + dialog.removeAttribute('aria-busy'); + var panel = document.createElement('div'); + panel.className = 'signup-dialog-panel'; + var summary = document.createElement('div'); + summary.className = 'signup-dialog-summary'; + var title = document.createElement('h2'); + title.id = 'signup-dialog-label'; + title.className = 'signup-dialog-title'; + title.textContent = heading && heading.textContent ? heading.textContent.trim() : 'Sign up'; + summary.appendChild(title); + appendFrom(summary, main, 'p.location'); + appendFrom(summary, main, 'p.when'); + appendFrom(summary, main, 'p.status'); + appendFrom(summary, main, 'p.spots'); + var notice = main.querySelector('p.error'); + if (notice && (!form || !form.contains(notice))) summary.appendChild(document.importNode(notice, true)); + panel.appendChild(summary); + if (!form) { + var closed = document.createElement('p'); + closed.className = 'signup-dialog-note'; + var paragraph = main.querySelector('p'); + if (!main.querySelector('p.when') && paragraph) { + closed.textContent = paragraph.textContent.trim(); + } else { + closed.textContent = 'This shift is not open for signup.'; + } + panel.appendChild(closed); + body.replaceChildren(panel); + if (closeButton) closeButton.focus(); + return; + } + var liveForm = document.importNode(form, true); + liveForm.querySelectorAll('noscript').forEach(function (node) { node.remove(); }); + panel.appendChild(liveForm); + body.replaceChildren(panel); + bindForm(liveForm); + mountTurnstile(liveForm); + var field = firstField(liveForm); + if (field) field.focus(); + } + + function bindForm(form) { + form.addEventListener('submit', function (event) { + event.preventDefault(); + if (form.getAttribute('data-sending') === 'true') return; + var action = form.getAttribute('action'); + if (!action) return; + form.setAttribute('data-sending', 'true'); + var submit = form.querySelector('button[type="submit"]'); + if (submit) { + submit.disabled = true; + submit.textContent = 'Signing up…'; + } + var id = requestId; + fetch(action, { + method: 'POST', + body: new URLSearchParams(new FormData(form)), + credentials: 'same-origin', + cache: 'no-store', + headers: { Accept: 'text/html' }, + }) + .then(function (response) { + if (isConfirmed(response)) { + window.location.assign(response.url); + return null; + } + return response.text(); + }) + .then(function (html) { + if (html === null || id !== requestId) return; + fill(html, action); + }) + .catch(function () { + if (id !== requestId || !form.isConnected) return; + form.removeAttribute('data-sending'); + if (submit) { + submit.disabled = false; + submit.textContent = 'Sign up'; + } + var alert = form.querySelector('[data-signup-send-error]'); + if (!alert) { + alert = document.createElement('p'); + alert.className = 'error'; + alert.setAttribute('role', 'alert'); + alert.setAttribute('data-signup-send-error', ''); + form.insertBefore(alert, form.firstChild); + } + alert.textContent = 'The signup could not be sent. Please try again.'; + }); + }); + } + + function mountTurnstile(scope) { + var widget = scope.querySelector('.cf-turnstile'); + var sitekey = widget && widget.getAttribute('data-sitekey'); + if (!widget || !sitekey) return; + loadTurnstile() + .then(function () { + if (!widget.isConnected || !window.turnstile) return; + // turnstile.ready() throws when this script is inserted dynamically. + widgetId = window.turnstile.render(widget, { + sitekey: sitekey, + action: widget.getAttribute('data-action') || 'signup', + }); + }) + .catch(function () {}); + } + + function loadTurnstile() { + if (window.turnstile) return Promise.resolve(); + if (turnstileLoading) return turnstileLoading; + turnstileLoading = new Promise(function (resolve, reject) { + var script = document.createElement('script'); + script.src = 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit'; + script.onload = function () { resolve(); }; + script.onerror = function () { + turnstileLoading = null; + reject(new Error('Turnstile did not load.')); + }; + document.head.appendChild(script); + }); + return turnstileLoading; + } + + function clearWidget() { + if (widgetId !== null && window.turnstile && window.turnstile.remove) { + try { + window.turnstile.remove(widgetId); + } catch (err) { + // The widget node is already gone. + } + } + widgetId = null; + } + + function panelWithTitle(text) { + var panel = document.createElement('div'); + panel.className = 'signup-dialog-panel'; + var summary = document.createElement('div'); + summary.className = 'signup-dialog-summary'; + var title = document.createElement('h2'); + title.id = 'signup-dialog-label'; + title.className = 'signup-dialog-title'; + title.textContent = text; + summary.appendChild(title); + panel.appendChild(summary); + return panel; + } + + function appendFrom(parent, root, selector) { + var node = root.querySelector(selector); + if (node) parent.appendChild(document.importNode(node, true)); + } + + function firstField(form) { + var invalid = form.querySelector('[aria-invalid="true"]'); + if (invalid && typeof invalid.focus === 'function') return invalid; + var inputs = form.querySelectorAll('input, textarea, select'); + for (var i = 0; i < inputs.length; i += 1) { + var input = inputs[i]; + if (input.type === 'hidden' || input.tabIndex < 0) continue; + return input; + } + return null; + } + + function isConfirmed(response) { + try { + return /\/confirmed\/?$/.test(new URL(response.url, window.location.href).pathname); + } catch (err) { + return false; + } + } +})(); diff --git a/src/pages.tsx b/src/pages.tsx index fe975d0..6183720 100644 --- a/src/pages.tsx +++ b/src/pages.tsx @@ -543,10 +543,27 @@ function PublicSchedule(props: { ) : ( )} + ); } +function SignupDialog(props: { config: AppConfig }) { + return ( + <> + + + + '); expect(html).toContain('href="/css/app.css?v='); expect(html).toContain('href="/brand/tokens.css?v='); @@ -130,6 +133,7 @@ describe('public pages', () => { expect(calendar).toContain('Lane Wednesday'); expect(calendar).toContain('View shift'); expect(calendar).toContain(`href="/s/${ids.open}"`); + expect(calendar).toContain('data-signup=""'); expect(calendar).not.toContain('secret.person@example.invalid'); expect(calendar).not.toContain('SECRET-PERSON-NOTE'); expect(calendar).not.toContain('November 2026'); @@ -261,6 +265,7 @@ describe('public pages', () => { expect(body.prefetch[0].eagerness).toBe('moderate'); expect(body.prefetch[0].where.and[1].not.selector_matches).toContain('[data-confirm]'); expect(body.prefetch[0].where.and[1].not.selector_matches).toContain('[data-open-event]'); + expect(body.prefetch[0].where.and[1].not.selector_matches).toContain('[data-signup]'); }); it('prefixes routes and assets when the app is mounted at /volunteer', async () => { @@ -452,6 +457,7 @@ describe('public pages', () => { const openPane = html.slice(html.lastIndexOf('
', openStart)); expect(openPane).toContain('>Sign up'); expect(openPane).toContain('href="/s/'); + expect(openPane).toContain('data-signup=""'); }); it('shows an empty list when nothing is upcoming', async () => {