diff --git a/examples/react/src/App.tsx b/examples/react/src/App.tsx index a6830ddcd..d35bc2830 100644 --- a/examples/react/src/App.tsx +++ b/examples/react/src/App.tsx @@ -55,7 +55,7 @@ function UnauthenticatedApp() {

Full Customization Demo

- A complete sign in flow with its own UI and three switchable themes, built on the ui-react hooks. + A complete sign in flow with its own UI and four switchable themes, built on the ui-react hooks.

diff --git a/examples/react/src/full-customization/components.tsx b/examples/react/src/full-customization/components.tsx index 7e449ad8e..d395b703e 100644 --- a/examples/react/src/full-customization/components.tsx +++ b/examples/react/src/full-customization/components.tsx @@ -156,7 +156,7 @@ export function PhoneFields({ country, onCountryChange, value, onChange }: Phone return (
-
+
+ {isPassword && ( + + )} +
+ {hint && ( +

+ {hint} +

+ )} +
+ ); +} + +/** The code entry used by phone sign in, MFA setup and the MFA challenge: a labelled six digit field. */ +export function ProCodeField({ value, onChange }: { value: string; onChange: (code: string) => void }) { + return ( + onChange(event.target.value.replace(/\D/g, "").slice(0, 6))} + autoFocus + /> + ); +} + +type ProPhoneFieldProps = { + country: CountryData; + onCountryChange: (country: CountryData) => void; + value: string; + onChange: (value: string) => void; +}; + +/** One bordered box holding the country picker and the number, as on Android and iOS. */ +export function ProPhoneField({ country, onCountryChange, value, onChange }: ProPhoneFieldProps) { + const id = useId(); + const countries = useCountries(); + + return ( +
+
+ +
+
+ +
+
+ ); +} + +type ProButtonProps = Omit, "className"> & { + variant?: "primary" | "outline" | "text"; + loading?: boolean; + icon?: ReactNode; +}; + +export function ProButton({ + variant = "primary", + loading, + disabled, + icon, + children, + type = "button", + ...props +}: ProButtonProps) { + return ( + + ); +} + +export function ProDivider({ children }: { children: ReactNode }) { + return
{children}
; +} + +export function ProError({ children }: { children: ReactNode }) { + if (!children) return null; + return ( +

+ + {children} +

+ ); +} + +/** "Don't have an account? Create account" */ +export function ProPrompt({ prompt, action, onClick }: { prompt: string; action: string; onClick: () => void }) { + return ( +

+ {prompt}{" "} + +

+ ); +} + +/** The terms line, from the policies configured on FirebaseUIProvider. */ +export function ProLegal({ lead }: { lead: string }) { + const policies = useContext(PolicyContext); + if (!policies) return null; + const link = (url: string | URL, label: string) => ( + + {label} + + ); + + return ( +

+ {lead} {link(policies.termsOfServiceUrl, "Terms of Service")} and{" "} + {link(policies.privacyPolicyUrl, "Privacy Policy")}. +

+ ); +} + +export function ProSection({ children }: { children: ReactNode }) { + return

{children}

; +} + +export function ProGroup({ children, label }: { children: ReactNode; label?: string }) { + return ( +
+ {children} +
+ ); +} + +export function ProSeparator({ inset = 48 }: { inset?: number }) { + return + + + Full Customization Demo + + A complete sign in flow built from this example's shadcn/ui components, with four switchable themes. + + + + + + + + {routes.map((route) => ( diff --git a/examples/shadcn/src/components/ui/dialog.tsx b/examples/shadcn/src/components/ui/dialog.tsx new file mode 100644 index 000000000..13adcd1e4 --- /dev/null +++ b/examples/shadcn/src/components/ui/dialog.tsx @@ -0,0 +1,121 @@ +import * as React from "react"; +import * as DialogPrimitive from "@radix-ui/react-dialog"; +import { XIcon } from "lucide-react"; + +import { cn } from "@/lib/utils"; + +function Dialog({ ...props }: React.ComponentProps) { + return ; +} + +function DialogTrigger({ ...props }: React.ComponentProps) { + return ; +} + +function DialogPortal({ ...props }: React.ComponentProps) { + return ; +} + +function DialogClose({ ...props }: React.ComponentProps) { + return ; +} + +function DialogOverlay({ className, ...props }: React.ComponentProps) { + return ( + + ); +} + +function DialogContent({ + className, + children, + showCloseButton = true, + ...props +}: React.ComponentProps & { + showCloseButton?: boolean; +}) { + return ( + + + + {children} + {showCloseButton && ( + + + Close + + )} + + + ); +} + +function DialogHeader({ className, ...props }: React.ComponentProps<"div">) { + return ( +
+ ); +} + +function DialogFooter({ className, ...props }: React.ComponentProps<"div">) { + return ( +
+ ); +} + +function DialogTitle({ className, ...props }: React.ComponentProps) { + return ( + + ); +} + +function DialogDescription({ className, ...props }: React.ComponentProps) { + return ( + + ); +} + +export { + Dialog, + DialogClose, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogOverlay, + DialogPortal, + DialogTitle, + DialogTrigger, +}; diff --git a/examples/shadcn/src/full-customization/assets/doggo-mascot.svg b/examples/shadcn/src/full-customization/assets/doggo-mascot.svg new file mode 100644 index 000000000..2991f829c --- /dev/null +++ b/examples/shadcn/src/full-customization/assets/doggo-mascot.svg @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/examples/shadcn/src/full-customization/assets/doggo-pattern.svg b/examples/shadcn/src/full-customization/assets/doggo-pattern.svg new file mode 100644 index 000000000..dadd187af --- /dev/null +++ b/examples/shadcn/src/full-customization/assets/doggo-pattern.svg @@ -0,0 +1,544 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/shadcn/src/full-customization/assets/grid-pattern.svg b/examples/shadcn/src/full-customization/assets/grid-pattern.svg new file mode 100644 index 000000000..1e4a5af88 --- /dev/null +++ b/examples/shadcn/src/full-customization/assets/grid-pattern.svg @@ -0,0 +1,90 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/shadcn/src/full-customization/assets/spark-blob.webp b/examples/shadcn/src/full-customization/assets/spark-blob.webp new file mode 100644 index 000000000..1b9e974e2 Binary files /dev/null and b/examples/shadcn/src/full-customization/assets/spark-blob.webp differ diff --git a/examples/shadcn/src/full-customization/components.tsx b/examples/shadcn/src/full-customization/components.tsx new file mode 100644 index 000000000..c447e10d9 --- /dev/null +++ b/examples/shadcn/src/full-customization/components.tsx @@ -0,0 +1,322 @@ +/** + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { createRef, useCallback, useState, type ComponentProps, type ReactNode } from "react"; +import { FirebaseError } from "firebase/app"; +import { REGEXP_ONLY_DIGITS } from "input-otp"; +import { FirebaseUIError, type CountryData } from "@firebase-oss/ui-core"; +import { GoogleLogo, useCountries, useRecaptchaVerifier, useUI } from "@firebase-oss/ui-react"; + +import { Button as ShadcnButton } from "@/components/ui/button"; +import { Card } from "@/components/ui/card"; +import { Input } from "@/components/ui/input"; +import { InputOTP, InputOTPGroup, InputOTPSlot } from "@/components/ui/input-otp"; +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; + +import mascot from "./assets/doggo-mascot.svg"; + +type PageProps = { + /** Shown in the Doggo welcome bubble, and read by screen readers in every theme. */ + title: string; + /** The line above the form. Spark and Grid use it as their visible heading. */ + prompt: ReactNode; + children: ReactNode; + /** Rendered pinned to the bottom of the page, below the form. */ + footer?: ReactNode; +}; + +export function Page({ title, prompt, children, footer }: PageProps) { + return ( + <> +
+
+ + Coffee Spark +
+ +

+ {title} +

+ +

{prompt}

+ {children} +
+
+ {footer} + + ); +} + +type TextFieldProps = Omit, "className"> & { + /** A Material Symbols icon name for the leading slot. */ + icon?: string; + invalid?: boolean; +}; + +export function TextField({ icon, invalid, type, placeholder, ...props }: TextFieldProps) { + const [revealed, setRevealed] = useState(false); + const isPassword = type === "password"; + + return ( +
+ {icon && ( + + )} + + {isPassword && ( + + )} +
+ ); +} + +type CodeFormProps = { + task: ReturnType; + onSubmit: (code: string) => Promise; + links?: ReactNode; + submitLabel?: string; +}; + +/** The six digit code step shared by phone sign in, SMS and TOTP enrollment, and the MFA challenge. */ +export function CodeForm({ task, onSubmit, links, submitLabel = "Verify" }: CodeFormProps) { + const [code, setCode] = useState(""); + + return ( +
{ + event.preventDefault(); + void task.run(() => onSubmit(code)); + }} + > + pasted.replace(/\D/g, "")} + value={code} + onChange={setCode} + aria-label="Verification code" + autoFocus + > + + {[0, 1, 2, 3, 4, 5].map((index) => ( + + ))} + + + {task.error} + {links && {links}} + + + +
+ ); +} + +type PhoneFieldsProps = { + country: CountryData; + onCountryChange: (country: CountryData) => void; + value: string; + onChange: (value: string) => void; +}; + +/** A country picker and phone number field, fed by the countryCodes behavior configured in initializeUI. */ +export function PhoneFields({ country, onCountryChange, value, onChange }: PhoneFieldsProps) { + const countries = useCountries(); + + return ( +
+
+ +
+ onChange(event.target.value)} + /> +
+ ); +} + +type ButtonProps = Omit, "className"> & { + variant?: "primary" | "secondary"; + loading?: boolean; +}; + +/** shadcn's default and secondary variants, which read --primary and --secondary from the theme. */ +export function Button({ variant = "primary", loading, disabled, children, type = "button", ...props }: ButtonProps) { + return ( + + {children} + + ); +} + +/** A row of buttons. `stacked` gives each button the full width, for choices with longer labels. */ +export function Actions({ children, stacked }: { children: ReactNode; stacked?: boolean }) { + return
{children}
; +} + +export function Links({ children }: { children: ReactNode }) { + return
{children}
; +} + +export function TextLink({ children, ...props }: Omit, "className" | "type">) { + return ( + + {children} + + ); +} + +export function ErrorText({ children }: { children: ReactNode }) { + if (!children) return null; + return ( +

+ {children} +

+ ); +} + +type ProviderButtonProps = Omit, "className" | "type"> & { + provider: "google" | "phone" | "guest"; +}; + +const providerIcons = { + google: , + phone: ( + + ), + guest: ( + + ), +}; + +export function ProviderButton({ provider, children, ...props }: ProviderButtonProps) { + return ( + + {providerIcons[provider]} + {children} + + ); +} + +/** + * useRecaptchaVerifier, plus a way to start over. A reCAPTCHA token is single use, so after each + * send (failed, or before a resend) the container is swapped for a new one with a fresh verifier. + */ +export function useResettableRecaptcha() { + // The ref and the element key change together, so each reset gets a new element and a new verifier. + const [slot, setSlot] = useState(() => ({ key: 0, ref: createRef() })); + const verifier = useRecaptchaVerifier(slot.ref); + + return { + verifier, + container:
, + reset: () => setSlot(({ key }) => ({ key: key + 1, ref: createRef() })), + }; +} + +/** + * Runs an auth call, tracking whether it is in flight and the message to show if it fails. + * + * The ui-react action hooks throw errors whose message is already translated. Calls made straight + * to firebase/auth throw a raw FirebaseError, which is translated here with FirebaseUIError. + */ +export function useAuthTask() { + const ui = useUI(); + const [pending, setPending] = useState(false); + const [error, setError] = useState(null); + + const run = useCallback( + async (task: () => Promise): Promise => { + setPending(true); + setError(null); + try { + return await task(); + } catch (e) { + if (e instanceof FirebaseError && !(e instanceof FirebaseUIError)) { + setError(new FirebaseUIError(ui, e).message); + } else if (!(e instanceof ReauthCancelledError)) { + setError(e instanceof Error ? e.message : String(e)); + } + return undefined; + } finally { + setPending(false); + } + }, + [ui] + ); + + return { pending, error, setError, run }; +} + +/** Thrown when the user dismisses the reauthentication dialog, which is a cancellation rather than a failure. */ +export class ReauthCancelledError extends Error { + constructor() { + super("Reauthentication cancelled"); + } +} diff --git a/examples/shadcn/src/full-customization/full-customization.css b/examples/shadcn/src/full-customization/full-customization.css new file mode 100644 index 000000000..46980dd41 --- /dev/null +++ b/examples/shadcn/src/full-customization/full-customization.css @@ -0,0 +1,750 @@ +/** + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * Full customization demo, shadcn edition. The screens are built from this example's shadcn/ui + * components and only the auth logic comes from @firebase-oss/ui-react. Each theme is a set of + * shadcn variables (--primary, --secondary, --radius, ...) taken from the "Firebase auth flows" + * Figma file (web Landing frames 20:6031, 20:7310 and 20:6675), plus a few --fc-* extras for what + * shadcn has no token for, such as hard offset shadows and per-theme fonts. + * + * The theme is set on (data-fc-theme) rather than the demo root, so shadcn's portalled + * pieces, the reauth dialog and the country picker menu, pick it up too. + */ + +@import url("https://fonts.googleapis.com/css2?family=Bagel+Fat+One&family=Chivo+Mono:wght@500&family=JetBrains+Mono:wght@400;500&family=Onest:wght@500;700&family=Roboto+Mono:wght@500&family=Roboto:wght@500;600&family=Rowdies:wght@300;400&display=swap"); +@import url("https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400..700,0..1,0&icon_names=alternate_email,arrow_back,coffee,dark_mode,light_mode,lock,palette,person,phone,visibility,visibility_off&display=block"); + +/* Doggo: Figma 20:6031 */ +body[data-fc-theme="doggo"] { + --background: #fff8f8; + --foreground: #211a1d; + --card: #fff8f8; + --card-foreground: #211a1d; + --popover: #ffffff; + --popover-foreground: #211a1d; + --primary: #864b6f; + --primary-foreground: #ffffff; + --secondary: #ffd8eb; + --secondary-foreground: #7b3b73; + --muted: #f9eaef; + --muted-foreground: #a08b95; + --accent: #ffd8eb; + --accent-foreground: #7b3b73; + --destructive: #ba1a1a; + --border: #d3c2c9; + --input: #d3c2c9; + --ring: #4c8bff; + --radius: 24px; + + --fc-page-art: radial-gradient(1019px 1177px at 50% 53%, #fff8f8 4%, #ffddb4 34%, #ffd8eb 74%); + --fc-main-top: 271px; + --fc-font: 500 16px/24px "Onest", sans-serif; + --fc-prompt-font: 500 16px/24px "Onest", sans-serif; + --fc-prompt-align: center; + --fc-link: var(--primary); + --fc-card-padding: 16px; + --fc-card-shadow: 3px 6px 0 #ffd8eb; + --fc-actions-gap: 16px; + --fc-actions-justify: center; + --fc-field-bg: #ffffff; + --fc-field-border-width: 1px; + --fc-field-icon-fill: 0; + --fc-field-icon-weight: 700; + --fc-button-width: 327px; + --fc-button-radius: 36px; + --fc-button-font: 700 20px/20px "Onest", sans-serif; + --fc-button-border: 0 solid transparent; + --fc-button-shadow: 2px 4px 0 #5d0b47; + --fc-footer-font: 500 16px/24px "Onest", sans-serif; + --fc-footer-color: var(--muted-foreground); + --fc-provider-radius: 80px; + --fc-provider-font: 500 14px/20px "Roboto", sans-serif; + --fc-provider-phone-bg: #894680; + --fc-provider-phone-color: #ffffff; + --fc-provider-guest-color: #202124; + --fc-provider-guest-border: 2px solid #81737c; +} + +/* Grid: Figma 20:7310 */ +body[data-fc-theme="grid"] { + --background: #f3fbfb; + --foreground: #2c1067; + --card: transparent; + --card-foreground: #2c1067; + --popover: #ffffff; + --popover-foreground: #2c1067; + --primary: #ff52b3; + --primary-foreground: #2c1067; + --secondary: #ffffff; + --secondary-foreground: #2c1067; + --muted: #e6f9f9; + --muted-foreground: #2c1067; + --accent: #00dce2; + --accent-foreground: #2c1067; + --destructive: #ba1a1a; + --border: #00dce2; + --input: #00dce2; + --ring: #ff52b3; + --radius: 16px; + + --fc-page-art: none; + --fc-main-top: 397px; + --fc-font: 500 16px/24px "Roboto Mono", monospace; + --fc-prompt-font: 300 22px/31px "Rowdies", sans-serif; + --fc-prompt-align: left; + --fc-link: var(--foreground); + --fc-card-padding: 0; + --fc-card-shadow: none; + /* The Figma text-field instance is 64px tall around the 56px field, so the gap reads as 48px. */ + --fc-actions-gap: 48px; + --fc-actions-justify: center; + --fc-field-bg: #ffffff; + --fc-field-border-width: 2px; + --fc-field-icon-fill: 1; + --fc-field-icon-weight: 400; + --fc-button-width: 332px; + --fc-button-radius: 24px; + --fc-button-font: 400 32px/20px "Rowdies", sans-serif; + --fc-button-border: 2px solid #00dce2; + --fc-button-shadow: 3px 6px 0 #00dce2; + --fc-footer-font: 500 16px/24px "JetBrains Mono", monospace; + --fc-footer-color: #000000; + --fc-provider-radius: 12px; + --fc-provider-font: 500 14px/20px "JetBrains Mono", monospace; + --fc-provider-phone-font: 500 14px/20px "Chivo Mono", monospace; + --fc-provider-phone-bg: #00dce2; + --fc-provider-phone-color: #2c1067; + --fc-provider-guest-color: #ff52b3; + --fc-provider-guest-border: 2px solid #ff52b3; +} + +/* Coffee Spark: Figma 20:6675 */ +body[data-fc-theme="spark"] { + --background: #f1f1f1; + --foreground: #1f1f1f; + --card: transparent; + --card-foreground: #1f1f1f; + --popover: #fdfcfb; + --popover-foreground: #1f1f1f; + --primary: #fdadee; + --primary-foreground: #2c1067; + --secondary: #fdfcfb; + --secondary-foreground: #2c1067; + --muted: #e8e8e8; + --muted-foreground: #1f1f1f; + --accent: #fdadee; + --accent-foreground: #2c1067; + --destructive: #ba1a1a; + --border: #000000; + --input: #000000; + --ring: #fdadee; + --radius: 20px; + + --fc-page-art: none; + --fc-main-top: 186px; + --fc-font: 500 16px/24px "JetBrains Mono", monospace; + --fc-prompt-font: 400 22px/31px "JetBrains Mono", monospace; + --fc-prompt-align: left; + --fc-link: var(--foreground); + --fc-card-padding: 0; + --fc-card-shadow: none; + --fc-actions-gap: 40px; + --fc-actions-justify: flex-end; + --fc-field-bg: #fdfcfb; + --fc-field-border-width: 2px; + --fc-field-icon-fill: 1; + --fc-field-icon-weight: 400; + --fc-button-width: 332px; + --fc-button-radius: 48px; + --fc-button-font: 500 24px/20px "JetBrains Mono", monospace; + --fc-button-border: 2px solid #000000; + --fc-button-shadow: none; + --fc-footer-font: 500 16px/24px "JetBrains Mono", monospace; + --fc-footer-color: #000000; + --fc-provider-radius: 80px; + --fc-provider-font: 500 14px/20px "JetBrains Mono", monospace; + --fc-provider-phone-bg: #c6ff00; + --fc-provider-phone-color: #34a853; + --fc-provider-guest-color: #202124; + --fc-provider-guest-border: 2px solid #81737c; +} + +body[data-fc-theme] { + font: var(--fc-font); +} + +/* Layout */ + +.fc-root { + position: relative; + display: flex; + flex-direction: column; + align-items: center; + min-height: 100dvh; + overflow: hidden; + background: var(--fc-page-art), var(--background); + color: var(--foreground); +} + +/* Background art is placed on a 1440x1024 canvas, the size of the Figma frames, centred on the page. */ +.fc-art { + position: absolute; + top: 0; + left: 50%; + width: 1440px; + height: 1024px; + transform: translateX(-50%); + pointer-events: none; +} + +.fc-art img { + position: absolute; + max-width: none; +} + +/* + * --fc-main-top is where the first element sits in the 1024px tall Figma frame. Taller or shorter + * windows move the content by half the difference, so it stays centred like the design. + */ +.fc-main { + position: relative; + display: flex; + flex: 1; + flex-direction: column; + width: 100%; + max-width: calc(592px + 32px); + padding: max(80px, calc((100dvh - 1024px) / 2 + var(--fc-main-top))) 16px 48px; +} + +.fc-icon { + font-family: "Material Symbols Outlined"; + font-size: 24px; + font-weight: 500; + line-height: 1; + font-variation-settings: "FILL" 0; + user-select: none; +} + +/* Brand, shown above the content. Only one theme draws each piece; the title stays readable by screen readers in all of them. */ + +.fc-mascot, +.fc-headline { + display: none; +} + +.fc-intro { + position: absolute; + width: 1px; + height: 1px; + margin: 0; + overflow: hidden; + clip-path: inset(50%); + white-space: nowrap; +} + +[data-fc-theme="doggo"] .fc-mascot { + position: relative; + z-index: 1; + display: block; + width: 177px; + height: 118px; + margin: 0 auto -11px; +} + +[data-fc-theme="doggo"] .fc-intro { + position: static; + display: flex; + align-items: center; + justify-content: center; + width: auto; + height: auto; + overflow: visible; + clip-path: none; + white-space: normal; + margin: 0 0 4px; + padding: 16px 8px; + border-radius: 80px; + background: #4c8bff; + box-shadow: 0 0 0 4px #4c8bff; + font: 400 36px/44px "Bagel Fat One", sans-serif; + text-align: center; +} + +.fc-intro__text { + background: radial-gradient(farthest-corner at 0% 0%, #fff8f8 4%, #ffddb4 34%, #ffd8eb 74%); + background-clip: text; + color: transparent; +} + +[data-fc-theme="spark"] .fc-headline { + display: flex; + align-self: center; + align-items: center; + gap: 10px; + height: 48px; + margin-bottom: clamp(40px, calc(100dvh - 857px), 167px); + padding: 0 16px; + border-radius: 24px; + background: #ffffff; + color: #1c1b1b; + font: 500 20px/40px "JetBrains Mono", monospace; +} + +/* Card: shadcn Card, reshaped to the Figma "email" frame */ + +.fc-card { + display: flex; + flex-direction: column; + gap: 16px; + padding: var(--fc-card-padding); + border: 0; + border-radius: var(--radius); + background: var(--card); + box-shadow: var(--fc-card-shadow); +} + +.fc-form { + display: flex; + flex-direction: column; + gap: 16px; +} + +.fc-prompt { + margin: 0; + color: var(--muted-foreground); + font: var(--fc-prompt-font); + text-align: var(--fc-prompt-align); +} + +.fc-body { + margin: 0; + color: var(--muted-foreground); +} + +.fc-error { + margin: 0; + color: var(--destructive); + font-size: 14px; +} + +.fc-row { + display: flex; + gap: 16px; +} + +.fc-row > * { + flex: 1 1 0; + min-width: 0; +} + +.fc-row > .fc-row__country { + flex: 0 0 128px; +} + +/* Text field: shadcn Input inside a shell that adds the Figma "text-field" leading icon slot */ + +.fc-field { + position: relative; +} + +.fc-field__icon { + position: absolute; + top: 50%; + left: 12px; + color: var(--muted-foreground); + font-variation-settings: "FILL" var(--fc-field-icon-fill); + font-weight: var(--fc-field-icon-weight); + transform: translateY(-50%); + pointer-events: none; +} + +.fc-input, +.fc-select { + height: 56px; + padding-inline: 16px; + border-width: var(--fc-field-border-width); + border-radius: var(--radius); + background: var(--fc-field-bg); + color: var(--foreground); + font: inherit; + box-shadow: none; +} + +/* shadcn's focus ring is a box-shadow, which the rule above clears, so draw one in the theme's ring colour. */ +.fc-input:focus-visible, +.fc-select:focus-visible { + border-color: var(--ring); + box-shadow: 0 0 0 3px color-mix(in srgb, var(--ring) 50%, transparent); +} + +.fc-field:has(.fc-field__icon) .fc-input { + padding-left: 48px; +} + +.fc-field:has(.fc-field__toggle) .fc-input { + padding-right: 48px; +} + +.fc-field__toggle { + position: absolute; + top: 50%; + right: 8px; + display: grid; + place-items: center; + width: 40px; + height: 40px; + border: 0; + border-radius: 50%; + background: transparent; + color: var(--muted-foreground); + transform: translateY(-50%); + cursor: pointer; +} + +/* Verification code: shadcn InputOTP, one slot per digit */ + +.fc-otp [data-slot="input-otp-group"] { + gap: 8px; +} + +.fc-otp [data-slot="input-otp-slot"] { + width: 56px; + height: 56px; + border-width: var(--fc-field-border-width); + border-radius: var(--radius); + background: var(--fc-field-bg); + font-size: 24px; +} + +/* Buttons: shadcn Button, 80px tall with a zero-blur offset shadow, Figma component "button" */ + +.fc-actions { + display: flex; + flex-wrap: wrap; + justify-content: var(--fc-actions-justify); + gap: 16px; + margin-top: calc(var(--fc-actions-gap) - 16px); +} + +.fc-actions--stacked .fc-button { + flex-basis: 100%; + max-width: none; +} + +.fc-button { + flex: 1 1 0; + min-width: min(100%, 240px); + max-width: var(--fc-button-width); + height: 80px; + padding: 10px 24px; + border: var(--fc-button-border); + border-radius: var(--fc-button-radius); + box-shadow: var(--fc-button-shadow); + font: var(--fc-button-font); + transition: + transform 120ms ease, + box-shadow 120ms ease; +} + +.fc-button:active:not(:disabled) { + transform: translate(1px, 2px); + box-shadow: none; +} + +/* The button's border and offset shadow replace shadcn's focus ring, so keyboard focus gets an outline. */ +.fc-button:focus-visible, +.fc-link:focus-visible, +.fc-provider:focus-visible, +.fc-theme-switcher button:focus-visible { + outline: 3px solid var(--ring); + outline-offset: 3px; +} + +.fc-links { + display: flex; + flex-wrap: wrap; + justify-content: space-between; + gap: 8px 16px; +} + +.fc-link { + height: auto; + padding: 0; + color: var(--fc-link); + font: inherit; + text-decoration: underline; + text-underline-offset: 3px; +} + +.fc-link:disabled { + opacity: 0.6; + text-decoration: none; +} + +/* Other sign in methods: Figma frame "Content" */ + +.fc-footer { + position: relative; + display: flex; + flex-direction: column; + align-items: center; + gap: 24px; + width: 100%; + padding: 24px 16px 74px; +} + +.fc-footer__label { + margin: 0; + color: var(--fc-footer-color); + font: var(--fc-footer-font); +} + +.fc-providers { + display: flex; + flex-wrap: wrap; + justify-content: center; + gap: 24px; +} + +.fc-provider { + height: 52px; + padding: 16px; + gap: 12px; + border: 1px solid #000000; + border-radius: var(--fc-provider-radius); + background: #ffffff; + color: #000000; + font: 600 14px/20px "Roboto", sans-serif; + letter-spacing: 0.1px; +} + +.fc-provider:hover { + background: #ffffff; + color: #000000; +} + +.fc-provider svg { + width: 20px; + height: 20px; +} + +.fc-provider[data-provider="phone"] { + border: 0; + background: var(--fc-provider-phone-bg); + color: var(--fc-provider-phone-color); + font: var(--fc-provider-phone-font, var(--fc-provider-font)); +} + +.fc-provider[data-provider="guest"] { + border: var(--fc-provider-guest-border); + background: transparent; + color: var(--fc-provider-guest-color); + font: var(--fc-provider-font); +} + +/* Lists (enrolled factors) */ + +.fc-list { + display: flex; + flex-direction: column; + gap: 8px; + margin: 0; + padding: 0; + list-style: none; +} + +.fc-list li { + display: flex; + align-items: center; + justify-content: space-between; + gap: 16px; + min-height: 48px; + padding: 8px 16px; + border: var(--fc-field-border-width) solid var(--border); + border-radius: var(--radius); + background: var(--fc-field-bg); +} + +.fc-qr { + align-self: center; + width: 200px; + height: 200px; + padding: 12px; + border: 1px solid var(--border); + border-radius: var(--radius); + background: #ffffff; +} + +.fc-secret { + margin: 0; + text-align: center; + word-break: break-all; + user-select: all; +} + +/* Reauthentication: shadcn Dialog */ + +.fc-dialog { + gap: 16px; + border-radius: var(--radius); + box-shadow: var(--fc-card-shadow); +} + +/* Theme switcher and exit link, outside the themed design */ + +.fc-theme-switcher { + position: fixed; + z-index: 10; + top: 16px; + right: 16px; + display: flex; + align-items: center; + gap: 4px; + padding: 4px; + border: 1px solid rgb(0 0 0 / 12%); + border-radius: 999px; + background: rgb(255 255 255 / 85%); + backdrop-filter: blur(8px); + font: 500 13px/20px system-ui, sans-serif; +} + +.fc-theme-switcher > .fc-icon { + padding: 0 4px 0 8px; + color: #444444; + font-size: 20px; +} + +.fc-theme-switcher button { + padding: 6px 12px; + border: 0; + border-radius: 999px; + background: transparent; + color: #444444; + font: inherit; + cursor: pointer; +} + +.fc-theme-switcher button[aria-pressed="true"] { + background: #1f1f1f; + color: #ffffff; +} + +.fc-theme-switcher button.fc-theme-switcher__mode { + display: grid; + place-items: center; + padding: 6px 8px; +} + +.fc-theme-switcher__mode .fc-icon { + font-size: 18px; +} + +.fc-exit { + position: fixed; + z-index: 10; + top: 16px; + left: 16px; + display: inline-flex; + align-items: center; + gap: 4px; + padding: 8px 14px 8px 10px; + border: 1px solid rgb(0 0 0 / 12%); + border-radius: 999px; + background: rgb(255 255 255 / 85%); + backdrop-filter: blur(8px); + color: #444444; + font: 500 13px/20px system-ui, sans-serif; + text-decoration: none; +} + +.fc-exit .fc-icon { + font-size: 18px; +} + +.fc-label--short { + display: none; +} + +@media (max-width: 640px) { + .fc-main { + padding-top: 80px; + } + + [data-fc-theme="doggo"] .fc-intro { + font-size: 28px; + line-height: 36px; + } + + .fc-row { + flex-direction: column; + } + + .fc-row > .fc-row__country { + flex-basis: auto; + } + + /* Six code boxes fit the reauth dialog on a 360px phone. */ + .fc-dialog { + padding: 16px; + } + + .fc-dialog .fc-otp [data-slot="input-otp-group"] { + gap: 6px; + } + + .fc-dialog .fc-otp [data-slot="input-otp-slot"] { + width: 40px; + } + + .fc-button { + flex-basis: 100%; + max-width: none; + } + + .fc-otp [data-slot="input-otp-slot"] { + width: 44px; + } + + /* Stay clear of the back button, scrolling as a last resort. */ + .fc-theme-switcher { + max-width: calc(100vw - 80px); + overflow-x: auto; + scrollbar-width: none; + } + + .fc-theme-switcher button { + flex: none; + padding: 6px 7px; + } + + .fc-label--long { + display: none; + } + + .fc-label--short { + display: inline; + } + + .fc-theme-switcher > .fc-icon, + .fc-exit__label { + display: none; + } + + .fc-exit { + padding: 8px; + } +} diff --git a/examples/shadcn/src/full-customization/index.tsx b/examples/shadcn/src/full-customization/index.tsx new file mode 100644 index 000000000..1dd0f83b1 --- /dev/null +++ b/examples/shadcn/src/full-customization/index.tsx @@ -0,0 +1,127 @@ +/** + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { useEffect, useState, type ReactElement } from "react"; +import { Link, Navigate, Route, Routes } from "react-router"; +import type { User } from "firebase/auth"; +import { completeEmailLinkSignIn } from "@firebase-oss/ui-core"; +import { useMultiFactorAssertionCleanup, useUI } from "@firebase-oss/ui-react"; + +import { useUser } from "@/firebase/hooks"; +import "./full-customization.css"; +import { DemoProvider, paths } from "./state"; +import { ProAccount } from "./professional/account"; +import { ProMfaChallengeStep } from "./professional/mfa-challenge"; +import { ProMfaEnrollment } from "./professional/mfa-enrollment"; +import { ProPhone } from "./professional/phone"; +import { ProEmailLink, ProResetPassword, ProSignIn, ProSignUp } from "./professional/sign-in"; +import { ThemeArt, ThemeIdContext, ThemeSwitcher, useTheme } from "./theme"; +import { AccountStep } from "./steps/account"; +import { EmailEntryStep } from "./steps/email-entry"; +import { LoginStep } from "./steps/login"; +import { MfaChallengeStep } from "./steps/mfa-challenge"; +import { MfaEnrollmentStep } from "./steps/mfa-enrollment"; +import { PhoneStep } from "./steps/phone"; +import { SignUpStep } from "./steps/sign-up"; + +/** Owns the sign-in resolver, so switching theme mid-challenge swaps the screen without cancelling the sign in. */ +function SignInChallenge({ children }: { children: ReactElement }) { + useMultiFactorAssertionCleanup(); + return children; +} + +/** + * A complete sign in flow with its own UI, where FirebaseUI supplies only the auth logic through + * the ui-react action hooks. Sign in state drives the routing: a signed-in user is sent to the + * account page, and a pending multi-factor challenge takes over whichever screen raised it. + */ +export default function FullCustomizationDemo() { + const ui = useUI(); + const user = useUser(); + const [theme, setTheme] = useTheme(); + + // Completes sign in when the page is opened from a link sent by "Send login link". The ui-react hook for this + // drops failures, so the demo calls core directly and shows an expired or used link on the login screen. + const [linkError, setLinkError] = useState(null); + useEffect(() => { + completeEmailLinkSignIn(ui, window.location.href).catch((error) => + setLinkError(error instanceof Error ? error.message : String(error)) + ); + // Runs once on mount, like useEmailLinkAuthFormCompleteSignIn; ui is deliberately not a dependency. + }, []); + + const signedOut = (step: ReactElement) => (user ? : step); + const signedIn = (step: (user: User) => ReactElement) => (user ? step(user) : ); + + // Professional brings its own screens; the other themes share one set and differ only in styling. + const pro = theme === "professional"; + const screens = pro + ? { + email: , + login: , + signUp: , + phone: , + resetPassword: , + emailLink: , + account: (u: User) => , + twoFactor: (u: User) => , + challenge: , + } + : { + email: , + login: , + signUp: , + phone: , + resetPassword: , + emailLink: , + account: (u: User) => , + twoFactor: (u: User) => , + challenge: , + }; + + return ( + +
+ + + + All examples + + + + {/* A signed-in user with a resolver is reauthenticating; the reauth dialog completes that one. */} + {ui.multiFactorResolver && !user ? ( + {screens.challenge} + ) : ( + + + + + + + + + + } /> + + )} + +
+
+ ); +} diff --git a/examples/shadcn/src/full-customization/professional/account.tsx b/examples/shadcn/src/full-customization/professional/account.tsx new file mode 100644 index 000000000..54e064124 --- /dev/null +++ b/examples/shadcn/src/full-customization/professional/account.tsx @@ -0,0 +1,183 @@ +/** + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { useEffect, useReducer, useState } from "react"; +import { useNavigate } from "react-router"; +import { + EmailAuthProvider, + multiFactor, + onIdTokenChanged, + sendEmailVerification, + signOut, + updatePassword, + type User, +} from "firebase/auth"; +import { KeyRound, Mail, ShieldCheck, User as UserIcon } from "lucide-react"; +import { useUI } from "@firebase-oss/ui-react"; + +import { useAuthTask } from "../components"; +import { paths } from "../state"; +import { useReauth } from "../reauth"; +import { ProButton, ProError, ProField, ProGroup, ProHeader, ProRow, ProSection, ProSeparator } from "./kit"; + +function signedInWith(user: User) { + if (user.isAnonymous) return "a temporary account"; + const provider = user.providerData[0]?.providerId; + if (provider === "google.com") return "Google"; + if (provider === "phone") return "phone"; + return "email"; +} + +/** A settings-style account page, as in the Android and iOS demos. */ +export function ProAccount({ user }: { user: User }) { + const ui = useUI(); + const navigate = useNavigate(); + const { withReauth, dialog } = useReauth(); + const [editingPassword, setEditingPassword] = useState(false); + const [newPassword, setNewPassword] = useState(""); + const [message, setMessage] = useState(null); + const [verificationSent, setVerificationSent] = useState(false); + const task = useAuthTask(); + + // The user object is mutated in place by reload() and profile updates, so re-render on demand and on token refresh. + const [, refresh] = useReducer((n: number) => n + 1, 0); + useEffect(() => onIdTokenChanged(ui.auth, () => refresh()), [ui.auth]); + + const identity = user.displayName || user.email || user.phoneNumber; + const hasPassword = user.providerData.some((p) => p.providerId === EmailAuthProvider.PROVIDER_ID); + const twoFactorOn = multiFactor(user).enrolledFactors.length > 0; + const needsVerification = Boolean(user.email) && !user.emailVerified; + + return ( +
+ +
+
+ +
+
{identity || "Signed in"}
+
Signed in with {signedInWith(user)}
+
+
+ + {!user.isAnonymous && ( + <> + Security + + {hasPassword && ( + <> + { + setEditingPassword(!editingPassword); + setMessage(null); + }} + /> + {editingPassword && ( +
{ + event.preventDefault(); + void task.run(async () => { + await withReauth("Verify your identity to change your password.", () => + updatePassword(user, newPassword) + ); + setNewPassword(""); + setEditingPassword(false); + setMessage("Password updated."); + }); + }} + > + setNewPassword(event.target.value)} + autoFocus + /> + {task.error} + + Update password + + + )} + + + )} + + needsVerification + ? setMessage("Verify your email before setting up two-factor.") + : navigate(paths.twoFactor) + } + /> + {user.email && ( + <> + + + void task.run(async () => { + if (verificationSent) { + await user.reload(); + refresh(); + return; + } + await sendEmailVerification(user); + setVerificationSent(true); + setMessage(`We sent a verification link to ${user.email}.`); + }) + } + /> + + )} +
+ {!editingPassword && {task.error}} + {message && ( +

+ {message} +

+ )} + + )} + +
+ + void signOut(ui.auth)} /> + +
+ {dialog} +
+ ); +} diff --git a/examples/shadcn/src/full-customization/professional/kit.tsx b/examples/shadcn/src/full-customization/professional/kit.tsx new file mode 100644 index 000000000..75e745a25 --- /dev/null +++ b/examples/shadcn/src/full-customization/professional/kit.tsx @@ -0,0 +1,374 @@ +/** + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { useContext, useId, useState, type ComponentProps, type ComponentType, type ReactNode } from "react"; +import { ChevronLeft, ChevronRight, CircleAlert, Eye, EyeOff, Shield } from "lucide-react"; +import type { CountryData } from "@firebase-oss/ui-core"; +import { PolicyContext, useCountries } from "@firebase-oss/ui-react"; + +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; + +import "./professional.css"; + +type Icon = ComponentType<{ className?: string }>; + +type ProPageProps = { + title: string; + subtitle?: ReactNode; + /** A header icon tile, shown above the title. */ + icon?: Icon; + /** The brand row, shown on the first screen only. */ + brand?: boolean; + onBack?: () => void; + children: ReactNode; + footer?: ReactNode; +}; + +/** A flat, left-aligned page: header, content, then a footer pinned to the bottom when content is short. */ +export function ProPage({ title, subtitle, icon, brand, onBack, children, footer }: ProPageProps) { + return ( +
+ {onBack && } + +
{children}
+
+ {footer} +
+ ); +} + +export function ProHeader({ + title, + subtitle, + icon: IconComponent, + brand, +}: Omit) { + return ( +
+ {brand && ( +
+ + FirebaseUI Demo +
+ )} + {IconComponent && ( + + )} +

{title}

+ {subtitle &&

{subtitle}

} +
+ ); +} + +export function BackButton({ onClick }: { onClick: () => void }) { + return ( + + ); +} + +type ProFieldProps = Omit, "className" | "id"> & { + label: string; + /** A link on the right of the label row, such as "Forgot password?". */ + action?: { label: string; onClick: () => void }; + hint?: ReactNode; + invalid?: boolean; +}; + +/** A labelled shadcn Input, with a show/hide toggle for passwords. */ +export function ProField({ label, action, hint, invalid, type, ...props }: ProFieldProps) { + const id = useId(); + const hintId = `${id}-hint`; + const [revealed, setRevealed] = useState(false); + const isPassword = type === "password"; + + return ( +
+
+ + {action && ( + + )} +
+
+ + {isPassword && ( + + )} +
+ {hint && ( +

+ {hint} +

+ )} +
+ ); +} + +/** The code entry used by phone sign in, MFA setup and the MFA challenge: a labelled six digit field. */ +export function ProCodeField({ value, onChange }: { value: string; onChange: (code: string) => void }) { + return ( + onChange(event.target.value.replace(/\D/g, "").slice(0, 6))} + autoFocus + /> + ); +} + +type ProPhoneFieldProps = { + country: CountryData; + onCountryChange: (country: CountryData) => void; + value: string; + onChange: (value: string) => void; +}; + +/** One bordered box holding the country picker and the number, as on Android and iOS. */ +export function ProPhoneField({ country, onCountryChange, value, onChange }: ProPhoneFieldProps) { + const id = useId(); + const countries = useCountries(); + + return ( +
+
+ +
+
+ +
+
+ ); +} + +type ProButtonProps = Omit, "className"> & { + variant?: "primary" | "outline" | "text"; + loading?: boolean; + icon?: ReactNode; +}; + +export function ProButton({ + variant = "primary", + loading, + disabled, + icon, + children, + type = "button", + ...props +}: ProButtonProps) { + return ( + + ); +} + +export function ProDivider({ children }: { children: ReactNode }) { + return
{children}
; +} + +export function ProError({ children }: { children: ReactNode }) { + if (!children) return null; + return ( +

+

+ ); +} + +/** "Don't have an account? Create account" */ +export function ProPrompt({ prompt, action, onClick }: { prompt: string; action: string; onClick: () => void }) { + return ( +

+ {prompt}{" "} + +

+ ); +} + +/** The terms line, from the policies configured on FirebaseUIProvider. */ +export function ProLegal({ lead }: { lead: string }) { + const policies = useContext(PolicyContext); + if (!policies) return null; + const link = (url: string | URL, label: string) => ( + + {label} + + ); + + return ( +

+ {lead} {link(policies.termsOfServiceUrl, "Terms of Service")} and{" "} + {link(policies.privacyPolicyUrl, "Privacy Policy")}. +

+ ); +} + +export function ProSection({ children }: { children: ReactNode }) { + return

{children}

; +} + +export function ProGroup({ children, label }: { children: ReactNode; label?: string }) { + return ( +
+ {children} +
+ ); +} + +export function ProSeparator({ inset = 48 }: { inset?: number }) { + return