import { classMap } from 'lit/directives/class-map.js'; import { customElement, eventOptions, property, query, state } from 'lit/decorators.js'; import { defaultValue } from '../../internal/default-value.js'; import { FormControlController } from '../../internal/form.js'; import { HasSlotController } from '../../internal/slot.js'; import { html } from 'lit'; import { ifDefined } from 'lit/directives/if-defined.js'; import { live } from 'lit/directives/live.js'; import { LocalizeController } from '../../utilities/localize.js'; import { watch } from '../../internal/watch.js'; import ShoelaceElement from '../../internal/shoelace-element.js'; import styles from './range.styles.js'; import type { CSSResultGroup } from 'lit'; import type { ShoelaceFormControl } from '../../internal/shoelace-element.js'; /** * @summary Ranges allow the user to select a single value within a given range using a slider. * @documentation https://shoelace.style/components/range * @status stable * @since 2.0 * * @slot label - The range's label. Alternatively, you can use the `label` attribute. * @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-focus - Emitted when the control gains focus. * @event sl-input - Emitted when the control receives input. * @event sl-invalid - Emitted when the form control has been checked for validity and its constraints aren't satisfied. * * @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 range's wrapper. * @csspart form-control-help-text - The help text's wrapper. * @csspart base - The component's base wrapper. * @csspart input - The internal `` element. * @csspart tooltip - The range's tooltip. * * @cssproperty --thumb-size - The size of the thumb. * @cssproperty --tooltip-offset - The vertical distance the tooltip is offset from the track. * @cssproperty --track-color-active - The color of the portion of the track that represents the current value. * @cssproperty --track-color-inactive - The of the portion of the track that represents the remaining value. * @cssproperty --track-height - The height of the track. * @cssproperty --track-active-offset - The point of origin of the active track. */ @customElement('sl-range') export default class SlRange 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); private resizeObserver: ResizeObserver; @query('.range__control') input: HTMLInputElement; @query('.range__tooltip') output: HTMLOutputElement | null; @state() private hasFocus = false; @state() private hasTooltip = false; @property() title = ''; // make reactive to pass through /** The name of the range, submitted as a name/value pair with form data. */ @property() name = ''; /** The current value of the range, submitted as a name/value pair with form data. */ @property({ type: Number }) value = 0; /** The range's label. If you need to display HTML, use the `label` slot instead. */ @property() label = ''; /** The range's help text. If you need to display HTML, use the help-text slot instead. */ @property({ attribute: 'help-text' }) helpText = ''; /** Disables the range. */ @property({ type: Boolean, reflect: true }) disabled = false; /** The minimum acceptable value of the range. */ @property({ type: Number }) min = 0; /** The maximum acceptable value of the range. */ @property({ type: Number }) max = 100; /** The interval at which the range will increase and decrease. */ @property({ type: Number }) step = 1; /** The preferred placement of the range's tooltip. */ @property() tooltip: 'top' | 'bottom' | 'none' = 'top'; /** * A function used to format the tooltip's value. The range's value is passed as the first and only argument. The * function should return a string to display in the tooltip. */ @property({ attribute: false }) tooltipFormatter: (value: number) => string = (value: number) => value.toString(); /** * By default, form controls are associated with the nearest containing `