@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
436 lines (425 loc) • 22.3 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 interactive = require('./interactive-26294f2c.js');
const loadable = require('./loadable-53f729bb.js');
const locale = require('./locale-fc347462.js');
const observers = require('./observers-83b3999d.js');
const t9n = require('./t9n-14d528c4.js');
const resources = require('./resources-2e69630c.js');
const sortableComponent = require('./sortableComponent-48a428a7.js');
const dom = require('./dom-18ca68ff.js');
const conditionalSlot = require('./conditionalSlot-4472b3c1.js');
const guid = require('./guid-db20443e.js');
require('./browser-28ea2ce1.js');
require('./key-2ce02f02.js');
require('./array-056ac2a2.js');
require('./debounce-492319d0.js');
require('./resources-45d84c94.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 = resources.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) {
index.registerInstance(this, hostRef);
this.calciteListChange = index.createEvent(this, "calciteListChange", 6);
this.calciteListOrderChange = index.createEvent(this, "calciteListOrderChange", 6);
this.calciteListFilter = index.createEvent(this, "calciteListFilter", 6);
this.lastSelectedItem = null;
this.mutationObserver = observers.createObserver("mutation", resources.mutationObserverCallback.bind(this));
this.setFilterEl = (el) => {
this.filterEl = el;
};
this.setFilteredItems = (filteredItems) => {
this.filteredItems = filteredItems;
};
this.deselectRemovedItems = resources.deselectRemovedItems.bind(this);
this.deselectSiblingItems = resources.deselectSiblingItems.bind(this);
this.selectSiblings = resources.selectSiblings.bind(this);
this.handleFilter = resources.handleFilter.bind(this);
this.handleFilterEvent = resources.handleFilterEvent.bind(this);
this.getItemData = resources.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) {
resources.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 = resources.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(() => dom.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() {
t9n.updateMessages(this, this.effectiveLocale);
}
// --------------------------------------------------------------------------
//
// Lifecycle
//
// --------------------------------------------------------------------------
connectedCallback() {
interactive.connectInteractive(this);
locale.connectLocalized(this);
t9n.connectMessages(this);
resources.initialize.call(this);
resources.initializeObserver.call(this);
this.setUpSorting();
}
async componentWillLoad() {
loadable.setUpLoadableComponent(this);
await t9n.setUpMessages(this);
}
componentDidLoad() {
loadable.setComponentLoaded(this);
resources.handleInitialFilter.call(this);
}
componentDidRender() {
interactive.updateHostInteraction(this);
}
disconnectedCallback() {
interactive.disconnectInteractive(this);
sortableComponent.disconnectSortableComponent(this);
locale.disconnectLocalized(this);
t9n.disconnectMessages(this);
resources.cleanUpObserver.call(this);
}
calciteListFocusOutHandler(event) {
resources.calciteListFocusOutHandler.call(this, event);
}
calciteListItemRemoveHandler(event) {
resources.removeItem.call(this, event);
}
calciteListItemChangeHandler(event) {
resources.calciteListItemChangeHandler.call(this, event);
}
calciteInternalListItemPropsChangeHandler(event) {
event.stopPropagation();
this.setUpFilter();
}
calciteInternalListItemValueChangeHandler(event) {
resources.calciteInternalListItemValueChangeHandler.call(this, event);
event.stopPropagation();
}
// --------------------------------------------------------------------------
//
// Private Methods
//
// --------------------------------------------------------------------------
getItems() {
return Array.from(this.el.querySelectorAll("calcite-value-list-item"));
}
setUpItems() {
resources.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;
}
sortableComponent.connectSortableComponent(this, {
dataIdAttr: "id",
group,
handle: `.${CSS.handle}`,
draggable: "calcite-value-list-item",
onStart: () => {
resources.cleanUpObserver.call(this);
sortableComponent.onSortingStart(this);
},
onEnd: () => {
sortableComponent.onSortingEnd(this);
resources.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 loadable.componentLoaded(this);
return resources.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 (index.h(resources.List, { onBlur: this.handleBlur, onFocusin: this.handleFocusIn, onKeyDown: this.keyDownHandler, props: this }));
}
static get assetsDirs() { return ["assets"]; }
get el() { return index.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) {
index.registerInstance(this, hostRef);
this.calciteListItemChange = index.createEvent(this, "calciteListItemChange", 6);
this.calciteListItemRemove = index.createEvent(this, "calciteListItemRemove", 7);
this.calciteValueListItemDragHandleBlur = index.createEvent(this, "calciteValueListItemDragHandleBlur", 6);
this.pickListItem = null;
this.guid = `calcite-value-list-item-${guid.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() {
conditionalSlot.connectConditionalSlotComponent(this);
interactive.connectInteractive(this);
}
disconnectedCallback() {
conditionalSlot.disconnectConditionalSlotComponent(this);
interactive.disconnectInteractive(this);
}
componentWillLoad() {
loadable.setUpLoadableComponent(this);
}
componentDidLoad() {
loadable.setComponentLoaded(this);
}
componentDidRender() {
interactive.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 loadable.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 = dom.getSlotted(el, SLOTS.actionsEnd);
return hasActionsEnd ? (index.h("slot", { name: SLOTS.actionsEnd, slot: resources.SLOTS.actionsEnd })) : null;
}
renderActionsStart() {
const { el } = this;
const hasActionsStart = dom.getSlotted(el, SLOTS.actionsStart);
return hasActionsStart ? (index.h("slot", { name: SLOTS.actionsStart, slot: resources.SLOTS.actionsStart })) : null;
}
renderHandle() {
const { icon, iconFlipRtl } = this;
if (icon === resources.ICON_TYPES.grip) {
return (index.h("span", { class: {
[resources.CSS.handle]: true,
[resources.CSS.handleActivated]: this.handleActivated
}, "data-js-handle": true, onBlur: this.handleBlur, onKeyDown: this.handleKeyDown, ref: (el) => (this.handleEl = el), role: "button", tabindex: "0" }, index.h("calcite-icon", { flipRtl: iconFlipRtl, icon: ICONS.drag, scale: "s" })));
}
}
render() {
return (index.h(index.Host, { id: this.el.id || this.guid }, this.renderHandle(), index.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 index.getElement(this); }
};
ValueListItem.style = valueListItemCss;
exports.calcite_value_list = ValueList;
exports.calcite_value_list_item = ValueListItem;