UNPKG

@esri/calcite-components

Version:

Web Components for Esri's Calcite Design System.

431 lines (422 loc) • 22.6 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 { r as registerInstance, c as createEvent, h, a as getElement, H as Host } from './index-c8875db5.js'; import { c as connectInteractive, u as updateHostInteraction, d as disconnectInteractive } from './interactive-cff0a7ea.js'; import { s as setUpLoadableComponent, a as setComponentLoaded, c as componentLoaded } from './loadable-8d606217.js'; import { c as connectLocalized, d as disconnectLocalized } from './locale-0c018cdb.js'; import { c as createObserver } from './observers-d19b0d93.js'; import { u as updateMessages, c as connectMessages, s as setUpMessages, d as disconnectMessages } from './t9n-f819e5a2.js'; import { g as getItemIndex, m as mutationObserverCallback, d as deselectRemovedItems, a as deselectSiblingItems, s as selectSiblings, h as handleFilter, b as handleFilterEvent, c as getItemData, k as keyDownHandler, e as moveItemIndex, i as initialize, f as initializeObserver, j as handleInitialFilter, l as cleanUpObserver, n as calciteListFocusOutHandler, r as removeItem, o as calciteListItemChangeHandler, p as calciteInternalListItemValueChangeHandler, q as setUpItems, t as setFocus, L as List, I as ICON_TYPES$1, C as CSS$1, S as SLOTS$1 } from './resources-f20dbf1c.js'; import { d as disconnectSortableComponent, c as connectSortableComponent, o as onSortingStart, a as onSortingEnd } from './sortableComponent-5b95dae2.js'; import { f as focusElement, g as getSlotted } from './dom-4f5d1224.js'; import { c as connectConditionalSlotComponent, d as disconnectConditionalSlotComponent } from './conditionalSlot-ed85dd9d.js'; import { g as guid } from './guid-bdb80778.js'; import './browser-4be2add2.js'; import './key-582cee8f.js'; import './array-c45ef254.js'; import './debounce-ca1bd093.js'; import './resources-35f23920.js'; const CSS = { container: "container", handle: "handle" }; var ICON_TYPES; (function (ICON_TYPES) { ICON_TYPES["grip"] = "grip"; })(ICON_TYPES || (ICON_TYPES = {})); function getScreenReaderText(item, status, valueList) { const { items, messages } = valueList; const total = items.length; const position = getItemIndex(valueList, item) + 1; const template = status === "idle" ? messages.dragHandleIdle : status === "active" ? messages.dragHandleActive : status === "change" ? messages.dragHandleChange : messages.dragHandleCommit; return replacePlaceholders(template, item.label, position, total); } function getHandleAndItemElement(event) { const handle = event .composedPath() .find((item) => item.dataset?.jsHandle !== undefined); const item = event .composedPath() .find((item) => item.tagName?.toLowerCase() === "calcite-value-list-item"); return { handle, item }; } function replacePlaceholders(text, label, position, total) { const replacePosition = text.replace("${position}", position.toString()); const replaceLabel = replacePosition.replace("${itemLabel}", label); return replaceLabel.replace("${total}", total.toString()); } const valueListCss = "@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{position:relative;box-sizing:border-box;display:flex;flex-shrink:0;flex-grow:0;flex-direction:column;align-items:stretch;background-color:transparent;font-size:var(--calcite-font-size--1);color:var(--calcite-ui-text-2)}:host *{box-sizing:border-box}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}calcite-value-list-item:last-of-type{--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow)}:host([filter-enabled]) header{margin-block-end:0.25rem;display:flex;align-items:center;justify-content:flex-end;background-color:var(--calcite-ui-foreground-1);--tw-shadow:0 1px 0 var(--calcite-ui-border-3);--tw-shadow-colored:0 1px 0 var(--tw-shadow-color);box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow)}:host([filter-enabled]) header.sticky-pos{position:sticky;inset-block-start:0px;z-index:var(--calcite-app-z-index-sticky)}calcite-filter{margin-block-end:1px}.assistive-text{position:absolute;inline-size:1px;block-size:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0, 0, 0, 0);white-space:nowrap;border-width:0}"; const ValueList = class { constructor(hostRef) { registerInstance(this, hostRef); this.calciteListChange = createEvent(this, "calciteListChange", 6); this.calciteListOrderChange = createEvent(this, "calciteListOrderChange", 6); this.calciteListFilter = createEvent(this, "calciteListFilter", 6); this.lastSelectedItem = null; this.mutationObserver = createObserver("mutation", mutationObserverCallback.bind(this)); this.setFilterEl = (el) => { this.filterEl = el; }; this.setFilteredItems = (filteredItems) => { this.filteredItems = filteredItems; }; this.deselectRemovedItems = deselectRemovedItems.bind(this); this.deselectSiblingItems = deselectSiblingItems.bind(this); this.selectSiblings = selectSiblings.bind(this); this.handleFilter = handleFilter.bind(this); this.handleFilterEvent = handleFilterEvent.bind(this); this.getItemData = getItemData.bind(this); this.keyDownHandler = (event) => { if (event.defaultPrevented) { return; } const { handle, item } = getHandleAndItemElement(event); if (handle && !item.handleActivated && event.key === " ") { this.updateScreenReaderText(getScreenReaderText(item, "commit", this)); } if (!handle || !item.handleActivated) { keyDownHandler.call(this, event); return; } event.preventDefault(); const { items } = this; if (event.key === " ") { this.updateScreenReaderText(getScreenReaderText(item, "active", this)); } if ((event.key !== "ArrowUp" && event.key !== "ArrowDown") || items.length <= 1) { return; } const { el } = this; const nextIndex = moveItemIndex(this, item, event.key === "ArrowUp" ? "up" : "down"); if (nextIndex === items.length - 1) { el.appendChild(item); } else { const itemAtNextIndex = el.children[nextIndex]; const insertionReferenceItem = itemAtNextIndex === item.nextElementSibling ? itemAtNextIndex.nextElementSibling : itemAtNextIndex; el.insertBefore(item, insertionReferenceItem); } this.items = this.getItems(); this.calciteListOrderChange.emit(this.items.map(({ value }) => value)); requestAnimationFrame(() => focusElement(handle)); item.handleActivated = true; this.updateHandleAriaLabel(handle, getScreenReaderText(item, "change", this)); }; this.storeAssistiveEl = (el) => { this.assistiveTextEl = el; }; this.handleFocusIn = (event) => { const { handle, item } = getHandleAndItemElement(event); if (!item?.handleActivated && item && handle) { this.updateHandleAriaLabel(handle, getScreenReaderText(item, "idle", this)); } }; this.disabled = false; this.dragEnabled = false; this.filteredItems = []; this.filteredData = []; this.filterEnabled = false; this.filterPlaceholder = undefined; this.filterText = undefined; this.group = undefined; this.loading = false; this.multiple = false; this.selectionFollowsFocus = false; this.messageOverrides = undefined; this.messages = undefined; this.dataForFilter = []; this.defaultMessages = undefined; this.effectiveLocale = ""; this.selectedValues = new Map(); } onMessagesChange() { /* wired up by t9n util */ } effectiveLocaleChange() { updateMessages(this, this.effectiveLocale); } // -------------------------------------------------------------------------- // // Lifecycle // // -------------------------------------------------------------------------- connectedCallback() { connectInteractive(this); connectLocalized(this); connectMessages(this); initialize.call(this); initializeObserver.call(this); this.setUpSorting(); } async componentWillLoad() { setUpLoadableComponent(this); await setUpMessages(this); } componentDidLoad() { setComponentLoaded(this); handleInitialFilter.call(this); } componentDidRender() { updateHostInteraction(this); } disconnectedCallback() { disconnectInteractive(this); disconnectSortableComponent(this); disconnectLocalized(this); disconnectMessages(this); cleanUpObserver.call(this); } calciteListFocusOutHandler(event) { calciteListFocusOutHandler.call(this, event); } calciteListItemRemoveHandler(event) { removeItem.call(this, event); } calciteListItemChangeHandler(event) { calciteListItemChangeHandler.call(this, event); } calciteInternalListItemPropsChangeHandler(event) { event.stopPropagation(); this.setUpFilter(); } calciteInternalListItemValueChangeHandler(event) { calciteInternalListItemValueChangeHandler.call(this, event); event.stopPropagation(); } // -------------------------------------------------------------------------- // // Private Methods // // -------------------------------------------------------------------------- getItems() { return Array.from(this.el.querySelectorAll("calcite-value-list-item")); } setUpItems() { setUpItems.call(this, "calcite-value-list-item"); this.setUpSorting(); } setUpFilter() { if (this.filterEnabled) { this.dataForFilter = this.getItemData(); } } setUpSorting() { const { dragEnabled, group } = this; if (!dragEnabled) { return; } connectSortableComponent(this, { dataIdAttr: "id", group, handle: `.${CSS.handle}`, draggable: "calcite-value-list-item", onStart: () => { cleanUpObserver.call(this); onSortingStart(this); }, onEnd: () => { onSortingEnd(this); initializeObserver.call(this); }, onUpdate: () => { this.items = Array.from(this.el.querySelectorAll("calcite-value-list-item")); const values = this.items.map((item) => item.value); this.calciteListOrderChange.emit(values); } }); } handleBlur() { if (this.dragEnabled) { this.updateScreenReaderText(""); } } // -------------------------------------------------------------------------- // // Public Methods // // -------------------------------------------------------------------------- /** Returns the currently selected items */ async getSelectedItems() { return this.selectedValues; } /** * Sets focus on the component's first focusable element. * * @param focusId */ async setFocus(focusId) { await componentLoaded(this); return setFocus.call(this, focusId); } // -------------------------------------------------------------------------- // // Render Methods // // -------------------------------------------------------------------------- getIconType() { let type = null; if (this.dragEnabled) { type = ICON_TYPES.grip; } return type; } updateScreenReaderText(text) { this.assistiveTextEl.textContent = text; } updateHandleAriaLabel(handleElement, text) { handleElement.ariaLabel = text; } handleValueListItemBlur(event) { const { item, handle } = event.detail; if (!item?.handleActivated && item) { this.updateHandleAriaLabel(handle, getScreenReaderText(item, "idle", this)); } event.stopPropagation(); } render() { return (h(List, { onBlur: this.handleBlur, onFocusin: this.handleFocusIn, onKeyDown: this.keyDownHandler, props: this })); } static get assetsDirs() { return ["assets"]; } get el() { return getElement(this); } static get watchers() { return { "messageOverrides": ["onMessagesChange"], "effectiveLocale": ["effectiveLocaleChange"] }; } }; ValueList.style = valueListCss; const ICONS = { drag: "drag" }; const SLOTS = { actionsEnd: "actions-end", actionsStart: "actions-start" }; const valueListItemCss = "@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{margin-block-end:1px;box-sizing:border-box;display:flex;background-color:var(--calcite-ui-foreground-1);font-size:var(--calcite-font-size--1);color:var(--calcite-ui-text-2);--tw-shadow:0 1px 0 var(--calcite-ui-border-3);--tw-shadow-colored:0 1px 0 var(--tw-shadow-color);box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);transition:background-color var(--calcite-animation-timing), box-shadow var(--calcite-animation-timing)}:host *{box-sizing:border-box}calcite-pick-list-item{position:relative;margin:0px;flex-grow:1;--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow)}:host([active]),:host([selected]){--tw-shadow:0 0 0 1px var(--calcite-ui-brand);--tw-shadow-colored:0 0 0 1px var(--tw-shadow-color);box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow)}.handle{display:flex;cursor:move;align-items:center;justify-content:center;border-style:none;background-color:transparent;padding-block:0px;padding-inline:0.25rem;color:var(--calcite-ui-border-input);outline-color:transparent}.handle:hover{background-color:var(--calcite-ui-foreground-2);color:var(--calcite-ui-text-1)}.handle:focus{outline:2px solid var(--calcite-ui-focus-color, var(--calcite-ui-brand));outline-offset:calc(\n -2px *\n calc(\n 1 -\n 2 * clamp(\n 0,\n var(--calcite-ui-focus-offset-invert),\n 1\n )\n )\n )}.handle--activated{background-color:var(--calcite-ui-foreground-3);color:var(--calcite-ui-text-1)}.handle calcite-icon{color:inherit}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}"; const ValueListItem = class { constructor(hostRef) { registerInstance(this, hostRef); this.calciteListItemChange = createEvent(this, "calciteListItemChange", 6); this.calciteListItemRemove = createEvent(this, "calciteListItemRemove", 7); this.calciteValueListItemDragHandleBlur = createEvent(this, "calciteValueListItemDragHandleBlur", 6); this.pickListItem = null; this.guid = `calcite-value-list-item-${guid()}`; // -------------------------------------------------------------------------- // // Private Methods // // -------------------------------------------------------------------------- this.getPickListRef = (el) => (this.pickListItem = el); this.handleKeyDown = (event) => { if (event.key === " ") { this.handleActivated = !this.handleActivated; } }; this.handleBlur = () => { this.handleActivated = false; this.calciteValueListItemDragHandleBlur.emit({ item: this.el, handle: this.handleEl }); }; this.handleSelectChange = (event) => { this.selected = event.detail.selected; }; this.description = undefined; this.disabled = false; this.deselectDisabled = false; this.nonInteractive = false; this.handleActivated = false; this.icon = null; this.iconFlipRtl = false; this.label = undefined; this.metadata = undefined; this.removable = false; this.selected = false; this.value = undefined; } // -------------------------------------------------------------------------- // // Lifecycle // // -------------------------------------------------------------------------- connectedCallback() { connectConditionalSlotComponent(this); connectInteractive(this); } disconnectedCallback() { disconnectConditionalSlotComponent(this); disconnectInteractive(this); } componentWillLoad() { setUpLoadableComponent(this); } componentDidLoad() { setComponentLoaded(this); } componentDidRender() { updateHostInteraction(this, this.el.closest("calcite-value-list") ? "managed" : false); } // -------------------------------------------------------------------------- // // Public Methods // // -------------------------------------------------------------------------- /** * Toggle the selection state. By default this won't trigger an event. * The first argument allows the value to be coerced, rather than swapping values. * * @param coerce */ async toggleSelected(coerce) { this.pickListItem.toggleSelected(coerce); } /** Set focus on the component. */ async setFocus() { await componentLoaded(this); this.pickListItem?.setFocus(); } calciteListItemChangeHandler(event) { // adjust item payload from wrapped item before bubbling event.detail.item = this.el; } // -------------------------------------------------------------------------- // // Render Methods // // -------------------------------------------------------------------------- renderActionsEnd() { const { el } = this; const hasActionsEnd = getSlotted(el, SLOTS.actionsEnd); return hasActionsEnd ? (h("slot", { name: SLOTS.actionsEnd, slot: SLOTS$1.actionsEnd })) : null; } renderActionsStart() { const { el } = this; const hasActionsStart = getSlotted(el, SLOTS.actionsStart); return hasActionsStart ? (h("slot", { name: SLOTS.actionsStart, slot: SLOTS$1.actionsStart })) : null; } renderHandle() { const { icon, iconFlipRtl } = this; if (icon === ICON_TYPES$1.grip) { return (h("span", { class: { [CSS$1.handle]: true, [CSS$1.handleActivated]: this.handleActivated }, "data-js-handle": true, onBlur: this.handleBlur, onKeyDown: this.handleKeyDown, ref: (el) => (this.handleEl = el), role: "button", tabindex: "0" }, h("calcite-icon", { flipRtl: iconFlipRtl, icon: ICONS.drag, scale: "s" }))); } } render() { return (h(Host, { id: this.el.id || this.guid }, this.renderHandle(), h("calcite-pick-list-item", { description: this.description, deselectDisabled: this.deselectDisabled, disabled: this.disabled, label: this.label, metadata: this.metadata, nonInteractive: this.nonInteractive, onCalciteListItemChange: this.handleSelectChange, removable: this.removable, selected: this.selected, value: this.value, // eslint-disable-next-line react/jsx-sort-props ref: this.getPickListRef }, this.renderActionsStart(), this.renderActionsEnd()))); } get el() { return getElement(this); } }; ValueListItem.style = valueListItemCss; export { ValueList as calcite_value_list, ValueListItem as calcite_value_list_item };