import '../icon/icon'; import { classMap } from 'lit/directives/class-map.js'; import { customElement, property, query, state } from 'lit/decorators.js'; import { defaultValue } from '../../internal/default-value'; import { FormControlController } from '../../internal/form'; import { HasSlotController } from '../../internal/slot'; import { html } from 'lit'; import { ifDefined } from 'lit/directives/if-defined.js'; import { live } from 'lit/directives/live.js'; import { LocalizeController } from '../../utilities/localize'; import { watch } from '../../internal/watch'; import ShoelaceElement from '../../internal/shoelace-element'; import styles from './input.styles'; import type { CSSResultGroup } from 'lit'; import type { ShoelaceFormControl } from '../../internal/shoelace-element'; // // It's currently impossible to hide Firefox's built-in clear icon when using , so we need this // check to apply a clip-path to hide it. I know, I know…user agent sniffing is nasty but, if it fails, we only see a // redundant clear icon so nothing important is breaking. The benefits outweigh the costs for this one. See the // discussion at: https://github.com/shoelace-style/shoelace/pull/794 // // Also note that we do the Chromium check first to prevent Chrome from logging a console notice as described here: // https://github.com/shoelace-style/shoelace/issues/855 // const isChromium = navigator.userAgentData?.brands.some(b => b.brand.includes('Chromium')); const isFirefox = isChromium ? false : navigator.userAgent.includes('Firefox'); /** * @summary Inputs collect data from the user. * @documentation https://shoelace.style/components/input * @status stable * @since 2.0 * * @dependency sl-icon * * @slot label - The input's label. Alternatively, you can use the `label` attribute. * @slot prefix - Used to prepend a presentational icon or similar element to the input. * @slot suffix - Used to append a presentational icon or similar element to the input. * @slot clear-icon - An icon to use in lieu of the default clear icon. * @slot show-password-icon - An icon to use in lieu of the default show password icon. * @slot hide-password-icon - An icon to use in lieu of the default hide password icon. * @slot help-text - Text that describes how to use the input. Alternatively, you can use the `help-text` attribute. * * @event sl-blur - Emitted when the control loses focus. * @event sl-change - Emitted when an alteration to the control's value is committed by the user. * @event sl-clear - Emitted when the clear button is activated. * @event sl-focus - Emitted when the control gains focus. * @event sl-input - Emitted when the control receives input. * * @csspart form-control - The form control that wraps the label, input, and help text. * @csspart form-control-label - The label's wrapper. * @csspart form-control-input - The input's wrapper. * @csspart form-control-help-text - The help text's wrapper. * @csspart base - The component's base wrapper. * @csspart input - The internal `` control. * @csspart prefix - The container that wraps the prefix. * @csspart clear-button - The clear button. * @csspart password-toggle-button - The password toggle button. * @csspart suffix - The container that wraps the suffix. */ @customElement('sl-input') export default class SlInput extends ShoelaceElement implements ShoelaceFormControl { static styles: CSSResultGroup = styles; private readonly formControlController = new FormControlController(this); private readonly hasSlotController = new HasSlotController(this, 'help-text', 'label'); private readonly localize = new LocalizeController(this); @query('.input__control') input: HTMLInputElement; @state() private hasFocus = false; @property() title = ''; // make reactive to pass through /** * The type of input. Works the same as a native `` element, but only a subset of types are supported. Defaults * to `text`. */ @property({ reflect: true }) type: | 'date' | 'datetime-local' | 'email' | 'number' | 'password' | 'search' | 'tel' | 'text' | 'time' | 'url' = 'text'; /** The name of the input, submitted as a name/value pair with form data. */ @property() name = ''; /** The current value of the input, submitted as a name/value pair with form data. */ @property() value = ''; /** The default value of the form control. Primarily used for resetting the form control. */ @defaultValue() defaultValue = ''; /** The input's size. */ @property({ reflect: true }) size: 'small' | 'medium' | 'large' = 'medium'; /** Draws a filled input. */ @property({ type: Boolean, reflect: true }) filled = false; /** Draws a pill-style input with rounded edges. */ @property({ type: Boolean, reflect: true }) pill = false; /** The input's label. If you need to display HTML, use the `label` slot instead. */ @property() label = ''; /** The input's help text. If you need to display HTML, use the `help-text` slot instead. */ @property({ attribute: 'help-text' }) helpText = ''; /** Adds a clear button when the input is not empty. */ @property({ type: Boolean }) clearable = false; /** Disables the input. */ @property({ type: Boolean, reflect: true }) disabled = false; /** Placeholder text to show as a hint when the input is empty. */ @property() placeholder = ''; /** Makes the input readonly. */ @property({ type: Boolean, reflect: true }) readonly = false; /** Adds a button to toggle the password's visibility. Only applies to password types. */ @property({ attribute: 'password-toggle', type: Boolean }) passwordToggle = false; /** Determines whether or not the password is currently visible. Only applies to password input types. */ @property({ attribute: 'password-visible', type: Boolean }) passwordVisible = false; /** Hides the browser's built-in increment/decrement spin buttons for number inputs. */ @property({ attribute: 'no-spin-buttons', type: Boolean }) noSpinButtons = false; /** * By default, form controls are associated with the nearest containing `