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,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC;yCAAgB;AAEtD;IAAX,QAAQ,EAAE;0CAA4B;AAWpC;IAHF,QAAQ,CAAC;QACR,SAAS,EAAE,eAAe;QAC1B,SAAS,EAAE,mBAAmB;KAC/B,CAAC;+CAA4B;AA2GjB;IAAZ,KAAK;qCAYL;AAEY;IAAZ,KAAK;qCAEL;AAlJU,SAAS;IADrB,aAAa,CAAC,YAAY,CAAC;GACf,SAAS","sourcesContent":["import type { PropertyValues, TemplateResult } from 'lit';\nimport { LitElement, html, isServer } from 'lit';\nimport { customElement } from 'lit/decorators/custom-element.js';\nimport { property } from 'lit/decorators/property.js';\nimport { styleMap } from 'lit/directives/style-map.js';\nimport { classMap } from 'lit/directives/class-map.js';\n\nimport {\n FloatingDOMController,\n type Placement,\n} from '@patternfly/pfe-core/controllers/floating-dom-controller.js';\n\nimport { bound } from '@patternfly/pfe-core/decorators/bound.js';\n\nimport { StringListConverter } from '@patternfly/pfe-core';\n\nimport styles from './pf-tooltip.css';\n\nconst EnterEvents = ['focusin', 'tap', 'click', 'mouseenter'];\nconst ExitEvents = ['focusout', 'blur', 'mouseleave'];\n\n/**\n * A **tooltip** is in-app messaging used to identify elements on a page with short,\n * clarifying text.\n * @summary Toggle the visibility of helpful or contextual information.\n * @slot\n * This slot wraps around the element that should be used to invoke the tooltip content to display.\n * Typically this would be an icon, button, or other small sized element.\n * @slot content\n * This slot renders the content that will be displayed inside of the tooltip.\n * Typically this would include a string of text without any additional elements.\n * This element is wrapped with a div inside of the component to give it the stylings and background colors.\n * @cssprop {<color>} [--pf-c-tooltip__content--BackgroundColor=#1b1d21]\n * Sets the background color for the tooltip content.\n *\n * @cssprop {<color>} [--pf-c-tooltip__content--Color=#e0e0e0]\n * Sets the font color for the tooltip content.\n *\n * @cssprop {<number>} [--pf-c-tooltip--line-height=1.5]\n * Sets the font color for the tooltip content.\n *\n * @cssprop {<length>} [--pf-c-tooltip--MaxWidth=18.75rem]\n * Maximum width for the tooltip.\n *\n * @cssprop [--pf-c-tooltip--BoxShadow=0 0.25rem 0.5rem 0rem rgba(3, 3, 3, 0.12), 0 0 0.25rem 0 rgba(3, 3, 3, 0.06)]\n * Box shadow for the tooltip.\n *\n * @cssprop {<length>} [--pf-c-tooltip__content--PaddingTop=0.5rem]\n * Top padding for the tooltip.\n *\n * @cssprop {<length>} [--pf-c-tooltip__content--PaddingRight=0.5rem]\n * Right padding for the tooltip.\n *\n * @cssprop {<length>} [--pf-c-tooltip__content--PaddingBottom=0.5rem]\n * Bottom padding for the tooltip.\n *\n * @cssprop {<length>} [--pf-c-tooltip__content--PaddingLeft=0.5rem]\n * Left Padding for the tooltip.\n *\n * @cssprop [--pf-c-tooltip__content--FontSize=0.875rem]\n * Font size for the tooltip content.\n *\n * @cssprop {<length>} [--pf-c-tooltip__arrow--Width=0.5rem]\n * Tooltip arrow width.\n *\n * @cssprop {<length>} [--pf-c-tooltip__arrow--Height=0.5rem]\n * Tooltip arrow height.\n *\n * @cssprop [--pf-c-tooltip__arrow--m-top--TranslateX=-50]\n * Positions the tooltip arrow along the x axis for `top` positioned arrows.\n *\n * @cssprop [--pf-c-tooltip__arrow--m-top--TranslateY=50]\n * Positions the tooltip arrow along the y axis for `top` positioned arrows.\n *\n * @cssprop [--pf-c-tooltip__arrow--m-top--Rotate=45deg]\n * Rotates the tooltip arrow based on degrees of movement for `top` positioned arrows.\n *\n * @cssprop [--pf-c-tooltip__arrow--m-right--TranslateX=-50]\n * Positions the tooltip arrow along the x axis for `right` positioned arrows.\n *\n * @cssprop [--pf-c-tooltip__arrow--m-right--TranslateY=-50]\n * Positions the tooltip arrow along the y axis for `right` positioned arrows.\n *\n * @cssprop [--pf-c-tooltip__arrow--m-right--Rotate=45deg]\n * Rotates the tooltip arrow based on degrees of movement for `right` positioned arrows.\n *\n * @cssprop [--pf-c-tooltip__arrow--m-bottom--TranslateX=-50]\n * Positions the tooltip arrow along the x axis for `bottom` positioned arrows.\n *\n * @cssprop [--pf-c-tooltip__arrow--m-bottom--TranslateY=-50]\n * Positions the tooltip arrow along the y axis for `bottom` positioned arrows.\n *\n * @cssprop [--pf-c-tooltip__arrow--m-bottom--Rotate=45deg]\n * Rotates the tooltip arrow based on degrees of movement for `bottom` positioned arrows.\n *\n * @cssprop [--pf-c-tooltip__arrow--m-left--TranslateX=50]\n * Positions the tooltip arrow along the x axis for `left` positioned arrows.\n *\n * @cssprop [--pf-c-tooltip__arrow--m-left--TranslateY=-50]\n * Positions the tooltip arrow along the y axis for `left` positioned arrows.\n *\n * @cssprop [--pf-c-tooltip__arrow--m-left--Rotate=45deg]\n * Rotates the tooltip arrow based on degrees of movement for `left` positioned arrows.\n *\n */\n@customElement('pf-tooltip')\nexport class PfTooltip extends LitElement {\n static readonly styles: CSSStyleSheet[] = [styles];\n\n /** The position of the tooltip, relative to the invoking content */\n @property() position: Placement = 'top';\n\n /** Tooltip content. Overridden by the content slot */\n @property() content?: string;\n\n /** If false, prevents the tooltip from trying to remain in view by flipping itself when necessary */\n @property({ type: Boolean, attribute: 'no-flip' }) noFlip = false;\n\n @property() trigger?: string | Element;\n\n /**\n * The flip order when flip is enabled and the initial position is not possible.\n * There are 12 options: `top`, `bottom`, `left`, `right`, `top-start`, `top-end`,\n * `bottom-start`, `bottom-end`, `left-start`, `left-end`,`right-start`, `right-end`.\n * The default is [oppositePlacement], where only the opposite placement is tried.\n */\n @property({\n attribute: 'flip-behavior',\n converter: StringListConverter,\n }) flipBehavior?: Placement[];\n\n get #invoker(): HTMLSlotElement | null {\n return this.shadowRoot?.querySelector?.('#invoker') ?? null;\n }\n\n get #content(): HTMLElement | null {\n return this.shadowRoot?.querySelector?.('#tooltip') ?? null;\n }\n\n #referenceTrigger?: HTMLElement | null;\n\n #float = new FloatingDOMController(this, {\n content: (): HTMLElement | null | undefined => this.#content,\n invoker: (): HTMLElement | null | undefined => {\n if (this.#referenceTrigger) {\n return this.#referenceTrigger;\n } else if (this.#invoker instanceof HTMLSlotElement\n && this.#invoker.assignedElements().length > 0) {\n return this.#invoker.assignedElements().at(0) as HTMLElement;\n } else {\n return this.#invoker;\n }\n },\n });\n\n override connectedCallback(): void {\n super.connectedCallback();\n this.#invokerChanged();\n this.#updateTrigger();\n }\n\n /**\n * Removes event listeners from the old trigger element and attaches\n * them to the new trigger element.\n * @param changed changed properties\n */\n override willUpdate(changed: PropertyValues<this>): void {\n if (changed.has('trigger')) {\n this.#updateTrigger();\n }\n }\n\n override render(): TemplateResult<1> {\n const { alignment, anchor, open, styles } = this.#float;\n\n const blockInvoker =\n this.#invoker?.assignedElements().length === 0\n && this.#invoker?.assignedNodes().length > 0;\n const display = blockInvoker ? 'block' : 'contents';\n\n return html`\n <div id=\"container\"\n style=\"${styleMap(styles)}\"\n class=\"${classMap({ open,\n [anchor]: !!anchor,\n [alignment]: !!alignment })}\">\n <div role=\"tooltip\"\n style=\"${styleMap({ display })}\"\n aria-labelledby=\"tooltip\">\n <slot id=\"invoker\" @slotchange=\"${this.#invokerChanged}\"></slot>\n </div>\n <div aria-hidden=\"${String(!open) as 'true' | 'false'}\">\n <slot id=\"tooltip\" name=\"content\">${this.content}</slot>\n </div>\n </div>\n `;\n }\n\n /** the invoker slot should render at block level if it only has text nodes */\n #invokerChanged() {\n this.requestUpdate();\n }\n\n #getReferenceTrigger() {\n return (this.getRootNode() as Document | ShadowRoot)\n .getElementById(this.trigger?.normalize() ?? '');\n }\n\n #updateTrigger() {\n if (!isServer) {\n const oldReferenceTrigger = this.#referenceTrigger;\n this.#referenceTrigger =\n this.trigger instanceof HTMLElement ? this.trigger\n : typeof this.trigger === 'string' ? this.#getReferenceTrigger()\n : null;\n for (const evt of EnterEvents) {\n if (this.#referenceTrigger) {\n this.removeEventListener(evt, this.show);\n this.#referenceTrigger.addEventListener(evt, this.show);\n } else {\n oldReferenceTrigger?.removeEventListener(evt, this.show);\n this.addEventListener(evt, this.show);\n }\n }\n for (const evt of ExitEvents) {\n if (this.#referenceTrigger) {\n this.removeEventListener(evt, this.hide);\n this.#referenceTrigger.addEventListener(evt, this.hide);\n } else {\n oldReferenceTrigger?.removeEventListener(evt, this.hide);\n this.addEventListener(evt, this.hide);\n }\n }\n }\n }\n\n @bound async show(): Promise<void> {\n await this.updateComplete;\n const placement = this.position;\n const offset =\n !placement?.match(/top|bottom/) ? 15\n : { mainAxis: 15, alignmentAxis: -4 };\n await this.#float.show({\n offset,\n placement,\n flip: !this.noFlip,\n fallbackPlacements: this.flipBehavior,\n });\n }\n\n @bound async hide(): Promise<void> {\n await this.#float.hide();\n }\n}\n\ndeclare global {\n interface HTMLElementTagNameMap {\n 'pf-tooltip': PfTooltip;\n }\n}\n"]}