Skip to content

Commit d4a66ba

Browse files
committed
fix(popup): fix missing styles for popup overlay
1 parent 006168d commit d4a66ba

File tree

1 file changed

+42
-17
lines changed

1 file changed

+42
-17
lines changed

src/popup/popup.tsx

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { PropType, VNodeDirective } from 'vue';
2-
import { createPopper } from '@popperjs/core';
2+
import {
3+
createPopper, Modifier, ModifierArguments, Options as popperOptions,
4+
} from '@popperjs/core';
35
import { debounce } from 'lodash-es';
46
import { on, off, once } from '../utils/dom';
57
import { renderTNodeJSX, renderContent } from '../utils/render-tnode';
@@ -58,7 +60,7 @@ export default mixins(classPrefixMixins, getAttachConfigMixins('popup')).extend(
5860
/** popperjs instance */
5961
popper: null as ReturnType<typeof createPopper>,
6062
/** timeout id */
61-
timeout: null,
63+
timeout: null as NodeJS.Timeout | null,
6264
hasDocumentEvent: false,
6365
/** if a trusted action (opening or closing) is prevented, increase this flag */
6466
visibleState: 0,
@@ -87,7 +89,7 @@ export default mixins(classPrefixMixins, getAttachConfigMixins('popup')).extend(
8789
...map,
8890
[trigger]: this.trigger.includes(trigger),
8991
}),
90-
{} as any,
92+
{} as Record<(typeof triggers)[number], boolean>,
9193
);
9294
},
9395
normalizedDelay(): { open: number; close: number } {
@@ -213,21 +215,44 @@ export default mixins(classPrefixMixins, getAttachConfigMixins('popup')).extend(
213215
this.popper.update();
214216
return;
215217
}
218+
219+
const modifiers: popperOptions['modifiers'] = getIEVersion() > 9
220+
? []
221+
: [
222+
{
223+
name: 'computeStyles',
224+
options: {
225+
// 默认为 true,即使用 transform 定位,开启 gpu 加速
226+
// ie9 不支持 transform,需要添加 -ms- 前缀,@popperjs/core 没有添加这个样式,
227+
// 在 ie9 下则去掉 gpu 优化加速,使用 top, right, bottom, left 定位
228+
gpuAcceleration: false,
229+
},
230+
},
231+
];
232+
233+
// 当使用 popperContentElement 时,添加custom modifier来维护样式
234+
if (this.popperContentElement) {
235+
modifiers.push({
236+
name: 'applyStyles',
237+
phase: 'write',
238+
fn: ({ state }: { state: ModifierArguments<popperOptions>['state'] }) => {
239+
// 确保样式被应用到指定的元素
240+
const styles = state.styles.popper;
241+
if (styles && popperEl) {
242+
Object.assign(popperEl.style, styles);
243+
}
244+
const attributes = state.attributes.popper;
245+
if (attributes && popperEl) {
246+
Object.keys(attributes).forEach((key) => {
247+
popperEl.setAttribute(key, String(attributes[key]));
248+
});
249+
}
250+
},
251+
} as unknown as any);
252+
}
253+
216254
this.popper = createPopper(triggerEl, popperEl, {
217-
modifiers:
218-
getIEVersion() > 9
219-
? []
220-
: [
221-
{
222-
name: 'computeStyles',
223-
options: {
224-
// 默认为 true,即使用 transform 定位,开启 gpu 加速
225-
// ie9 不支持 transform,需要添加 -ms- 前缀,@popperjs/core 没有添加这个样式,
226-
// 在 ie9 下则去掉 gpu 优化加速,使用 top, right, bottom, left 定位
227-
gpuAcceleration: false,
228-
},
229-
},
230-
],
255+
modifiers: [...modifiers, ...((this.popperOptions as popperOptions)?.modifiers || [])],
231256
placement: getPopperPlacement(this.placement as TdPopupProps['placement']),
232257
onFirstUpdate: () => {
233258
this.$nextTick(this.updatePopper);

0 commit comments

Comments
 (0)