Skip to content

Commit 1148120

Browse files
committed
* popover: add lastTriggerAttr to optimize toggle options handling and prevent redundant updates.
1 parent 790c123 commit 1148120

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

lib/popover/src/vanilla/popover.ts

Lines changed: 14 additions & 3 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, nextGid} from '@zui/core';
2+
import {Component, $, ComponentEvents, JSX, evalValue, toCssSize, nextGid, ComponentOptions} 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';
@@ -65,6 +65,8 @@ export class Popover<O extends PopoverOptions = PopoverOptions, E extends Compon
6565

6666
protected declare _zIndex: number;
6767

68+
protected _lastTriggerAttr = '';
69+
6870
get shown() {
6971
return this._shown;
7072
}
@@ -97,10 +99,19 @@ export class Popover<O extends PopoverOptions = PopoverOptions, E extends Compon
9799
const {namespace} = this;
98100
if (trigger) {
99101
const setOptionsFromTrigger = () => {
102+
const toggleOptions = $triggerElement.attr(`zui-toggle-${this.constructor.ZUI}`) ?? '';
103+
if (toggleOptions && this._lastTriggerAttr === toggleOptions) {
104+
return;
105+
}
106+
this._lastTriggerAttr = toggleOptions;
100107
let options = $triggerElement.dataset();
101-
const toggleOptions = $triggerElement.attr(`zui-toggle-${this.constructor.ZUI}`);
102108
if (toggleOptions) {
103-
options = $.extend(options, evalValue(toggleOptions));
109+
const newOptions = evalValue(toggleOptions) as ComponentOptions<O>;
110+
let extraOptions = newOptions.$options;
111+
if (typeof extraOptions === 'function') {
112+
extraOptions = extraOptions.call(this, this.element, newOptions);
113+
}
114+
options = $.extend(options, newOptions, extraOptions || {});
104115
}
105116
this.setOptions(options as Partial<O>);
106117
};

0 commit comments

Comments
 (0)