2021-03-08 12:51:31 +00:00
|
|
|
import { LitElement, html, internalProperty, property, query, unsafeCSS } from 'lit-element';
|
2021-03-06 17:01:39 +00:00
|
|
|
import { classMap } from 'lit-html/directives/class-map';
|
|
|
|
import { ifDefined } from 'lit-html/directives/if-defined';
|
2021-03-08 12:51:31 +00:00
|
|
|
import { event, EventEmitter, tag, watch } from '../../internal/decorators';
|
2021-02-26 14:09:13 +00:00
|
|
|
import styles from 'sass:./range.scss';
|
|
|
|
import { renderFormControl } from '../../internal/form-control';
|
|
|
|
import { hasSlot } from '../../internal/slot';
|
2021-02-04 15:22:04 +00:00
|
|
|
|
|
|
|
let id = 0;
|
2020-07-15 21:30:37 +00:00
|
|
|
|
|
|
|
/**
|
2020-07-17 10:09:10 +00:00
|
|
|
* @since 2.0
|
2020-07-15 21:30:37 +00:00
|
|
|
* @status stable
|
|
|
|
*
|
2021-02-04 15:22:04 +00:00
|
|
|
* @slot label - The input's label. Alternatively, you can use the label prop.
|
|
|
|
* @slot help-text - Help text that describes how to use the input. Alternatively, you can use the help-text prop.
|
|
|
|
*
|
2020-07-15 21:30:37 +00:00
|
|
|
* @part base - The component's base wrapper.
|
|
|
|
* @part input - The native range input.
|
|
|
|
* @part tooltip - The range tooltip.
|
|
|
|
*/
|
2021-03-08 12:51:31 +00:00
|
|
|
@tag('sl-range')
|
2021-03-09 00:14:32 +00:00
|
|
|
export default class SlRange extends LitElement {
|
2021-03-06 17:01:39 +00:00
|
|
|
static styles = unsafeCSS(styles);
|
|
|
|
|
|
|
|
@query('.range__control') input: HTMLInputElement;
|
|
|
|
@query('.range__tooltip') output: HTMLOutputElement;
|
|
|
|
|
2021-02-26 14:09:13 +00:00
|
|
|
private helpTextId = `input-help-text-${id}`;
|
|
|
|
private inputId = `input-${++id}`;
|
|
|
|
private labelId = `input-label-${id}`;
|
|
|
|
private resizeObserver: ResizeObserver;
|
2020-07-15 21:30:37 +00:00
|
|
|
|
2021-03-06 17:01:39 +00:00
|
|
|
@internalProperty() private hasFocus = false;
|
|
|
|
@internalProperty() private hasHelpTextSlot = false;
|
|
|
|
@internalProperty() private hasLabelSlot = false;
|
|
|
|
@internalProperty() private hasTooltip = false;
|
|
|
|
|
2020-07-15 21:30:37 +00:00
|
|
|
/** The input's name attribute. */
|
2021-03-06 17:01:39 +00:00
|
|
|
@property() name = '';
|
2020-07-15 21:30:37 +00:00
|
|
|
|
|
|
|
/** The input's value attribute. */
|
2021-03-06 17:01:39 +00:00
|
|
|
@property({ type: Number }) value: number;
|
2020-07-15 21:30:37 +00:00
|
|
|
|
2021-02-04 15:22:04 +00:00
|
|
|
/** The range's label. Alternatively, you can use the label slot. */
|
2021-03-06 17:01:39 +00:00
|
|
|
@property() label = '';
|
2021-02-04 15:22:04 +00:00
|
|
|
|
|
|
|
/** The range's help text. Alternatively, you can use the help-text slot. */
|
2021-03-06 17:01:39 +00:00
|
|
|
@property({ attribute: 'help-text' }) helpText = '';
|
2021-02-04 15:22:04 +00:00
|
|
|
|
2021-02-26 14:09:13 +00:00
|
|
|
/** Disables the input. */
|
2021-03-06 17:01:39 +00:00
|
|
|
@property({ type: Boolean, reflect: true }) disabled = false;
|
2020-07-15 21:30:37 +00:00
|
|
|
|
2020-08-28 20:50:18 +00:00
|
|
|
/**
|
|
|
|
* This will be true when the control is in an invalid state. Validity in range inputs is determined by the message
|
|
|
|
* provided by the `setCustomValidity` method.
|
|
|
|
*/
|
2021-03-06 17:01:39 +00:00
|
|
|
@property({ type: Boolean, reflect: true }) invalid = false;
|
2020-08-28 20:50:18 +00:00
|
|
|
|
2020-07-15 21:30:37 +00:00
|
|
|
/** The input's min attribute. */
|
2021-03-06 17:01:39 +00:00
|
|
|
@property({ type: Number }) min = 0;
|
2020-07-15 21:30:37 +00:00
|
|
|
|
|
|
|
/** The input's max attribute. */
|
2021-03-06 17:01:39 +00:00
|
|
|
@property({ type: Number }) max = 100;
|
2020-07-15 21:30:37 +00:00
|
|
|
|
|
|
|
/** The input's step attribute. */
|
2021-03-06 17:01:39 +00:00
|
|
|
@property({ type: Number }) step = 1;
|
2020-07-15 21:30:37 +00:00
|
|
|
|
|
|
|
/** The preferred placedment of the tooltip. */
|
2021-03-06 17:01:39 +00:00
|
|
|
@property() tooltip: 'top' | 'bottom' | 'none' = 'top';
|
2020-07-15 21:30:37 +00:00
|
|
|
|
|
|
|
/** A function used to format the tooltip's value. */
|
2021-03-06 17:01:39 +00:00
|
|
|
@property() tooltipFormatter = (value: number) => value.toString();
|
|
|
|
|
|
|
|
/** Emitted when the control's value changes. */
|
|
|
|
@event('sl-change') slChange: EventEmitter<void>;
|
2021-02-04 15:22:04 +00:00
|
|
|
|
2021-03-06 17:01:39 +00:00
|
|
|
/** Emitted when the control loses focus. */
|
|
|
|
@event('sl-blur') slBlur: EventEmitter<void>;
|
|
|
|
|
|
|
|
/** Emitted when the control gains focus. */
|
|
|
|
@event('sl-focus') slFocus: EventEmitter<void>;
|
|
|
|
|
|
|
|
connectedCallback() {
|
|
|
|
super.connectedCallback();
|
2021-02-04 15:22:04 +00:00
|
|
|
this.handleSlotChange = this.handleSlotChange.bind(this);
|
2021-02-26 14:09:13 +00:00
|
|
|
this.shadowRoot!.addEventListener('slotchange', this.handleSlotChange);
|
2021-02-04 15:22:04 +00:00
|
|
|
|
2020-07-15 21:30:37 +00:00
|
|
|
if (this.value === undefined || this.value === null) this.value = this.min;
|
|
|
|
if (this.value < this.min) this.value = this.min;
|
|
|
|
if (this.value > this.max) this.value = this.max;
|
2021-02-04 15:22:04 +00:00
|
|
|
|
|
|
|
this.handleSlotChange();
|
2020-07-15 21:30:37 +00:00
|
|
|
}
|
|
|
|
|
2021-03-06 17:01:39 +00:00
|
|
|
firstUpdated() {
|
2020-07-15 21:30:37 +00:00
|
|
|
this.syncTooltip();
|
|
|
|
this.resizeObserver = new ResizeObserver(() => this.syncTooltip());
|
|
|
|
}
|
|
|
|
|
2021-03-06 17:01:39 +00:00
|
|
|
disconnectedCallback() {
|
|
|
|
super.disconnectedCallback();
|
2021-02-26 14:09:13 +00:00
|
|
|
this.shadowRoot!.removeEventListener('slotchange', this.handleSlotChange);
|
2021-02-04 15:22:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 21:30:37 +00:00
|
|
|
/** Sets focus on the input. */
|
2021-02-26 14:09:13 +00:00
|
|
|
setFocus(options?: FocusOptions) {
|
2021-01-07 15:13:08 +00:00
|
|
|
this.input.focus(options);
|
2020-07-15 21:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Removes focus from the input. */
|
2021-02-26 14:09:13 +00:00
|
|
|
removeFocus() {
|
2020-07-15 21:30:37 +00:00
|
|
|
this.input.blur();
|
|
|
|
}
|
|
|
|
|
2020-08-28 20:50:18 +00:00
|
|
|
/** Sets a custom validation message. If `message` is not empty, the field will be considered invalid. */
|
2021-02-26 14:09:13 +00:00
|
|
|
setCustomValidity(message: string) {
|
2020-08-28 20:50:18 +00:00
|
|
|
this.input.setCustomValidity(message);
|
|
|
|
this.invalid = !this.input.checkValidity();
|
|
|
|
}
|
|
|
|
|
2020-07-15 21:30:37 +00:00
|
|
|
handleInput() {
|
|
|
|
this.value = Number(this.input.value);
|
2021-03-06 17:01:39 +00:00
|
|
|
this.slChange.emit();
|
2020-07-15 21:30:37 +00:00
|
|
|
|
|
|
|
requestAnimationFrame(() => this.syncTooltip());
|
|
|
|
}
|
|
|
|
|
|
|
|
handleBlur() {
|
|
|
|
this.hasFocus = false;
|
2020-07-22 11:12:08 +00:00
|
|
|
this.hasTooltip = false;
|
2021-03-06 17:01:39 +00:00
|
|
|
this.slBlur.emit();
|
2020-07-15 21:30:37 +00:00
|
|
|
this.resizeObserver.unobserve(this.input);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleFocus() {
|
|
|
|
this.hasFocus = true;
|
2020-07-22 11:12:08 +00:00
|
|
|
this.hasTooltip = true;
|
2021-03-06 17:01:39 +00:00
|
|
|
this.slFocus.emit();
|
2020-07-15 21:30:37 +00:00
|
|
|
this.resizeObserver.observe(this.input);
|
|
|
|
}
|
|
|
|
|
2021-03-06 19:39:48 +00:00
|
|
|
@watch('label')
|
|
|
|
@watch('helpText')
|
2021-02-04 15:22:04 +00:00
|
|
|
handleSlotChange() {
|
2021-02-26 14:09:13 +00:00
|
|
|
this.hasHelpTextSlot = hasSlot(this, 'help-text');
|
|
|
|
this.hasLabelSlot = hasSlot(this, 'label');
|
2021-02-04 15:22:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 11:12:08 +00:00
|
|
|
handleTouchStart() {
|
|
|
|
this.setFocus();
|
|
|
|
}
|
|
|
|
|
2020-07-15 21:30:37 +00:00
|
|
|
syncTooltip() {
|
|
|
|
if (this.tooltip !== 'none') {
|
|
|
|
const percent = Math.max(0, (this.value - this.min) / (this.max - this.min));
|
|
|
|
const inputWidth = this.input.offsetWidth;
|
|
|
|
const tooltipWidth = this.output.offsetWidth;
|
|
|
|
const thumbSize = getComputedStyle(this.input).getPropertyValue('--thumb-size');
|
|
|
|
const x = `calc(${inputWidth * percent}px - calc(calc(${percent} * ${thumbSize}) - calc(${thumbSize} / 2)))`;
|
|
|
|
this.output.style.transform = `translateX(${x})`;
|
|
|
|
this.output.style.marginLeft = `-${tooltipWidth / 2}px`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2021-02-26 14:09:13 +00:00
|
|
|
return renderFormControl(
|
|
|
|
{
|
|
|
|
inputId: this.inputId,
|
|
|
|
label: this.label,
|
|
|
|
labelId: this.labelId,
|
|
|
|
hasLabelSlot: this.hasLabelSlot,
|
|
|
|
helpTextId: this.helpTextId,
|
|
|
|
helpText: this.helpText,
|
|
|
|
hasHelpTextSlot: this.hasHelpTextSlot,
|
|
|
|
size: 'medium'
|
|
|
|
},
|
|
|
|
html`
|
2021-02-04 15:22:04 +00:00
|
|
|
<div
|
|
|
|
part="base"
|
2021-02-26 14:09:13 +00:00
|
|
|
class=${classMap({
|
2021-02-04 15:22:04 +00:00
|
|
|
range: true,
|
|
|
|
'range--disabled': this.disabled,
|
|
|
|
'range--focused': this.hasFocus,
|
|
|
|
'range--tooltip-visible': this.hasTooltip,
|
|
|
|
'range--tooltip-top': this.tooltip === 'top',
|
|
|
|
'range--tooltip-bottom': this.tooltip === 'bottom'
|
2021-02-26 14:09:13 +00:00
|
|
|
})}
|
2021-03-06 17:01:39 +00:00
|
|
|
@touchstart=${this.handleTouchStart.bind(this)}
|
2021-02-04 15:22:04 +00:00
|
|
|
>
|
|
|
|
<input
|
|
|
|
part="input"
|
|
|
|
type="range"
|
|
|
|
class="range__control"
|
2021-02-26 14:09:13 +00:00
|
|
|
name=${this.name}
|
|
|
|
.value=${this.value}
|
2021-03-06 20:22:59 +00:00
|
|
|
?disabled=${this.disabled}
|
2021-03-06 17:01:39 +00:00
|
|
|
min=${ifDefined(this.min)}
|
|
|
|
max=${ifDefined(this.max)}
|
|
|
|
step=${ifDefined(this.step)}
|
|
|
|
@input=${this.handleInput.bind(this)}
|
|
|
|
@focus=${this.handleFocus.bind(this)}
|
|
|
|
@blur=${this.handleBlur.bind(this)}
|
2021-02-04 15:22:04 +00:00
|
|
|
/>
|
2021-02-26 14:09:13 +00:00
|
|
|
${this.tooltip !== 'none'
|
2021-03-06 17:01:39 +00:00
|
|
|
? html` <output part="tooltip" class="range__tooltip"> ${this.tooltipFormatter(this.value)} </output> `
|
2021-02-26 14:09:13 +00:00
|
|
|
: ''}
|
2021-02-04 15:22:04 +00:00
|
|
|
</div>
|
2021-02-26 14:09:13 +00:00
|
|
|
`
|
2020-07-15 21:30:37 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-03-12 14:07:38 +00:00
|
|
|
|
|
|
|
declare global {
|
|
|
|
interface HTMLElementTagNameMap {
|
|
|
|
'sl-range': SlRange;
|
|
|
|
}
|
|
|
|
}
|