Skip to content

Commit

Permalink
fixed #282 dropdown, tooltip 监听所有支持 overflow 的父元素滚动
Browse files Browse the repository at this point in the history
  • Loading branch information
zdhxiong committed Dec 1, 2023
1 parent 2bf4e75 commit fbade5b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/mdui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@mdui/shared": "workspace:^",
"@mdui/jq": "workspace:^",
"@open-wc/dedupe-mixin": "^1.4.0",
"@floating-ui/utils": "^0.1.6",
"classcat": "^5.0.4",
"is-promise": "^4.0.0",
"lit": "^3.0.0",
Expand Down
13 changes: 11 additions & 2 deletions packages/mdui/src/components/dropdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
queryAssignedElements,
} from 'lit/decorators.js';
import { createRef, ref } from 'lit/directives/ref.js';
import { getOverflowAncestors } from '@floating-ui/utils/dom';
import { $ } from '@mdui/jq/$.js';
import '@mdui/jq/methods/height.js';
import '@mdui/jq/methods/is.js';
Expand Down Expand Up @@ -177,6 +178,7 @@ export class Dropdown extends MduiElement<DropdownEventMap> {
private closeTimeout!: number;

private observeResize?: ObserveResize;
private overflowAncestors?: ReturnType<typeof getOverflowAncestors>; // todo 后续改用 floating-ui 实现
private readonly panelRef: Ref<HTMLElement> = createRef();
private readonly definedController = new DefinedController(this, {
relatedElements: [''],
Expand Down Expand Up @@ -328,7 +330,11 @@ export class Dropdown extends MduiElement<DropdownEventMap> {
this.definedController.whenDefined().then(() => {
document.addEventListener('pointerdown', this.onDocumentClick);
document.addEventListener('keydown', this.onDocumentKeydown);
window.addEventListener('scroll', this.onWindowScroll);

this.overflowAncestors = getOverflowAncestors(this.triggerElement);
this.overflowAncestors.forEach((ancestor) => {
ancestor.addEventListener('scroll', this.onWindowScroll);
});
});
}

Expand All @@ -337,7 +343,10 @@ export class Dropdown extends MduiElement<DropdownEventMap> {

document.removeEventListener('pointerdown', this.onDocumentClick);
document.removeEventListener('keydown', this.onDocumentKeydown);
window.removeEventListener('scroll', this.onWindowScroll);

this.overflowAncestors?.forEach((ancestor) => {
ancestor.removeEventListener('scroll', this.onWindowScroll);
});

this.observeResize?.unobserve();
}
Expand Down
13 changes: 11 additions & 2 deletions packages/mdui/src/components/tooltip/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { html } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { createRef, ref } from 'lit/directives/ref.js';
import { when } from 'lit/directives/when.js';
import { getOverflowAncestors } from '@floating-ui/utils/dom';
import { $ } from '@mdui/jq/$.js';
import '@mdui/jq/methods/css.js';
import '@mdui/jq/methods/filter.js';
Expand Down Expand Up @@ -168,6 +169,7 @@ export class Tooltip extends MduiElement<TooltipEventMap> {
public open = false;

private observeResize?: ObserveResize;
private overflowAncestors?: ReturnType<typeof getOverflowAncestors>; // todo 后续改用 floating-ui 实现
private hoverTimeout!: number;
private readonly popupRef: Ref<HTMLElement> = createRef();
private readonly hasSlotController = new HasSlotController(
Expand Down Expand Up @@ -282,14 +284,21 @@ export class Tooltip extends MduiElement<TooltipEventMap> {
super.connectedCallback();

document.addEventListener('pointerdown', this.onDocumentClick);
window.addEventListener('scroll', this.onWindowScroll);

this.overflowAncestors = getOverflowAncestors(this.target);
this.overflowAncestors.forEach((ancestor) => {
ancestor.addEventListener('scroll', this.onWindowScroll);
});
}

public override disconnectedCallback(): void {
super.disconnectedCallback();

document.removeEventListener('pointerdown', this.onDocumentClick);
window.removeEventListener('scroll', this.onWindowScroll);

this.overflowAncestors?.forEach((ancestor) => {
ancestor.removeEventListener('scroll', this.onWindowScroll);
});

this.observeResize?.unobserve();
}
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fbade5b

Please sign in to comment.