|
1 | 1 | import { PropType, VNodeDirective } from 'vue'; |
2 | | -import { createPopper } from '@popperjs/core'; |
| 2 | +import { |
| 3 | + createPopper, Modifier, ModifierArguments, Options as popperOptions, |
| 4 | +} from '@popperjs/core'; |
3 | 5 | import { debounce } from 'lodash-es'; |
4 | 6 | import { on, off, once } from '../utils/dom'; |
5 | 7 | import { renderTNodeJSX, renderContent } from '../utils/render-tnode'; |
@@ -58,7 +60,7 @@ export default mixins(classPrefixMixins, getAttachConfigMixins('popup')).extend( |
58 | 60 | /** popperjs instance */ |
59 | 61 | popper: null as ReturnType<typeof createPopper>, |
60 | 62 | /** timeout id */ |
61 | | - timeout: null, |
| 63 | + timeout: null as NodeJS.Timeout | null, |
62 | 64 | hasDocumentEvent: false, |
63 | 65 | /** if a trusted action (opening or closing) is prevented, increase this flag */ |
64 | 66 | visibleState: 0, |
@@ -87,7 +89,7 @@ export default mixins(classPrefixMixins, getAttachConfigMixins('popup')).extend( |
87 | 89 | ...map, |
88 | 90 | [trigger]: this.trigger.includes(trigger), |
89 | 91 | }), |
90 | | - {} as any, |
| 92 | + {} as Record<(typeof triggers)[number], boolean>, |
91 | 93 | ); |
92 | 94 | }, |
93 | 95 | normalizedDelay(): { open: number; close: number } { |
@@ -213,21 +215,44 @@ export default mixins(classPrefixMixins, getAttachConfigMixins('popup')).extend( |
213 | 215 | this.popper.update(); |
214 | 216 | return; |
215 | 217 | } |
| 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 | + |
216 | 254 | 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 || [])], |
231 | 256 | placement: getPopperPlacement(this.placement as TdPopupProps['placement']), |
232 | 257 | onFirstUpdate: () => { |
233 | 258 | this.$nextTick(this.updatePopper); |
|
0 commit comments