@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
376 lines (367 loc) • 21.3 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.
*/
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const index = require('./index-1af0efd3.js');
const popper = require('./popper-64d89ef7.js');
const guid = require('./guid-b270f0db.js');
const dom = require('./dom-f3df220d.js');
const Heading = require('./Heading-ec707493.js');
const CSS = {
container: "container",
arrow: "arrow",
imageContainer: "image-container",
closeButtonContainer: "close-button-container",
closeButton: "close-button",
content: "content",
hasHeader: "has-header",
header: "header",
headerContent: "header-content",
heading: "heading"
};
const TEXT = {
close: "Close"
};
const POPOVER_REFERENCE = "data-calcite-popover-reference";
const ARIA_CONTROLS = "aria-controls";
const ARIA_EXPANDED = "aria-expanded";
const HEADING_LEVEL = 2;
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 = class {
constructor(hostRef) {
index.registerInstance(this, hostRef);
this.calcitePopoverClose = index.createEvent(this, "calcitePopoverClose", 7);
this.calcitePopoverOpen = index.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 = popper.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.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: popper$1, el, placement } = this;
const modifiers = this.getModifiers();
popper$1
? await popper.updatePopper({
el,
modifiers,
placement,
popper: popper$1
})
: this.createPopper();
}
/** Sets focus on the component. */
async setFocus(focusId) {
var _a;
const { closeButtonEl } = this;
if (focusId === "close-button" && closeButtonEl) {
index.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"
? dom.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 = 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 ? (index.h("div", { class: CSS.closeButtonContainer }, index.h("calcite-action", { class: CSS.closeButton, onClick: this.hide, ref: (closeButtonEl) => (this.closeButtonEl = closeButtonEl), text: intlClose }, index.h("calcite-icon", { icon: "x", scale: "m" })))) : null;
}
renderHeader() {
const { heading, headingLevel } = this;
const headingNode = heading ? (index.h(Heading.Heading, { class: CSS.heading, level: headingLevel || HEADING_LEVEL }, heading)) : null;
return headingNode ? (index.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 ? (index.h("div", { class: CSS.arrow, ref: (arrowEl) => (this.arrowEl = arrowEl) })) : null;
return (index.h(index.Host, { "aria-hidden": hidden.toString(), "aria-label": label, "calcite-hydrated-hidden": hidden, id: this.getId(), role: "dialog" }, index.h("div", { class: {
[popper.CSS.animation]: true,
[popper.CSS.animationActive]: displayed
}, onTransitionEnd: this.transitionEnd }, 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))));
}
get el() { return index.getElement(this); }
static get watchers() { return {
"offsetDistance": ["offsetDistanceOffsetHandler"],
"offsetSkidding": ["offsetSkiddingHandler"],
"open": ["openHandler"],
"placement": ["placementHandler"],
"referenceElement": ["referenceElementHandler"]
}; }
};
Popover.style = popoverCss;
const popoverManagerCss = "@-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{position:relative;display:block}";
const PopoverManager = class {
constructor(hostRef) {
index.registerInstance(this, hostRef);
// --------------------------------------------------------------------------
//
// Properties
//
// --------------------------------------------------------------------------
/**
* CSS Selector to match reference elements for popovers. Reference elements will be identified by this selector in order to open their associated popover.
* @default `[data-calcite-popover-reference]`
*/
this.selector = `[${POPOVER_REFERENCE}]`;
/**
* Automatically closes any currently open popovers when clicking outside of a popover.
*/
this.autoClose = false;
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
this.queryPopover = (composedPath) => {
const { el } = this;
if (!composedPath.includes(el)) {
return null;
}
const referenceElement = composedPath.find((pathEl) => { var _a; return (_a = pathEl === null || pathEl === void 0 ? void 0 : pathEl.hasAttribute) === null || _a === void 0 ? void 0 : _a.call(pathEl, POPOVER_REFERENCE); });
if (!referenceElement) {
return null;
}
const id = referenceElement.getAttribute(POPOVER_REFERENCE);
return dom.queryElementRoots(el, { id });
};
}
// --------------------------------------------------------------------------
//
// Render Methods
//
// --------------------------------------------------------------------------
render() {
return index.h("slot", null);
}
//--------------------------------------------------------------------------
//
// Event Listeners
//
//--------------------------------------------------------------------------
closeOpenPopovers(event) {
const { autoClose, el } = this;
const popoverSelector = "calcite-popover";
const composedPath = event.composedPath();
const popover = this.queryPopover(composedPath);
if (popover) {
popover.toggle();
return;
}
if (autoClose) {
dom.queryElementsRoots(el, popoverSelector)
.filter((popover) => popover.open && !composedPath.includes(popover))
.forEach((popover) => popover.toggle(false));
}
}
get el() { return index.getElement(this); }
};
PopoverManager.style = popoverManagerCss;
exports.calcite_popover = Popover;
exports.calcite_popover_manager = PopoverManager;