popover.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.97
  5. */
  6. import { proxyCustomElement, HTMLElement, createEvent, forceUpdate, h, Host } from '@stencil/core/internal/client/index.js';
  7. import { b as defaultOffsetDistance, f as filterComputedPlacements, c as connectFloatingUI, u as updateAfterClose, a as disconnectFloatingUI, r as reposition, F as FloatingCSS } from './floating-ui.js';
  8. import { g as guid } from './guid.js';
  9. import { i as isPrimaryPointerButton, t as toAriaBoolean, q as queryElementRoots } from './dom.js';
  10. import { c as connectOpenCloseComponent, d as disconnectOpenCloseComponent } from './openCloseComponent.js';
  11. import { H as Heading } from './Heading.js';
  12. import { i as isActivationKey } from './key.js';
  13. import { d as defineCustomElement$3 } from './action.js';
  14. import { d as defineCustomElement$2 } from './icon.js';
  15. import { d as defineCustomElement$1 } from './loader.js';
  16. const CSS = {
  17. container: "container",
  18. arrow: "arrow",
  19. imageContainer: "image-container",
  20. closeButtonContainer: "close-button-container",
  21. closeButton: "close-button",
  22. content: "content",
  23. hasHeader: "has-header",
  24. header: "header",
  25. headerContent: "header-content",
  26. heading: "heading"
  27. };
  28. const TEXT = {
  29. close: "Close"
  30. };
  31. const defaultPopoverPlacement = "auto";
  32. const ARIA_CONTROLS = "aria-controls";
  33. const ARIA_EXPANDED = "aria-expanded";
  34. const HEADING_LEVEL = 2;
  35. class PopoverManager {
  36. constructor() {
  37. // --------------------------------------------------------------------------
  38. //
  39. // Private Properties
  40. //
  41. // --------------------------------------------------------------------------
  42. this.registeredElements = new Map();
  43. this.registeredElementCount = 0;
  44. // --------------------------------------------------------------------------
  45. //
  46. // Private Methods
  47. //
  48. // --------------------------------------------------------------------------
  49. this.queryPopover = (composedPath) => {
  50. const { registeredElements } = this;
  51. const registeredElement = composedPath.find((pathEl) => registeredElements.has(pathEl));
  52. return registeredElements.get(registeredElement);
  53. };
  54. this.togglePopovers = (event) => {
  55. const composedPath = event.composedPath();
  56. const togglePopover = this.queryPopover(composedPath);
  57. if (togglePopover && !togglePopover.triggerDisabled) {
  58. togglePopover.toggle();
  59. }
  60. Array.from(this.registeredElements.values())
  61. .filter((popover) => popover !== togglePopover && popover.autoClose && popover.open && !composedPath.includes(popover))
  62. .forEach((popover) => popover.toggle(false));
  63. };
  64. this.keyHandler = (event) => {
  65. if (event.defaultPrevented || !isActivationKey(event.key)) {
  66. return;
  67. }
  68. this.togglePopovers(event);
  69. };
  70. this.clickHandler = (event) => {
  71. if (isPrimaryPointerButton(event)) {
  72. this.togglePopovers(event);
  73. }
  74. };
  75. }
  76. // --------------------------------------------------------------------------
  77. //
  78. // Public Methods
  79. //
  80. // --------------------------------------------------------------------------
  81. registerElement(referenceEl, popover) {
  82. this.registeredElementCount++;
  83. this.registeredElements.set(referenceEl, popover);
  84. if (this.registeredElementCount === 1) {
  85. this.addListeners();
  86. }
  87. }
  88. unregisterElement(referenceEl) {
  89. if (this.registeredElements.delete(referenceEl)) {
  90. this.registeredElementCount--;
  91. }
  92. if (this.registeredElementCount === 0) {
  93. this.removeListeners();
  94. }
  95. }
  96. addListeners() {
  97. document.addEventListener("pointerdown", this.clickHandler, { capture: true });
  98. document.addEventListener("keydown", this.keyHandler, { capture: true });
  99. }
  100. removeListeners() {
  101. document.removeEventListener("pointerdown", this.clickHandler, { capture: true });
  102. document.removeEventListener("keydown", this.keyHandler, { capture: true });
  103. }
  104. }
  105. 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-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-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)}:host([hidden]){display:none}:host{display:block;position:absolute;z-index:900}.calcite-floating-ui-anim{position:relative;transition:var(--calcite-floating-ui-transition);visibility:hidden;transition-property:transform, visibility, opacity;opacity:0;box-shadow:0 0 16px 0 rgba(0, 0, 0, 0.16);z-index:1;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;visibility:visible;transform:translate(0)}:host([calcite-hydrated-hidden]){visibility:hidden !important;pointer-events:none}.arrow,.arrow::before{position:absolute;inline-size:8px;block-size: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);box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);transform:rotate(45deg);background:var(--calcite-ui-foreground-1)}:host([data-placement^=top]) .arrow{inset-block-end:-4px}:host([data-placement^=bottom]) .arrow{inset-block-start:-4px}:host([data-placement^=left]) .arrow{direction:ltr;inset-inline-end:-4px}:host([data-placement^=right]) .arrow{direction:ltr;inset-inline-start:-4px}: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;background-color:var(--calcite-ui-foreground-1);border-block-end-color:var(--calcite-ui-border-3)}.heading{margin:0px;display:block;flex:1 1 auto;align-self:center;white-space:normal;padding-inline:1rem;padding-block: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:flex;block-size:100%;flex-direction:row;flex-wrap:nowrap;border-radius:0.25rem;background-color:var(--calcite-ui-foreground-1);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%}";
  106. const manager = new PopoverManager();
  107. const Popover = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  108. constructor() {
  109. super();
  110. this.__registerHost();
  111. this.__attachShadow();
  112. this.calcitePopoverBeforeClose = createEvent(this, "calcitePopoverBeforeClose", 6);
  113. this.calcitePopoverClose = createEvent(this, "calcitePopoverClose", 6);
  114. this.calcitePopoverBeforeOpen = createEvent(this, "calcitePopoverBeforeOpen", 6);
  115. this.calcitePopoverOpen = createEvent(this, "calcitePopoverOpen", 6);
  116. // --------------------------------------------------------------------------
  117. //
  118. // Properties
  119. //
  120. // --------------------------------------------------------------------------
  121. /**
  122. * When `true`, clicking outside of the component automatically closes open `calcite-popover`s.
  123. */
  124. this.autoClose = false;
  125. /**
  126. * When `true`, a close button is added to the component.
  127. *
  128. * @deprecated use dismissible instead.
  129. */
  130. this.closeButton = false;
  131. /**
  132. * When `true`, a close button is added to the component.
  133. *
  134. * @deprecated use `closable` instead.
  135. */
  136. this.dismissible = false;
  137. /** When `true`, display a close button within the component. */
  138. this.closable = false;
  139. /**
  140. * When `true`, prevents flipping the component's placement when overlapping its `referenceElement`.
  141. */
  142. this.disableFlip = false;
  143. /**
  144. * When `true`, removes the caret pointer.
  145. */
  146. this.disablePointer = false;
  147. /**
  148. * Offsets the position of the component away from the `referenceElement`.
  149. *
  150. * @default 6
  151. */
  152. this.offsetDistance = defaultOffsetDistance;
  153. /**
  154. * Offsets the position of the component along the `referenceElement`.
  155. */
  156. this.offsetSkidding = 0;
  157. /**
  158. * When `true`, displays and positions the component.
  159. */
  160. this.open = false;
  161. /**
  162. * Determines the type of positioning to use for the overlaid content.
  163. *
  164. * Using `"absolute"` will work for most cases. The component will be positioned inside of overflowing parent containers and will affect the container's layout.
  165. *
  166. * `"fixed"` value should be used to escape an overflowing parent container, or when the reference element's `position` CSS property is `"fixed"`.
  167. *
  168. */
  169. this.overlayPositioning = "absolute";
  170. /**
  171. * Determines where the component will be positioned relative to the `referenceElement`.
  172. *
  173. * @see [LogicalPlacement](https://github.com/Esri/calcite-components/blob/master/src/utils/floating-ui.ts#L25)
  174. */
  175. this.placement = defaultPopoverPlacement;
  176. /**
  177. * When `true`, disables automatically toggling the component when its `referenceElement` has been triggered.
  178. *
  179. * This property can be set to `true` to manage when the component is open.
  180. */
  181. this.triggerDisabled = false;
  182. /**
  183. * Accessible name for the component's close button.
  184. *
  185. * @default "Close"
  186. */
  187. this.intlClose = TEXT.close;
  188. this.guid = `calcite-popover-${guid()}`;
  189. this.openTransitionProp = "opacity";
  190. this.hasLoaded = false;
  191. // --------------------------------------------------------------------------
  192. //
  193. // Private Methods
  194. //
  195. // --------------------------------------------------------------------------
  196. this.setTransitionEl = (el) => {
  197. this.transitionEl = el;
  198. connectOpenCloseComponent(this);
  199. };
  200. this.setFilteredPlacements = () => {
  201. const { el, flipPlacements } = this;
  202. this.filteredFlipPlacements = flipPlacements
  203. ? filterComputedPlacements(flipPlacements, el)
  204. : null;
  205. };
  206. this.setUpReferenceElement = (warn = true) => {
  207. this.removeReferences();
  208. this.effectiveReferenceElement = this.getReferenceElement();
  209. connectFloatingUI(this, this.effectiveReferenceElement, this.el);
  210. const { el, referenceElement, effectiveReferenceElement } = this;
  211. if (warn && referenceElement && !effectiveReferenceElement) {
  212. console.warn(`${el.tagName}: reference-element id "${referenceElement}" was not found.`, {
  213. el
  214. });
  215. }
  216. this.addReferences();
  217. };
  218. this.getId = () => {
  219. return this.el.id || this.guid;
  220. };
  221. this.setExpandedAttr = () => {
  222. const { effectiveReferenceElement, open } = this;
  223. if (!effectiveReferenceElement) {
  224. return;
  225. }
  226. if ("setAttribute" in effectiveReferenceElement) {
  227. effectiveReferenceElement.setAttribute(ARIA_EXPANDED, toAriaBoolean(open));
  228. }
  229. };
  230. this.addReferences = () => {
  231. const { effectiveReferenceElement } = this;
  232. if (!effectiveReferenceElement) {
  233. return;
  234. }
  235. const id = this.getId();
  236. if ("setAttribute" in effectiveReferenceElement) {
  237. effectiveReferenceElement.setAttribute(ARIA_CONTROLS, id);
  238. }
  239. manager.registerElement(effectiveReferenceElement, this.el);
  240. this.setExpandedAttr();
  241. };
  242. this.removeReferences = () => {
  243. const { effectiveReferenceElement } = this;
  244. if (!effectiveReferenceElement) {
  245. return;
  246. }
  247. if ("removeAttribute" in effectiveReferenceElement) {
  248. effectiveReferenceElement.removeAttribute(ARIA_CONTROLS);
  249. effectiveReferenceElement.removeAttribute(ARIA_EXPANDED);
  250. }
  251. manager.unregisterElement(effectiveReferenceElement);
  252. };
  253. this.hide = () => {
  254. this.open = false;
  255. };
  256. this.storeArrowEl = (el) => {
  257. this.arrowEl = el;
  258. this.reposition(true);
  259. };
  260. }
  261. handleDismissible(value) {
  262. this.closable = value;
  263. }
  264. handleClosable(value) {
  265. this.dismissible = value;
  266. }
  267. flipPlacementsHandler() {
  268. this.setFilteredPlacements();
  269. this.reposition(true);
  270. }
  271. offsetDistanceOffsetHandler() {
  272. this.reposition(true);
  273. }
  274. offsetSkiddingHandler() {
  275. this.reposition(true);
  276. }
  277. openHandler(value) {
  278. if (value) {
  279. this.reposition(true);
  280. }
  281. else {
  282. updateAfterClose(this.el);
  283. }
  284. this.setExpandedAttr();
  285. }
  286. overlayPositioningHandler() {
  287. this.reposition(true);
  288. }
  289. placementHandler() {
  290. this.reposition(true);
  291. }
  292. referenceElementHandler() {
  293. this.setUpReferenceElement();
  294. this.reposition(true);
  295. }
  296. // --------------------------------------------------------------------------
  297. //
  298. // Lifecycle
  299. //
  300. // --------------------------------------------------------------------------
  301. connectedCallback() {
  302. this.setFilteredPlacements();
  303. connectOpenCloseComponent(this);
  304. const closable = this.closable || this.dismissible;
  305. if (closable) {
  306. this.handleDismissible(closable);
  307. }
  308. if (closable) {
  309. this.handleClosable(closable);
  310. }
  311. this.setUpReferenceElement(this.hasLoaded);
  312. }
  313. componentDidLoad() {
  314. if (this.referenceElement && !this.effectiveReferenceElement) {
  315. this.setUpReferenceElement();
  316. }
  317. this.reposition();
  318. this.hasLoaded = true;
  319. }
  320. disconnectedCallback() {
  321. this.removeReferences();
  322. disconnectFloatingUI(this, this.effectiveReferenceElement, this.el);
  323. disconnectOpenCloseComponent(this);
  324. }
  325. // --------------------------------------------------------------------------
  326. //
  327. // Public Methods
  328. //
  329. // --------------------------------------------------------------------------
  330. /**
  331. * Updates the position of the component.
  332. *
  333. * @param delayed
  334. */
  335. async reposition(delayed = false) {
  336. const { el, effectiveReferenceElement, placement, overlayPositioning, disableFlip, filteredFlipPlacements, offsetDistance, offsetSkidding, arrowEl } = this;
  337. return reposition(this, {
  338. floatingEl: el,
  339. referenceEl: effectiveReferenceElement,
  340. overlayPositioning,
  341. placement,
  342. disableFlip,
  343. flipPlacements: filteredFlipPlacements,
  344. offsetDistance,
  345. offsetSkidding,
  346. includeArrow: !this.disablePointer,
  347. arrowEl,
  348. type: "popover"
  349. }, delayed);
  350. }
  351. /**
  352. * Sets focus on the component.
  353. *
  354. * @param focusId
  355. */
  356. async setFocus(focusId) {
  357. var _a;
  358. const { closeButtonEl } = this;
  359. if (focusId === "close-button" && closeButtonEl) {
  360. forceUpdate(closeButtonEl);
  361. closeButtonEl.setFocus();
  362. return;
  363. }
  364. (_a = this.el) === null || _a === void 0 ? void 0 : _a.focus();
  365. }
  366. /**
  367. * Toggles the component's open property.
  368. *
  369. * @param value
  370. */
  371. async toggle(value = !this.open) {
  372. this.open = value;
  373. }
  374. getReferenceElement() {
  375. const { referenceElement, el } = this;
  376. return ((typeof referenceElement === "string"
  377. ? queryElementRoots(el, { id: referenceElement })
  378. : referenceElement) || null);
  379. }
  380. onBeforeOpen() {
  381. this.calcitePopoverBeforeOpen.emit();
  382. }
  383. onOpen() {
  384. this.calcitePopoverOpen.emit();
  385. }
  386. onBeforeClose() {
  387. this.calcitePopoverBeforeClose.emit();
  388. }
  389. onClose() {
  390. this.calcitePopoverClose.emit();
  391. }
  392. // --------------------------------------------------------------------------
  393. //
  394. // Render Methods
  395. //
  396. // --------------------------------------------------------------------------
  397. renderCloseButton() {
  398. const { closeButton, intlClose, heading, closable } = this;
  399. return closable || closeButton ? (h("div", { class: CSS.closeButtonContainer }, h("calcite-action", { class: CSS.closeButton, onClick: this.hide, ref: (closeButtonEl) => (this.closeButtonEl = closeButtonEl), scale: heading ? "s" : "m", text: intlClose }, h("calcite-icon", { icon: "x", scale: heading ? "s" : "m" })))) : null;
  400. }
  401. renderHeader() {
  402. const { heading, headingLevel } = this;
  403. const headingNode = heading ? (h(Heading, { class: CSS.heading, level: headingLevel || HEADING_LEVEL }, heading)) : null;
  404. return headingNode ? (h("div", { class: CSS.header }, headingNode, this.renderCloseButton())) : null;
  405. }
  406. render() {
  407. const { effectiveReferenceElement, heading, label, open, disablePointer } = this;
  408. const displayed = effectiveReferenceElement && open;
  409. const hidden = !displayed;
  410. const arrowNode = !disablePointer ? h("div", { class: CSS.arrow, ref: this.storeArrowEl }) : null;
  411. return (h(Host, { "aria-hidden": toAriaBoolean(hidden), "aria-label": label, "aria-live": "polite", "calcite-hydrated-hidden": hidden, id: this.getId(), role: "dialog" }, h("div", { class: {
  412. [FloatingCSS.animation]: true,
  413. [FloatingCSS.animationActive]: displayed
  414. }, ref: this.setTransitionEl }, arrowNode, h("div", { class: {
  415. [CSS.hasHeader]: !!heading,
  416. [CSS.container]: true
  417. } }, this.renderHeader(), h("div", { class: CSS.content }, h("slot", null)), !heading ? this.renderCloseButton() : null))));
  418. }
  419. get el() { return this; }
  420. static get watchers() { return {
  421. "dismissible": ["handleDismissible"],
  422. "closable": ["handleClosable"],
  423. "flipPlacements": ["flipPlacementsHandler"],
  424. "offsetDistance": ["offsetDistanceOffsetHandler"],
  425. "offsetSkidding": ["offsetSkiddingHandler"],
  426. "open": ["openHandler"],
  427. "overlayPositioning": ["overlayPositioningHandler"],
  428. "placement": ["placementHandler"],
  429. "referenceElement": ["referenceElementHandler"]
  430. }; }
  431. static get style() { return popoverCss; }
  432. }, [1, "calcite-popover", {
  433. "autoClose": [516, "auto-close"],
  434. "closeButton": [516, "close-button"],
  435. "dismissible": [1540],
  436. "closable": [1540],
  437. "disableFlip": [516, "disable-flip"],
  438. "disablePointer": [516, "disable-pointer"],
  439. "flipPlacements": [16],
  440. "heading": [1],
  441. "headingLevel": [514, "heading-level"],
  442. "label": [1],
  443. "offsetDistance": [514, "offset-distance"],
  444. "offsetSkidding": [514, "offset-skidding"],
  445. "open": [1540],
  446. "overlayPositioning": [513, "overlay-positioning"],
  447. "placement": [513],
  448. "referenceElement": [1, "reference-element"],
  449. "triggerDisabled": [516, "trigger-disabled"],
  450. "intlClose": [1, "intl-close"],
  451. "effectiveReferenceElement": [32],
  452. "reposition": [64],
  453. "setFocus": [64],
  454. "toggle": [64]
  455. }]);
  456. function defineCustomElement() {
  457. if (typeof customElements === "undefined") {
  458. return;
  459. }
  460. const components = ["calcite-popover", "calcite-action", "calcite-icon", "calcite-loader"];
  461. components.forEach(tagName => { switch (tagName) {
  462. case "calcite-popover":
  463. if (!customElements.get(tagName)) {
  464. customElements.define(tagName, Popover);
  465. }
  466. break;
  467. case "calcite-action":
  468. if (!customElements.get(tagName)) {
  469. defineCustomElement$3();
  470. }
  471. break;
  472. case "calcite-icon":
  473. if (!customElements.get(tagName)) {
  474. defineCustomElement$2();
  475. }
  476. break;
  477. case "calcite-loader":
  478. if (!customElements.get(tagName)) {
  479. defineCustomElement$1();
  480. }
  481. break;
  482. } });
  483. }
  484. defineCustomElement();
  485. export { Popover as P, defineCustomElement as d };