{"slug":"password-input","title":"Password Input","description":"Used to request or confirm a password from users.","contentType":"component","framework":"react","content":"## Resources\n\n\n[Latest version: v1.31.0](https://www.npmjs.com/package/@zag-js/password-input)\n[Logic Visualizer](https://zag-visualizer.vercel.app/password-input)\n[Source Code](https://github.com/chakra-ui/zag/tree/main/packages/machines/password-input)\n\n\n\n**Features**\n\n- Includes button to toggle visibility of the password\n- Automatic focus restoration to the input\n- Resets visibility to hidden after form submission\n- Ignore password management apps like 1Password, LastPass, etc.\n\n## Installation\n\nTo use the password-input machine in your project, run the following command in\nyour command line:\n\n```bash\nnpm install @zag-js/password-input @zag-js/react\n# or\nyarn add @zag-js/password-input @zag-js/react\n```\n\n## Anatomy\n\nTo set up the password-input correctly, you'll need to understand its anatomy\nand how we name its parts.\n\n> Each part includes a `data-part` attribute to help identify them in the DOM.\n\n\n\n## Usage\n\nFirst, import the password-input package into your project\n\n```jsx\nimport * as passwordInput from \"@zag-js/password-input\"\n```\n\nThe password-input package exports two key functions:\n\n- `machine` — The state machine logic for the password-input widget.\n- `connect` — The function that translates the machine's state to JSX attributes\n  and event handlers.\n\n> You'll also need to provide a unique `id` to the `useMachine` hook. This is\n> used to ensure that every part has a unique identifier.\n\nNext, import the required hooks and functions for your framework and use the\npassword-input machine in your project 🔥\n\n```jsx\nimport * as passwordInput from \"@zag-js/password-input\"\nimport { useMachine, normalizeProps } from \"@zag-js/react\"\nimport { EyeIcon, EyeOffIcon } from \"lucide-react\"\nimport { useId } from \"react\"\n\nfunction PasswordInput() {\n  const service = useMachine(passwordInput.machine, { id: useId() })\n\n  const api = passwordInput.connect(service, normalizeProps)\n\n  return (\n    <div {...api.getRootProps()}>\n      <label {...api.getLabelProps()}>Password</label>\n      <div {...api.getControlProps()}>\n        <input {...api.getInputProps()} />\n        <button {...api.getVisibilityTriggerProps()}>\n          <span {...api.getIndicatorProps()}>\n            {api.visible ? <EyeIcon /> : <EyeOffIcon />}\n          </span>\n        </button>\n      </div>\n    </div>\n  )\n}\n```\n\n### Setting the initial visibility\n\nUse the `defaultVisible` context property to set the initial visibility of the\npassword input.\n\n```tsx {3}\nconst service = useMachine(passwordInput.machine, {\n  id: useId(),\n  defaultVisible: true,\n})\n```\n\n### Controlling the visibility\n\nUse the `visible` and `onVisibilityChange` context properties to control the\nvisibility of the password input.\n\n> The `onVisibilityChange` callback is invoked when the visibility changes.\n\n```tsx {3-5}\nconst service = useMachine(passwordInput.machine, {\n  id: useId(),\n  visible: true,\n  onVisibilityChange(details) {\n    console.log(details)\n  },\n})\n```\n\n### Ignoring password managers\n\nSet the `ignorePasswordManager` context property to `true` to ignore password\nmanagers like 1Password, LastPass, etc.\n\nThis is useful when you want to ensure that the password input is not managed by\npassword managers. **Currently, this only works for 1Password, LastPass,\nBitwarden, Dashlane, and Proton Pass.**\n\n```tsx {3}\nconst service = useMachine(passwordInput.machine, {\n  id: useId(),\n  ignorePasswordManager: true,\n})\n```\n\n**Why is this useful?**\n\n- You might want to use this primitive for non-login scenarios (e.g., \"secure\n  notes\", \"temporary passwords\")\n\n- In a verify password step, you might want to disable password managers for the\n  confirm password field to ensure manual entry\n\n- Building a security-sensitive app where password managers violate compliance\n  requirements.\n\n### Managing autocompletion\n\nConfigure the `autoComplete` context property to manage autocompletion.\n\n- `new-password` — The user is creating a new password.\n- `current-password` — The user is entering an existing password.\n\n```tsx {3}\nconst service = useMachine(passwordInput.machine, {\n  id: useId(),\n  autoComplete: \"new-password\",\n})\n```\n\n### Making the input required\n\nSet the `required` context property to `true` to make the input required.\n\n```tsx {3}\nconst service = useMachine(passwordInput.machine, {\n  id: useId(),\n  required: true,\n})\n```\n\n### Making the input read only\n\nSet the `readOnly` context property to `true` to make the input read only.\n\n```tsx {3}\nconst service = useMachine(passwordInput.machine, {\n  id: useId(),\n  readOnly: true,\n})\n```\n\n## Styling guide\n\nEarlier, we mentioned that each password-input part has a `data-part` attribute\nadded to them to select and style them in the DOM.\n\n```css\n[data-scope=\"password-input\"][data-part=\"root\"] {\n  /* styles for the root part */\n}\n\n[data-scope=\"password-input\"][data-part=\"input\"] {\n  /* styles for the input part */\n}\n\n[data-scope=\"password-input\"][data-part=\"visibility-trigger\"] {\n  /* styles for the visibility trigger part */\n}\n\n[data-scope=\"password-input\"][data-part=\"indicator\"] {\n  /* styles for the indicator part */\n}\n\n[data-scope=\"password-input\"][data-part=\"control\"] {\n  /* styles for the control part */\n}\n\n[data-scope=\"password-input\"][data-part=\"label\"] {\n  /* styles for the label part */\n}\n```\n\n### Visibility State\n\nUse the `[data-state=\"visible\"]` and `[data-state=\"hidden\"]` attributes to style\nthe password input when it is visible or hidden.\n\n```css\n[data-scope=\"password-input\"][data-part=\"input\"][data-state=\"visible\"] {\n  /* styles for the visible state (for input) */\n}\n\n[data-scope=\"password-input\"][data-part=\"visibility-trigger\"][data-state=\"visible\"] {\n  /* styles for the visible state (for visibility trigger) */\n}\n```\n\n### Disabled State\n\nUse the `[data-disabled]` attribute to style the password input when it is\ndisabled.\n\n```css\n[data-scope=\"password-input\"][data-part=\"input\"][data-disabled] {\n  /* styles for the disabled state */\n}\n```\n\n### Invalid State\n\nUse the `[data-invalid]` attribute to style the password input when it is\ninvalid.\n\n```css\n[data-scope=\"password-input\"][data-part=\"input\"][data-invalid] {\n  /* styles for the invalid state */\n}\n```\n\n### Readonly State\n\nUse the `[data-readonly]` attribute to style the password input when it is read\nonly.\n\n```css\n[data-scope=\"password-input\"][data-part=\"input\"][data-readonly] {\n  /* styles for the readonly state */\n}\n```\n\n## Methods and Properties\n\n### Machine Context\n\nThe password-input machine exposes the following context properties:\n\n**`defaultVisible`**\nType: `boolean`\nDescription: The default visibility of the password input.\n\n**`visible`**\nType: `boolean`\nDescription: Whether the password input is visible.\n\n**`onVisibilityChange`**\nType: `(details: VisibilityChangeDetails) => void`\nDescription: Function called when the visibility changes.\n\n**`ids`**\nType: `Partial<{ input: string; visibilityTrigger: string; }>`\nDescription: The ids of the password input parts\n\n**`disabled`**\nType: `boolean`\nDescription: Whether the password input is disabled.\n\n**`invalid`**\nType: `boolean`\nDescription: The invalid state of the password input.\n\n**`readOnly`**\nType: `boolean`\nDescription: Whether the password input is read only.\n\n**`required`**\nType: `boolean`\nDescription: Whether the password input is required.\n\n**`translations`**\nType: `Partial<{ visibilityTrigger: (visible: boolean) => string; }>`\nDescription: The localized messages to use.\n\n**`ignorePasswordManagers`**\nType: `boolean`\nDescription: When `true`, the input will ignore password managers.\n\n**Only works for the following password managers**\n- 1Password, LastPass, Bitwarden, Dashlane, Proton Pass\n\n**`autoComplete`**\nType: `\"current-password\" | \"new-password\"`\nDescription: The autocomplete attribute for the password input.\n\n**`name`**\nType: `string`\nDescription: The name of the password input.\n\n**`dir`**\nType: `\"ltr\" | \"rtl\"`\nDescription: The document's text/writing direction.\n\n**`id`**\nType: `string`\nDescription: The unique identifier of the machine.\n\n**`getRootNode`**\nType: `() => ShadowRoot | Node | Document`\nDescription: A root node to correctly resolve document in custom environments. E.x.: Iframes, Electron.\n\n### Machine API\n\nThe password-input `api` exposes the following methods:\n\n**`visible`**\nType: `boolean`\nDescription: Whether the password input is visible.\n\n**`disabled`**\nType: `boolean`\nDescription: Whether the password input is disabled.\n\n**`invalid`**\nType: `boolean`\nDescription: Whether the password input is invalid.\n\n**`focus`**\nType: `VoidFunction`\nDescription: Focus the password input.\n\n**`setVisible`**\nType: `(value: boolean) => void`\nDescription: Set the visibility of the password input.\n\n**`toggleVisible`**\nType: `VoidFunction`\nDescription: Toggle the visibility of the password input.","package":"@zag-js/password-input","editUrl":"https://github.com/chakra-ui/zag/edit/main/website/data/components/password-input.mdx"}