UNPKG

@esri/calcite-components

Version:

Web Components for Esri's Calcite Design System.

496 lines (486 loc) • 25.4 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 */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); const index = require('./index-55f8a3b7.js'); const dom = require('./dom-18ca68ff.js'); const loadable = require('./loadable-53f729bb.js'); const locale = require('./locale-fc347462.js'); const t9n = require('./t9n-14d528c4.js'); const observers = require('./observers-83b3999d.js'); const resources = require('./resources-45d84c94.js'); require('./guid-db20443e.js'); require('./key-2ce02f02.js'); const allowedGlobalAttributes = ["lang", "role"]; const elementToComponentAndObserverOptionsMap = new Map(); let mutationObserver; function updateGlobalAttributes(component, attributeFilter, reuseAttributes = false) { const { el } = component; const updatedAttributes = reuseAttributes ? component.globalAttributes : {}; attributeFilter .filter((attr) => !!allowedGlobalAttributes.includes(attr) && !!el.hasAttribute(attr)) .forEach((attr) => { const value = el.getAttribute(attr); if (value !== null) { updatedAttributes[attr] = value; } }); component.globalAttributes = updatedAttributes; } function processMutations(mutations) { mutations.forEach(({ target }) => { const [component, options] = elementToComponentAndObserverOptionsMap.get(target); updateGlobalAttributes(component, options.attributeFilter); }); } /** * Helper to set up listening for changes to global attributes. * * render(): VNode { * return (<Host> * <ul {...this.globalAttributes}></div> * </Host>); * } * * @param component * @param attributeFilter */ function watchGlobalAttributes(component, attributeFilter) { const { el } = component; const observerOptions = { attributeFilter }; elementToComponentAndObserverOptionsMap.set(el, [component, observerOptions]); updateGlobalAttributes(component, attributeFilter, true); if (!mutationObserver) { mutationObserver = observers.createObserver("mutation", processMutations); } mutationObserver.observe(el, observerOptions); } /** * Helper remove listening for changes to inherited attributes. * * @param component */ function unwatchGlobalAttributes(component) { elementToComponentAndObserverOptionsMap.delete(component.el); // we explicitly process queued mutations and disconnect and reconnect // the observer until MutationObserver gets an `unobserve` method // see https://github.com/whatwg/dom/issues/126 processMutations(mutationObserver.takeRecords()); mutationObserver.disconnect(); for (const [element, [, observerOptions]] of elementToComponentAndObserverOptionsMap.entries()) { mutationObserver.observe(element, observerOptions); } } const menuCss = "@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{display:flex}ul{margin:0px;display:inline-flex;block-size:100%;align-items:center;padding:0px}:host([layout=vertical]) ul{display:flex;inline-size:100%;flex-direction:column}"; const CalciteMenu = class { constructor(hostRef) { index.registerInstance(this, hostRef); this.menuItems = []; //-------------------------------------------------------------------------- // // Private Methods // //-------------------------------------------------------------------------- this.handleMenuSlotChange = (event) => { this.menuItems = dom.slotChangeGetAssignedElements(event); this.setMenuItemLayout(this.menuItems, this.layout); }; this.label = undefined; this.layout = "horizontal"; this.messageOverrides = undefined; this.messages = undefined; this.defaultMessages = undefined; this.effectiveLocale = ""; this.globalAttributes = { role: "menubar" }; } handleLayoutChange(value) { this.setMenuItemLayout(this.menuItems, value); } onMessagesChange() { /* wired up by t9n util */ } effectiveLocaleChange() { t9n.updateMessages(this, this.effectiveLocale); } //-------------------------------------------------------------------------- // // Lifecycle // //-------------------------------------------------------------------------- connectedCallback() { locale.connectLocalized(this); t9n.connectMessages(this); watchGlobalAttributes(this, ["role"]); } async componentWillLoad() { loadable.setUpLoadableComponent(this); await t9n.setUpMessages(this); } componentDidLoad() { loadable.setComponentLoaded(this); } disconnectedCallback() { locale.disconnectLocalized(this); t9n.disconnectMessages(this); unwatchGlobalAttributes(this); } //-------------------------------------------------------------------------- // // Public Methods // //-------------------------------------------------------------------------- /** Sets focus on the component's first focusable element. */ async setFocus() { await loadable.componentLoaded(this); this.el.focus(); } //-------------------------------------------------------------------------- // // Event Listeners // //-------------------------------------------------------------------------- calciteInternalNavMenuItemKeyEvent(event) { const target = event.target; const submenuItems = event.detail.children; const key = event.detail.event.key; event.stopPropagation(); if (key === "ArrowDown") { if (target.layout === "vertical") { dom.focusElementInGroup(this.menuItems, target, "next", false); } else { if (event.detail.isSubmenuOpen) { submenuItems[0].setFocus(); } } } else if (key === "ArrowUp") { if (this.layout === "vertical") { dom.focusElementInGroup(this.menuItems, target, "previous", false); } else { if (event.detail.isSubmenuOpen) { submenuItems[submenuItems.length - 1].setFocus(); } } } else if (key === "ArrowRight") { if (this.layout === "horizontal") { dom.focusElementInGroup(this.menuItems, target, "next", false); } else { if (event.detail.isSubmenuOpen) { submenuItems[0].setFocus(); } } } else if (key === "ArrowLeft") { if (this.layout === "horizontal") { dom.focusElementInGroup(this.menuItems, target, "previous", false); } else { if (event.detail.isSubmenuOpen) { this.focusParentElement(event.target); } } } else if (key === "Escape") { this.focusParentElement(event.target); } event.preventDefault(); } focusParentElement(el) { const parentEl = el.parentElement; if (parentEl) { dom.focusElement(parentEl); parentEl.open = false; } } setMenuItemLayout(items, layout) { items.forEach((item) => { item.layout = layout; if (this.globalAttributes.role === "menubar") { item.isTopLevelItem = true; item.topLevelMenuLayout = this.layout; } }); } // -------------------------------------------------------------------------- // // Render Methods // // -------------------------------------------------------------------------- render() { return (index.h(index.Host, null, index.h("ul", { "aria-label": this.label, ...this.globalAttributes }, index.h("slot", { onSlotchange: this.handleMenuSlotChange })))); } static get delegatesFocus() { return true; } static get assetsDirs() { return ["assets"]; } get el() { return index.getElement(this); } static get watchers() { return { "layout": ["handleLayoutChange"], "messageOverrides": ["onMessagesChange"], "effectiveLocale": ["effectiveLocaleChange"] }; } }; CalciteMenu.style = menuCss; const CSS = { container: "container", content: "content", dropdownVertical: "dropdown--vertical", dropdownMenuItems: "dropdown-menu-items", dropdownAction: "dropdown-action", layoutVertical: "layout--vertical", hoverHrefIcon: "hover-href-icon", icon: "icon", iconBreadcrumb: "icon--breadcrumb", iconDropdown: "icon--dropdown", iconEnd: "icon--end", iconStart: "icon--start", isParentVertical: "parent--vertical", itemContent: "item-content", open: "open", nested: "nested", textContainer: "text-container" }; const menuItemCss = "@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;box-sizing:border-box;display:flex;align-items:center;flex-shrink:0}:host .container,:host .item-content,:host .content{min-block-size:3rem}:host([layout=vertical]){inline-size:100%}:host(:not([layout=vertical])){block-size:100%}.container,.item-content{display:flex;block-size:100%;inline-size:100%;flex-direction:row;align-items:stretch}.content{position:relative;box-sizing:border-box;display:flex;block-size:100%;inline-size:100%;cursor:pointer;align-items:center;justify-content:center;background-color:var(--calcite-ui-foreground-1);padding-inline:1rem;font-size:var(--calcite-font-size-0);color:var(--calcite-ui-text-2);outline:2px solid transparent;outline-offset:2px;text-decoration:none;border-block-end:0.125rem solid transparent;padding-block-start:0.125rem}.content:hover{background-color:var(--calcite-ui-foreground-2);color:var(--calcite-ui-text-2)}.content:focus{background-color:var(--calcite-ui-foreground-2);color:var(--calcite-ui-text-2);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 )}.content:active{background-color:var(--calcite-ui-foreground-3);color:var(--calcite-ui-text-1)}.content span{display:inline-flex}.content.layout--vertical{display:flex;inline-size:100%;justify-content:flex-start;padding-block:1rem;border-block-end:0;border-inline-end:0.25rem solid transparent}:host([active]) .content{color:var(--calcite-ui-text-1);border-color:var(--calcite-ui-brand)}:host([active]) .content .icon{--calcite-ui-icon-color:var(--calcite-ui-brand)}.icon--start{margin-inline-end:0.75rem}.icon--end{margin-inline-start:0.75rem}.icon--dropdown{position:relative;margin-inline-start:auto;margin-inline-end:0px;padding-inline-start:0.5rem;--calcite-ui-icon-color:var(--calcite-ui-text-3)}:host([layout=vertical]) .icon--dropdown{inset-inline-start:0.25rem}.icon--breadcrumb{margin-inline-end:0px;padding-inline-start:0.5rem;--calcite-ui-icon-color:var(--calcite-ui-text-3)}:host([breadcrumb]) .content{padding-inline-end:0.75rem}calcite-action{position:relative;block-size:auto;border-inline-start:1px solid var(--calcite-ui-foreground-1)}calcite-action:after{position:absolute;inset-inline-start:-1px;display:block;inline-size:1px;content:\"\";inset-block:0.75rem;background-color:var(--calcite-ui-border-3)}calcite-action:hover:after{block-size:100%;inset-block:0}.content:focus~calcite-action,.content:hover~calcite-action{color:var(--calcite-ui-text-1);border-inline-start:1px solid var(--calcite-ui-border-3)}.container:hover .dropdown-action{background-color:var(--calcite-ui-foreground-2)}.dropdown-menu-items{position:absolute;display:none;block-size:auto;min-inline-size:100%;flex-direction:column;overflow:visible;border:1px solid var(--calcite-ui-border-3);background:var(--calcite-ui-foreground-1);inset-block-start:100%;z-index:var(--calcite-app-z-index-dropdown)}.dropdown-menu-items.open{display:block}.dropdown-menu-items.nested{position:absolute;inset-block-start:-1px;transform:translateX(calc(100% - 2px))}.parent--vertical{flex-direction:column}.dropdown--vertical.dropdown-menu-items{position:relative;border-radius:0px;box-shadow:none;inset-block-start:0;transform:none}.dropdown--vertical.dropdown-menu-items:last-of-type{border-inline:0}:host([slot=submenu-item]) .parent--vertical{padding-inline-start:1.75rem}.dropdown-menu-items.nested.calcite--rtl{transform:translateX(calc(-100% + 2px))}.dropdown--vertical.dropdown-menu-items.nested.calcite--rtl{transform:none}.hover-href-icon{position:relative;inset-inline-end:0.25rem;margin-inline-start:auto;padding-inline-start:2rem;opacity:0;transition:all var(--calcite-internal-animation-timing-medium) ease-in-out}.content:focus .hover-href-icon,.content:hover .hover-href-icon{inset-inline-end:-0.25rem;opacity:1}"; const CalciteMenuItem = class { constructor(hostRef) { index.registerInstance(this, hostRef); this.calciteInternalMenuItemKeyEvent = index.createEvent(this, "calciteInternalMenuItemKeyEvent", 7); this.calciteMenuItemSelect = index.createEvent(this, "calciteMenuItemSelect", 7); this.clickHandler = (event) => { if ((this.href && event.target === this.dropdownActionEl) || (!this.href && this.hasSubmenu)) { this.open = !this.open; } this.selectMenuItem(event); }; this.handleMenuItemSlotChange = (event) => { this.submenuItems = dom.slotChangeGetAssignedElements(event); this.submenuItems.forEach((item) => { if (!item.topLevelMenuLayout) { item.topLevelMenuLayout = this.topLevelMenuLayout; } }); this.hasSubmenu = this.submenuItems.length > 0; }; this.keyDownHandler = async (event) => { // opening and closing of submenu is handled here. Any other functionality is bubbled to parent menu. if (event.key === " " || event.key === "Enter") { this.selectMenuItem(event); if (this.hasSubmenu && (!this.href || (this.href && event.target === this.dropdownActionEl))) { this.open = !this.open; } event.preventDefault(); } else if (event.key === "Escape") { if (this.open) { this.open = false; return; } this.calciteInternalMenuItemKeyEvent.emit({ event }); event.preventDefault(); } else if (event.key === "ArrowDown" || event.key === "ArrowUp") { event.preventDefault(); if ((event.target === this.dropdownActionEl || !this.href) && this.hasSubmenu && !this.open && this.layout === "horizontal") { this.open = true; return; } this.calciteInternalMenuItemKeyEvent.emit({ event, children: this.submenuItems, isSubmenuOpen: this.open && this.hasSubmenu }); } else if (event.key === "ArrowLeft") { event.preventDefault(); this.calciteInternalMenuItemKeyEvent.emit({ event, children: this.submenuItems, isSubmenuOpen: true }); } else if (event.key === "ArrowRight") { event.preventDefault(); if ((event.target === this.dropdownActionEl || !this.href) && this.hasSubmenu && !this.open && this.layout === "vertical") { this.open = true; return; } this.calciteInternalMenuItemKeyEvent.emit({ event, children: this.submenuItems, isSubmenuOpen: this.open && this.hasSubmenu }); } }; this.active = undefined; this.breadcrumb = undefined; this.href = undefined; this.iconEnd = undefined; this.iconFlipRtl = undefined; this.iconStart = undefined; this.isTopLevelItem = false; this.label = undefined; this.layout = undefined; this.messageOverrides = undefined; this.messages = undefined; this.open = false; this.rel = undefined; this.target = undefined; this.text = undefined; this.topLevelMenuLayout = undefined; this.defaultMessages = undefined; this.effectiveLocale = ""; this.hasSubmenu = false; this.submenuItems = undefined; } onMessagesChange() { /* wired up by t9n util */ } effectiveLocaleChange() { t9n.updateMessages(this, this.effectiveLocale); } //-------------------------------------------------------------------------- // // Public Methods // //-------------------------------------------------------------------------- /** Sets focus on the component. */ async setFocus() { await loadable.componentLoaded(this); this.anchorEl.focus(); } //-------------------------------------------------------------------------- // // Event Listeners // //-------------------------------------------------------------------------- handleClickOut(event) { if (this.topLevelMenuLayout !== "vertical" && this.hasSubmenu && this.open && !event.composedPath().includes(this.el)) { this.open = false; } } handleFocusOut(event) { if (this.topLevelMenuLayout !== "vertical" && !this.el.contains(event.relatedTarget)) { this.open = false; } } //-------------------------------------------------------------------------- // // Lifecycle // //-------------------------------------------------------------------------- connectedCallback() { locale.connectLocalized(this); t9n.connectMessages(this); } async componentWillLoad() { loadable.setUpLoadableComponent(this); await t9n.setUpMessages(this); } componentDidLoad() { loadable.setComponentLoaded(this); } disconnectedCallback() { locale.disconnectLocalized(this); t9n.disconnectMessages(this); } // -------------------------------------------------------------------------- // // Private Methods // // -------------------------------------------------------------------------- blurHandler() { this.isFocused = false; } focusHandler(event) { const target = event.target; this.isFocused = true; if (target.open && !this.open) { target.open = false; } } selectMenuItem(event) { if (event.target !== this.dropdownActionEl) { this.calciteMenuItemSelect.emit(); } } //-------------------------------------------------------------------------- // // Render Methods // //-------------------------------------------------------------------------- renderIconStart() { return (index.h("calcite-icon", { class: `${CSS.icon} ${CSS.iconStart}`, flipRtl: this.iconFlipRtl === "start" || this.iconFlipRtl === "both", icon: this.iconStart, key: CSS.iconStart, scale: "s" })); } renderIconEnd() { return (index.h("calcite-icon", { class: `${CSS.icon} ${CSS.iconEnd}`, flipRtl: this.iconFlipRtl === "end" || this.iconFlipRtl === "both", icon: this.iconEnd, key: CSS.iconEnd, scale: "s" })); } renderBreadcrumbIcon(dir) { return (index.h("calcite-icon", { class: `${CSS.icon} ${CSS.iconBreadcrumb}`, icon: dir === "rtl" ? "chevron-left" : "chevron-right", key: CSS.iconBreadcrumb, scale: "s" })); } renderDropdownIcon(dir) { const dirChevron = dir === "rtl" ? "chevron-left" : "chevron-right"; return (index.h("calcite-icon", { class: `${CSS.icon} ${CSS.iconDropdown}`, icon: this.topLevelMenuLayout === "vertical" || this.isTopLevelItem ? this.open ? "chevron-up" : "chevron-down" : dirChevron, key: CSS.iconDropdown, scale: "s" })); } renderDropdownAction(dir) { const dirChevron = dir === "rtl" ? "chevron-left" : "chevron-right"; return (index.h("calcite-action", { class: CSS.dropdownAction, icon: this.topLevelMenuLayout === "vertical" || this.isTopLevelItem ? this.open ? "chevron-up" : "chevron-down" : dirChevron, key: CSS.dropdownAction, onClick: this.clickHandler, onKeyDown: this.keyDownHandler, text: this.messages.open, // eslint-disable-next-line react/jsx-sort-props ref: (el) => (this.dropdownActionEl = el) })); } renderSubmenuItems(dir) { return (index.h("calcite-menu", { class: { [CSS.dropdownMenuItems]: true, [CSS.open]: this.open, [CSS.nested]: !this.isTopLevelItem, [resources.CSS_UTILITY.rtl]: dir === "rtl", [CSS.dropdownVertical]: this.topLevelMenuLayout === "vertical" }, label: this.messages.submenu, layout: "vertical", role: "menu" }, index.h("slot", { name: "submenu-item", onSlotchange: this.handleMenuItemSlotChange }))); } renderItemContent(dir) { return (index.h(index.Fragment, null, this.iconStart && this.renderIconStart(), index.h("div", { class: CSS.textContainer }, index.h("span", null, this.text)), this.iconEnd && this.renderIconEnd(), this.breadcrumb ? this.renderBreadcrumbIcon(dir) : null, !this.href && this.hasSubmenu ? this.renderDropdownIcon(dir) : null)); } render() { const dir = dom.getElementDir(this.el); return (index.h(index.Host, { onBlur: this.blurHandler, onFocus: this.focusHandler }, index.h("li", { class: { [CSS.container]: true, [CSS.isParentVertical]: this.topLevelMenuLayout === "vertical" }, role: "none" }, index.h("div", { class: CSS.itemContent }, index.h("a", { "aria-current": this.isFocused ? "page" : false, "aria-expanded": this.open, "aria-haspopup": this.hasSubmenu, "aria-label": this.label, class: { [CSS.layoutVertical]: this.layout === "vertical", [CSS.content]: true }, href: this.href, onClick: this.clickHandler, onKeyDown: this.keyDownHandler, rel: this.rel, role: "menuitem", tabIndex: this.isTopLevelItem ? 0 : -1, target: this.target, // eslint-disable-next-line react/jsx-sort-props ref: (el) => (this.anchorEl = el) }, this.renderItemContent(dir), this.href && (this.topLevelMenuLayout === "vertical" || !this.isTopLevelItem) ? (index.h("calcite-icon", { class: CSS.hoverHrefIcon, icon: dir === "rtl" ? "arrow-left" : "arrow-right", scale: "s" })) : null), this.href && this.hasSubmenu ? this.renderDropdownAction(dir) : null), this.renderSubmenuItems(dir)))); } static get assetsDirs() { return ["assets"]; } get el() { return index.getElement(this); } static get watchers() { return { "messageOverrides": ["onMessagesChange"], "effectiveLocale": ["effectiveLocaleChange"] }; } }; CalciteMenuItem.style = menuItemCss; exports.calcite_menu = CalciteMenu; exports.calcite_menu_item = CalciteMenuItem;