UNPKG

@esri/calcite-components

Version:

Web Components for Esri's Calcite Design System.

414 lines (406 loc) • 19.5 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 floatingUi = require('./floating-ui-b6af0d52.js'); const focusTrapComponent = require('./focusTrapComponent-645b4a75.js'); const dom = require('./dom-18ca68ff.js'); const guid = require('./guid-db20443e.js'); const openCloseComponent = require('./openCloseComponent-6cd419dc.js'); const Heading = require('./Heading-e1a3c7e4.js'); const locale = require('./locale-fc347462.js'); const t9n = require('./t9n-14d528c4.js'); const key = require('./key-2ce02f02.js'); const loadable = require('./loadable-53f729bb.js'); const observers = require('./observers-83b3999d.js'); const FloatingArrow = require('./FloatingArrow-eae44e77.js'); require('./browser-28ea2ce1.js'); require('./debounce-492319d0.js'); require('./resources-45d84c94.js'); const CSS = { container: "container", imageContainer: "image-container", closeButtonContainer: "close-button-container", closeButton: "close-button", content: "content", hasHeader: "has-header", header: "header", headerContent: "header-content", heading: "heading" }; const defaultPopoverPlacement = "auto"; const ARIA_CONTROLS = "aria-controls"; const ARIA_EXPANDED = "aria-expanded"; class PopoverManager { constructor() { // -------------------------------------------------------------------------- // // Private Properties // // -------------------------------------------------------------------------- this.registeredElements = new Map(); this.registeredElementCount = 0; // -------------------------------------------------------------------------- // // Private Methods // // -------------------------------------------------------------------------- this.queryPopover = (composedPath) => { const { registeredElements } = this; const registeredElement = composedPath.find((pathEl) => registeredElements.has(pathEl)); return registeredElements.get(registeredElement); }; this.togglePopovers = (event) => { const composedPath = event.composedPath(); const togglePopover = this.queryPopover(composedPath); if (togglePopover && !togglePopover.triggerDisabled) { togglePopover.open = !togglePopover.open; } Array.from(this.registeredElements.values()) .filter((popover) => popover !== togglePopover && popover.autoClose && popover.open && !composedPath.includes(popover)) .forEach((popover) => (popover.open = false)); }; this.keyHandler = (event) => { if (event.defaultPrevented) { return; } if (event.key === "Escape") { this.closeAllPopovers(); } else if (key.isActivationKey(event.key)) { this.togglePopovers(event); } }; this.clickHandler = (event) => { if (dom.isPrimaryPointerButton(event)) { this.togglePopovers(event); } }; } // -------------------------------------------------------------------------- // // Public Methods // // -------------------------------------------------------------------------- registerElement(referenceEl, popover) { this.registeredElementCount++; this.registeredElements.set(referenceEl, popover); if (this.registeredElementCount === 1) { this.addListeners(); } } unregisterElement(referenceEl) { if (this.registeredElements.delete(referenceEl)) { this.registeredElementCount--; } if (this.registeredElementCount === 0) { this.removeListeners(); } } closeAllPopovers() { Array.from(this.registeredElements.values()).forEach((popover) => (popover.open = false)); } addListeners() { document.addEventListener("pointerdown", this.clickHandler, { capture: true }); document.addEventListener("keydown", this.keyHandler, { capture: true }); } removeListeners() { document.removeEventListener("pointerdown", this.clickHandler, { capture: true }); document.removeEventListener("keydown", this.keyHandler, { capture: true }); } } const popoverCss = "@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{--calcite-floating-ui-z-index:var(--calcite-popover-z-index, var(--calcite-app-z-index-popup));display:block;position:absolute;z-index:var(--calcite-floating-ui-z-index)}.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}:host([data-placement^=bottom]) .calcite-floating-ui-anim{transform:translateY(-5px)}:host([data-placement^=top]) .calcite-floating-ui-anim{transform:translateY(5px)}:host([data-placement^=left]) .calcite-floating-ui-anim{transform:translateX(5px)}:host([data-placement^=right]) .calcite-floating-ui-anim{transform:translateX(-5px)}:host([data-placement]) .calcite-floating-ui-anim--active{opacity:1;transform:translate(0)}:host([calcite-hydrated-hidden]){visibility:hidden !important;pointer-events:none}.calcite-floating-ui-arrow{pointer-events:none;position:absolute;z-index:calc(var(--calcite-app-z-index) * -1);fill:var(--calcite-ui-foreground-1)}.calcite-floating-ui-arrow__stroke{stroke:var(--calcite-ui-border-3)}:host([scale=s]) .heading{padding-inline:0.75rem;padding-block:0.5rem;font-size:var(--calcite-font-size--1);line-height:1.375}:host([scale=m]) .heading{padding-inline:1rem;padding-block:0.75rem;font-size:var(--calcite-font-size-0);line-height:1.375}:host([scale=l]) .heading{padding-inline:1.25rem;padding-block:1rem;font-size:var(--calcite-font-size-1);line-height:1.375}:host{pointer-events:none}:host([open]){pointer-events:initial}.calcite-floating-ui-anim{border-radius:0.25rem;border-width:1px;border-style:solid;border-color:var(--calcite-ui-border-3);background-color:var(--calcite-ui-foreground-1)}.arrow::before{outline:1px solid var(--calcite-ui-border-3)}.header{display:flex;flex:1 1 auto;align-items:stretch;justify-content:flex-start;border-width:0px;border-block-end-width:1px;border-style:solid;border-block-end-color:var(--calcite-ui-border-3)}.heading{margin:0px;display:block;flex:1 1 auto;align-self:center;white-space:normal;font-weight:var(--calcite-font-weight-medium);color:var(--calcite-ui-text-1);word-wrap:break-word;word-break:break-word}.container{position:relative;display:flex;block-size:100%;flex-direction:row;flex-wrap:nowrap;border-radius:0.25rem;color:var(--calcite-ui-text-1)}.container.has-header{flex-direction:column}.content{display:flex;block-size:100%;inline-size:100%;flex-direction:column;flex-wrap:nowrap;align-self:center;word-wrap:break-word;word-break:break-word}.close-button-container{display:flex;overflow:hidden;flex:0 0 auto;border-start-end-radius:0.25rem;border-end-end-radius:0.25rem}::slotted(calcite-panel),::slotted(calcite-flow){block-size:100%}"; const manager = new PopoverManager(); const Popover = class { constructor(hostRef) { index.registerInstance(this, hostRef); this.calcitePopoverBeforeClose = index.createEvent(this, "calcitePopoverBeforeClose", 6); this.calcitePopoverClose = index.createEvent(this, "calcitePopoverClose", 6); this.calcitePopoverBeforeOpen = index.createEvent(this, "calcitePopoverBeforeOpen", 6); this.calcitePopoverOpen = index.createEvent(this, "calcitePopoverOpen", 6); // -------------------------------------------------------------------------- // // Private Properties // // -------------------------------------------------------------------------- this.mutationObserver = observers.createObserver("mutation", () => this.updateFocusTrapElements()); this.guid = `calcite-popover-${guid.guid()}`; this.openTransitionProp = "opacity"; this.hasLoaded = false; // -------------------------------------------------------------------------- // // Private Methods // // -------------------------------------------------------------------------- this.setTransitionEl = (el) => { this.transitionEl = el; openCloseComponent.connectOpenCloseComponent(this); }; this.setFilteredPlacements = () => { const { el, flipPlacements } = this; this.filteredFlipPlacements = flipPlacements ? floatingUi.filterComputedPlacements(flipPlacements, el) : null; }; this.setUpReferenceElement = (warn = true) => { this.removeReferences(); this.effectiveReferenceElement = this.getReferenceElement(); floatingUi.connectFloatingUI(this, this.effectiveReferenceElement, this.el); const { el, referenceElement, effectiveReferenceElement } = this; if (warn && referenceElement && !effectiveReferenceElement) { console.warn(`${el.tagName}: reference-element id "${referenceElement}" was not found.`, { el }); } this.addReferences(); }; this.getId = () => { return this.el.id || this.guid; }; this.setExpandedAttr = () => { const { effectiveReferenceElement, open } = this; if (!effectiveReferenceElement) { return; } if ("setAttribute" in effectiveReferenceElement) { effectiveReferenceElement.setAttribute(ARIA_EXPANDED, dom.toAriaBoolean(open)); } }; this.addReferences = () => { const { effectiveReferenceElement } = this; if (!effectiveReferenceElement) { return; } const id = this.getId(); if ("setAttribute" in effectiveReferenceElement) { effectiveReferenceElement.setAttribute(ARIA_CONTROLS, id); } manager.registerElement(effectiveReferenceElement, this.el); this.setExpandedAttr(); }; this.removeReferences = () => { const { effectiveReferenceElement } = this; if (!effectiveReferenceElement) { return; } if ("removeAttribute" in effectiveReferenceElement) { effectiveReferenceElement.removeAttribute(ARIA_CONTROLS); effectiveReferenceElement.removeAttribute(ARIA_EXPANDED); } manager.unregisterElement(effectiveReferenceElement); }; this.hide = () => { this.open = false; }; this.storeArrowEl = (el) => { this.arrowEl = el; this.reposition(true); }; this.autoClose = false; this.closable = false; this.flipDisabled = false; this.focusTrapDisabled = false; this.pointerDisabled = false; this.flipPlacements = undefined; this.heading = undefined; this.headingLevel = undefined; this.label = undefined; this.messageOverrides = undefined; this.messages = undefined; this.offsetDistance = floatingUi.defaultOffsetDistance; this.offsetSkidding = 0; this.open = false; this.overlayPositioning = "absolute"; this.placement = defaultPopoverPlacement; this.referenceElement = undefined; this.scale = "m"; this.triggerDisabled = false; this.effectiveLocale = ""; this.floatingLayout = "vertical"; this.effectiveReferenceElement = undefined; this.defaultMessages = undefined; } handlefocusTrapDisabled(focusTrapDisabled) { if (!this.open) { return; } focusTrapDisabled ? focusTrapComponent.deactivateFocusTrap(this) : focusTrapComponent.activateFocusTrap(this); } flipPlacementsHandler() { this.setFilteredPlacements(); this.reposition(true); } onMessagesChange() { /* wired up by t9n util */ } offsetDistanceOffsetHandler() { this.reposition(true); } offsetSkiddingHandler() { this.reposition(true); } openHandler(value) { if (value) { this.reposition(true); } this.setExpandedAttr(); } overlayPositioningHandler() { this.reposition(true); } placementHandler() { this.reposition(true); } referenceElementHandler() { this.setUpReferenceElement(); this.reposition(true); } effectiveLocaleChange() { t9n.updateMessages(this, this.effectiveLocale); } // -------------------------------------------------------------------------- // // Lifecycle // // -------------------------------------------------------------------------- connectedCallback() { this.setFilteredPlacements(); locale.connectLocalized(this); t9n.connectMessages(this); openCloseComponent.connectOpenCloseComponent(this); this.setUpReferenceElement(this.hasLoaded); focusTrapComponent.connectFocusTrap(this); } async componentWillLoad() { await t9n.setUpMessages(this); loadable.setUpLoadableComponent(this); } componentDidLoad() { loadable.setComponentLoaded(this); if (this.referenceElement && !this.effectiveReferenceElement) { this.setUpReferenceElement(); } this.reposition(); this.hasLoaded = true; } disconnectedCallback() { this.removeReferences(); locale.disconnectLocalized(this); t9n.disconnectMessages(this); floatingUi.disconnectFloatingUI(this, this.effectiveReferenceElement, this.el); openCloseComponent.disconnectOpenCloseComponent(this); focusTrapComponent.deactivateFocusTrap(this); } // -------------------------------------------------------------------------- // // Public Methods // // -------------------------------------------------------------------------- /** * Updates the position of the component. * * @param delayed */ async reposition(delayed = false) { const { el, effectiveReferenceElement, placement, overlayPositioning, flipDisabled, filteredFlipPlacements, offsetDistance, offsetSkidding, arrowEl } = this; return floatingUi.reposition(this, { floatingEl: el, referenceEl: effectiveReferenceElement, overlayPositioning, placement, flipDisabled, flipPlacements: filteredFlipPlacements, offsetDistance, offsetSkidding, arrowEl, type: "popover" }, delayed); } /** * Sets focus on the component's first focusable element. */ async setFocus() { await loadable.componentLoaded(this); index.forceUpdate(this.el); dom.focusFirstTabbable(this.el); } /** * Updates the element(s) that are used within the focus-trap of the component. */ async updateFocusTrapElements() { focusTrapComponent.updateFocusTrapElements(this); } getReferenceElement() { const { referenceElement, el } = this; return ((typeof referenceElement === "string" ? dom.queryElementRoots(el, { id: referenceElement }) : referenceElement) || null); } onBeforeOpen() { this.calcitePopoverBeforeOpen.emit(); } onOpen() { this.calcitePopoverOpen.emit(); focusTrapComponent.activateFocusTrap(this); } onBeforeClose() { this.calcitePopoverBeforeClose.emit(); } onClose() { this.calcitePopoverClose.emit(); focusTrapComponent.deactivateFocusTrap(this); } // -------------------------------------------------------------------------- // // Render Methods // // -------------------------------------------------------------------------- renderCloseButton() { const { messages, closable } = this; return closable ? (index.h("div", { class: CSS.closeButtonContainer, key: CSS.closeButtonContainer }, index.h("calcite-action", { appearance: "transparent", class: CSS.closeButton, onClick: this.hide, scale: this.scale, text: messages.close, // eslint-disable-next-line react/jsx-sort-props ref: (closeButtonEl) => (this.closeButtonEl = closeButtonEl) }, index.h("calcite-icon", { icon: "x", scale: this.scale === "l" ? "m" : this.scale })))) : null; } renderHeader() { const { heading, headingLevel } = this; const headingNode = heading ? (index.h(Heading.Heading, { class: CSS.heading, level: headingLevel }, heading)) : null; return headingNode ? (index.h("div", { class: CSS.header, key: CSS.header }, headingNode, this.renderCloseButton())) : null; } render() { const { effectiveReferenceElement, heading, label, open, pointerDisabled, floatingLayout } = this; const displayed = effectiveReferenceElement && open; const hidden = !displayed; const arrowNode = !pointerDisabled ? (index.h(FloatingArrow.FloatingArrow, { floatingLayout: floatingLayout, key: "floating-arrow", // eslint-disable-next-line react/jsx-sort-props ref: this.storeArrowEl })) : null; return (index.h(index.Host, { "aria-hidden": dom.toAriaBoolean(hidden), "aria-label": label, "aria-live": "polite", "calcite-hydrated-hidden": hidden, id: this.getId(), role: "dialog" }, index.h("div", { class: { [floatingUi.FloatingCSS.animation]: true, [floatingUi.FloatingCSS.animationActive]: displayed }, // eslint-disable-next-line react/jsx-sort-props ref: this.setTransitionEl }, arrowNode, index.h("div", { class: { [CSS.hasHeader]: !!heading, [CSS.container]: true } }, this.renderHeader(), index.h("div", { class: CSS.content }, index.h("slot", null)), !heading ? this.renderCloseButton() : null)))); } static get assetsDirs() { return ["assets"]; } get el() { return index.getElement(this); } static get watchers() { return { "focusTrapDisabled": ["handlefocusTrapDisabled"], "flipPlacements": ["flipPlacementsHandler"], "messageOverrides": ["onMessagesChange"], "offsetDistance": ["offsetDistanceOffsetHandler"], "offsetSkidding": ["offsetSkiddingHandler"], "open": ["openHandler"], "overlayPositioning": ["overlayPositioningHandler"], "placement": ["placementHandler"], "referenceElement": ["referenceElementHandler"], "effectiveLocale": ["effectiveLocaleChange"] }; } }; Popover.style = popoverCss; exports.calcite_popover = Popover;