@duetds/components
Version:
This package includes Duet Core Components and related tools.
145 lines (140 loc) • 9.27 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const core = require('./core-e5cda7d4.js');
const jsUtils = require('./js-utils-286cf29e.js');
const tokens_common = require('./tokens.common-5057aed3.js');
const DuetRangeSlider = class {
constructor(hostRef) {
core.registerInstance(this, hostRef);
this.rangeId = jsUtils.jsUtils_2("DuetRange");
this.ua = navigator.userAgent || navigator.vendor;
this.percentage = 0;
/**
* Theme of the input. Can be one of: "default", "turva".
*/
this.theme = "";
/**
* Controls the margin of the component. Can be one of: "auto", "none".
*/
this.margin = "auto";
/**
* Expands the input to fill 100% of the container width.
*/
this.expand = false;
/**
* Minimum value.
*/
this.min = 0;
/**
* Label for the range slider.
*/
this.label = "label";
/**
* Unit for the range slider.
*/
this.unit = "";
/**
* Maximum value.
*/
this.max = 100;
/**
* Step amount.
*/
this.step = 1;
/**
* Value of the input.
*/
this.value = 0;
/**
* Set the amount of time, in milliseconds, to wait to trigger the duetChange event after each keystroke.
*/
this.debounce = 0;
/**
* Component event handling.
*/
this.onInput = (ev) => {
const input = ev.target;
if (input) {
this.value = parseInt(input.value) || 0;
}
this.duetInput.emit(ev);
this.calculatePercentage();
};
this.duetChange = core.createEvent(this, "duetChange", 3);
this.duetInput = core.createEvent(this, "duetInput", 7);
}
debounceChanged() {
this.duetChange = jsUtils.jsUtils_4(this.duetChange, this.debounce);
}
/**
* Update the native input element when the value changes.
*/
valueChanged() {
this.duetChange.emit({
value: this.value,
component: "duet-range-slider",
});
}
/**
* Component lifecycle events.
*/
componentWillLoad() {
if (this.theme !== "default" && document.documentElement.classList.contains("duet-theme-turva")) {
this.theme = "turva";
}
this.calculatePercentage();
}
componentDidLoad() {
this.debounceChanged();
}
calculatePercentage() {
this.percentage = Math.ceil(((this.value - this.min) / (this.max - this.min)) * 100);
}
/**
* Sets focus on the specified `duet-range-slider`. Use this method instead of the global
* `input.focus()`.
*/
async setFocus() {
const nativeInput = this.element.querySelector("input");
if (nativeInput) {
nativeInput.focus();
}
}
/**
* render() function
* Always the last one in the class.
*/
render() {
const identifier = this.identifier || this.rangeId;
if (this.expand) {
this.element.classList.add("duet-expand");
}
const inactive = this.theme === "turva" ? tokens_common.tokens["colorGrayTurva"] : tokens_common.tokens["colorGray"];
const active = this.theme === "turva" ? tokens_common.tokens["colorPrimaryTurva"] : tokens_common.tokens["colorPrimary"];
const rangeStyles = {
background: "-webkit-linear-gradient(left, " +
active +
" 0%, " +
active +
" " +
this.percentage +
"%," +
inactive +
" " +
this.percentage +
"%)",
};
return (core.h(core.Host, { class: { "duet-m-0": this.margin === "none" } }, core.h("div", { class: {
"duet-range-container": true,
"duet-theme-turva": this.theme === "turva",
"duet-ie": document["documentMode"] || /Edge/.test(this.ua),
} }, core.h("duet-label", { theme: this.theme === "turva" ? "turva" : "default", margin: "small", for: identifier }, this.label), core.h("output", { htmlFor: identifier, class: "duet-output" }, this.value, this.unit), core.h("input", { onInput: this.onInput, type: "range", min: this.min, max: this.max, step: this.step, value: this.value, style: rangeStyles, "aria-valuemin": this.min, "aria-valuemax": this.max, "aria-valuenow": this.value, id: identifier, name: "range", class: "duet-range" }))));
}
get element() { return core.getElement(this); }
static get watchers() { return {
"debounce": ["debounceChanged"],
"value": ["valueChanged"]
}; }
static get style() { return ".sc-duet-range-slider-h{-webkit-appearance:none;-moz-appearance:none;appearance:none;-webkit-box-sizing:border-box;box-sizing:border-box;background:transparent;border:0;padding:0;margin:0;margin-right:16px!important;margin-bottom:20px!important;display:-ms-inline-flexbox;display:inline-flex;vertical-align:bottom;text-align:left;max-width:100%;width:100%}.sc-duet-range-slider-h:last-child, .sc-duet-range-slider-h:last-of-type{margin-right:0!important}\@media only screen and (min-width:36em){.sc-duet-range-slider-h{width:calc(50% - 16px - 3px)}.duet-expand.sc-duet-range-slider-h{width:100%!important}}.duet-m-0.sc-duet-range-slider-h{margin:0!important}.duet-range-container.sc-duet-range-slider{background:transparent;position:relative;width:100%}.duet-output.sc-duet-range-slider, .duet-range-container.sc-duet-range-slider{-webkit-appearance:none;-moz-appearance:none;appearance:none;-webkit-box-sizing:border-box;box-sizing:border-box;border:0;padding:0;margin:0}.duet-output.sc-duet-range-slider{background:transparent;position:absolute;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;font-variant-numeric:tabular-nums;font-family:localtapiola-sans,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol;background:#fff;color:#00294d;font-weight:400;line-height:1.25;font-size:.875rem;z-index:200;top:12px;right:0}.duet-theme-turva.sc-duet-range-slider .duet-output.sc-duet-range-slider{color:#111;font-family:turva-sans,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol}.duet-range.sc-duet-range-slider{-webkit-appearance:none;-moz-appearance:none;appearance:none;-webkit-box-sizing:border-box;box-sizing:border-box;background:transparent;border:0;padding:0;margin:0;background:#cfd2d4;position:relative;z-index:100;border-radius:4px;height:4px;width:100%}.duet-ie.sc-duet-range-slider .duet-range.sc-duet-range-slider{height:26px;background:transparent!important;width:auto;display:block}.duet-range.sc-duet-range-slider::-moz-range-track{-moz-appearance:none;border:0;height:1px;background:transparent}.duet-range.sc-duet-range-slider::-ms-track{background:transparent;color:transparent;border:0}.duet-range.sc-duet-range-slider:active, .duet-range.sc-duet-range-slider:focus{outline:none}.user-is-tabbing.sc-duet-range-slider-h .duet-range.sc-duet-range-slider:focus{outline:1px dotted #0077b3;outline:5px auto -webkit-focus-ring-color}.duet-range.sc-duet-range-slider::-webkit-slider-runnable-track{background:transparent}.duet-range.sc-duet-range-slider::-webkit-slider-thumb{-webkit-transition:-webkit-box-shadow .3s ease;transition:-webkit-box-shadow .3s ease;transition:box-shadow .3s ease;transition:box-shadow .3s ease,-webkit-box-shadow .3s ease;background:#fff;-webkit-appearance:none;border-radius:50%;border:1px solid #909599;position:relative;cursor:-webkit-grab;height:24px;width:24px}.duet-theme-turva.sc-duet-range-slider .duet-range.sc-duet-range-slider::-webkit-slider-thumb{border-color:#757575}.duet-range.sc-duet-range-slider:active::-webkit-slider-thumb{cursor:-webkit-grabbing}.duet-range.sc-duet-range-slider::-moz-range-track{background:transparent!important}.duet-range.sc-duet-range-slider::-moz-range-thumb{background:#fff;border:1px solid #909599;-moz-appearance:none;appearance:none;border-radius:50%;position:relative;cursor:-moz-grab;height:24px;width:24px}.duet-theme-turva.sc-duet-range-slider .duet-range.sc-duet-range-slider::-moz-range-thumb{border-color:#757575}.duet-range.sc-duet-range-slider:active::-moz-range-thumb{cursor:-moz-grabbing}.duet-range.sc-duet-range-slider::-ms-thumb{background:#fff;border:1px solid #909599;overflow:visible;border-radius:50%;position:relative;height:24px;width:24px}.duet-theme-turva.sc-duet-range-slider .duet-range.sc-duet-range-slider::-ms-thumb{border-color:#757575}.duet-range.sc-duet-range-slider::-ms-fill-lower{background:#0077b3;height:4px}.duet-theme-turva.sc-duet-range-slider .duet-range.sc-duet-range-slider::-ms-fill-lower{background:#c60c30}.duet-range.sc-duet-range-slider::-ms-fill-upper{background:#cfd2d4;height:4px}.duet-theme-turva.sc-duet-range-slider .duet-range.sc-duet-range-slider::-ms-fill-upper{background:#d1d1d1}"; }
};
exports.duet_range_slider = DuetRangeSlider;