UNPKG

@esri/calcite-components

Version:

Web Components for Esri's Calcite Design System.

231 lines (226 loc) • 9.96 kB
/*! * All material copyright ESRI, All Rights Reserved, unless otherwise specified. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details. * v1.5.0-next.4 */ import { proxyCustomElement, HTMLElement, createEvent, h, Host, Build } from '@stencil/core/internal/client'; import { c as getElementDir } from './dom.js'; import { a as afterConnectDefaultValueSet, c as connectForm, d as disconnectForm, H as HiddenFormInputSlot } from './form.js'; import { c as connectInteractive, d as disconnectInteractive, u as updateHostInteraction } from './interactive.js'; import { c as connectLabel, d as disconnectLabel } from './label2.js'; import { a as setUpLoadableComponent, s as setComponentLoaded, c as componentLoaded } from './loadable.js'; const segmentedControlCss = "@keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in-down{0%{opacity:0;transform:translate3D(0, -5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;transform:translate3D(0, 5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-right{0%{opacity:0;transform:translate3D(-5px, 0, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-left{0%{opacity:0;transform:translate3D(5px, 0, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-scale{0%{opacity:0;transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;animation-fill-mode:both;animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{animation-name:in}.calcite-animate__in-down{animation-name:in-down}.calcite-animate__in-up{animation-name:in-up}.calcite-animate__in-right{animation-name:in-right}.calcite-animate__in-left{animation-name:in-left}.calcite-animate__in-scale{animation-name:in-scale}@media (prefers-reduced-motion: reduce){:root{--calcite-internal-duration-factor:0.01}}:root{--calcite-floating-ui-transition:var(--calcite-animation-timing);--calcite-floating-ui-z-index:var(--calcite-app-z-index-dropdown)}:host([hidden]){display:none}:host([disabled]){cursor:default;-webkit-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host([disabled]) *,:host([disabled]) ::slotted(*){pointer-events:none}:host{display:flex;background-color:var(--calcite-ui-foreground-1);inline-size:-moz-fit-content;inline-size:fit-content;outline:1px solid var(--calcite-ui-border-input);outline-offset:-1px}:host([appearance=outline]){background-color:transparent}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}:host([layout=vertical]){flex-direction:column;align-items:flex-start;align-self:flex-start}:host([width=full]){inline-size:100%;min-inline-size:-moz-fit-content;min-inline-size:fit-content}:host([width=full]) ::slotted(calcite-segmented-control-item){flex:1 1 auto}:host([width=full][layout=vertical]) ::slotted(calcite-segmented-control-item){justify-content:flex-start}::slotted(input[slot=hidden-form-input]){margin:0 !important;opacity:0 !important;outline:none !important;padding:0 !important;position:absolute !important;inset:0 !important;transform:none !important;-webkit-appearance:none !important;z-index:-1 !important}"; const SegmentedControl = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement { constructor() { super(); this.__registerHost(); this.__attachShadow(); this.calciteSegmentedControlChange = createEvent(this, "calciteSegmentedControlChange", 6); //-------------------------------------------------------------------------- // // Event Listeners // //-------------------------------------------------------------------------- this.handleClick = (event) => { if (this.disabled) { return; } if (event.target.localName === "calcite-segmented-control-item") { this.selectItem(event.target, true); } }; this.appearance = "solid"; this.disabled = false; this.form = undefined; this.required = false; this.layout = "horizontal"; this.name = undefined; this.scale = "m"; this.value = null; this.selectedItem = undefined; this.width = "auto"; } valueHandler(value) { const items = this.getItems(); items.forEach((item) => (item.checked = item.value === value)); } handleSelectedItemChange(newItem, oldItem) { this.value = newItem?.value; if (newItem === oldItem) { return; } const items = this.getItems(); const match = items.filter((item) => item === newItem).pop(); if (match) { this.selectItem(match); } else if (items[0]) { items[0].tabIndex = 0; } } //-------------------------------------------------------------------------- // // Lifecycle // //-------------------------------------------------------------------------- componentWillLoad() { setUpLoadableComponent(this); const items = this.getItems(); const lastChecked = items.filter((item) => item.checked).pop(); if (lastChecked) { this.selectItem(lastChecked); } else if (items[0]) { items[0].tabIndex = 0; } } componentDidLoad() { afterConnectDefaultValueSet(this, this.value); setComponentLoaded(this); } connectedCallback() { connectInteractive(this); connectLabel(this); connectForm(this); } disconnectedCallback() { disconnectInteractive(this); disconnectLabel(this); disconnectForm(this); } componentDidRender() { updateHostInteraction(this); } render() { return (h(Host, { onClick: this.handleClick, role: "radiogroup" }, h("slot", null), h(HiddenFormInputSlot, { component: this }))); } handleSelected(event) { event.preventDefault(); this.selectItem(event.target); event.stopPropagation(); } handleKeyDown(event) { const keys = ["ArrowLeft", "ArrowUp", "ArrowRight", "ArrowDown", " "]; const { key } = event; const { el, selectedItem } = this; if (keys.indexOf(key) === -1) { return; } let adjustedKey = key; if (getElementDir(el) === "rtl") { if (key === "ArrowRight") { adjustedKey = "ArrowLeft"; } if (key === "ArrowLeft") { adjustedKey = "ArrowRight"; } } const items = this.getItems(); let selectedIndex = -1; items.forEach((item, index) => { if (item === selectedItem) { selectedIndex = index; } }); switch (adjustedKey) { case "ArrowLeft": case "ArrowUp": event.preventDefault(); const previous = selectedIndex < 1 ? items[items.length - 1] : items[selectedIndex - 1]; this.selectItem(previous, true); return; case "ArrowRight": case "ArrowDown": event.preventDefault(); const next = selectedIndex === -1 ? items[1] : items[selectedIndex + 1] || items[0]; this.selectItem(next, true); return; case " ": event.preventDefault(); this.selectItem(event.target, true); return; default: return; } } // -------------------------------------------------------------------------- // // Methods // // -------------------------------------------------------------------------- /** Sets focus on the component. */ async setFocus() { await componentLoaded(this); (this.selectedItem || this.getItems()[0])?.focus(); } //-------------------------------------------------------------------------- // // Private Methods // //-------------------------------------------------------------------------- onLabelClick() { this.setFocus(); } getItems() { return Array.from(this.el.querySelectorAll("calcite-segmented-control-item")); } selectItem(selected, emit = false) { if (selected === this.selectedItem) { return; } const items = this.getItems(); let match = null; items.forEach((item) => { const matches = item === selected; if ((matches && !item.checked) || (!matches && item.checked)) { item.checked = matches; } item.tabIndex = matches ? 0 : -1; if (matches) { match = item; if (emit) { this.calciteSegmentedControlChange.emit(); } } }); this.selectedItem = match; if (Build.isBrowser && match) { match.focus(); } } get el() { return this; } static get watchers() { return { "value": ["valueHandler"], "selectedItem": ["handleSelectedItemChange"] }; } static get style() { return segmentedControlCss; } }, [1, "calcite-segmented-control", { "appearance": [513], "disabled": [516], "form": [513], "required": [516], "layout": [513], "name": [513], "scale": [513], "value": [1025], "selectedItem": [1040], "width": [513], "setFocus": [64] }, [[0, "calciteInternalSegmentedControlItemChange", "handleSelected"], [0, "keydown", "handleKeyDown"]]]); function defineCustomElement$1() { if (typeof customElements === "undefined") { return; } const components = ["calcite-segmented-control"]; components.forEach(tagName => { switch (tagName) { case "calcite-segmented-control": if (!customElements.get(tagName)) { customElements.define(tagName, SegmentedControl); } break; } }); } defineCustomElement$1(); const CalciteSegmentedControl = SegmentedControl; const defineCustomElement = defineCustomElement$1; export { CalciteSegmentedControl, defineCustomElement };