|
| 1 | +import Component from '@glimmer/component'; |
| 2 | +import { tracked } from '@glimmer/tracking'; |
| 3 | + |
| 4 | +import { modifier } from 'ember-modifier'; |
| 5 | + |
| 6 | +import EuiPopover from './eui-popover.gts'; |
| 7 | +import EuiPortal from './eui-portal.gts'; |
| 8 | + |
| 9 | +import type { EuiPopoverSignature } from './eui-popover.gts'; |
| 10 | + |
| 11 | +export interface EuiWrappingPopoverSignature { |
| 12 | + Element: EuiPopoverSignature['Element']; |
| 13 | + Args: EuiPopoverSignature['Args'] & { |
| 14 | + button?: HTMLElement; |
| 15 | + }; |
| 16 | + Blocks: { |
| 17 | + default: []; |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +export default class EuiWrappingPopover extends Component<EuiWrappingPopoverSignature> { |
| 22 | + private portal: HTMLElement | null = null; |
| 23 | + private anchor: HTMLElement | null = null; |
| 24 | + |
| 25 | + setAnchorRef = modifier((element: HTMLElement) => { |
| 26 | + this.anchor = element; |
| 27 | + this.anchor.insertAdjacentElement('beforebegin', this.args.button!); |
| 28 | + }); |
| 29 | + |
| 30 | + setPortalRef = (element: HTMLElement) => { |
| 31 | + this.portal = element; |
| 32 | + }; |
| 33 | + |
| 34 | + get insert(): { sibling: HTMLElement; position: 'after' } | undefined { |
| 35 | + if (this.args.button) { |
| 36 | + return { |
| 37 | + sibling: this.args.button, |
| 38 | + position: 'after' |
| 39 | + }; |
| 40 | + } |
| 41 | + |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + willDestroy() { |
| 46 | + super.willDestroy(); |
| 47 | + |
| 48 | + if (this.args.button && this.args.button.parentNode) { |
| 49 | + if (this.portal) { |
| 50 | + this.portal.insertAdjacentElement('beforebegin', this.args.button); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + <template> |
| 56 | + <EuiPortal @portalRef={{this.setPortalRef}} @insert={{this.insert}}> |
| 57 | + <EuiPopover |
| 58 | + @closePopover={{@closePopover}} |
| 59 | + @isOpen={{@isOpen}} |
| 60 | + @anchorClassName={{@anchorClassName}} |
| 61 | + @anchorPosition={{@anchorPosition}} |
| 62 | + @attachToAnchor={{@attachToAnchor}} |
| 63 | + @container={{@container}} |
| 64 | + @display={{@display}} |
| 65 | + @hasArrow={{@hasArrow}} |
| 66 | + @initialFocus={{@initialFocus}} |
| 67 | + @ownFocus={{@ownFocus}} |
| 68 | + @panelClassName={{@panelClassName}} |
| 69 | + @panelPaddingSize={{@panelPaddingSize}} |
| 70 | + @panelStyle={{@panelStyle}} |
| 71 | + @panelRef={{@panelRef}} |
| 72 | + @popoverRef={{@popoverRef}} |
| 73 | + ...attributes |
| 74 | + > |
| 75 | + <:button> |
| 76 | + <div {{this.setAnchorRef}} class="euiWrappingPopover__anchor" /> |
| 77 | + </:button> |
| 78 | + |
| 79 | + <:content> |
| 80 | + {{yield}} |
| 81 | + </:content> |
| 82 | + </EuiPopover> |
| 83 | + </EuiPortal> |
| 84 | + </template> |
| 85 | +} |
0 commit comments