Skip to content

Commit

Permalink
fix: drawer scroll strategy adjust (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
yangxiaolang authored Oct 30, 2023
1 parent a2a2daa commit 607c016
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-comics-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@alauda/ui": patch
---

fix: drawer scroll strategy adjust
18 changes: 17 additions & 1 deletion src/dialog/dialog.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
ViewEncapsulation,
Renderer2,
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { debounceTime, filter, fromEvent } from 'rxjs';

import { Bem, buildBem, getElementOffset } from '../utils';

Expand Down Expand Up @@ -99,7 +101,21 @@ export class DialogComponent {
constructor(
private readonly elementRef: ElementRef<HTMLElement>,
private readonly render: Renderer2,
) {}
) {
// Issues: https://github.com/angular/components/issues/10841
// scrollStrategy 为 Block 时,若创建 Overlay 时,高度不足以出现滚动,则 scrollStrategy 不会生效
fromEvent(window, 'resize')
.pipe(
debounceTime(100),
filter(
() => document.documentElement.scrollHeight > window.innerHeight,
),
takeUntilDestroyed(),
)
.subscribe(() => {
this.overlayRef?.getConfig().scrollStrategy.enable();
});
}

attachComponentPortal<T>(portal: ComponentPortal<T>): ComponentRef<T> {
if (this.portalOutlet.hasAttached()) {
Expand Down
30 changes: 28 additions & 2 deletions src/drawer/component/drawer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ import {
ViewContainerRef,
ViewEncapsulation,
} from '@angular/core';
import { Observable, Subject, takeUntil } from 'rxjs';
import {
Observable,
Subject,
debounceTime,
filter,
fromEvent,
takeUntil,
} from 'rxjs';

import { IconComponent } from '../../icon/icon.component';
import { isTemplateRef } from '../../utils';
Expand Down Expand Up @@ -192,6 +199,23 @@ export class DrawerComponent<
this.attachOverlay();
this.updateBodyOverflow();
this.templateContext = { $implicit: this.contentParams };

if (this.mask) {
// Issues: https://github.com/angular/components/issues/10841
// scrollStrategy 为 Block 时,若创建 Overlay 时,高度不足以出现滚动,则 scrollStrategy 不会生效
fromEvent(window, 'resize')
.pipe(
debounceTime(100),
filter(
() => document.documentElement.scrollHeight > window.innerHeight,
),
takeUntil(this.onDestroy$),
)
.subscribe(() => {
this.overlayRef.getConfig().scrollStrategy.enable();
});
}

this.cdr.detectChanges();
}

Expand Down Expand Up @@ -249,7 +273,9 @@ export class DrawerComponent<
return new OverlayConfig({
panelClass: DRAWER_OVERLAY_CLASS,
positionStrategy: this.overlay.position().global(),
scrollStrategy: this.overlay.scrollStrategies.block(),
scrollStrategy: this.mask
? this.overlay.scrollStrategies.block()
: this.overlay.scrollStrategies.noop(),
});
}

Expand Down

0 comments on commit 607c016

Please sign in to comment.