@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
656 lines (645 loc) • 35.5 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 { r as registerInstance, c as createEvent, h, H as Host, a as getElement } from './index-c8875db5.js';
import { t as toAriaBoolean, i as isPrimaryPointerButton, j as focusElementInGroup, f as focusElement, v as getElementProp } from './dom-4f5d1224.js';
import { f as filterComputedPlacements, c as connectFloatingUI, d as defaultMenuPlacement, a as disconnectFloatingUI, F as FloatingCSS, r as reposition } from './floating-ui-06f1d078.js';
import { g as guid } from './guid-bdb80778.js';
import { c as connectInteractive, u as updateHostInteraction, d as disconnectInteractive } from './interactive-cff0a7ea.js';
import { i as isActivationKey } from './key-582cee8f.js';
import { c as componentLoaded, s as setUpLoadableComponent, a as setComponentLoaded } from './loadable-8d606217.js';
import { c as createObserver } from './observers-d19b0d93.js';
import { c as connectOpenCloseComponent, d as disconnectOpenCloseComponent } from './openCloseComponent-d4910bc4.js';
import './resources-35f23920.js';
import './browser-4be2add2.js';
import './debounce-ca1bd093.js';
const SLOTS = {
dropdownTrigger: "trigger"
};
const dropdownCss = "@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:inline-flex;flex:0 1 auto}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}:host .calcite-dropdown-wrapper{--calcite-floating-ui-z-index:var(--calcite-app-z-index-dropdown);display:block;position:absolute;z-index:var(--calcite-floating-ui-z-index);visibility:hidden}.calcite-dropdown-wrapper .calcite-floating-ui-anim{position:relative;transition:var(--calcite-floating-ui-transition);transition-property:transform, visibility, opacity;opacity:0;box-shadow:0 0 16px 0 rgba(0, 0, 0, 0.16);z-index:var(--calcite-app-z-index);border-radius:0.25rem}.calcite-dropdown-wrapper[data-placement^=bottom] .calcite-floating-ui-anim{transform:translateY(-5px)}.calcite-dropdown-wrapper[data-placement^=top] .calcite-floating-ui-anim{transform:translateY(5px)}.calcite-dropdown-wrapper[data-placement^=left] .calcite-floating-ui-anim{transform:translateX(5px)}.calcite-dropdown-wrapper[data-placement^=right] .calcite-floating-ui-anim{transform:translateX(-5px)}.calcite-dropdown-wrapper[data-placement] .calcite-floating-ui-anim--active{opacity:1;transform:translate(0)}:host([open]) .calcite-dropdown-wrapper{visibility:visible}:host .calcite-dropdown-content{max-block-size:45vh;inline-size:auto;overflow-y:auto;overflow-x:hidden;background-color:var(--calcite-ui-foreground-1);inline-size:var(--calcite-dropdown-width)}.calcite-trigger-container{position:relative;display:flex;flex:1 1 auto;word-wrap:break-word;word-break:break-word}@media (forced-colors: active){:host([open]) .calcite-dropdown-wrapper{border:1px solid canvasText}}:host([width=s]){--calcite-dropdown-width:12rem}:host([width=m]){--calcite-dropdown-width:14rem}:host([width=l]){--calcite-dropdown-width:16rem}";
const Dropdown = class {
constructor(hostRef) {
registerInstance(this, hostRef);
this.calciteDropdownSelect = createEvent(this, "calciteDropdownSelect", 6);
this.calciteDropdownBeforeClose = createEvent(this, "calciteDropdownBeforeClose", 6);
this.calciteDropdownClose = createEvent(this, "calciteDropdownClose", 6);
this.calciteDropdownBeforeOpen = createEvent(this, "calciteDropdownBeforeOpen", 6);
this.calciteDropdownOpen = createEvent(this, "calciteDropdownOpen", 6);
this.items = [];
this.groups = [];
this.mutationObserver = createObserver("mutation", () => this.updateItems());
this.resizeObserver = createObserver("resize", (entries) => this.resizeObserverCallback(entries));
this.openTransitionProp = "opacity";
this.guid = `calcite-dropdown-${guid()}`;
this.defaultAssignedElements = [];
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
this.slotChangeHandler = (event) => {
this.defaultAssignedElements = event.target.assignedElements({
flatten: true
});
this.updateItems();
};
this.setFilteredPlacements = () => {
const { el, flipPlacements } = this;
this.filteredFlipPlacements = flipPlacements
? filterComputedPlacements(flipPlacements, el)
: null;
};
this.updateTriggers = (event) => {
this.triggers = event.target.assignedElements({
flatten: true
});
this.reposition(true);
};
this.updateItems = () => {
this.items = this.groups
.map((group) => Array.from(group?.querySelectorAll("calcite-dropdown-item")))
.reduce((previousValue, currentValue) => [...previousValue, ...currentValue], []);
this.updateSelectedItems();
this.reposition(true);
};
this.updateGroups = (event) => {
const groups = event.target
.assignedElements({ flatten: true })
.filter((el) => el?.matches("calcite-dropdown-group"));
this.groups = groups;
this.updateItems();
};
this.resizeObserverCallback = (entries) => {
entries.forEach((entry) => {
const { target } = entry;
if (target === this.referenceEl) {
this.setDropdownWidth();
}
else if (target === this.scrollerEl) {
this.setMaxScrollerHeight();
}
});
};
this.setDropdownWidth = () => {
const { referenceEl, scrollerEl } = this;
const referenceElWidth = referenceEl?.clientWidth;
if (!referenceElWidth || !scrollerEl) {
return;
}
scrollerEl.style.minWidth = `${referenceElWidth}px`;
};
this.setMaxScrollerHeight = () => {
const { scrollerEl } = this;
if (!scrollerEl) {
return;
}
this.reposition(true);
const maxScrollerHeight = this.getMaxScrollerHeight();
scrollerEl.style.maxHeight = maxScrollerHeight > 0 ? `${maxScrollerHeight}px` : "";
this.reposition(true);
};
this.setScrollerAndTransitionEl = (el) => {
this.resizeObserver.observe(el);
this.scrollerEl = el;
this.transitionEl = el;
connectOpenCloseComponent(this);
};
this.setReferenceEl = (el) => {
this.referenceEl = el;
connectFloatingUI(this, this.referenceEl, this.floatingEl);
this.resizeObserver.observe(el);
};
this.setFloatingEl = (el) => {
this.floatingEl = el;
connectFloatingUI(this, this.referenceEl, this.floatingEl);
};
this.keyDownHandler = (event) => {
const target = event.target;
if (target !== this.referenceEl) {
return;
}
const { defaultPrevented, key } = event;
if (defaultPrevented) {
return;
}
if (this.open) {
if (key === "Escape") {
this.closeCalciteDropdown();
event.preventDefault();
return;
}
else if (event.shiftKey && key === "Tab") {
this.closeCalciteDropdown();
event.preventDefault();
return;
}
}
if (isActivationKey(key)) {
this.openCalciteDropdown();
event.preventDefault();
}
else if (key === "Escape") {
this.closeCalciteDropdown();
event.preventDefault();
}
};
this.focusOnFirstActiveOrFirstItem = () => {
this.getFocusableElement(this.items.find((item) => item.selected) || this.items[0]);
};
this.toggleOpenEnd = () => {
this.focusOnFirstActiveOrFirstItem();
this.el.removeEventListener("calciteDropdownOpen", this.toggleOpenEnd);
};
this.openCalciteDropdown = () => {
this.open = !this.open;
if (this.open) {
this.el.addEventListener("calciteDropdownOpen", this.toggleOpenEnd);
}
};
this.open = false;
this.closeOnSelectDisabled = false;
this.disabled = false;
this.flipPlacements = undefined;
this.maxItems = 0;
this.overlayPositioning = "absolute";
this.placement = defaultMenuPlacement;
this.scale = "m";
this.selectedItems = [];
this.type = "click";
this.width = undefined;
}
openHandler(value) {
if (!this.disabled) {
if (value) {
this.reposition(true);
}
return;
}
this.open = false;
}
handleDisabledChange(value) {
if (!value) {
this.open = false;
}
}
flipPlacementsHandler() {
this.setFilteredPlacements();
this.reposition(true);
}
maxItemsHandler() {
this.setMaxScrollerHeight();
}
overlayPositioningHandler() {
this.reposition(true);
}
placementHandler() {
this.reposition(true);
}
//--------------------------------------------------------------------------
//
// Public Methods
//
//--------------------------------------------------------------------------
/** Sets focus on the component's first focusable element. */
async setFocus() {
await componentLoaded(this);
this.el.focus();
}
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
connectedCallback() {
this.mutationObserver?.observe(this.el, { childList: true, subtree: true });
this.setFilteredPlacements();
this.reposition(true);
if (this.open) {
this.openHandler(this.open);
}
connectInteractive(this);
connectOpenCloseComponent(this);
}
componentWillLoad() {
setUpLoadableComponent(this);
}
componentDidLoad() {
setComponentLoaded(this);
this.reposition(true);
}
componentDidRender() {
updateHostInteraction(this);
}
disconnectedCallback() {
this.mutationObserver?.disconnect();
this.resizeObserver?.disconnect();
disconnectInteractive(this);
disconnectFloatingUI(this, this.referenceEl, this.floatingEl);
disconnectOpenCloseComponent(this);
}
render() {
const { open, guid } = this;
return (h(Host, null, h("div", { class: "calcite-trigger-container", id: `${guid}-menubutton`, onClick: this.openCalciteDropdown, onKeyDown: this.keyDownHandler,
// eslint-disable-next-line react/jsx-sort-props
ref: this.setReferenceEl }, h("slot", { "aria-controls": `${guid}-menu`, "aria-expanded": toAriaBoolean(open), "aria-haspopup": "menu", name: SLOTS.dropdownTrigger, onSlotchange: this.updateTriggers })), h("div", { "aria-hidden": toAriaBoolean(!open), class: "calcite-dropdown-wrapper",
// eslint-disable-next-line react/jsx-sort-props
ref: this.setFloatingEl }, h("div", { "aria-labelledby": `${guid}-menubutton`, class: {
["calcite-dropdown-content"]: true,
[FloatingCSS.animation]: true,
[FloatingCSS.animationActive]: open
}, id: `${guid}-menu`, role: "menu",
// eslint-disable-next-line react/jsx-sort-props
ref: this.setScrollerAndTransitionEl }, h("slot", { onSlotchange: this.updateGroups })))));
}
//--------------------------------------------------------------------------
//
// Public Methods
//
//--------------------------------------------------------------------------
/**
* Updates the position of the component.
*
* @param delayed
*/
async reposition(delayed = false) {
const { floatingEl, referenceEl, placement, overlayPositioning, filteredFlipPlacements } = this;
return reposition(this, {
floatingEl,
referenceEl,
overlayPositioning,
placement,
flipPlacements: filteredFlipPlacements,
type: "menu"
}, delayed);
}
closeCalciteDropdownOnClick(event) {
if (this.disabled ||
!isPrimaryPointerButton(event) ||
!this.open ||
event.composedPath().includes(this.el)) {
return;
}
this.closeCalciteDropdown(false);
}
closeCalciteDropdownOnEvent(event) {
this.closeCalciteDropdown();
event.stopPropagation();
}
closeCalciteDropdownOnOpenEvent(event) {
if (event.composedPath().includes(this.el)) {
return;
}
this.open = false;
}
pointerEnterHandler() {
if (this.disabled || this.type !== "hover") {
return;
}
this.openCalciteDropdown();
}
pointerLeaveHandler() {
if (this.disabled || this.type !== "hover") {
return;
}
this.closeCalciteDropdown();
}
calciteInternalDropdownItemKeyEvent(event) {
const { keyboardEvent } = event.detail;
const target = keyboardEvent.target;
switch (keyboardEvent.key) {
case "Tab":
if (this.items.indexOf(target) === this.items.length - 1 && !keyboardEvent.shiftKey) {
this.closeCalciteDropdown();
}
else if (this.items.indexOf(target) === 0 && keyboardEvent.shiftKey) {
this.closeCalciteDropdown();
}
break;
case "ArrowDown":
focusElementInGroup(this.items, target, "next");
break;
case "ArrowUp":
focusElementInGroup(this.items, target, "previous");
break;
case "Home":
focusElementInGroup(this.items, target, "first");
break;
case "End":
focusElementInGroup(this.items, target, "last");
break;
}
event.stopPropagation();
}
handleItemSelect(event) {
this.updateSelectedItems();
event.stopPropagation();
this.calciteDropdownSelect.emit();
if (!this.closeOnSelectDisabled ||
event.detail.requestedDropdownGroup.selectionMode === "none") {
this.closeCalciteDropdown();
}
event.stopPropagation();
}
onBeforeOpen() {
this.calciteDropdownBeforeOpen.emit();
}
onOpen() {
this.calciteDropdownOpen.emit();
}
onBeforeClose() {
this.calciteDropdownBeforeClose.emit();
}
onClose() {
this.calciteDropdownClose.emit();
}
updateSelectedItems() {
this.selectedItems = this.items.filter((item) => item.selected);
}
getMaxScrollerHeight() {
const { maxItems, items } = this;
let itemsToProcess = 0;
let maxScrollerHeight = 0;
let groupHeaderHeight;
this.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 items.length > maxItems ? maxScrollerHeight : 0;
}
closeCalciteDropdown(focusTrigger = true) {
this.open = false;
if (focusTrigger) {
focusElement(this.triggers[0]);
}
}
getFocusableElement(item) {
if (!item) {
return;
}
const target = item.attributes.isLink
? item.shadowRoot.querySelector("a")
: item;
focusElement(target);
}
static get delegatesFocus() { return true; }
get el() { return getElement(this); }
static get watchers() { return {
"open": ["openHandler"],
"disabled": ["handleDisabledChange"],
"flipPlacements": ["flipPlacementsHandler"],
"maxItems": ["maxItemsHandler"],
"overlayPositioning": ["overlayPositioningHandler"],
"placement": ["placementHandler"]
}; }
};
Dropdown.style = dropdownCss;
const CSS$1 = {
containerSmall: "container--s",
containerMedium: "container--m",
containerLarge: "container--l"
};
const dropdownGroupCss = "@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{position:relative;display:block}.container{text-align:start}.container--s{font-size:var(--calcite-font-size--2);line-height:1rem}.container--s .dropdown-title{padding:0.5rem}.container--m{font-size:var(--calcite-font-size--1);line-height:1rem}.container--m .dropdown-title{padding:0.75rem}.container--l{font-size:var(--calcite-font-size-0);line-height:1.25rem}.container--l .dropdown-title{padding:1rem}.dropdown-title{margin-block-end:-1px;display:block;cursor:default;overflow-wrap:break-word;border-width:0px;border-block-end-width:1px;border-style:solid;border-color:var(--calcite-ui-border-3);font-weight:var(--calcite-font-weight-bold);color:var(--calcite-ui-text-2)}.dropdown-separator{display:block;block-size:1px;background-color:var(--calcite-ui-border-3)}";
const DropdownGroup = class {
constructor(hostRef) {
registerInstance(this, hostRef);
this.calciteInternalDropdownItemChange = createEvent(this, "calciteInternalDropdownItemChange", 6);
this.groupTitle = undefined;
this.selectionMode = "single";
this.scale = undefined;
}
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
componentWillLoad() {
this.groupPosition = this.getGroupPosition();
}
render() {
const scale = this.scale || getElementProp(this.el, "scale", "m");
const groupTitle = this.groupTitle ? (h("span", { "aria-hidden": "true", class: "dropdown-title" }, this.groupTitle)) : null;
const dropdownSeparator = this.groupPosition > 0 ? h("div", { class: "dropdown-separator", role: "separator" }) : null;
return (h(Host, { "aria-label": this.groupTitle, role: "group" }, h("div", { class: {
container: true,
[CSS$1.containerSmall]: scale === "s",
[CSS$1.containerMedium]: scale === "m",
[CSS$1.containerLarge]: scale === "l"
}, title: this.groupTitle }, dropdownSeparator, groupTitle, h("slot", null))));
}
//--------------------------------------------------------------------------
//
// Event Listeners
//
//--------------------------------------------------------------------------
updateActiveItemOnChange(event) {
this.requestedDropdownGroup = event.detail.requestedDropdownGroup;
this.requestedDropdownItem = event.detail.requestedDropdownItem;
this.calciteInternalDropdownItemChange.emit({
requestedDropdownGroup: this.requestedDropdownGroup,
requestedDropdownItem: this.requestedDropdownItem
});
}
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
getGroupPosition() {
return Array.prototype.indexOf.call(this.el.parentElement.querySelectorAll("calcite-dropdown-group"), this.el);
}
static get delegatesFocus() { return true; }
get el() { return getElement(this); }
};
DropdownGroup.style = dropdownGroupCss;
const CSS = {
containerLink: "container--link",
containerSmall: "container--s",
containerMedium: "container--m",
containerLarge: "container--l",
containerMulti: "container--multi-selection",
containerSingle: "container--single-selection",
containerNone: "container--none-selection",
icon: "dropdown-item-icon",
iconEnd: "dropdown-item-icon-end",
iconStart: "dropdown-item-icon-start",
itemContent: "dropdown-item-content",
link: "dropdown-link"
};
const dropdownItemCss = "@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}.container--s{padding-block:0.25rem;font-size:var(--calcite-font-size--2);line-height:1rem;padding-inline-end:0.5rem;padding-inline-start:1.5rem}.container--m{padding-block:0.5rem;font-size:var(--calcite-font-size--1);line-height:1rem;padding-inline-end:0.75rem;padding-inline-start:2rem}.container--l{padding-block:0.625rem;font-size:var(--calcite-font-size-0);line-height:1.25rem;padding-inline-end:1rem;padding-inline-start:2.5rem}.container--s.container--none-selection{padding-inline-start:0.25rem}.container--s.container--none-selection .dropdown-link{padding-inline-start:0px}.container--m.container--none-selection{padding-inline-start:0.5rem}.container--m.container--none-selection .dropdown-link{padding-inline-start:0px}.container--l.container--none-selection{padding-inline-start:0.75rem}.container--l.container--none-selection .dropdown-link{padding-inline-start:0px}:host{position:relative;display:flex;flex-grow:1;align-items:center}.container{position:relative;display:flex;flex-grow:1;cursor:pointer;align-items:center;color:var(--calcite-ui-text-3);text-decoration-line:none;transition-duration:150ms;transition-timing-function:cubic-bezier(0.4, 0, 0.2, 1);text-align:start}.dropdown-item-content{flex:1 1 auto;padding-block:0.125rem;padding-inline-end:auto;padding-inline-start:0.25rem}:host,.container--link a{outline-color:transparent}:host(:focus){outline:2px solid transparent;outline-offset:2px;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 )}.container--link{padding:0px}.container--link a{position:relative;display:flex;flex-grow:1;cursor:pointer;align-items:center;color:var(--calcite-ui-text-3);text-decoration-line:none;transition-duration:150ms;transition-timing-function:cubic-bezier(0.4, 0, 0.2, 1)}.container--s .dropdown-link{padding-block:0.25rem;font-size:var(--calcite-font-size--2);line-height:1rem;padding-inline-end:0.5rem;padding-inline-start:1.5rem}.container--m .dropdown-link{padding-block:0.5rem;font-size:var(--calcite-font-size--1);line-height:1rem;padding-inline-end:0.75rem;padding-inline-start:2rem}.container--l .dropdown-link{padding-block:0.75rem;font-size:var(--calcite-font-size-0);line-height:1.25rem;padding-inline-end:1rem;padding-inline-start:2.5rem}:host(:hover) .container,:host(:active) .container{background-color:var(--calcite-ui-foreground-2);color:var(--calcite-ui-text-1);text-decoration-line:none}:host(:hover) .container--link .dropdown-link,:host(:active) .container--link .dropdown-link{color:var(--calcite-ui-text-1)}:host(:focus) .container{color:var(--calcite-ui-text-1);text-decoration-line:none}:host(:active) .container{background-color:var(--calcite-ui-foreground-3)}:host(:hover) .container:before,:host(:active) .container:before,:host(:focus) .container:before{opacity:1}:host([selected]) .container:not(.container--none-selection),:host([selected]) .container--link .dropdown-link{font-weight:var(--calcite-font-weight-medium);color:var(--calcite-ui-text-1)}:host([selected]) .container:not(.container--none-selection):before,:host([selected]) .container--link .dropdown-link:before{opacity:1;color:var(--calcite-ui-brand)}:host([selected]) .container:not(.container--none-selection) calcite-icon,:host([selected]) .container--link .dropdown-link calcite-icon{color:var(--calcite-ui-brand)}.container--multi-selection:before,.container--none-selection:before{display:none}.container--s:before{inset-inline-start:0.5rem}.container--m:before{inset-inline-start:0.75rem}.container--l:before{inset-inline-start:1rem}.dropdown-item-icon{position:absolute;opacity:0;transition-duration:150ms;transition-timing-function:cubic-bezier(0.4, 0, 0.2, 1);transform:scale(0.9)}.container--s .dropdown-item-icon{inset-inline-start:0.25rem}.container--m .dropdown-item-icon{inset-inline-start:0.5rem}.container--l .dropdown-item-icon{inset-inline-start:0.75rem}:host(:hover) .dropdown-item-icon{color:var(--calcite-ui-border-1);opacity:1}:host([selected]) .dropdown-item-icon{color:var(--calcite-ui-brand);opacity:1}.container--s .dropdown-item-icon-start{margin-inline-end:0.5rem;margin-inline-start:0.25rem}.container--s .dropdown-item-icon-end{margin-inline-start:0.5rem}.container--m .dropdown-item-icon-start{margin-inline-end:0.75rem;margin-inline-start:0.25rem}.container--m .dropdown-item-icon-end{margin-inline-start:0.75rem}.container--l .dropdown-item-icon-start{margin-inline-end:1rem;margin-inline-start:0.25rem}.container--l .dropdown-item-icon-end{margin-inline-start:1rem}";
const DropdownItem = class {
constructor(hostRef) {
registerInstance(this, hostRef);
this.calciteDropdownItemSelect = createEvent(this, "calciteDropdownItemSelect", 6);
this.calciteInternalDropdownItemSelect = createEvent(this, "calciteInternalDropdownItemSelect", 6);
this.calciteInternalDropdownItemKeyEvent = createEvent(this, "calciteInternalDropdownItemKeyEvent", 6);
this.calciteInternalDropdownCloseRequest = createEvent(this, "calciteInternalDropdownCloseRequest", 6);
/** Specifies the scale of dropdown-item controlled by the parent, defaults to m */
this.scale = "m";
this.selected = false;
this.iconFlipRtl = undefined;
this.iconStart = undefined;
this.iconEnd = undefined;
this.href = undefined;
this.label = undefined;
this.rel = undefined;
this.target = undefined;
}
//--------------------------------------------------------------------------
//
// Public Methods
//
//--------------------------------------------------------------------------
/** Sets focus on the component. */
async setFocus() {
await componentLoaded(this);
this.el?.focus();
}
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
componentWillLoad() {
setUpLoadableComponent(this);
this.initialize();
}
componentDidLoad() {
setComponentLoaded(this);
}
connectedCallback() {
this.initialize();
}
render() {
const scale = getElementProp(this.el, "scale", this.scale);
const iconStartEl = (h("calcite-icon", { class: CSS.iconStart, flipRtl: this.iconFlipRtl === "start" || this.iconFlipRtl === "both", icon: this.iconStart, scale: scale === "l" ? "m" : "s" }));
const contentNode = (h("span", { class: CSS.itemContent }, h("slot", null)));
const iconEndEl = (h("calcite-icon", { class: CSS.iconEnd, flipRtl: this.iconFlipRtl === "end" || this.iconFlipRtl === "both", icon: this.iconEnd, scale: scale === "l" ? "m" : "s" }));
const slottedContent = this.iconStart && this.iconEnd
? [iconStartEl, contentNode, iconEndEl]
: this.iconStart
? [iconStartEl, contentNode]
: this.iconEnd
? [contentNode, iconEndEl]
: contentNode;
const contentEl = !this.href ? (slottedContent) : (h("a", { "aria-label": this.label, class: CSS.link, href: this.href, rel: this.rel, tabIndex: -1, target: this.target,
// eslint-disable-next-line react/jsx-sort-props
ref: (el) => (this.childLink = el) }, slottedContent));
const itemRole = this.href
? null
: this.selectionMode === "single"
? "menuitemradio"
: this.selectionMode === "multiple"
? "menuitemcheckbox"
: "menuitem";
const itemAria = this.selectionMode !== "none" ? toAriaBoolean(this.selected) : null;
return (h(Host, { "aria-checked": itemAria, role: itemRole, tabindex: "0" }, h("div", { class: {
container: true,
[CSS.containerLink]: !!this.href,
[CSS.containerSmall]: scale === "s",
[CSS.containerMedium]: scale === "m",
[CSS.containerLarge]: scale === "l",
[CSS.containerMulti]: this.selectionMode === "multiple",
[CSS.containerSingle]: this.selectionMode === "single",
[CSS.containerNone]: this.selectionMode === "none"
} }, this.selectionMode !== "none" ? (h("calcite-icon", { class: CSS.icon, icon: this.selectionMode === "multiple" ? "check" : "bullet-point", scale: scale === "l" ? "m" : "s" })) : null, contentEl)));
}
//--------------------------------------------------------------------------
//
// Event Listeners
//
//--------------------------------------------------------------------------
onClick() {
this.emitRequestedItem();
}
keyDownHandler(event) {
switch (event.key) {
case " ":
case "Enter":
this.emitRequestedItem();
if (this.href) {
this.childLink.click();
}
event.preventDefault();
break;
case "Escape":
this.calciteInternalDropdownCloseRequest.emit();
event.preventDefault();
break;
case "Tab":
this.calciteInternalDropdownItemKeyEvent.emit({ keyboardEvent: event });
break;
case "ArrowUp":
case "ArrowDown":
case "Home":
case "End":
event.preventDefault();
this.calciteInternalDropdownItemKeyEvent.emit({ keyboardEvent: event });
break;
}
}
updateActiveItemOnChange(event) {
const parentEmittedChange = event.composedPath().includes(this.parentDropdownGroupEl);
if (parentEmittedChange) {
this.requestedDropdownGroup = event.detail.requestedDropdownGroup;
this.requestedDropdownItem = event.detail.requestedDropdownItem;
this.determineActiveItem();
}
event.stopPropagation();
}
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
initialize() {
this.selectionMode = getElementProp(this.el, "selection-mode", "single");
this.parentDropdownGroupEl = this.el.closest("calcite-dropdown-group");
if (this.selectionMode === "none") {
this.selected = false;
}
}
determineActiveItem() {
switch (this.selectionMode) {
case "multiple":
if (this.el === this.requestedDropdownItem) {
this.selected = !this.selected;
}
break;
case "single":
if (this.el === this.requestedDropdownItem) {
this.selected = true;
}
else if (this.requestedDropdownGroup === this.parentDropdownGroupEl) {
this.selected = false;
}
break;
case "none":
this.selected = false;
break;
}
}
emitRequestedItem() {
this.calciteDropdownItemSelect.emit();
this.calciteInternalDropdownItemSelect.emit({
requestedDropdownItem: this.el,
requestedDropdownGroup: this.parentDropdownGroupEl
});
}
get el() { return getElement(this); }
};
DropdownItem.style = dropdownItemCss;
export { Dropdown as calcite_dropdown, DropdownGroup as calcite_dropdown_group, DropdownItem as calcite_dropdown_item };