@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
418 lines (413 loc) • 18.6 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.
*/
import { proxyCustomElement, HTMLElement, createEvent, h, Host } from '@stencil/core/internal/client';
import { b as getSlotted, f as focusElement } from './dom.js';
import { a as defaultMenuPlacement, C as CSS, u as updatePopper, c as createPopper, p as popperMenuFlipPlacements } from './popper.js';
import { c as createObserver } from './observers.js';
import { u as updateHostInteraction } from './interactive.js';
const SLOTS = {
dropdownTrigger: "dropdown-trigger"
};
const dropdownCss = "@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 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;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{display:-ms-inline-flexbox;display:inline-flex;-ms-flex:0 1 auto;flex:0 1 auto}:host([disabled]){pointer-events:none;cursor:default;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}:host .calcite-dropdown-wrapper{display:block;position:absolute;z-index:900;-webkit-transform:scale(0);transform:scale(0);visibility:hidden;pointer-events:none}.calcite-dropdown-wrapper .calcite-popper-anim{position:relative;z-index:1;-webkit-transition:var(--calcite-popper-transition);transition:var(--calcite-popper-transition);visibility:hidden;-webkit-transition-property:visibility, opacity, -webkit-transform;transition-property:visibility, opacity, -webkit-transform;transition-property:transform, visibility, opacity;transition-property:transform, visibility, opacity, -webkit-transform;opacity:0;-webkit-box-shadow:0 0 16px 0 rgba(0, 0, 0, 0.16);box-shadow:0 0 16px 0 rgba(0, 0, 0, 0.16);border-radius:0.25rem}.calcite-dropdown-wrapper[data-popper-placement^=bottom] .calcite-popper-anim{-webkit-transform:translateY(-5px);transform:translateY(-5px)}.calcite-dropdown-wrapper[data-popper-placement^=top] .calcite-popper-anim{-webkit-transform:translateY(5px);transform:translateY(5px)}.calcite-dropdown-wrapper[data-popper-placement^=left] .calcite-popper-anim{-webkit-transform:translateX(5px);transform:translateX(5px)}.calcite-dropdown-wrapper[data-popper-placement^=right] .calcite-popper-anim{-webkit-transform:translateX(-5px);transform:translateX(-5px)}.calcite-dropdown-wrapper[data-popper-placement] .calcite-popper-anim--active{opacity:1;visibility:visible;-webkit-transform:translate(0);transform:translate(0)}:host([active]) .calcite-dropdown-wrapper{pointer-events:initial;visibility:visible}:host .calcite-dropdown-content{width:auto;overflow-y:auto;overflow-x:hidden;background-color:var(--calcite-ui-foreground-1);max-height:90vh;width:var(--calcite-dropdown-width)}.calcite-dropdown-trigger-container{position:relative;display:-ms-flexbox;display:flex;-ms-flex:1 1 auto;flex:1 1 auto}:host([width=s]){--calcite-dropdown-width:12rem}:host([width=m]){--calcite-dropdown-width:14rem}:host([width=l]){--calcite-dropdown-width:16rem}";
const Dropdown = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
constructor() {
super();
this.__registerHost();
this.__attachShadow();
this.calciteDropdownSelect = createEvent(this, "calciteDropdownSelect", 7);
this.calciteDropdownOpen = createEvent(this, "calciteDropdownOpen", 7);
this.calciteDropdownClose = createEvent(this, "calciteDropdownClose", 7);
//--------------------------------------------------------------------------
//
// Public Properties
//
//--------------------------------------------------------------------------
/** Opens or closes the dropdown */
this.active = false;
/**
allow the dropdown to remain open after a selection is made
if the selection-mode of the selected item's containing group is "none", the dropdown will always close
*/
this.disableCloseOnSelect = false;
/** is the dropdown disabled */
this.disabled = false;
/**
specify the maximum number of calcite-dropdown-items to display before showing the scroller, must be greater than 0 -
this value does not include groupTitles passed to calcite-dropdown-group
*/
this.maxItems = 0;
/** Describes the type of positioning to use for the overlaid content. If your element is in a fixed container, use the 'fixed' value. */
this.overlayPositioning = "absolute";
/**
* Determines where the dropdown will be positioned relative to the button.
* @default "bottom-leading"
*/
this.placement = defaultMenuPlacement;
/** specify the scale of dropdown, defaults to m */
this.scale = "m";
/**
* **read-only** The currently selected items
*
* @readonly
*/
this.selectedItems = [];
/** specify whether the dropdown is opened by hover or click of a trigger element */
this.type = "click";
/** specify the width of dropdown, defaults to m */
this.width = "m";
//--------------------------------------------------------------------------
//
// Private State/Props
//
//--------------------------------------------------------------------------
this.items = [];
this.activeTransitionProp = "visibility";
this.mutationObserver = createObserver("mutation", () => this.updateItems());
this.resizeObserver = createObserver("resize", () => this.setMaxScrollerHeight());
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
this.updateItems = () => {
this.updateSelectedItems();
this.triggers = getSlotted(this.el, "dropdown-trigger", { all: true });
this.items = Array.from(this.el.querySelectorAll("calcite-dropdown-item"));
this.reposition();
};
this.setMaxScrollerHeight = () => {
const { active, scrollerEl } = this;
if (!scrollerEl || !active) {
return;
}
this.reposition();
const maxScrollerHeight = this.getMaxScrollerHeight();
scrollerEl.style.maxHeight = maxScrollerHeight > 0 ? `${maxScrollerHeight}px` : "";
this.reposition();
};
this.setScrollerEl = (scrollerEl) => {
this.resizeObserver.observe(scrollerEl);
this.scrollerEl = scrollerEl;
};
this.transitionEnd = (event) => {
if (event.propertyName === this.activeTransitionProp) {
this.active ? this.calciteDropdownOpen.emit() : this.calciteDropdownClose.emit();
}
};
this.setReferenceEl = (el) => {
this.referenceEl = el;
};
this.setMenuEl = (el) => {
this.menuEl = el;
};
this.keyDownHandler = (e) => {
const target = e.target;
const key = e.key;
if (this.triggers.includes(target) ||
this.triggers.some((trigger) => trigger.contains(target))) {
const ignoredNodeTypes = ["BUTTON", "CALCITE-BUTTON", "CALCITE-ACTION"];
if (ignoredNodeTypes.includes(target.nodeName)) {
if (this.active && (key === "Escape" || (e.shiftKey && key === "Tab"))) {
this.closeCalciteDropdown();
}
return;
}
switch (key) {
case " ":
case "Enter":
this.openCalciteDropdown();
break;
case "Escape":
this.closeCalciteDropdown();
break;
}
}
};
this.focusOnFirstActiveOrFirstItem = () => {
this.getFocusableElement(this.items.find((item) => item.active) || this.items[0]);
};
this.toggleOpenEnd = () => {
this.focusOnFirstActiveOrFirstItem();
this.el.removeEventListener("calciteDropdownOpen", this.toggleOpenEnd);
};
this.openCalciteDropdown = () => {
this.active = !this.active;
if (this.active) {
this.el.addEventListener("calciteDropdownOpen", this.toggleOpenEnd);
}
};
}
activeHandler() {
if (!this.disabled) {
this.reposition();
return;
}
this.active = false;
}
handleDisabledChange(value) {
if (!value) {
this.active = false;
}
}
maxItemsHandler() {
this.setMaxScrollerHeight();
}
placementHandler() {
this.reposition();
}
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
connectedCallback() {
var _a;
(_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true, subtree: true });
this.createPopper();
this.updateItems();
}
componentDidLoad() {
this.reposition();
}
componentDidRender() {
updateHostInteraction(this);
}
disconnectedCallback() {
var _a, _b;
(_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
(_b = this.resizeObserver) === null || _b === void 0 ? void 0 : _b.disconnect();
this.destroyPopper();
}
render() {
const { active } = this;
return (h(Host, null, h("div", { class: "calcite-dropdown-trigger-container", onClick: this.openCalciteDropdown, onKeyDown: this.keyDownHandler, ref: this.setReferenceEl }, h("slot", { "aria-expanded": active.toString(), "aria-haspopup": "true", name: SLOTS.dropdownTrigger })), h("div", { "aria-hidden": (!active).toString(), class: "calcite-dropdown-wrapper", ref: this.setMenuEl }, h("div", { class: {
["calcite-dropdown-content"]: true,
[CSS.animation]: true,
[CSS.animationActive]: active
}, onTransitionEnd: this.transitionEnd, ref: this.setScrollerEl }, h("div", { hidden: !this.active }, h("slot", null))))));
}
//--------------------------------------------------------------------------
//
// Public Methods
//
//--------------------------------------------------------------------------
/** Updates the position of the component. */
async reposition() {
const { popper, menuEl, placement } = this;
const modifiers = this.getModifiers();
popper
? await updatePopper({
el: menuEl,
modifiers,
placement,
popper
})
: this.createPopper();
}
closeCalciteDropdownOnClick(e) {
if (!this.active || e.composedPath().includes(this.el)) {
return;
}
this.closeCalciteDropdown(false);
}
closeCalciteDropdownOnEvent() {
this.closeCalciteDropdown();
}
closeCalciteDropdownOnOpenEvent(e) {
if (e.composedPath().includes(this.el)) {
return;
}
this.active = false;
}
mouseEnterHandler() {
if (this.type === "hover") {
this.openCalciteDropdown();
}
}
mouseLeaveHandler() {
if (this.type === "hover") {
this.closeCalciteDropdown();
}
}
calciteDropdownItemKeyEvent(e) {
const { keyboardEvent } = e.detail;
// handle edge
const target = keyboardEvent.target;
const itemToFocus = target.nodeName !== "A" ? target : target.parentNode;
const isFirstItem = this.itemIndex(itemToFocus) === 0;
const isLastItem = this.itemIndex(itemToFocus) === this.items.length - 1;
switch (keyboardEvent.key) {
case "Tab":
if (isLastItem && !keyboardEvent.shiftKey) {
this.closeCalciteDropdown();
}
else if (isFirstItem && keyboardEvent.shiftKey) {
this.closeCalciteDropdown();
}
else if (keyboardEvent.shiftKey) {
this.focusPrevItem(itemToFocus);
}
else {
this.focusNextItem(itemToFocus);
}
break;
case "ArrowDown":
this.focusNextItem(itemToFocus);
break;
case "ArrowUp":
this.focusPrevItem(itemToFocus);
break;
case "Home":
this.focusFirstItem();
break;
case "End":
this.focusLastItem();
break;
}
e.stopPropagation();
}
handleItemSelect(event) {
this.updateSelectedItems();
event.stopPropagation();
this.calciteDropdownSelect.emit();
if (!this.disableCloseOnSelect ||
event.detail.requestedDropdownGroup.selectionMode === "none") {
this.closeCalciteDropdown();
}
}
getModifiers() {
const flipModifier = {
name: "flip",
enabled: true
};
flipModifier.options = {
fallbackPlacements: this.flipPlacements || popperMenuFlipPlacements
};
const eventListenerModifier = {
name: "eventListeners",
enabled: this.active
};
return [flipModifier, eventListenerModifier];
}
createPopper() {
this.destroyPopper();
const { menuEl, referenceEl, placement, overlayPositioning } = this;
const modifiers = this.getModifiers();
this.popper = createPopper({
el: menuEl,
modifiers,
overlayPositioning,
placement,
referenceEl
});
}
destroyPopper() {
const { popper } = this;
if (popper) {
popper.destroy();
}
this.popper = null;
}
updateSelectedItems() {
const items = Array.from(this.el.querySelectorAll("calcite-dropdown-item"));
this.selectedItems = items.filter((item) => item.active);
}
getMaxScrollerHeight() {
const groups = Array.from(this.el.querySelectorAll("calcite-dropdown-group"));
const { maxItems } = this;
let itemsToProcess = 0;
let maxScrollerHeight = 0;
let groupHeaderHeight;
groups.forEach((group) => {
if (maxItems > 0 && itemsToProcess < maxItems) {
Array.from(group.children).forEach((item, index) => {
if (index === 0) {
if (isNaN(groupHeaderHeight)) {
groupHeaderHeight = item.offsetTop;
}
maxScrollerHeight += groupHeaderHeight;
}
if (itemsToProcess < maxItems) {
maxScrollerHeight += item.offsetHeight;
itemsToProcess += 1;
}
});
}
});
return maxScrollerHeight;
}
closeCalciteDropdown(focusTrigger = true) {
this.active = false;
if (focusTrigger) {
focusElement(this.triggers[0]);
}
}
focusFirstItem() {
const firstItem = this.items[0];
this.getFocusableElement(firstItem);
}
focusLastItem() {
const lastItem = this.items[this.items.length - 1];
this.getFocusableElement(lastItem);
}
focusNextItem(e) {
const index = this.itemIndex(e);
const nextItem = this.items[index + 1] || this.items[0];
this.getFocusableElement(nextItem);
}
focusPrevItem(e) {
const index = this.itemIndex(e);
const prevItem = this.items[index - 1] || this.items[this.items.length - 1];
this.getFocusableElement(prevItem);
}
itemIndex(e) {
return this.items.indexOf(e);
}
getFocusableElement(item) {
if (!item) {
return;
}
const target = item.attributes.isLink
? item.shadowRoot.querySelector("a")
: item;
focusElement(target);
}
get el() { return this; }
static get watchers() { return {
"active": ["activeHandler"],
"disabled": ["handleDisabledChange"],
"maxItems": ["maxItemsHandler"],
"placement": ["placementHandler"]
}; }
static get style() { return dropdownCss; }
}, [1, "calcite-dropdown", {
"active": [1540],
"disableCloseOnSelect": [516, "disable-close-on-select"],
"disabled": [516],
"flipPlacements": [16],
"maxItems": [2, "max-items"],
"overlayPositioning": [1, "overlay-positioning"],
"placement": [513],
"scale": [513],
"selectedItems": [1040],
"type": [513],
"width": [513],
"reposition": [64]
}, [[8, "click", "closeCalciteDropdownOnClick"], [0, "calciteDropdownCloseRequest", "closeCalciteDropdownOnEvent"], [8, "calciteDropdownOpen", "closeCalciteDropdownOnOpenEvent"], [1, "mouseenter", "mouseEnterHandler"], [1, "mouseleave", "mouseLeaveHandler"], [0, "calciteDropdownItemKeyEvent", "calciteDropdownItemKeyEvent"], [0, "calciteDropdownItemSelect", "handleItemSelect"]]]);
function defineCustomElement() {
if (typeof customElements === "undefined") {
return;
}
const components = ["calcite-dropdown"];
components.forEach(tagName => { switch (tagName) {
case "calcite-dropdown":
if (!customElements.get(tagName)) {
customElements.define(tagName, Dropdown);
}
break;
} });
}
defineCustomElement();
export { Dropdown as D, defineCustomElement as d };