@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
337 lines (333 loc) • 17.7 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, forceUpdate, h, Host } from '@stencil/core/internal/client';
import { T as TEXT, A as ARIA_EXPANDED, P as POPOVER_REFERENCE, a as ARIA_CONTROLS, C as CSS, H as HEADING_LEVEL } from './resources.js';
import { d as defaultOffsetDistance, u as updatePopper, c as createPopper, C as CSS$1 } from './popper.js';
import { g as guid } from './guid.js';
import { q as queryElementRoots } from './dom.js';
import { H as Heading } from './Heading.js';
import { d as defineCustomElement$3 } from './action.js';
import { d as defineCustomElement$2 } from './icon.js';
import { d as defineCustomElement$1 } from './loader.js';
const popoverCss = "@-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:block;position:absolute;z-index:900;-webkit-transform:scale(0);transform:scale(0)}.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}:host([data-popper-placement^=bottom]) .calcite-popper-anim{-webkit-transform:translateY(-5px);transform:translateY(-5px)}:host([data-popper-placement^=top]) .calcite-popper-anim{-webkit-transform:translateY(5px);transform:translateY(5px)}:host([data-popper-placement^=left]) .calcite-popper-anim{-webkit-transform:translateX(5px);transform:translateX(5px)}:host([data-popper-placement^=right]) .calcite-popper-anim{-webkit-transform:translateX(-5px);transform:translateX(-5px)}:host([data-popper-placement]) .calcite-popper-anim--active{opacity:1;visibility:visible;-webkit-transform:translate(0);transform:translate(0)}:host([data-popper-placement][data-popper-escaped]),:host([data-popper-placement][data-popper-reference-hidden]){pointer-events:none;opacity:0}.arrow,.arrow::before{position:absolute;width:8px;height:8px;z-index:-1}.arrow::before{content:\"\";--tw-shadow:0 4px 8px -1px rgba(0, 0, 0, 0.08), 0 2px 4px -1px rgba(0, 0, 0, 0.04);--tw-shadow-colored:0 4px 8px -1px var(--tw-shadow-color), 0 2px 4px -1px var(--tw-shadow-color);-webkit-box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);-webkit-transform:rotate(45deg);transform:rotate(45deg);background:var(--calcite-ui-foreground-1)}:host([data-popper-placement^=top]) .arrow{bottom:-4px}:host([data-popper-placement^=bottom]) .arrow{top:-4px}:host([data-popper-placement^=left]) .arrow{right:-4px}:host([data-popper-placement^=right]) .arrow{left:-4px}:host{pointer-events:none}:host([open]){pointer-events:initial}.calcite-popper-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:-ms-flexbox;display:flex;-ms-flex:1 1 auto;flex:1 1 auto;-ms-flex-align:stretch;align-items:stretch;-ms-flex-pack:start;justify-content:flex-start;border-width:0px;border-bottom-width:1px;border-style:solid;background-color:var(--calcite-ui-foreground-1);border-bottom-color:var(--calcite-ui-border-3)}.heading{margin:0px;display:block;-ms-flex:1 1 auto;flex:1 1 auto;-ms-flex-item-align:center;align-self:center;white-space:normal;padding-left:1rem;padding-right:1rem;padding-top:0.75rem;padding-bottom:0.75rem;font-size:var(--calcite-font-size-0);line-height:1.375;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:-ms-flexbox;display:flex;height:100%;-ms-flex-direction:row;flex-direction:row;-ms-flex-wrap:nowrap;flex-wrap:nowrap;border-radius:0.25rem;background-color:var(--calcite-ui-foreground-1);color:var(--calcite-ui-text-1)}.container.has-header{-ms-flex-direction:column;flex-direction:column}.content{display:-ms-flexbox;display:flex;height:100%;width:100%;-ms-flex-direction:column;flex-direction:column;-ms-flex-wrap:nowrap;flex-wrap:nowrap;-ms-flex-item-align:center;align-self:center;word-wrap:break-word;word-break:break-word}.close-button-container{display:-ms-flexbox;display:flex;overflow:hidden;-ms-flex:0 0 auto;flex:0 0 auto;border-start-end-radius:0.25rem;border-end-end-radius:0.25rem}::slotted(calcite-panel),::slotted(calcite-flow){height:100%}";
const Popover = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
constructor() {
super();
this.__registerHost();
this.__attachShadow();
this.calcitePopoverClose = createEvent(this, "calcitePopoverClose", 7);
this.calcitePopoverOpen = createEvent(this, "calcitePopoverOpen", 7);
// --------------------------------------------------------------------------
//
// Properties
//
// --------------------------------------------------------------------------
/**
* Display a close button within the Popover.
* @deprecated use dismissible instead.
*/
this.closeButton = false;
/**
* Display a close button within the Popover.
*/
this.dismissible = false;
/**
* Prevents flipping the popover's placement when it starts to overlap its reference element.
*/
this.disableFlip = false;
/**
* Removes the caret pointer.
*/
this.disablePointer = false;
/**
* Offset the position of the popover away from the reference element.
* @default 6
*/
this.offsetDistance = defaultOffsetDistance;
/**
* Offset the position of the popover along the reference element.
*/
this.offsetSkidding = 0;
/**
* Display and position the component.
*/
this.open = false;
/** 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 component will be positioned relative to the referenceElement.
* @see [PopperPlacement](https://github.com/Esri/calcite-components/blob/master/src/utils/popper.ts#L25)
*/
this.placement = "auto";
/** Text for close button.
* @default "Close"
*/
this.intlClose = TEXT.close;
this.guid = `calcite-popover-${guid()}`;
this.activeTransitionProp = "opacity";
// --------------------------------------------------------------------------
//
// Private Methods
//
// --------------------------------------------------------------------------
this.setUpReferenceElement = () => {
this.removeReferences();
this.effectiveReferenceElement = this.getReferenceElement();
const { el, referenceElement, effectiveReferenceElement } = this;
if (referenceElement && !effectiveReferenceElement) {
console.warn(`${el.tagName}: reference-element id "${referenceElement}" was not found.`, {
el
});
}
this.addReferences();
this.createPopper();
};
this.getId = () => {
return this.el.id || this.guid;
};
this.setExpandedAttr = () => {
const { effectiveReferenceElement, open } = this;
if (!effectiveReferenceElement) {
return;
}
effectiveReferenceElement.setAttribute(ARIA_EXPANDED, open.toString());
};
this.addReferences = () => {
const { effectiveReferenceElement } = this;
if (!effectiveReferenceElement) {
return;
}
const id = this.getId();
effectiveReferenceElement.setAttribute(POPOVER_REFERENCE, id);
effectiveReferenceElement.setAttribute(ARIA_CONTROLS, id);
this.setExpandedAttr();
};
this.removeReferences = () => {
const { effectiveReferenceElement } = this;
if (!effectiveReferenceElement) {
return;
}
effectiveReferenceElement.removeAttribute(POPOVER_REFERENCE);
effectiveReferenceElement.removeAttribute(ARIA_CONTROLS);
effectiveReferenceElement.removeAttribute(ARIA_EXPANDED);
};
this.hide = () => {
this.open = false;
};
this.transitionEnd = (event) => {
if (event.propertyName === this.activeTransitionProp) {
this.open ? this.calcitePopoverOpen.emit() : this.calcitePopoverClose.emit();
}
};
}
offsetDistanceOffsetHandler() {
this.reposition();
}
offsetSkiddingHandler() {
this.reposition();
}
openHandler() {
this.reposition();
this.setExpandedAttr();
}
placementHandler() {
this.reposition();
}
referenceElementHandler() {
this.setUpReferenceElement();
}
// --------------------------------------------------------------------------
//
// Lifecycle
//
// --------------------------------------------------------------------------
componentWillLoad() {
this.setUpReferenceElement();
}
componentDidLoad() {
this.reposition();
}
disconnectedCallback() {
this.removeReferences();
this.destroyPopper();
}
// --------------------------------------------------------------------------
//
// Public Methods
//
// --------------------------------------------------------------------------
/** Updates the position of the component. */
async reposition() {
const { popper, el, placement } = this;
const modifiers = this.getModifiers();
popper
? await updatePopper({
el,
modifiers,
placement,
popper
})
: this.createPopper();
}
/** Sets focus on the component. */
async setFocus(focusId) {
var _a;
const { closeButtonEl } = this;
if (focusId === "close-button" && closeButtonEl) {
forceUpdate(closeButtonEl);
closeButtonEl.setFocus();
return;
}
(_a = this.el) === null || _a === void 0 ? void 0 : _a.focus();
}
/** Toggles the popover's open property. */
async toggle(value = !this.open) {
this.open = value;
}
getReferenceElement() {
const { referenceElement, el } = this;
return ((typeof referenceElement === "string"
? queryElementRoots(el, { id: referenceElement })
: referenceElement) || null);
}
getModifiers() {
const { arrowEl, flipPlacements, disableFlip, disablePointer, offsetDistance, offsetSkidding } = this;
const flipModifier = {
name: "flip",
enabled: !disableFlip
};
if (flipPlacements) {
flipModifier.options = {
fallbackPlacements: flipPlacements
};
}
const arrowModifier = {
name: "arrow",
enabled: !disablePointer
};
if (arrowEl) {
arrowModifier.options = {
element: arrowEl
};
}
const offsetModifier = {
name: "offset",
enabled: true,
options: {
offset: [offsetSkidding, offsetDistance]
}
};
const eventListenerModifier = {
name: "eventListeners",
enabled: this.open
};
return [arrowModifier, flipModifier, offsetModifier, eventListenerModifier];
}
createPopper() {
this.destroyPopper();
const { el, placement, effectiveReferenceElement: referenceEl, overlayPositioning } = this;
const modifiers = this.getModifiers();
this.popper = createPopper({
el,
modifiers,
overlayPositioning,
placement,
referenceEl
});
}
destroyPopper() {
const { popper } = this;
if (popper) {
popper.destroy();
}
this.popper = null;
}
// --------------------------------------------------------------------------
//
// Render Methods
//
// --------------------------------------------------------------------------
renderCloseButton() {
const { dismissible, closeButton, intlClose } = this;
return dismissible || closeButton ? (h("div", { class: CSS.closeButtonContainer }, h("calcite-action", { class: CSS.closeButton, onClick: this.hide, ref: (closeButtonEl) => (this.closeButtonEl = closeButtonEl), text: intlClose }, h("calcite-icon", { icon: "x", scale: "m" })))) : null;
}
renderHeader() {
const { heading, headingLevel } = this;
const headingNode = heading ? (h(Heading, { class: CSS.heading, level: headingLevel || HEADING_LEVEL }, heading)) : null;
return headingNode ? (h("div", { class: CSS.header }, headingNode, this.renderCloseButton())) : null;
}
render() {
const { effectiveReferenceElement, heading, label, open, disablePointer } = this;
const displayed = effectiveReferenceElement && open;
const hidden = !displayed;
const arrowNode = !disablePointer ? (h("div", { class: CSS.arrow, ref: (arrowEl) => (this.arrowEl = arrowEl) })) : null;
return (h(Host, { "aria-hidden": hidden.toString(), "aria-label": label, "calcite-hydrated-hidden": hidden, id: this.getId(), role: "dialog" }, h("div", { class: {
[CSS$1.animation]: true,
[CSS$1.animationActive]: displayed
}, onTransitionEnd: this.transitionEnd }, arrowNode, h("div", { class: {
[CSS.hasHeader]: !!heading,
[CSS.container]: true
} }, this.renderHeader(), h("div", { class: CSS.content }, h("slot", null)), !heading ? this.renderCloseButton() : null))));
}
get el() { return this; }
static get watchers() { return {
"offsetDistance": ["offsetDistanceOffsetHandler"],
"offsetSkidding": ["offsetSkiddingHandler"],
"open": ["openHandler"],
"placement": ["placementHandler"],
"referenceElement": ["referenceElementHandler"]
}; }
static get style() { return popoverCss; }
}, [1, "calcite-popover", {
"closeButton": [516, "close-button"],
"dismissible": [516],
"disableFlip": [516, "disable-flip"],
"disablePointer": [516, "disable-pointer"],
"flipPlacements": [16],
"heading": [1],
"headingLevel": [2, "heading-level"],
"label": [1],
"offsetDistance": [514, "offset-distance"],
"offsetSkidding": [514, "offset-skidding"],
"open": [1540],
"overlayPositioning": [1, "overlay-positioning"],
"placement": [513],
"referenceElement": [1, "reference-element"],
"intlClose": [1, "intl-close"],
"effectiveReferenceElement": [32],
"reposition": [64],
"setFocus": [64],
"toggle": [64]
}]);
function defineCustomElement() {
if (typeof customElements === "undefined") {
return;
}
const components = ["calcite-popover", "calcite-action", "calcite-icon", "calcite-loader"];
components.forEach(tagName => { switch (tagName) {
case "calcite-popover":
if (!customElements.get(tagName)) {
customElements.define(tagName, Popover);
}
break;
case "calcite-action":
if (!customElements.get(tagName)) {
defineCustomElement$3();
}
break;
case "calcite-icon":
if (!customElements.get(tagName)) {
defineCustomElement$2();
}
break;
case "calcite-loader":
if (!customElements.get(tagName)) {
defineCustomElement$1();
}
break;
} });
}
defineCustomElement();
export { Popover as P, defineCustomElement as d };