Skip to content

Commit d56ebf4

Browse files
committed
* popover: enhance Popover class with temporary element creation and cleanup, improving dynamic element management and ensuring proper destruction on hide.
1 parent 59d5e1b commit d56ebf4

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

lib/popover/src/vanilla/popover.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {arrow, computePosition, flip, shift, size, autoUpdate, offset, VirtualElement, ReferenceElement, ComputePositionConfig} from '@floating-ui/dom';
2-
import {Component, $, ComponentEvents, JSX, evalValue, toCssSize} from '@zui/core';
2+
import {Component, $, ComponentEvents, JSX, evalValue, toCssSize, nextGid} from '@zui/core';
33
import {PopoverEvents, PopoverOptions, PopoverPanelOptions, PopoverSide} from '../types';
44
import {PopoverPanel} from './popover-panel';
55
import {isElementDetached} from '@zui/core/src/dom';
@@ -309,6 +309,9 @@ export class Popover<O extends PopoverOptions = PopoverOptions, E extends Compon
309309
const {namespace} = this;
310310
$(this._triggerElement as HTMLElement).off(namespace);
311311
}
312+
if (this.$element.hasClass('popover-tmp')) {
313+
this.$element.remove();
314+
}
312315
this._resetTimer();
313316
this._destoryTarget();
314317
this._clearDelayHide();
@@ -589,10 +592,25 @@ export class Popover<O extends PopoverOptions = PopoverOptions, E extends Compon
589592
};
590593
};
591594

595+
static create<O extends PopoverOptions, E extends ComponentEvents, T extends typeof Popover<O, E>>(this: T, options?: O & {event?: Event}, element?: HTMLElement): InstanceType<T> {
596+
const {element: elementSetting, container = 'body', id = `popover_${nextGid()}`} = options || {};
597+
element = element || (elementSetting instanceof HTMLElement ? elementSetting : undefined);
598+
if (!element) {
599+
element = $(`<div id="${id}" class="popover-tmp"></div>`).appendTo($(container))[0]!;
600+
}
601+
return (this as typeof Popover).ensure(element as HTMLElement, {id, destroyOnHide: true, ...options}) as InstanceType<T>;
602+
}
603+
592604
static show<O extends PopoverOptions, E extends ComponentEvents, T extends typeof Popover<O, E>>(this: T, options: O & {event?: Event}): InstanceType<T> {
593605
const {element: elementSetting, event, ...otherOptions} = options;
594606
const element = elementSetting || (event?.currentTarget as HTMLElement);
595-
return (this as typeof Popover).ensure(element instanceof HTMLElement ? element : document.body, {element, show: true, destroyOnHide: true, triggerEvent: event, ...otherOptions}) as InstanceType<T>;
607+
return this.create({
608+
element,
609+
show: true,
610+
destroyOnHide: true,
611+
triggerEvent: event,
612+
...otherOptions,
613+
} as O & {event?: Event}, element instanceof HTMLElement ? element : document.body);
596614
}
597615
}
598616

0 commit comments

Comments
 (0)