重設密碼有兩個畫面:一個用來索取連結,另一個由該連結進入,用來輸入新密碼。兩者都是只做一件事的卡片,完成後由結果取代原本的內容。和登入一樣,卡片是一個面板,欄位由表單排版。
索取連結
<div class="sheet container-inline w-full max-w(380) p-xl" data-example="password-reset-request"> <div data-reset-ask> <form class="form"> <header class="flex flex-col gap-xs"> <h1 class="heading heading-lg">Reset your password</h1> <p class="m(0) fg-text-muted">Enter the email you sign in with. We send a link that works for 30 minutes.</p> </header> <div class="field"> <label for="reset-email">Email</label> <input class="input" id="reset-email" type="email" name="email" autocomplete="username" placeholder="you@example.com" required data-error="reset-email-error"> <span class="field-error" id="reset-email-error">Enter an email address, such as you@example.com.</span> </div> <noscript> <p class="m(0) fg-text-muted">This form needs JavaScript to send. Write to <a class="link" href="mailto:support@atlas.dev">support@atlas.dev</a>instead.</p> </noscript> <button class="btn btn-primary" disabled>Send reset link</button> <p class="m(0) text-center fg-text-muted">Remembered it? <a class="link" href="#">Back to sign in</a> </p> </form> </div> <div data-reset-result hidden> <div class="flex flex-col gap-lg"> <header class="flex flex-col items-center gap-xs text-center"> <span class="icon-frame" aria-hidden="true"> <svg class="icon" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <rect x="2.5" y="5" width="15" height="10" rx="1.25" /> <path d="M3 6.5L9.8918 10.9305A0.2 0.2 0 0 0 10.1082 10.9305L17 6.5" /> </svg> </span> <h1 class="heading heading-lg mt-xs" tabindex="-1" data-reset-result-title>Check your email</h1> <p class="m(0) fg-text-muted">If an Atlas account uses <span class="fg-text-strong" data-reset-email></span>, a reset link is on its way. It works for 30 minutes.</p> </header> <div class="alert"> <div class="alert-body"> <p class="alert-description">Nothing after a few minutes? Look in the spam folder, then ask for the link again.</p> </div> </div> <button type="button" class="btn" data-reset-again>Use another email</button> <p class="m(0) text-center fg-text-muted"> <a class="link" href="#">Back to sign in</a> </p> </div> </div></div>/** * Asking for a reset link, with no mail to send. The browser checks the address: `required` and `type="email"` * are the rules. This module turns the button on, names the error from its field while it shows, and on a valid * submit swaps the form for the result, which reads the same whether or not an account has that address. * Nothing leaves the page: sending the link is the application's. * Returns the function that stops it and puts the form back. */export function mount(root) { const controller = new AbortController() const { signal } = controller const ask = root.querySelector('[data-reset-ask]') const form = ask.querySelector('.form') const email = form.elements.email const send = form.querySelector('.btn') const result = root.querySelector('[data-reset-result]') const title = result.querySelector('[data-reset-result-title]') /** The error is read with the field only while it shows: its id joins aria-describedby, and leaves with it. */ const describe = (invalid) => { if (invalid) email.setAttribute('aria-describedby', email.dataset.error) else email.removeAttribute('aria-describedby') } email.addEventListener('invalid', () => describe(true), { signal }) for (const type of ['input', 'change', 'blur']) { email.addEventListener(type, () => describe(!email.validity.valid && email.matches(':user-invalid')), { signal }) } const show = (sent) => { ask.hidden = sent result.hidden = !sent } // A submit event means the address passed; an invalid form never gets here. form.addEventListener('submit', (event) => { event.preventDefault() // The address is set as text, never as markup. result.querySelector('[data-reset-email]').textContent = email.value.trim() show(true) title.focus() }, { signal }) result.querySelector('[data-reset-again]').addEventListener('click', () => { show(false) email.focus() email.select() }, { signal }) send.disabled = false return () => { controller.abort() send.disabled = true show(false) }}區塊提供可直接複製的標記,並沿用所在頁面的文字顏色和大小。預覽在 body 上套用 fg-text-body text-sm。每個區塊都是尺寸容器,無論放在寬頁面、只有側邊欄寬的區域,或對話框中,都會依自身寬度重新排列。用置中版面配置將卡片放在頁面中央。
#組成
sheet w-full max-w(380) p-xl。 卡片在手機上填滿可用寬度,最寬為 380px,與通往這裡的登入卡片同寬。- 一個欄位。 電子郵件地址,設有
autocomplete="username",讓瀏覽器提供讀者用來登入的那一個。 - 在索取之前,先說明連結的有效時間。 「30 minutes」(30 分鐘)寫在標題下方的句子裡,結果中再出現一次。
- 結果不透露任何帳戶資訊。 「If an Atlas account uses this address」(如果有 Atlas 帳戶使用這個地址)對每個地址都成立。顯示「沒有這個帳戶」的畫面,等於告訴陌生人誰有帳戶。
- 結果中以高對比文字色重複顯示地址,讓打錯的字在還能修正時就被看見;「Use another email」(改用其他電子郵件)會把表單帶回來,並選取地址。
- 沒收到信時該怎麼做是一則中性提示訊息:先看垃圾郵件匣,再重新索取連結。
- 回到登入的入口在兩種狀態下都有,位於最後一行。
#新密碼
連結會開啟第二張卡片。由於欄位會隱藏輸入的內容,它要求輸入新密碼兩次,並說明這是誰的帳戶。規則是至少 12 個字元,與註冊卡片給的相同,寫在 field-hint 中,讓讀者在出錯前就能看見。
新密碼
<div class="sheet container-inline w-full max-w(380) p-xl" data-example="password-reset-new"> <div data-reset-ask> <form class="form"> <header class="flex flex-col gap-xs"> <h1 class="heading heading-lg">Choose a new password</h1> <p class="m(0) fg-text-muted">For <span class="fg-text-strong">yiching@example.com</span>. Your other devices are signed out when it changes.</p> </header> <input class="sr-only" type="email" name="email" autocomplete="username" value="yiching@example.com" readonly tabindex="-1" aria-hidden="true"> <div class="field"> <label for="reset-password">New password</label> <input class="input" id="reset-password" type="password" name="password" autocomplete="new-password" minlength="12" required aria-describedby="reset-password-hint" data-error="reset-password-error"> <span class="field-hint" id="reset-password-hint">At least 12 characters. A sentence works well.</span> <span class="field-error" id="reset-password-error">Use at least 12 characters.</span> </div> <div class="field"> <label for="reset-confirm">Confirm password</label> <input class="input" id="reset-confirm" type="password" name="confirm" autocomplete="new-password" required data-error="reset-confirm-error"> <span class="field-error" id="reset-confirm-error">Enter the same password again.</span> </div> <noscript> <p class="m(0) fg-text-muted">This form needs JavaScript to send. Write to <a class="link" href="mailto:support@atlas.dev">support@atlas.dev</a>instead.</p> </noscript> <button class="btn btn-primary" disabled>Update password</button> </form> </div> <div data-reset-result hidden> <div class="flex flex-col gap-lg"> <header class="flex flex-col items-center gap-xs text-center"> <span class="icon-frame icon-frame-success" aria-hidden="true"> <svg class="icon" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M3.5 10L7.8545 14.3545A0.2 0.2 0 0 0 8.1413 14.3504L16.5 5.5" /> </svg> </span> <h1 class="heading heading-lg mt-xs" tabindex="-1" data-reset-result-title>Password updated</h1> <p class="m(0) fg-text-muted">Your other devices are signed out. Sign in with the new password to go on.</p> </header> <a class="btn btn-primary" href="#">Continue to sign in</a> </div> </div></div>/** * Choosing a new password, with no account to change. The browser checks each field: `required` and * `minlength="12"` are the rules, and the second field is held to the first with a custom validity, so one * mechanism reports all three. This module turns the button on, names each error from its field while it shows, * and on a valid submit clears both passwords and shows the result. The passwords are never logged, stored or * put in a URL. Changing the account is the application's. * Returns the function that stops it, clears the fields and puts the form back. */export function mount(root) { const controller = new AbortController() const { signal } = controller const ask = root.querySelector('[data-reset-ask]') const form = ask.querySelector('.form') const { password, confirm } = form.elements const send = form.querySelector('.btn') const result = root.querySelector('[data-reset-result]') const title = result.querySelector('[data-reset-result-title]') const fields = [password, confirm] /** * A field's error is read with the field only while it shows: its id joins aria-describedby, after the hint, * when the browser reports the field, and leaves when the value is valid again. */ const describe = (control, invalid) => { const ids = (control.getAttribute('aria-describedby') ?? '').split(' ').filter(id => id && id !== control.dataset.error) if (invalid) ids.push(control.dataset.error) if (ids.length) control.setAttribute('aria-describedby', ids.join(' ')) else control.removeAttribute('aria-describedby') } const reported = (control) => !control.validity.valid && control.matches(':user-invalid') /** What the browser's own bubble says is what the line under the field says: one error has one wording. */ const mismatch = root.querySelector(`#${confirm.dataset.error}`).textContent.trim() /** The second field is valid only while it repeats the first. An empty one is left to `required`. */ const compare = () => { confirm.setCustomValidity(confirm.value && confirm.value !== password.value ? mismatch : '') describe(confirm, reported(confirm)) } for (const control of fields) { control.addEventListener('invalid', () => describe(control, true), { signal }) // Either field can end the mismatch, so both compare; an error that no longer holds leaves at once. control.addEventListener('input', compare, { signal }) for (const type of ['input', 'change', 'blur']) control.addEventListener(type, () => describe(control, reported(control)), { signal }) } const show = (done) => { ask.hidden = done result.hidden = !done } /** Empties both passwords and everything that was said about them. */ const clear = () => { form.reset() confirm.setCustomValidity('') for (const control of fields) describe(control, false) } // A submit event means every constraint passed, the match among them; an invalid form never gets here. form.addEventListener('submit', (event) => { event.preventDefault() clear() show(true) title.focus() }, { signal }) send.disabled = false return () => { controller.abort() send.disabled = true clear() show(false) }}- 兩個欄位都是
autocomplete="new-password",密碼管理工具才能據此建議密碼並儲存。另有一個唯讀的地址欄位,設有autocomplete="username",既看不見也不在 Tab 順序中,用來告訴密碼管理工具這組密碼屬於哪個帳戶。 - 兩次輸入一致,是和其他規則一樣的限制條件。 第二個欄位與第一個不同時,會帶有自訂的有效性訊息,因此瀏覽器回報不一致的方式與回報缺少值完全相同,顯示的也是同一個
field-error。 - 結果說明還有什麼跟著改變:其他裝置已登出。接著是一個按鈕,通往登入。
#哪些功能需要腳本
驗證規則由瀏覽器負責:required、type="email" 和 minlength="12"。樣式表依 :user-invalid 將欄位畫成無效狀態,而它只在讀者編輯過欄位或嘗試送出之後才會符合,因此頁面載入時不會有任何欄位變紅,值一旦有效,錯誤訊息也隨即消失。
每張卡片各有自己的模組,做的都是同樣幾件事。它啟用按鈕:按鈕在標記中是 disabled,因此背後沒有伺服器的表單不會被送到它所在的頁面。錯誤訊息顯示時,它把該欄位的錯誤訊息加入 aria-describedby,之後再移除。表單有效並送出時,它顯示結果,並將焦點移到結果的標題。
第二個模組還會在任一欄位每次觸發 input 時,以 setCustomValidity 比對兩組密碼,因此修正第一個欄位會立刻清除第二個欄位的錯誤。結果出現時它會重設表單:兩組密碼都從頁面上消失,而且從不會寫入 URL、主控台或儲存空間。
兩個模組都不會送出任何內容。在應用程式中,submit 處理常式會提交表單,並在伺服器回應時顯示結果;第二張卡片還必須處理已過期的連結,顯示 alert alert-danger 並提供重新索取的方式。沒有腳本時,欄位、標籤和提示都還在,按鈕維持停用,並由一行 <noscript> 提供可寫信的地址。
#無障礙
- 卡片標題是頁面的
<h1>。結果有自己的<h1>,帶有tabindex="-1",焦點會移到它上面,因此畫面的切換會被讀出,鍵盤操作也從答覆處接續。 - 每個欄位都有可見的標籤。提示從一開始就在欄位的
aria-describedby中;錯誤訊息只在顯示期間加入。 - 送出失敗時保留瀏覽器本身的行為:焦點移到第一個有誤的欄位,瀏覽器也會說明原因。
- 每則錯誤訊息都說明該怎麼做:「Use at least 12 characters.」(請使用至少 12 個字元。)、「Enter the same password again.」(請再輸入一次相同的密碼。)
- 按鈕只在腳本就緒之前停用,絕不因為表單尚未填完而停用。
- 不要加上大寫字母或符號的規則。讓密碼難以猜中的是長度,而讀者看不見的規則只能靠反覆嘗試來滿足。