비밀번호 재설정은 두 화면으로 이루어집니다. 하나는 링크를 요청하는 화면이고, 다른 하나는 그 링크로 들어와 새 비밀번호를 받는 화면입니다. 각각은 한 가지 일만 하는 카드이며, 결과가 카드의 자리를 대신합니다. 로그인과 마찬가지로 카드는 시트이고, 필드는 폼으로 배치합니다.
링크 요청
<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) }}블록은 복사해서 사용하는 마크업입니다. 페이지의 텍스트 색상과 크기를 그대로 사용하며, 미리보기에서는 본문에 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>입니다. 결과에는tabindex="-1"을 지정한 자체<h1>이 있고 포커스가 그곳으로 이동하므로, 화면이 바뀐 것이 안내되고 키보드 조작이 답변에서 이어집니다. - 모든 필드에는 화면에 보이는 레이블이 있습니다. 힌트는 처음부터 필드의
aria-describedby에 들어 있고, 오류는 표시되는 동안에만 추가됩니다. - 제출에 실패하면 브라우저의 기본 동작을 그대로 따릅니다. 포커스가 잘못된 첫 번째 필드로 이동하고, 브라우저가 이유를 알려 줍니다.
- 각 오류는 무엇을 해야 하는지 알려 줍니다. “Use at least 12 characters.”(12자 이상 사용하세요.), “Enter the same password again.”(같은 비밀번호를 다시 입력하세요.)
- 버튼은 스크립트가 준비될 때까지만 비활성화되며, 폼이 단지 덜 채워졌다는 이유로 비활성화되는 일은 없습니다.
- 대문자나 기호에 관한 규칙은 추가하지 마세요. 비밀번호를 추측하기 어렵게 만드는 것은 길이이며, 독자가 볼 수 없는 규칙은 시행착오로 맞추게 됩니다.
#관련 항목
- 표면모든 컴포넌트의 바탕이 되는 트레이, 볼록한 표면, 오목한 영역의 높이 체계입니다.
- 폼열 너비에 맞춰 행, 섹션, 동작 버튼을 배치하는 폼 레이아웃입니다.
- 입력 필드표면을 파낸 듯한 텍스트 입력 필드와 레이블, 힌트, 오류, 입력 그룹.
- 버튼표면 위로 살짝 돌출되어 있으며, 포인터를 올리면 떠오르고 누르면 가라앉는 버튼입니다.
- 알림페이지의 콘텐츠 흐름 안에서 메시지를 전달하며, 필요에 따라 상태를 지정할 수 있습니다.
- 아이콘 프레임제목 옆에 아이콘을 크기에 맞게 담는 작은 오목한 영역입니다.
- 제목페이지의 기본 글자색으로 표시하는 제목과 제목 위 안내 문구, 아래 소개 문단입니다.
- 링크문장 속에서 주변 텍스트와 같은 색으로 표시되며 다른 위치로 이동하는 텍스트입니다.