パスワードの再設定は 2 つの画面で成り立ちます。リンクを依頼する画面と、そのリンクから開く、新しいパスワードを受け取る画面です。どちらも役割が 1 つのカードで、結果がカードと入れ替わります。ログインと同じく、カードはシートで、入力欄はフォームで配置します。
リンクを依頼
<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 です。これは、このカードへの入り口になるログインカードの幅と同じです。- 入力欄は 1 つ。 メールアドレスの欄で、
autocomplete="username"を指定するため、ブラウザーは読み手がログインに使っているアドレスを候補に出します。 - リンクの有効期間を、依頼する前に伝えます。 「30 minutes」(30 分)は、タイトルの下の文と、結果の両方に書きます。
- 結果はアカウントについて何も明かしません。 「If an Atlas account uses this address」(このアドレスを使っている Atlas アカウントがあれば)は、どのアドレスに対しても真です。「そのようなアカウントはありません」と伝える画面は、誰がアカウントを持っているかを第三者に教えてしまいます。
- 結果では、アドレスを通常の文字色で繰り返します。 入力ミスに、まだ直せるうちに気づけます。「Use another email」(別のメールアドレスを使う)は、アドレスを選択した状態でフォームを戻します。
- 何も届かないときの対処は、ニュートラルなアラートです。 まず迷惑メールフォルダーを確認し、その次にリンクをもう一度依頼します。
- ログインに戻る手段を、どちらの状態でも最後の行に置きます。
#新しいパスワード
リンクを開くと 2 枚目のカードが表示されます。入力欄は入力した文字を隠すため、新しいパスワードを 2 回尋ね、誰のアカウントなのかも示します。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"を指定した読み取り専用のアドレス欄を、視覚的に隠してタブ順序からも外しておくと、そのパスワードがどのアカウントのものかをマネージャーに伝えられます。 - 一致の確認も、ほかと同じ制約の 1 つです。 2 つ目の入力欄は、1 つ目と異なる間はカスタムの検証メッセージを持ちます。そのためブラウザーは、値の未入力を報告するのとまったく同じように不一致を報告し、同じ
field-errorが表示されます。 - 結果は、ほかに何が変わったかを伝えます。 ほかのデバイスはログアウトされます。その後にボタンを 1 つ、ログインへの入り口として置きます。
#スクリプトが必要な処理
検証のルールはブラウザーのものです。required、type="email"、minlength="12" を指定します。スタイルシートは :user-invalid に基づいて入力欄を無効な状態として描画します。:user-invalid は読み手が入力欄を編集するか送信を試みた後にだけ一致するため、ページの読み込み時には何も赤くならず、値が有効になるとエラーはすぐに消えます。
各カードは専用のモジュールを持ち、どちらも同じいくつかのことを行います。ボタンを有効にします。ボタンはマークアップでは disabled なので、背後にサーバーのないフォームが、そのフォームを置いたページ自身へ送信されることはありません。エラーが表示されている間はその入力欄のエラーを aria-describedby に追加し、消えたら取り除きます。有効な送信では、結果を表示して、そのタイトルにフォーカスを移します。
2 つ目のモジュールはさらに、どちらかの入力欄で input が発生するたびに、setCustomValidity を使って 2 つのパスワードを比較します。そのため、1 つ目の入力欄を直すと、2 つ目のエラーはすぐに消えます。結果が表示されるときにはフォームをリセットします。どちらのパスワードもページから消え、URL、コンソール、ストレージに書き込まれることは決してありません。
どちらのモジュールも何も送信しません。アプリケーションでは、submit ハンドラーがフォームを送信し、サーバーが応答したら結果を表示します。2 枚目のカードでは、期限の切れたリンクにも対応する必要があり、alert alert-danger と、もう一度リンクを依頼する手段を示します。スクリプトがない場合も、入力欄、ラベル、ヒントはそろっています。ボタンは無効のままで、<noscript> の 1 行が連絡先のアドレスを示します。
#アクセシビリティ
- カードのタイトルは、ページの
<h1>です。結果にはtabindex="-1"を付けた専用の<h1>があり、フォーカスがそこへ移ります。そのため画面の切り替わりが読み上げられ、キーボード操作は回答から続けられます。 - すべての入力欄に、画面に表示されるラベルを付けます。ヒントは最初から入力欄の
aria-describedbyに含めます。エラーは、表示されている間だけそこに加わります。 - 送信に失敗したときは、ブラウザー本来の動作を保ちます。フォーカスは誤りのある最初の入力欄に移り、ブラウザーが理由を伝えます。
- どのエラーも、何をすればよいかを伝えます。「Use at least 12 characters.」(12 文字以上にしてください)、「Enter the same password again.」(同じパスワードをもう一度入力してください)です。
- ボタンを無効にするのはスクリプトの準備ができるまでだけで、フォームが未完成というだけでは決して無効にしません。
- 大文字や記号についての条件は加えません。パスワードを推測されにくくするのは長さです。読み手に見えない条件は、試行錯誤で満たすしかなくなります。
#関連項目
- サーフェストレイ、シート、ウェル。すべてのコンポーネントの土台となる、奥行きの異なる素材です。
- フォームフォームの幅に応じて行、セクション、操作ボタンを配置するレイアウト。
- 入力フィールド背景面より一段くぼんだテキスト入力フィールド。ラベル、ヒント、エラー、グループに対応します。
- ボタン面からわずかに浮いた操作要素です。ポインターを重ねるとさらに浮き上がり、押すと沈み込みます。
- アラートページの流れの中に表示するメッセージ。必要に応じてステータスを付けられます。
- アイコンフレーム見出しの横などで、アイコンを適切な大きさで収める小さなウェル。
- 見出しページの前景色で表示する見出しと、その上のラベル、下の導入文。
- リンク周囲の文と同じ文字色で表示する、移動先を持つテキストリンク。