UNPKG

@dialpad/dialtone

Version:

Dialpad's Dialtone design system monorepo

793 lines (792 loc) 28 kB
import { POPOVER_CONTENT_WIDTHS as w, POPOVER_HEADER_FOOTER_PADDING_CLASSES as P, POPOVER_PADDING_CLASSES as C, POPOVER_APPEND_TO_VALUES as S, POPOVER_INITIAL_FOCUS_STRINGS as A, POPOVER_STICKY_VALUES as _, POPOVER_ROLES as L } from "./popover-constants.js"; import { returnFirstEl as l, enableRootScrolling as T, disableRootScrolling as k, isOutOfViewPort as I, warnIfUnmounted as M, getUniqueString as O, hasSlotContent as x } from "../../common/utils/index.js"; import B from "../../common/mixins/modal.js"; import { createTippyPopover as N, getPopperOptions as V } from "./tippy-utils.js"; import D from "./popover-header-footer.js"; import q from "../../shared/sr_only_close_button.js"; import { resolveComponent as y, createElementBlock as F, openBlock as a, createBlock as p, createCommentVNode as c, Teleport as H, createElementVNode as E, withModifiers as g, resolveDynamicComponent as W, normalizeClass as f, withCtx as m, createVNode as z, withKeys as h, renderSlot as v, mergeProps as K, toHandlers as G } from "vue"; import { _ as U } from "../../_plugin-vue_export-helper-CHgC5LLL.js"; import j from "../lazy-show/lazy-show.js"; const R = { compatConfig: { MODE: 3 }, name: "DtPopover", /******************** * CHILD COMPONENTS * ********************/ components: { SrOnlyCloseButton: q, DtLazyShow: j, PopoverHeaderFooter: D }, mixins: [B], props: { /** * Controls whether the popover is shown. Leaving this null will have the popover trigger on click by default. * If you set this value, the default trigger behavior will be disabled, and you can control it as you need. * Supports v-model * @values null, true, false */ open: { type: Boolean, default: null }, /** * Opens the popover on right click (context menu). If you set this value to `true`, * the default trigger behavior will be disabled. * @values true, false */ openOnContext: { type: Boolean, default: !1 }, /** * Element type (tag name) of the root element of the component. */ elementType: { type: String, default: "div" }, /** * Named transition when the content display is toggled. * @see DtLazyShow */ transition: { type: String, default: "fade" }, /** * ARIA role for the content of the popover. Defaults to "dialog". * <a class="d-link" href="https://www.w3.org/TR/wai-aria/#aria-haspopup" target="_blank">aria-haspopup</a> */ role: { type: String, default: "dialog", validator: (e) => L.includes(e) }, /** * ID of the element that serves as the label for the popover content. * Defaults to the "anchor" element; this exists to provide a different * ID of the label element if, for example, the anchor slot contains * other items that do not serve as a label. You should provide this * or ariaLabel, but not both. */ ariaLabelledby: { type: String, default: null }, /** * Descriptive label for the popover content. You should provide this * or ariaLabelledby, but not both. */ ariaLabel: { type: String, default: null }, /** * Padding size class for the popover content. * @values none, small, medium, large */ padding: { type: String, default: "large", validator: (e) => Object.keys(C).some((t) => t === e) }, /** * Additional class name for the content wrapper element. */ contentClass: { type: [String, Array, Object], default: "" }, /** * Width configuration for the popover content. When its value is 'anchor', * the popover content will have the same width as the anchor. * @values null, anchor */ contentWidth: { type: String, default: "", validator: (e) => w.includes(e) }, /** * Whether to apply transition on initial render in the content lazy show component. */ contentAppear: { type: Boolean, default: null }, /** * Tabindex value for the content. Passing null, no tabindex attribute will be set. */ contentTabindex: { type: Number || null, default: -1 }, /** * External anchor id to use in those cases the anchor can't be provided via the slot. * For instance, using the combobox's input as the anchor for the popover. */ externalAnchor: { type: String, default: "" }, /** * The id of the tooltip */ id: { type: String, default() { return O(); } }, /** * Displaces the content box from its anchor element * by the specified number of pixels. * <a * class="d-link" * href="https://atomiks.github.io/tippyjs/v6/all-props/#offset" * target="_blank" * > * Tippy.js docs * </a> */ offset: { type: Array, default: () => [0, 4] }, /** * Determines if the popover hides upon clicking the * anchor or outside the content box. * @values true, false */ hideOnClick: { type: Boolean, default: !0 }, /** * Determines modal state. If enabled popover has a modal overlay * preventing interaction with elements below it, but it is invisible. * @values true, false */ modal: { type: Boolean, default: !0 }, /** * If the popover does not fit in the direction described by "placement", * it will attempt to change its direction to the "fallbackPlacements". * <a * class="d-link" * href="https://popper.js.org/docs/v2/modifiers/flip/#fallbackplacements" * target="_blank" * > * Popper.js docs * </a> * */ fallbackPlacements: { type: Array, default: () => ["auto"] }, /** * The direction the popover displays relative to the anchor. * <a * class="d-link" * href="https://atomiks.github.io/tippyjs/v6/all-props/#placement" * target="_blank" * > * Tippy.js docs * </a> * @values top, top-start, top-end, * right, right-start, right-end, * left, left-start, left-end, * bottom, bottom-start, bottom-end, * auto, auto-start, auto-end */ placement: { type: String, default: "bottom-end" }, /** * If set to false the dialog will display over top of the anchor when there is insufficient space. * If set to true it will never move from its position relative to the anchor and will clip instead. * <a * class="d-link" * href="https://popper.js.org/docs/v2/modifiers/prevent-overflow/#tether" * target="_blank" * > * Popper.js docs * </a> * @values true, false */ tether: { type: Boolean, default: !0 }, /** * If the popover sticks to the anchor. This is usually not needed, but can be needed * if the reference element's position is animating, or to automatically update the popover * position in those cases the DOM layout changes the reference element's position. * `true` enables it, `reference` only checks the "reference" rect for changes and `popper` only * checks the "popper" rect for changes. * <a * class="d-link" * href="https://atomiks.github.io/tippyjs/v6/all-props/#sticky" * target="_blank" * > * Tippy.js docs * </a> * @values true, false, reference, popper */ sticky: { type: [Boolean, String], default: !1, validator: (e) => _.includes(e) }, /** * Determines maximum height for the popover before overflow. * Possible units rem|px|em */ maxHeight: { type: String, default: "" }, /** * Determines maximum width for the popover before overflow. * Possible units rem|px|%|em */ maxWidth: { type: String, default: "" }, /** * Determines visibility for close button * @values true, false */ showCloseButton: { type: Boolean, default: !1 }, /** * Additional class name for the header content wrapper element. */ headerClass: { type: [String, Array, Object], default: "" }, /** * Additional class name for the footer content wrapper element. */ footerClass: { type: [String, Array, Object], default: "" }, /** * Additional class name for the dialog element. */ dialogClass: { type: [String, Array, Object], default: "" }, /** * The element that is focused when the popover is opened. This can be an * HTMLElement within the popover, a string starting with '#' which will * find the element by ID. 'first' which will automatically focus * the first element, or 'dialog' which will focus the dialog window itself. * If the dialog is modal this prop cannot be 'none'. * @values none, dialog, first */ initialFocusElement: { type: [String, HTMLElement], default: "first", validator: (e) => A.includes(e) || e instanceof HTMLElement || e.startsWith("#") }, /** * If the popover should open pressing up or down arrow key on the anchor element. * This can be set when not passing open prop. * @values true, false */ openWithArrowKeys: { type: Boolean, default: !1 }, /** * Sets the element to which the popover is going to append to. * 'body' will append to the nearest body (supports shadow DOM). * 'root' will try append to the iFrame's parent body if it is contained in an iFrame * and has permissions to access it, else, it'd default to 'parent'. * @values 'body', 'parent', 'root', HTMLElement */ appendTo: { type: [HTMLElement, String], default: "body", validator: (e) => S.includes(e) || e instanceof HTMLElement } }, emits: [ /** * Native keydown event * * @event keydown * @type {KeyboardEvent} */ "keydown", /** * Event fired to sync the open prop with the parent component * @event update:open */ "update:open", /** * Emitted when popover is shown or hidden * * @event opened * @type {Boolean | Array} */ "opened", /** * Emitted when the mouse enters the popover * * @event mouseenter-popover */ "mouseenter-popover", /** * Emitted when the mouse leaves the popover * * @event mouseleave-popover */ "mouseleave-popover", /** * Emitted when the mouse enters the popover anchor * * @event mouseenter-popover-anchor */ "mouseenter-popover-anchor", /** * Emitted when the mouse leaves the popover anchor * * @event mouseleave-popover-anchor */ "mouseleave-popover-anchor" ], data() { return { POPOVER_PADDING_CLASSES: C, POPOVER_HEADER_FOOTER_PADDING_CLASSES: P, intersectionObserver: null, isOutsideViewport: !1, isOpen: !1, toAppear: !1, anchorEl: null, popoverContentEl: null, hasSlotContent: x }; }, computed: { popoverListeners() { return { keydown: (e) => { this.onKeydown(e); }, "after-leave": () => { this.onLeaveTransitionComplete(); }, "after-enter": () => { this.onEnterTransitionComplete(); } }; }, calculatedMaxHeight() { return this.isOutsideViewport && this.modal ? "calc(100vh - var(--dt-space-300))" : this.maxHeight; }, labelledBy() { return this.ariaLabelledby || !this.ariaLabel && O("DtPopover__anchor"); } }, watch: { $props: { immediate: !0, deep: !0, handler() { this.validateProps(); } }, modal(e) { var t; (t = this.tip) == null || t.setProps({ zIndex: e ? 650 : this.calculateAnchorZindex() }); }, offset(e) { var t; (t = this.tip) == null || t.setProps({ offset: e }); }, sticky(e) { var t; (t = this.tip) == null || t.setProps({ sticky: e }); }, fallbackPlacements() { var e; (e = this.tip) == null || e.setProps({ popperOptions: this.popperOptions() }); }, tether() { var e; (e = this.tip) == null || e.setProps({ popperOptions: this.popperOptions() }); }, placement(e) { var t; (t = this.tip) == null || t.setProps({ placement: e }); }, open: { handler: function(e) { e !== null && (this.isOpen = e), e === !0 && (this.toAppear = !0); }, immediate: !0 }, contentAppear: { handler: function(e) { e !== null && (this.toAppear = e); } }, isOpen(e, t) { e ? (this.initTippyInstance(), this.tip.show()) : !e && t !== e && (this.removeEventListeners(), this.tip.hide()); } }, mounted() { var t; M(l(this.$el), this.$options.name); const e = this.externalAnchor ? this.$refs.anchor.getRootNode().querySelector(`#${this.externalAnchor}`) : null; this.anchorEl = e ?? this.$refs.anchor.children[0], this.popoverContentEl = l((t = this.$refs.content) == null ? void 0 : t.$el), this.isOpen && (this.initTippyInstance(), this.tip.show()), this.intersectionObserver = new IntersectionObserver(this.hasIntersectedViewport), this.intersectionObserver.observe(this.popoverContentEl); }, beforeUnmount() { var e, t; (e = this.tip) == null || e.destroy(), (t = this.intersectionObserver) == null || t.disconnect(), this.removeReferences(), this.removeEventListeners(); }, /****************** * METHODS * ******************/ methods: { hasIntersectedViewport(e) { var s; const t = (s = e == null ? void 0 : e[0]) == null ? void 0 : s.target; if (!t) return; const o = I(t); this.isOutsideViewport = o.bottom || o.top; }, popperOptions() { return V({ fallbackPlacements: this.fallbackPlacements, tether: this.tether, hasHideModifierEnabled: !0 }); }, validateProps() { this.modal && this.initialFocusElement === "none" && console.error('If the popover is modal you must set the initialFocusElement prop. Possible values: "dialog", "first", HTMLElement'); }, calculateAnchorZindex() { var e; return l(this.$el).getRootNode().querySelector('.d-modal[aria-hidden="false"], .d-modal--transparent[aria-hidden="false"]') || // Special case because we don't have any dialtone drawer component yet. Render at 650 when // anchor of popover is within a drawer. (e = this.anchorEl) != null && e.closest(".d-zi-drawer") ? 650 : 300; }, defaultToggleOpen(e) { var t, o, s; if (!this.openOnContext && (this.open === null || this.open === void 0)) { if (!((t = this.anchorEl) != null && t.contains(e.target)) && !((o = this.anchorEl) != null && o.isEqualNode(e.target)) || (s = this.anchorEl) != null && s.disabled) return; this.toggleOpen(); } }, async onContext(e) { this.openOnContext && (e.preventDefault(), this.isOpen = !0, await this.$nextTick(), this.tip.setProps({ placement: "right-start", getReferenceClientRect: () => ({ width: 0, height: 0, top: e.clientY, bottom: e.clientY, left: e.clientX, right: e.clientX }) })); }, toggleOpen() { this.isOpen = !this.isOpen; }, onArrowKeyPress(e) { var t; this.open === null && (this.openWithArrowKeys && ((t = this.anchorEl) != null && t.contains(e.target)) && (this.isOpen || (this.isOpen = !0)), this.$emit("keydown", e)); }, addEventListeners() { window.addEventListener("dt-popover-close", this.closePopover), this.contentWidth === "anchor" && window.addEventListener("resize", this.onResize); }, removeEventListeners() { window.removeEventListener("dt-popover-close", this.closePopover), this.contentWidth === "anchor" && window.removeEventListener("resize", this.onResize); }, closePopover() { this.isOpen = !1; }, /* * Prevents scrolling outside of the currently opened modal popover by: * - when anchor is not within another popover: setting the body to overflow: hidden * - when anchor is within another popover: set the popover dialog container to it's non-modal z-index * since it is no longer the active modal. This puts it underneath the overlay and prevents scrolling. **/ preventScrolling() { var e, t; if (this.modal) { const o = (e = this.anchorEl) == null ? void 0 : e.closest("body, .tippy-box"); if (!o) return; ((t = o.tagName) == null ? void 0 : t.toLowerCase()) === "body" ? (k(this.anchorEl.getRootNode().host), this.tip.setProps({ offset: this.offset })) : o.classList.add("d-zi-popover"); } }, /* * Resets the prevent scrolling properties set in preventScrolling() back to normal. **/ enableScrolling() { var t, o; const e = (t = this.anchorEl) == null ? void 0 : t.closest("body, .tippy-box"); e && (((o = e.tagName) == null ? void 0 : o.toLowerCase()) === "body" ? (T(this.anchorEl.getRootNode().host), this.tip.setProps({ offset: this.offset })) : e.classList.remove("d-zi-popover")); }, removeReferences() { this.anchorEl = null, this.popoverContentEl = null, this.tip = null; }, async onShow() { this.contentWidth === "anchor" && await this.setPopoverContentAnchorWidth(), this.contentWidth === null && (this.popoverContentEl.style.width = "auto"), this.addEventListeners(); }, async onLeaveTransitionComplete() { var e; this.modal && (await this.focusFirstElement(this.$refs.anchor), await this.$nextTick(), this.enableScrolling()), (e = this.tip) == null || e.unmount(), this.$emit("opened", !1), this.open !== null && this.$emit("update:open", !1); }, async onEnterTransitionComplete() { this.focusInitialElement(), await this.$nextTick(), this.preventScrolling(), this.$emit("opened", !0, this.$refs.popover__content), this.open !== null && this.$emit("update:open", !0); }, focusInitialElement() { var e, t; this.initialFocusElement === "dialog" && ((t = l((e = this.$refs.content) == null ? void 0 : e.$el)) == null || t.focus()), this.initialFocusElement.startsWith("#") && this.focusInitialElementById(), this.initialFocusElement === "first" && this.focusFirstElementIfNeeded(this.$refs.popover__content), this.initialFocusElement instanceof HTMLElement && this.initialFocusElement.focus(); }, focusInitialElementById() { var t, o, s; const e = (o = l((t = this.$refs.content) == null ? void 0 : t.$el)) == null ? void 0 : o.querySelector(this.initialFocusElement); e ? e.focus() : console.warn('Could not find the element specified in dt-popover prop "initialFocusElement". Defaulting to focusing the dialog.'), e ? e.focus() : l((s = this.$refs.content) == null ? void 0 : s.$el).focus(); }, onResize() { this.closePopover(); }, onClickOutside() { var t; if (!this.hideOnClick) return; ((t = this.popoverContentEl) == null ? void 0 : t.querySelector(".d-popover__anchor--opened")) || this.closePopover(); }, onKeydown(e) { e.key === "Tab" && this.modal && this.focusTrappedTabPress(e, this.popoverContentEl), e.key === "Escape" && this.closePopover(), this.$emit("keydown", e); }, async setPopoverContentAnchorWidth() { var e; await this.$nextTick(), this.popoverContentEl.style.width = `${(e = this.anchorEl) == null ? void 0 : e.clientWidth}px`; }, focusFirstElementIfNeeded(e) { var o, s; this._getFocusableElements(e, !0).length !== 0 ? this.focusFirstElement(e) : this.showCloseButton ? (o = this.$refs.popover__header) == null || o.focusCloseButton() : l((s = this.$refs.content) == null ? void 0 : s.$el).focus(); }, /** * Return's the anchor ClientRect object relative to the window. * Refer to: https://atomiks.github.io/tippyjs/v6/all-props/#getreferenceclientrect for more information * @param error */ getReferenceClientRect(e) { var d, u; const t = (d = this.anchorEl) == null ? void 0 : d.getBoundingClientRect(); if (this.appendTo !== "root" || e) return t; const o = (u = this.anchorEl) == null ? void 0 : u.ownerDocument, s = (o == null ? void 0 : o.defaultView) || (o == null ? void 0 : o.parentWindow), r = s == null ? void 0 : s.frameElement; if (!r) return t; const n = r.getBoundingClientRect(); return { width: t == null ? void 0 : t.width, height: t == null ? void 0 : t.height, top: (n == null ? void 0 : n.top) + (t == null ? void 0 : t.top), left: (n == null ? void 0 : n.left) + (t == null ? void 0 : t.left), right: (n == null ? void 0 : n.right) + (t == null ? void 0 : t.right), bottom: (n == null ? void 0 : n.bottom) + (t == null ? void 0 : t.bottom) }; }, initTippyInstance() { var o, s; let e = null, t = !1; switch (this.appendTo) { case "body": e = (s = (o = this.anchorEl) == null ? void 0 : o.getRootNode()) == null ? void 0 : s.querySelector("body"); break; case "root": try { e = window.parent.document.body; } catch (r) { console.error("Could not attach the popover to iframe parent window: ", r), e = "parent", t = !0; } break; default: e = this.appendTo; break; } this.tip = N(this.anchorEl, { popperOptions: this.popperOptions(), contentElement: this.popoverContentEl, placement: this.placement, offset: this.offset, sticky: this.sticky, appendTo: e, interactive: !0, trigger: "manual", getReferenceClientRect: () => this.getReferenceClientRect(t), // We have to manage hideOnClick functionality manually to handle // popover within popover situations. hideOnClick: !1, zIndex: this.modal ? 650 : this.calculateAnchorZindex(), onClickOutside: this.onClickOutside, onShow: this.onShow }); }, onMouseEnter() { this.$emit("mouseenter-popover"); }, onMouseLeave() { this.$emit("mouseleave-popover"); }, onMouseEnterAnchor() { this.$emit("mouseenter-popover-anchor"); }, onMouseLeaveAnchor() { this.$emit("mouseleave-popover-anchor"); } } }, Y = ["id", "data-qa", "tabindex"], Z = ["data-qa"]; function X(e, t, o, s, r, n) { const d = y("popover-header-footer"), u = y("sr-only-close-button"), b = y("dt-lazy-show"); return a(), F("div", null, [ o.modal && r.isOpen ? (a(), p(H, { key: 0, to: "body" }, [ E("div", { class: "d-modal--transparent", onClick: t[0] || (t[0] = g(() => { }, ["prevent", "stop"])) }) ])) : c("", !0), (a(), p(W(o.elementType), { ref: "popover", class: f(["d-popover", { "d-popover__anchor--opened": r.isOpen }]), "data-qa": "dt-popover-container" }, { default: m(() => [ E("div", { id: !o.ariaLabelledby && n.labelledBy, ref: "anchor", "data-qa": e.$attrs["data-qa"] ? `${e.$attrs["data-qa"]}-anchor` : "dt-popover-anchor", tabindex: o.openOnContext ? 0 : void 0, onClickCapture: t[1] || (t[1] = (...i) => n.defaultToggleOpen && n.defaultToggleOpen(...i)), onContextmenu: t[2] || (t[2] = (...i) => n.onContext && n.onContext(...i)), onKeydown: [ t[3] || (t[3] = h(g((...i) => n.onArrowKeyPress && n.onArrowKeyPress(...i), ["prevent"]), ["up"])), t[4] || (t[4] = h(g((...i) => n.onArrowKeyPress && n.onArrowKeyPress(...i), ["prevent"]), ["down"])), t[6] || (t[6] = h((i) => e.$emit("keydown", i), ["enter"])), t[7] || (t[7] = h((i) => e.$emit("keydown", i), ["space"])) ], onKeydownCapture: t[5] || (t[5] = h((...i) => n.closePopover && n.closePopover(...i), ["escape"])), onMouseenter: t[8] || (t[8] = (...i) => n.onMouseEnter && n.onMouseEnter(...i)), onMouseleave: t[9] || (t[9] = (...i) => n.onMouseLeave && n.onMouseLeave(...i)) }, [ v(e.$slots, "anchor", { attrs: { "aria-expanded": r.isOpen.toString(), "aria-controls": o.id, "aria-haspopup": o.role } }) ], 40, Y), z(b, K({ id: o.id, ref: "content", role: o.role, "data-qa": e.$attrs["data-qa"] ? `${e.$attrs["data-qa"]}__dialog` : "dt-popover", "aria-hidden": `${!r.isOpen}`, "aria-labelledby": n.labelledBy, "aria-label": o.ariaLabel, "aria-modal": `${!o.modal}`, transition: o.transition, show: r.isOpen, appear: r.toAppear, class: ["d-popover__dialog", { "d-popover__dialog--modal": o.modal }, o.dialogClass], style: { "max-height": n.calculatedMaxHeight, "max-width": o.maxWidth }, css: e.$attrs.css, tabindex: o.contentTabindex }, G(n.popoverListeners), { onMouseenter: n.onMouseEnterAnchor, onMouseleave: n.onMouseLeaveAnchor }), { default: m(() => [ r.hasSlotContent(e.$slots.headerContent) || o.showCloseButton ? (a(), p(d, { key: 0, ref: "popover__header", class: f(r.POPOVER_HEADER_FOOTER_PADDING_CLASSES[o.padding]), "content-class": o.headerClass, type: "header", "show-close-button": o.showCloseButton, onClose: n.closePopover }, { content: m(() => [ v(e.$slots, "headerContent", { close: n.closePopover }) ]), _: 3 }, 8, ["class", "content-class", "show-close-button", "onClose"])) : c("", !0), E("div", { ref: "popover__content", "data-qa": e.$attrs["data-qa"] ? `${e.$attrs["data-qa"]}-content` : "dt-popover-content", class: f([ "d-popover__content", r.POPOVER_PADDING_CLASSES[o.padding], o.contentClass ]) }, [ v(e.$slots, "content", { close: n.closePopover }) ], 10, Z), r.hasSlotContent(e.$slots.footerContent) ? (a(), p(d, { key: 1, ref: "popover__footer", type: "footer", class: f(r.POPOVER_HEADER_FOOTER_PADDING_CLASSES[o.padding]), "content-class": o.footerClass }, { content: m(() => [ v(e.$slots, "footerContent", { close: n.closePopover }) ]), _: 3 }, 8, ["class", "content-class"])) : c("", !0), o.showCloseButton ? c("", !0) : (a(), p(u, { key: 2, onClose: n.closePopover }, null, 8, ["onClose"])) ]), _: 3 }, 16, ["id", "role", "data-qa", "aria-hidden", "aria-labelledby", "aria-label", "aria-modal", "transition", "show", "appear", "class", "style", "css", "tabindex", "onMouseenter", "onMouseleave"]) ]), _: 3 }, 8, ["class"])) ]); } const re = /* @__PURE__ */ U(R, [["render", X]]); export { re as default }; //# sourceMappingURL=popover.js.map