import {LitElement, html, unsafeCSS} from 'lit'; import {customElement, property} from 'lit/decorators.js'; import {styleMap} from 'lit/directives/style-map.js'; import {ifDefined} from 'lit/directives/if-defined.js'; import componentStyle from './slider.css?inline'; import '../icon-button/icon-button'; import {classMap} from 'lit/directives/class-map.js'; export enum ObcSliderVariant { NoValue = 'no-value', NoInput = 'no-input', } export type ObcSliderVariantType = 'no-value' | 'no-input'; /** * @element obc-slider * * @prop {number} value - The value of the slider * @prop {number} min - The minimum value of the slider * @prop {number} max - The maximum value of the slider * @prop {number} step - The step value of the slider * @prop {number} stepClick - The step value when clicking the increase or decrease buttons * @attr hugcontainer - If set, the slider will not have any spacing between the slider icons and the container * * @slot icon-left - Slot for the left icon * @slot icon-right - Slot for the right icon * * @fires value - Fires when the value is changed */ @customElement('obc-slider') export class ObcSlider extends LitElement { @property({type: Number}) value = 50; @property({type: Number}) min = 0; @property({type: Number}) max = 100; @property({type: Number}) step: number | undefined; @property({type: Number}) stepClick = 10; @property({type: String}) variant: ObcSliderVariantType = 'no-value'; @property({type: Boolean}) hasLeftIcon = false; @property({type: Boolean}) hasRightIcon = false; onInput(value: number) { this.value = value; this.dispatchEvent(new CustomEvent('value', {detail: this.value})); } onReduceClick() { this.onInput(Math.max(this.value - this.stepClick, this.min)); } onIncreaseClick() { this.onInput(Math.min(this.value + this.stepClick, this.max)); } override render() { const ratio = (this.value - this.min) / (this.max - this.min); return html` ${this.hasLeftIcon ? html` ` : null}
${this.variant === ObcSliderVariant.NoInput ? html`
` : html` { this.value = Number((event.target as HTMLInputElement).value); this.dispatchEvent( new CustomEvent('value', {detail: this.value}) ); }} /> `}
${this.hasRightIcon ? html` ` : null} `; } static override styles = unsafeCSS(componentStyle); } declare global { interface HTMLElementTagNameMap { 'obc-slider': ObcSlider; } }