UNPKG

@zag-js/password-input

Version:

Core logic for the password-input widget implemented as a state machine

156 lines (154 loc) 5.88 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/password-input.connect.ts var password_input_connect_exports = {}; __export(password_input_connect_exports, { connect: () => connect }); module.exports = __toCommonJS(password_input_connect_exports); var import_dom_query = require("@zag-js/dom-query"); var import_password_input = require("./password-input.anatomy.js"); var dom = __toESM(require("./password-input.dom.js")); function connect(service, normalize) { const { scope, prop, context } = service; const visible = context.get("visible"); const disabled = !!prop("disabled"); const invalid = !!prop("invalid"); const readOnly = !!prop("readOnly"); const required = !!prop("required"); const interactive = !(readOnly || disabled); const translations = prop("translations"); return { visible, disabled, invalid, focus() { dom.getInputEl(scope)?.focus(); }, setVisible(value) { service.send({ type: "VISIBILITY.SET", value }); }, toggleVisible() { service.send({ type: "VISIBILITY.SET", value: !visible }); }, getRootProps() { return normalize.element({ ...import_password_input.parts.root.attrs, dir: prop("dir"), "data-disabled": (0, import_dom_query.dataAttr)(disabled), "data-invalid": (0, import_dom_query.dataAttr)(invalid), "data-readonly": (0, import_dom_query.dataAttr)(readOnly) }); }, getLabelProps() { return normalize.label({ ...import_password_input.parts.label.attrs, htmlFor: dom.getInputId(scope), "data-disabled": (0, import_dom_query.dataAttr)(disabled), "data-invalid": (0, import_dom_query.dataAttr)(invalid), "data-readonly": (0, import_dom_query.dataAttr)(readOnly), "data-required": (0, import_dom_query.dataAttr)(required) }); }, getInputProps() { return normalize.input({ ...import_password_input.parts.input.attrs, id: dom.getInputId(scope), autoCapitalize: "off", name: prop("name"), required: prop("required"), autoComplete: prop("autoComplete"), spellCheck: false, readOnly, disabled, type: visible ? "text" : "password", "data-state": visible ? "visible" : "hidden", "aria-invalid": (0, import_dom_query.ariaAttr)(invalid), "data-disabled": (0, import_dom_query.dataAttr)(disabled), "data-invalid": (0, import_dom_query.dataAttr)(invalid), "data-readonly": (0, import_dom_query.dataAttr)(readOnly), ...prop("ignorePasswordManagers") ? passwordManagerProps : {} }); }, getVisibilityTriggerProps() { return normalize.button({ ...import_password_input.parts.visibilityTrigger.attrs, type: "button", tabIndex: -1, "aria-controls": dom.getInputId(scope), "aria-expanded": visible, "data-readonly": (0, import_dom_query.dataAttr)(readOnly), disabled, "data-disabled": (0, import_dom_query.dataAttr)(disabled), "data-state": visible ? "visible" : "hidden", "aria-label": translations?.visibilityTrigger?.(visible), onPointerDown(event) { if (!(0, import_dom_query.isLeftClick)(event)) return; if (!interactive) return; event.preventDefault(); service.send({ type: "TRIGGER.CLICK" }); } }); }, getIndicatorProps() { return normalize.element({ ...import_password_input.parts.indicator.attrs, "aria-hidden": true, "data-state": visible ? "visible" : "hidden", "data-disabled": (0, import_dom_query.dataAttr)(disabled), "data-invalid": (0, import_dom_query.dataAttr)(invalid), "data-readonly": (0, import_dom_query.dataAttr)(readOnly) }); }, getControlProps() { return normalize.element({ ...import_password_input.parts.control.attrs, "data-disabled": (0, import_dom_query.dataAttr)(disabled), "data-invalid": (0, import_dom_query.dataAttr)(invalid), "data-readonly": (0, import_dom_query.dataAttr)(readOnly) }); } }; } var passwordManagerProps = { // 1Password "data-1p-ignore": "", // LastPass "data-lpignore": "true", // Bitwarden "data-bwignore": "true", // Dashlane "data-form-type": "other", // Proton Pass "data-protonpass-ignore": "true" }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { connect });