|
| 1 | +// @ts-nocheck |
| 2 | + |
| 3 | +export function polyfill() { |
| 4 | + const originalAddEventListener = this.addEventListener; |
| 5 | + |
| 6 | + // This is the only way to get access to the private functions onFocusOut and onBeforeToggle. |
| 7 | + this.addEventListener = function (type, listener, options) { |
| 8 | + if (type === 'focusout') { |
| 9 | + // focusout doesn't work properly in firefox, so we ignore it. |
| 10 | + return; |
| 11 | + } |
| 12 | + if (type === 'beforetoggle') { |
| 13 | + // Intercept the beforetoggle event so we can dispatch our own event. |
| 14 | + this.polyfill_onBeforeToggle = event => { |
| 15 | + this.dispatchEvent( |
| 16 | + new CustomEvent('polyfill-beforetoggle', { |
| 17 | + bubbles: false, |
| 18 | + composed: false, |
| 19 | + detail: { |
| 20 | + oldState: event.oldState, |
| 21 | + newState: event.newState, |
| 22 | + }, |
| 23 | + }) |
| 24 | + ); |
| 25 | + |
| 26 | + listener(event); |
| 27 | + }; |
| 28 | + return; |
| 29 | + } |
| 30 | + originalAddEventListener.call(this, type, listener, options); |
| 31 | + }; |
| 32 | + |
| 33 | + this.polyfill_onFocusout = event => { |
| 34 | + const target = event.relatedTarget; |
| 35 | + if (!target) return; |
| 36 | + |
| 37 | + const isInsidePopoverContainer = target?.closest('uui-popover-container'); |
| 38 | + const isInsidePopoverTarget = target?.closest('[popovertarget]'); |
| 39 | + |
| 40 | + if (!isInsidePopoverContainer && !isInsidePopoverTarget) { |
| 41 | + this.hidePopover(); |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + this.polyfill_onClick = event => { |
| 46 | + const path = event.composedPath(); |
| 47 | + const isInsidePopoverContainer = path.some(element => { |
| 48 | + return ( |
| 49 | + element.tagName === 'UUI-POPOVER-CONTAINER' || |
| 50 | + element.attributes?.popovertarget |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + if (!isInsidePopoverContainer) { |
| 55 | + this.hidePopover(); |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + this.polyfill_onParentPopoverUpdate = event => { |
| 60 | + if (event.detail.newState === 'closed') { |
| 61 | + this.hidePopover(); |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + const findParentPopover = element => { |
| 66 | + if (!element.parentElement) return null; |
| 67 | + if (element.parentElement?.tagName === 'UUI-POPOVER-CONTAINER') { |
| 68 | + return element.parentElement; |
| 69 | + } |
| 70 | + return findParentPopover(element.parentElement); |
| 71 | + }; |
| 72 | + |
| 73 | + if (!this.polyfill_hasBeenMovedToBody) { |
| 74 | + this.style.display = 'none'; |
| 75 | + this.style.position = 'fixed'; |
| 76 | + this.style.inset = '0'; |
| 77 | + this.style.zIndex = '9999'; |
| 78 | + } |
| 79 | + |
| 80 | + this.showPopover = () => { |
| 81 | + if (!this.polyfill_hasBeenMovedToBody) { |
| 82 | + this.polyfill_parentPopoverContainer = findParentPopover(this); |
| 83 | + } |
| 84 | + |
| 85 | + this.polyfill_onBeforeToggle({ |
| 86 | + oldState: 'closed', |
| 87 | + newState: 'open', |
| 88 | + }); |
| 89 | + this.style.display = 'block'; |
| 90 | + this.polyfill_parentPopoverContainer?.addEventListener( |
| 91 | + 'polyfill-beforetoggle', |
| 92 | + this.polyfill_onParentPopoverUpdate |
| 93 | + ); |
| 94 | + window.addEventListener('click', this.polyfill_onClick); |
| 95 | + window.addEventListener('focusout', this.polyfill_onFocusout); |
| 96 | + |
| 97 | + //Find the dom position that the popover is currently at and save it so we can restore it later. |
| 98 | + this.polyfill_originalParent = this.parentNode; |
| 99 | + this.polyfill_originalNextSibling = this.nextSibling; |
| 100 | + |
| 101 | + //TODO: Find the render root of this component and get the stylesheet from there. |
| 102 | + const renderRoot = this.getRootNode(); |
| 103 | + |
| 104 | + if (renderRoot && renderRoot.adoptedStyleSheets) { |
| 105 | + // Styles from the parent are lost when moving the popover to the body, so we need to add them back. |
| 106 | + const adoptedStyleSheets = renderRoot.adoptedStyleSheets; |
| 107 | + const combinedStyles = adoptedStyleSheets.reduce((acc, styleSheet) => { |
| 108 | + return ( |
| 109 | + acc + |
| 110 | + Object.values(styleSheet.cssRules).reduce((acc, rule) => { |
| 111 | + return acc + `#${this.id} ${rule.cssText}`; |
| 112 | + }, '') |
| 113 | + ); |
| 114 | + }, ''); |
| 115 | + |
| 116 | + const styleTag = document.createElement('style'); |
| 117 | + styleTag.innerHTML = combinedStyles; |
| 118 | + styleTag.id = 'uui-popover-polyfill-style'; |
| 119 | + |
| 120 | + //look in slot for existing style tag and remove it. |
| 121 | + const existingStyleTag = this.shadowRoot.host.querySelector( |
| 122 | + '#uui-popover-polyfill-style' |
| 123 | + ); |
| 124 | + |
| 125 | + if (existingStyleTag) { |
| 126 | + existingStyleTag.parentNode.removeChild(existingStyleTag); |
| 127 | + } |
| 128 | + |
| 129 | + this.insertAdjacentElement('beforeend', styleTag); |
| 130 | + } |
| 131 | + |
| 132 | + //Move the popover to the body so it sits on top of everything else. |
| 133 | + if (this.parentNode !== document.body) { |
| 134 | + this.parentNode?.removeChild(this); |
| 135 | + this.polyfill_hasBeenMovedToBody = true; |
| 136 | + document.body.appendChild(this); |
| 137 | + } |
| 138 | + }; |
| 139 | + this.hidePopover = () => { |
| 140 | + //Restore previous dom position. |
| 141 | + if (this.polyfill_hasBeenMovedToBody) { |
| 142 | + document.body.removeChild(this); |
| 143 | + this.polyfill_hasBeenMovedToBody = false; |
| 144 | + this.polyfill_originalParent?.insertBefore( |
| 145 | + this, |
| 146 | + this.polyfill_originalNextSibling |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + window.removeEventListener('click', this.polyfill_onClick); |
| 151 | + window.removeEventListener('focusout', this.polyfill_onFocusout); |
| 152 | + this.polyfill_parentPopoverContainer?.removeEventListener( |
| 153 | + 'polyfill-beforetoggle', |
| 154 | + this.polyfill_onParentPopoverUpdate |
| 155 | + ); |
| 156 | + this.polyfill_onBeforeToggle({ |
| 157 | + oldState: 'open', |
| 158 | + newState: 'closed', |
| 159 | + }); |
| 160 | + this.style.display = 'none'; |
| 161 | + }; |
| 162 | +} |
0 commit comments