Password Input
Password fields with a show/hide toggle, built on the input styles in every variant and size.
12345678910111213141516171819202122import { PasswordInput } from "#components/ui/ui-sdk/password-input.tsx";
export function Example() { return ( // UI SDK's password input wearing the input recipe, inside a UI SDK Field // that wires the label: it swaps the input type, keeps the caret in place // on toggle, re-masks on form submit, and marks parts with data-visible / // data-capslock. The toggle is a toggle button ("Show password" + // aria-pressed); VisibilityTrigger ships the eye icons <PasswordInput.Root variant="outline" size="md" autoComplete="current-password"> <PasswordInput.Label>Password</PasswordInput.Label> <PasswordInput.Control> <PasswordInput.Input placeholder="Enter your password" /> <PasswordInput.VisibilityTrigger /> </PasswordInput.Control> </PasswordInput.Root> );}
// value lives on Root: <PasswordInput.Root value={value} onValueChange={(v) => setValue(v)}>// controlled: <PasswordInput.Root visible={visible} onVisibilityChange={(d) => setVisible(d.visible)}>// custom icons: <PasswordInput.VisibilityTrigger><PasswordInput.Indicator fallback={<Eye />}><EyeOff /></PasswordInput.Indicator></PasswordInput.VisibilityTrigger>Variants
Sizes
Strength meter
Pair the field with a Meter that scores the password and switches palette by level.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950import { useState } from "react";import { PasswordInput } from "#components/ui/ui-sdk/password-input.tsx";import { Meter } from "#components/ui/ui-sdk/meter.tsx";import { amberPalette, greenPalette, redPalette } from "@stylexblocks/ui/themes/palettes.ts";
const levels = [ { label: "Too short", palette: redPalette }, { label: "Weak", palette: redPalette }, { label: "Weak", palette: redPalette }, { label: "Fair", palette: amberPalette }, { label: "Good", palette: amberPalette }, { label: "Strong", palette: greenPalette }, { label: "Very strong", palette: greenPalette },];
export function Example({ score }: { score: (password: string) => number /* 0–6 */ }) { const [password, setPassword] = useState(""); const points = score(password); const level = levels[points]; return ( <> {/* the value lives on Root in UI SDK */} <PasswordInput.Root autoComplete="new-password" value={password} onValueChange={(value) => setPassword(value)} > <PasswordInput.Label>New password</PasswordInput.Label> <PasswordInput.Control> <PasswordInput.Input /> <PasswordInput.VisibilityTrigger /> </PasswordInput.Control> </PasswordInput.Root> <Meter.Root value={points} max={6} variant="subtle" size="xs" shape="full" palette={level.palette} aria-label="Password strength" aria-valuetext={level.label} > <Meter.Track> <Meter.Range /> </Meter.Track> </Meter.Root> </> );}Invalid and disabled
Root flags reach every part: the input turns the error colour, the toggle disables with the field.
12345678910111213141516171819202122import { PasswordInput } from "#components/ui/ui-sdk/password-input.tsx";
export function Example() { return ( <> <PasswordInput.Root invalid> <PasswordInput.Label>Invalid</PasswordInput.Label> <PasswordInput.Control> <PasswordInput.Input /> <PasswordInput.VisibilityTrigger /> </PasswordInput.Control> </PasswordInput.Root> <PasswordInput.Root disabled> <PasswordInput.Label>Disabled</PasswordInput.Label> <PasswordInput.Control> <PasswordInput.Input /> <PasswordInput.VisibilityTrigger /> </PasswordInput.Control> </PasswordInput.Root> </> );}Install
1npx stylexblocks add password-inputSource
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570import * as stylex from "@stylexjs/stylex";import { createContext, useContext, useMemo, useState } from "react";import { Field as UiSdkField, FieldLabel as UiSdkFieldLabel } from "@ui-sdk/react/field";import { PasswordInput as UiSdkPasswordInput, PasswordInputControl as UiSdkPasswordInputControl, PasswordInputToggle as UiSdkPasswordInputToggle,} from "@ui-sdk/react/password-input";import { mergeProps, renderElement, type PartProps } from "@ui-sdk/react/primitive";import { createSlotRecipeContext } from "#lib/ui-sdk/create-slot-recipe-context.tsx";import { defineSlotRecipe, resolveSlotStyles, type SlotRecipeVariants, type WithStyleOverrides,} from "#lib/recipe-props.ts";import { fontWeights } from "@stylexblocks/ui/tokens/font-weights.stylex.ts";import { accentPalette } from "@stylexblocks/ui/themes/palettes.ts";import { colorPalette } from '@stylexblocks/ui/color-palette.stylex.ts'import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'import { semanticRadii } from '@stylexblocks/ui/semantic-tokens/radii.stylex.ts'import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'import { textStyles } from '@stylexblocks/ui/text-styles.ts'
const inputStyles = stylex.create({ base: { width: '100%', minWidth: 'var(--input-height)', outline: 'none', position: 'relative', appearance: 'none', textAlign: 'start', opacity: { default: null, ':is(:disabled, [disabled], [data-disabled])': 0.5, }, cursor: { default: null, ':is(:disabled, [disabled], [data-disabled])': 'not-allowed', }, height: 'var(--input-height)', '--focus-color': colorPalette.focusRing, '--error-color': semanticColors.borderError, '--focus-ring-color': { default: null, ':is([data-invalid], [aria-invalid="true"])': 'var(--error-color)', }, borderColor: { default: null, ':is([data-invalid], [aria-invalid="true"])': 'var(--error-color)', }, },})
const inputSizes = stylex.create({ xs: { borderRadius: semanticRadii.control, paddingInline: spacing._2, '--input-height': sizes.controlXs, }, sm: { borderRadius: semanticRadii.control, paddingInline: spacing._2_5, '--input-height': sizes.controlSm, }, md: { borderRadius: semanticRadii.control, paddingInline: spacing._3, '--input-height': sizes.controlMd, }, lg: { borderRadius: semanticRadii.control, paddingInline: spacing._4_5, '--input-height': sizes.controlLg, }, xl: { borderRadius: semanticRadii.control, paddingInline: spacing._6, '--input-height': sizes.controlXl, },})
const inputVariants = stylex.create({ outline: { backgroundColor: semanticColors.bg, borderWidth: '1px', borderColor: { default: semanticColors.border, ':is([data-invalid], [aria-invalid="true"])': 'var(--error-color)', ':hover': semanticColors.borderEmphasized, }, outlineWidth: { default: null, ':focus-visible': 'var(--focus-ring-width, 0)', }, outlineOffset: { default: null, ':focus-visible': '0px', }, outlineStyle: { default: null, ':focus-visible': 'solid', }, outlineColor: { default: null, ':focus-visible': colorPalette.focusRing, }, focusRingWidth: 0, }, subtle: { borderWidth: '1px', borderColor: { default: 'transparent', ':is([data-invalid], [aria-invalid="true"])': 'var(--error-color)', }, backgroundColor: semanticColors.bgMuted, outlineWidth: { default: null, ':focus-visible': 'var(--focus-ring-width, 0)', }, outlineOffset: { default: null, ':focus-visible': '0px', }, outlineStyle: { default: null, ':focus-visible': 'solid', }, outlineColor: { default: null, ':focus-visible': colorPalette.focusRing, }, }, flushed: { backgroundColor: 'transparent', borderBottomWidth: '1px', borderBottomColor: semanticColors.border, borderRadius: 0, paddingInline: 0, borderColor: { default: null, ':is([data-invalid], [aria-invalid="true"])': 'var(--error-color)', ':focus-visible': 'var(--focus-color)', }, boxShadow: { default: null, ':focus-visible': '0px 1px 0px 0px var(--focus-color)', }, },})
const inputTextStyles = { xs: textStyles.xs, sm: textStyles.sm, md: textStyles.sm, lg: textStyles.md, xl: textStyles.md,} as const
export type InputSize = keyof typeof inputSizes
export type InputVariant = keyof typeof inputVariants
// The field reuses the input recipe as-is; this layer only adds the group// around it: a stacked root, the label, a relative control that sizes the// end slot, and the ghost visibility toggle pinned inside the input's end.const group = stylex.create({ root: { display: "flex", flexDirection: "column", gap: spacing._1_5, width: "100%", }, label: { fontWeight: fontWeights.medium, userSelect: "none", opacity: { default: null, ":is([data-disabled])": 0.5, }, }, control: { position: "relative", display: "flex", alignItems: "center", width: "100%", }, input: { // room for the toggle, which is as wide as the input is tall paddingInlineEnd: "var(--input-height)", }, trigger: { position: "absolute", insetBlockStart: 0, insetInlineEnd: 0, zIndex: 1, display: "inline-flex", alignItems: "center", justifyContent: "center", width: "var(--input-height)", height: "var(--input-height)", padding: 0, margin: 0, borderWidth: 0, borderStyle: "none", borderRadius: semanticRadii.control, backgroundColor: "transparent", appearance: "none", color: { default: semanticColors.fgMuted, ":hover": semanticColors.fg, }, cursor: { default: "pointer", ":is(:disabled, [data-disabled])": "not-allowed", }, opacity: { default: null, ":is(:disabled, [data-disabled])": 0.5, }, outlineStyle: { default: null, ":focus-visible": "solid", }, outlineWidth: { default: null, ":focus-visible": "2px", }, outlineOffset: { default: null, ":focus-visible": "-2px", }, outlineColor: { default: null, ":focus-visible": colorPalette.focusRing, }, }, indicator: { display: "inline-flex", alignItems: "center", justifyContent: "center", }, icon: { width: "var(--password-input-icon-size)", height: "var(--password-input-icon-size)", flexShrink: 0, },});
const groupSizes = stylex.create({ xs: { "--input-height": sizes.controlXs, "--password-input-icon-size": sizes._3_5 }, sm: { "--input-height": sizes.controlSm, "--password-input-icon-size": sizes._4 }, md: { "--input-height": sizes.controlMd, "--password-input-icon-size": sizes._4 }, lg: { "--input-height": sizes.controlLg, "--password-input-icon-size": sizes._5 }, xl: { "--input-height": sizes.controlXl, "--password-input-icon-size": sizes._5 },});
const passwordInputRecipe = defineSlotRecipe({ slots: { root: { styles: { base: group.root } }, label: { styles: { base: group.label }, sizes: inputTextStyles }, control: { styles: { base: group.control }, sizes: groupSizes }, input: { styles: inputStyles, variants: inputVariants, sizes: inputSizes }, visibilityTrigger: { styles: { base: group.trigger } }, indicator: { styles: { base: group.indicator } }, }, defaultVariants: { variant: "outline", size: "md" },});
const { withProvider, withContext } = createSlotRecipeContext(passwordInputRecipe);
type PasswordInputVariants = SlotRecipeVariants<typeof passwordInputRecipe>;
function EyeIcon() { return ( <svg viewBox="0 0 16 16" fill="none" aria-hidden="true" {...stylex.props(group.icon)}> <path d="M1.5 8S3.8 3.5 8 3.5 14.5 8 14.5 8 12.2 12.5 8 12.5 1.5 8 1.5 8Z" stroke="currentColor" strokeWidth="1.25" strokeLinejoin="round" /> <circle cx="8" cy="8" r="2" stroke="currentColor" strokeWidth="1.25" /> </svg> );}
function EyeOffIcon() { return ( <svg viewBox="0 0 16 16" fill="none" aria-hidden="true" {...stylex.props(group.icon)}> <path d="M6.6 3.65A6.9 6.9 0 0 1 8 3.5c4.2 0 6.5 4.5 6.5 4.5a11 11 0 0 1-1.7 2.3M4.3 4.9C2.5 6.1 1.5 8 1.5 8s2.3 4.5 6.5 4.5c1.4 0 2.6-.5 3.6-1.2M6.6 6.6a2 2 0 0 0 2.8 2.8M2 2l12 12" stroke="currentColor" strokeWidth="1.25" strokeLinecap="round" strokeLinejoin="round" /> </svg> );}
// --- state --------------------------------------------------------------------// UI SDK's PasswordInput owns the field behavior (type swap, caret kept on// toggle, hidden again on submit / page hide, caps-lock state). It renders// the relative box around input + toggle, so it maps to our Control, while// our Root is a UI SDK Field: that wires the Label to the input and spreads// `data-invalid` / `data-disabled` / `data-required` over every part. Root// keeps visibility (controlled or not) and the field flags in context, so// Control can hand them to PasswordInput and Indicator can read them.
interface PasswordInputContextValue { visible: boolean; setVisible: (visible: boolean) => void; toggleVisible: () => void; readOnly: boolean; required: boolean; disabled: boolean; name?: string; autoComplete?: string; ignorePasswordManagers: boolean; value?: string; defaultValue?: string; onValueChange?: PasswordInputValueChange;}
type PasswordInputValueChange = React.ComponentProps<typeof UiSdkPasswordInput>["onValueChange"];
const PasswordInputContext = createContext<PasswordInputContextValue | null>(null);
function usePasswordInput() { const ctx = useContext(PasswordInputContext); if (!ctx) throw new Error("PasswordInput parts must be rendered inside <PasswordInput.Root>"); return ctx;}
interface PasswordInputOwnProps { /** Controlled visibility of the password. */ visible?: boolean; /** @default false */ defaultVisible?: boolean; onVisibilityChange?: (details: { visible: boolean }) => void; disabled?: boolean; invalid?: boolean; readOnly?: boolean; required?: boolean; /** Form field name, forwarded to the input. */ name?: string; /** @default "current-password" */ autoComplete?: "current-password" | "new-password"; /** Adds the opt-out attributes common password managers respect. */ ignorePasswordManagers?: boolean; /** * The value, for a controlled field. UI SDK keeps the value on the * password input itself, so it goes here rather than on `Input`. */ value?: string; /** The value it starts with. A form reset returns here. */ defaultValue?: string; /** Called with the new value and `details.reason` ("input-change", "reset", ...). */ onValueChange?: PasswordInputValueChange;}
type FieldRootProps = Omit<React.ComponentProps<typeof UiSdkField>, "disabled" | "invalid" | "name">;
function RootNode({ visible: visibleProp, defaultVisible = false, onVisibilityChange, disabled = false, invalid = false, readOnly = false, required = false, name, autoComplete = "current-password", ignorePasswordManagers = false, value, defaultValue, onValueChange, ...rest}: FieldRootProps & PasswordInputOwnProps) { const [innerVisible, setInnerVisible] = useState(defaultVisible); const visible = visibleProp ?? innerVisible; const ctx = useMemo<PasswordInputContextValue>(() => { const setVisible = (next: boolean) => { setInnerVisible(next); onVisibilityChange?.({ visible: next }); }; return { visible, setVisible, toggleVisible: () => setVisible(!visible), readOnly, required, disabled, name, autoComplete, ignorePasswordManagers, value, defaultValue, onValueChange, }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [visible, readOnly, required, disabled, name, autoComplete, ignorePasswordManagers, value, defaultValue, onValueChange]); return ( <PasswordInputContext.Provider value={ctx}> <UiSdkField disabled={disabled || undefined} // `false` would suppress the browser's own validation; leave it out invalid={invalid || undefined} {...rest} /> </PasswordInputContext.Provider> );}
function ControlNode( props: Omit< React.ComponentProps<typeof UiSdkPasswordInput>, "visible" | "defaultVisible" | "onVisibleChange" | "value" | "defaultValue" >,) { const s = usePasswordInput(); return ( <UiSdkPasswordInput visible={s.visible} onVisibleChange={(next) => s.setVisible(next)} name={s.name} autoComplete={s.autoComplete} readOnly={s.readOnly} required={s.required} value={s.value} defaultValue={s.defaultValue} onValueChange={(next, details) => s.onValueChange?.(next, details)} {...props} /> );}
const passwordManagerProps = { "data-1p-ignore": "", "data-lpignore": "true", "data-bwignore": "true", "data-form-type": "other", "data-protonpass-ignore": "true",};
function InputNode(props: React.ComponentProps<typeof UiSdkPasswordInputControl>) { const s = usePasswordInput(); return ( <UiSdkPasswordInputControl autoCapitalize="off" spellCheck={false} data-state={s.visible ? "visible" : "hidden"} {...(s.ignorePasswordManagers ? passwordManagerProps : {})} {...props} /> );}
// UI SDK's toggle is a toggle button: a constant name ("Show password") with// `aria-pressed` carrying whether the password is shown.function VisibilityTriggerNode({ children, ...rest}: React.ComponentProps<typeof UiSdkPasswordInputToggle>) { const s = usePasswordInput(); return ( <UiSdkPasswordInputToggle aria-label="Show password" data-state={s.visible ? "visible" : "hidden"} {...rest} > {children ?? <Indicator />} </UiSdkPasswordInputToggle> );}
interface IndicatorState { visible: boolean;}
/** * Indicator — renders `children` while the password is visible and * `fallback` while it is masked. Defaults: eye-off / eye. */function IndicatorNode({ render, children, fallback, ...rest}: PartProps<"span", IndicatorState> & { fallback?: React.ReactNode }) { const s = usePasswordInput(); const state: IndicatorState = { visible: s.visible }; return renderElement( "span", mergeProps( { "aria-hidden": true, "data-state": s.visible ? "visible" : "hidden", "data-visible": s.visible ? "" : undefined, children: s.visible ? (children ?? <EyeOffIcon />) : (fallback ?? <EyeIcon />), }, rest, ), render, state, );}
/** * PasswordInput — UI SDK's password input wearing the input recipe, inside * a UI SDK Field for the label. `<PasswordInput.Root variant size>` resolves * the recipe once and every part inherits it via context. Root owns * visibility (`visible`, `defaultVisible`, `onVisibilityChange`) and the * field flags, and the value (`value` / `defaultValue` / `onValueChange` go * on Root, not Input: UI SDK keeps the value on the password input). UI SDK * swaps the input `type`, keeps the caret in place when toggling, re-masks * on form submit, and marks every part with `data-visible`, `data-filled`, * `data-capslock`. Every part composes via `render`. */export type PasswordInputRootProps = WithStyleOverrides<FieldRootProps> & PasswordInputOwnProps & Partial<PasswordInputVariants>;
const PasswordInputRoot = withProvider(RootNode, "root", "PasswordInputRoot");
function Root({ palette = accentPalette, ...props }: PasswordInputRootProps) { return ( <PasswordInputRoot palette={palette} {...(props as React.ComponentProps<typeof PasswordInputRoot>)} /> );}Root.displayName = "PasswordInputRoot";
const Indicator = withContext(IndicatorNode, "indicator", "PasswordInputIndicator");
/** Render-prop access to the visibility state, like Ark's `PasswordInput.Context`. */function Context({ children,}: { children: (state: { visible: boolean; setVisible: (v: boolean) => void; toggleVisible: () => void }) => React.ReactNode;}) { return <>{children(usePasswordInput())}</>;}
export const PasswordInput = { Root, Label: withContext(UiSdkFieldLabel, "label", "PasswordInputLabel"), Control: withContext(ControlNode, "control", "PasswordInputControl"), Input: withContext( InputNode, (v) => [ ...resolveSlotStyles(passwordInputRecipe.slots.input, v), inputTextStyles[v.size], group.input, ], "PasswordInputInput", ), VisibilityTrigger: withContext( VisibilityTriggerNode, "visibilityTrigger", "PasswordInputVisibilityTrigger", ), Indicator, Context,};
Examples
Installable blocks built with Password Input — real use cases, delivered as source you own.
Minimal, zero-chrome centered credential sign-in form resting directly on the canvas with inline validation, password visibility toggle, inline forgot-password feedback, remember-me persistence, and toast confirmation.
Centered workspace sign-in card with inline field validation, password visibility toggle, remember-device persistence, Google, GitHub, and passkey provider buttons, and toast feedback.
Split-screen sign-in card pairing an interactive authentication form with validation, password visibility toggle, remember-me control, and social/SAML SSO actions beside an atmospheric architectural brand showcase.
Enterprise single sign-on flow with work-email domain lookup, SAML 2.0 and OIDC identity provider confirmation (Okta, Microsoft Entra ID, Google Workspace), password fallback with inline validation, and toast feedback.
Account and workspace preferences page with quick-jump settings search, responsive sidebar-to-tabs navigation, profile details, timezone and appearance controls, notification switches, password and security options, dirty-state detection, and Save / Reset actions.
