@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
198 lines (193 loc) • 9.07 kB
JavaScript
/*!
* 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 } from '@stencil/core/internal/client';
import { i as focusElementInGroup, t as toAriaBoolean } from './dom.js';
import { c as connectInteractive, d as disconnectInteractive, u as updateHostInteraction } from './interactive.js';
import { c as createObserver } from './observers.js';
import { s as setComponentLoaded, a as setUpLoadableComponent, c as componentLoaded } from './loadable.js';
const chipGroupCss = "@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}.container{display:flex;inline-size:100%;flex-wrap:wrap;gap:0.5rem}::slotted(calcite-chip){flex:none}:host([scale=s]) .container{gap:0.25rem}:host([scale=l]) .container{gap:0.75rem}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}";
const ChipGroup = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
constructor() {
super();
this.__registerHost();
this.__attachShadow();
this.calciteChipGroupSelect = createEvent(this, "calciteChipGroupSelect", 6);
//--------------------------------------------------------------------------
//
// Private Properties
//
//--------------------------------------------------------------------------
this.mutationObserver = createObserver("mutation", () => this.updateItems());
this.items = [];
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
this.updateItems = (event) => {
const target = event ? event.target : this.slotRefEl;
this.items = target
.assignedElements({ flatten: true })
.filter((el) => el?.matches("calcite-chip"));
this.items.forEach((el) => {
el.interactive = true;
el.scale = this.scale;
el.selectionMode = this.selectionMode;
});
this.setSelectedItems(false);
};
this.setSelectedItems = (emit, elToMatch) => {
if (elToMatch) {
this.items.forEach((el) => {
const matchingEl = elToMatch === el;
switch (this.selectionMode) {
case "multiple":
if (matchingEl) {
el.selected = !el.selected;
}
break;
case "single":
el.selected = matchingEl ? !el.selected : false;
break;
case "single-persist":
el.selected = !!matchingEl;
break;
}
});
}
this.selectedItems = this.items.filter((el) => el.selected);
if (emit) {
this.calciteChipGroupSelect.emit();
}
};
this.disabled = false;
this.label = undefined;
this.scale = "m";
this.selectionMode = "none";
this.selectedItems = [];
}
onSelectionModeChange() {
this.updateItems();
}
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
connectedCallback() {
connectInteractive(this);
this.mutationObserver?.observe(this.el, { childList: true, subtree: true });
}
componentDidRender() {
disconnectInteractive(this);
updateHostInteraction(this);
}
componentDidLoad() {
setComponentLoaded(this);
}
disconnectedCallback() {
this.mutationObserver?.disconnect();
}
async componentWillLoad() {
setUpLoadableComponent(this);
}
//--------------------------------------------------------------------------
//
// Event Listeners
//
//--------------------------------------------------------------------------
calciteInternalChipKeyEventListener(event) {
if (event.composedPath().includes(this.el)) {
const interactiveItems = this.items.filter((el) => !el.disabled);
switch (event.detail.key) {
case "ArrowRight":
focusElementInGroup(interactiveItems, event.detail.target, "next");
break;
case "ArrowLeft":
focusElementInGroup(interactiveItems, event.detail.target, "previous");
break;
case "Home":
focusElementInGroup(interactiveItems, event.detail.target, "first");
break;
case "End":
focusElementInGroup(interactiveItems, event.detail.target, "last");
break;
}
}
}
calciteChipCloseListener(event) {
const item = event.target;
if (this.items.includes(item)) {
if (this.items.indexOf(item) > 0) {
focusElementInGroup(this.items, item, "previous");
}
else if (this.items.indexOf(item) === 0) {
focusElementInGroup(this.items, item, "next");
}
else {
focusElementInGroup(this.items, item, "first");
}
}
this.items = this.items.filter((el) => el !== item);
}
calciteChipSelectListener(event) {
if (event.composedPath().includes(this.el)) {
this.setSelectedItems(true, event.target);
}
}
// --------------------------------------------------------------------------
//
// Public Methods
//
// --------------------------------------------------------------------------
/**
* Sets focus on the component's first focusable element.
*/
async setFocus() {
await componentLoaded(this);
if (!this.disabled) {
(this.selectedItems[0] || this.items[0])?.setFocus();
}
}
//--------------------------------------------------------------------------
//
// Render Methods
//
//--------------------------------------------------------------------------
render() {
const role = this.selectionMode === "none" || this.selectionMode === "multiple" ? "group" : "radiogroup";
return (h("div", { "aria-disabled": toAriaBoolean(this.disabled), "aria-label": this.label, class: "container", role: role }, h("slot", { onSlotchange: this.updateItems, ref: (el) => (this.slotRefEl = el) })));
}
get el() { return this; }
static get watchers() { return {
"selectionMode": ["onSelectionModeChange"]
}; }
static get style() { return chipGroupCss; }
}, [1, "calcite-chip-group", {
"disabled": [516],
"label": [1],
"scale": [513],
"selectionMode": [513, "selection-mode"],
"selectedItems": [1040],
"setFocus": [64]
}, [[0, "calciteInternalChipKeyEvent", "calciteInternalChipKeyEventListener"], [0, "calciteChipClose", "calciteChipCloseListener"], [0, "calciteChipSelect", "calciteChipSelectListener"]]]);
function defineCustomElement$1() {
if (typeof customElements === "undefined") {
return;
}
const components = ["calcite-chip-group"];
components.forEach(tagName => { switch (tagName) {
case "calcite-chip-group":
if (!customElements.get(tagName)) {
customElements.define(tagName, ChipGroup);
}
break;
} });
}
defineCustomElement$1();
const CalciteChipGroup = ChipGroup;
const defineCustomElement = defineCustomElement$1;
export { CalciteChipGroup, defineCustomElement };