@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
179 lines (173 loc) • 8.26 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
*/
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const index = require('./index-55f8a3b7.js');
const dom = require('./dom-18ca68ff.js');
const interactive = require('./interactive-26294f2c.js');
const observers = require('./observers-83b3999d.js');
const loadable = require('./loadable-53f729bb.js');
require('./guid-db20443e.js');
require('./resources-45d84c94.js');
require('./browser-28ea2ce1.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 = class {
constructor(hostRef) {
index.registerInstance(this, hostRef);
this.calciteChipGroupSelect = index.createEvent(this, "calciteChipGroupSelect", 6);
//--------------------------------------------------------------------------
//
// Private Properties
//
//--------------------------------------------------------------------------
this.mutationObserver = observers.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() {
interactive.connectInteractive(this);
this.mutationObserver?.observe(this.el, { childList: true, subtree: true });
}
componentDidRender() {
interactive.disconnectInteractive(this);
interactive.updateHostInteraction(this);
}
componentDidLoad() {
loadable.setComponentLoaded(this);
}
disconnectedCallback() {
this.mutationObserver?.disconnect();
}
async componentWillLoad() {
loadable.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":
dom.focusElementInGroup(interactiveItems, event.detail.target, "next");
break;
case "ArrowLeft":
dom.focusElementInGroup(interactiveItems, event.detail.target, "previous");
break;
case "Home":
dom.focusElementInGroup(interactiveItems, event.detail.target, "first");
break;
case "End":
dom.focusElementInGroup(interactiveItems, event.detail.target, "last");
break;
}
}
}
calciteChipCloseListener(event) {
const item = event.target;
if (this.items.includes(item)) {
if (this.items.indexOf(item) > 0) {
dom.focusElementInGroup(this.items, item, "previous");
}
else if (this.items.indexOf(item) === 0) {
dom.focusElementInGroup(this.items, item, "next");
}
else {
dom.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 loadable.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 (index.h("div", { "aria-disabled": dom.toAriaBoolean(this.disabled), "aria-label": this.label, class: "container", role: role }, index.h("slot", { onSlotchange: this.updateItems, ref: (el) => (this.slotRefEl = el) })));
}
get el() { return index.getElement(this); }
static get watchers() { return {
"selectionMode": ["onSelectionModeChange"]
}; }
};
ChipGroup.style = chipGroupCss;
exports.calcite_chip_group = ChipGroup;