Magic link
Passwordless sign-in card with OAuth buttons, email typo suggestion and check-your-inbox state with Gmail/Outlook shortcuts.
pnpm dlx shadcn@latest add @haze/magic-link---
import { ArrowRight, ExternalLink, Mail, RotateCw, Sparkles } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
interface Props {
id?: string;
heading?: string;
class?: string;
}
const { id = "haze-magic-link", heading = "Sign in without a password", class: className } = Astro.props;
---
<section aria-labelledby={`${id}-title`} data-haze-magic-link class={cn("bg-background px-4 py-10 sm:px-6 sm:py-14", className)}>
<div class="mx-auto max-w-md rounded-2xl border border-border bg-card p-6 shadow-float sm:p-8">
<div data-ml-step="form">
<span class="inline-flex items-center gap-1.5 rounded-full border border-frame bg-muted px-2.5 py-0.5 text-xs font-medium text-foreground">
<Sparkles class="size-3.5" aria-hidden="true" />
Passwordless
</span>
<h2 id={`${id}-title`} class="mt-3 text-xl/7 font-semibold tracking-tight text-foreground">{heading}</h2>
<p class="mt-1 text-xs/5 text-muted-foreground">We will email a one-time magic sign-in link that expires in 10 minutes.</p>
<div class="mt-5 grid grid-cols-2 gap-2">
<button type="button" data-ml-oauth="Google" class="inline-flex h-9 cursor-pointer items-center justify-center rounded-lg border border-frame bg-field text-xs font-medium text-foreground hover:border-frame-strong hover:bg-accent focus-visible:outline-2 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">Continue with Google</button>
<button type="button" data-ml-oauth="GitHub" class="inline-flex h-9 cursor-pointer items-center justify-center rounded-lg border border-frame bg-field text-xs font-medium text-foreground hover:border-frame-strong hover:bg-accent focus-visible:outline-2 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">Continue with GitHub</button>
</div>
<form data-ml-form class="mt-5 grid gap-3">
<div class="grid gap-1.5">
<label for={`${id}-email`} class="text-xs font-medium text-foreground">Work email</label>
<input id={`${id}-email`} data-ml-email type="email" required value="nina@gmial.com" class="h-10 rounded-lg border border-frame-input bg-field px-3 text-sm text-foreground focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/24 focus-visible:outline-hidden transition-[color,background-color,border-color,box-shadow] duration-150 hover:border-frame-foreground" />
<p data-ml-typo class="text-xs text-warning">
Did you mean <button type="button" data-ml-fix="nina@gmail.com" class="cursor-pointer font-medium underline underline-offset-4 hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring">nina@gmail.com</button>?
</p>
</div>
<button type="submit" class="inline-flex h-10 cursor-pointer items-center justify-center gap-1.5 rounded-lg bg-primary text-sm font-medium text-primary-foreground hover:opacity-90 focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">
Send magic link
<ArrowRight class="size-4" aria-hidden="true" />
</button>
</form>
</div>
<div data-ml-step="sent" hidden class="space-y-4 text-center">
<span class="mx-auto grid size-12 place-items-center rounded-2xl border border-frame bg-muted text-muted-foreground" aria-hidden="true">
<Mail class="size-5" />
</span>
<h3 class="text-lg/7 font-semibold text-foreground">Check your inbox</h3>
<p class="text-xs/5 text-pretty text-muted-foreground">
We sent a one-time sign-in link to <strong data-ml-dest class="text-foreground">nina@gmail.com</strong>.
</p>
<div class="grid grid-cols-2 gap-2 pt-1">
<a href="#open-gmail" class="inline-flex h-9 items-center justify-center gap-1.5 rounded-lg border border-frame bg-muted/40 text-xs font-medium text-foreground hover:border-frame-strong hover:bg-accent focus-visible:outline-2 focus-visible:outline-ring">
Open Gmail
<ExternalLink class="size-3.5" aria-hidden="true" />
</a>
<a href="#open-outlook" class="inline-flex h-9 items-center justify-center gap-1.5 rounded-lg border border-frame bg-muted/40 text-xs font-medium text-foreground hover:border-frame-strong hover:bg-accent focus-visible:outline-2 focus-visible:outline-ring">
Open Outlook
<ExternalLink class="size-3.5" aria-hidden="true" />
</a>
</div>
<div class="flex items-center justify-center gap-3 pt-1 text-xs">
<button type="button" data-ml-resend class="inline-flex h-8 cursor-pointer items-center gap-1 rounded-md px-2 font-medium text-foreground hover:bg-accent focus-visible:outline-2 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">
<RotateCw class="size-3" aria-hidden="true" />
Resend link
</button>
<button type="button" data-ml-back class="inline-flex h-8 cursor-pointer items-center rounded-md px-2 text-muted-foreground hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring transition-[background-color,border-color,color,opacity,scale] duration-150 ease-snappy active:scale-[0.97] active:duration-75">Use another email</button>
</div>
</div>
<p data-ml-status role="status" class="mt-4 min-h-4 text-xs/4 text-muted-foreground"></p>
</div>
</section>
<script>
function initMagicLinks() {
document.querySelectorAll<HTMLElement>("[data-haze-magic-link]").forEach((root) => {
if (root.hasAttribute("data-enhanced")) return;
root.setAttribute("data-enhanced", "");
const email = root.querySelector<HTMLInputElement>("[data-ml-email]");
const typo = root.querySelector<HTMLElement>("[data-ml-typo]");
const stepForm = root.querySelector<HTMLElement>('[data-ml-step="form"]');
const stepSent = root.querySelector<HTMLElement>('[data-ml-step="sent"]');
const dest = root.querySelector<HTMLElement>("[data-ml-dest]");
const status = root.querySelector<HTMLElement>("[data-ml-status]");
root.querySelector<HTMLButtonElement>("[data-ml-fix]")?.addEventListener("click", (ev) => {
const fixed = (ev.currentTarget as HTMLButtonElement).dataset.mlFix ?? "";
if (email) email.value = fixed;
if (typo) typo.hidden = true;
if (status) status.textContent = `Updated email to ${fixed}.`;
});
email?.addEventListener("input", () => {
if (typo) typo.hidden = !/gmial\.com/i.test(email.value);
});
root.querySelector<HTMLFormElement>("[data-ml-form]")?.addEventListener("submit", (ev) => {
ev.preventDefault();
if (dest && email) dest.textContent = email.value;
if (stepForm) stepForm.hidden = true;
if (stepSent) stepSent.hidden = false;
if (status) status.textContent = `Magic link sent to ${email?.value ?? "your inbox"}.`;
});
root.querySelector<HTMLButtonElement>("[data-ml-resend]")?.addEventListener("click", () => {
if (status) status.textContent = "Sent a new magic link.";
});
root.querySelector<HTMLButtonElement>("[data-ml-back]")?.addEventListener("click", () => {
if (stepForm) stepForm.hidden = false;
if (stepSent) stepSent.hidden = true;
});
root.querySelectorAll<HTMLButtonElement>("[data-ml-oauth]").forEach((btn) => {
btn.addEventListener("click", () => {
if (status) status.textContent = `Demo OAuth sign-in with ${btn.dataset.mlOauth}.`;
});
});
});
}
initMagicLinks();
document.addEventListener("astro:page-load", initMagicLinks);
</script>When to use
- Offering passwordless email sign-in with one-click typo correction and inbox shortcuts
- Transitioning cleanly from email entry to a resend-ready check-your-inbox state
When not to use
- Requiring a 6-digit typed verification code instead of a link; use auth-login or otp-verification
Installation
Add the Haze registry to components.json once, then install Magic link with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/magic-linknpx shadcn@latest add @haze/magic-linkyarn dlx shadcn@latest add @haze/magic-linkbunx --bun shadcn@latest add @haze/magic-linkFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id | string | "haze-magic-link" | Unique prefix for email label and heading IDs. |
| heading | string | "Sign in without a password" | Card heading. |
| class | string | – | Extra classes on the outer section. |
Keyboard interactions
- Tab
- Move across OAuth buttons, email input, typo fix button and inbox actions
- EnterSpace
- Accept the typo fix, send the magic link or resend it
Accessibility
- The email typo suggestion uses an accessible inline button that updates the input directly and announces the fix.
- Sending, resending and switching back announce status via a polite role="status" message.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI block "Magic link" with `npx shadcn@latest add @haze/magic-link`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import MagicLink from "@/components/haze/blocks/magic-link.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/blocks/magic-link.md
Customize with real content
Adapt the Haze magic-link block for our product. Replace the demo values of these props with our real content: id, heading, class. Keep the layout, the heading level rules and these accessibility details: The email typo suggestion uses an accessible inline button that updates the input directly and announces the fix. Sending, resending and switching back announce status via a polite role="status" message. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using magic-link, check it fits: use it when offering passwordless email sign-in with one-click typo correction and inbox shortcuts; transitioning cleanly from email entry to a resend-ready check-your-inbox state. Avoid it when requiring a 6-digit typed verification code instead of a link; use auth-login or otp-verification. Alternatives: @haze/auth-login, @haze/auth-sign-in-card.
Dependencies
- npm: @lucide/astro
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.