Skip to content

Commit 94ec6e0

Browse files
committed
Add scrollbar width support on modal
1 parent 38db804 commit 94ec6e0

1 file changed

Lines changed: 29 additions & 3 deletions

File tree

v2-angular-color-schemes-and-modal/src/app/shared/modal/modal.component.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ import {
33
Component,
44
EventEmitter,
55
HostListener,
6+
OnDestroy,
7+
OnInit,
68
Output,
79
booleanAttribute,
810
inject,
911
input,
1012
signal,
1113
} from "@angular/core";
1214
import { takeUntilDestroyed, toObservable } from "@angular/core/rxjs-interop";
13-
import { Subject, concatMap, delay, iif, skip, switchMap, tap } from "rxjs";
15+
16+
import { Subject, delay, filter, skip, tap } from "rxjs";
1417

1518
const modalAnimationDuration = 400;
1619

@@ -38,7 +41,7 @@ const modalAnimationDuration = 400;
3841
</dialog>
3942
`,
4043
})
41-
export class ModalComponent {
44+
export class ModalComponent implements OnInit, OnDestroy {
4245
readonly #htmlEl: HTMLElement = inject(DOCUMENT).documentElement;
4346
readonly isModalOpen = signal<boolean>(false);
4447

@@ -81,11 +84,34 @@ export class ModalComponent {
8184
.pipe(
8285
takeUntilDestroyed(),
8386
skip(1),
84-
concatMap((shouldOpen) => iif(() => shouldOpen, this.#closeModal$, this.#openModal$))
87+
filter((shouldOpen) => shouldOpen !== this.isModalOpen()),
88+
tap((shouldOpen) => (shouldOpen ? this.#closeModal$.next() : this.#openModal$.next()))
8589
)
8690
.subscribe();
8791
}
8892

93+
ngOnInit(): void {
94+
const scrollBarWidth = this.getScrollBarWidth();
95+
this.#htmlEl.style.setProperty("--pico-scrollbar-width", `${scrollBarWidth}px`);
96+
}
97+
98+
ngOnDestroy(): void {
99+
this.#htmlEl.style.removeProperty("--pico-scrollbar-width");
100+
}
101+
102+
getScrollBarWidth(): number {
103+
const isSSR = typeof window === "undefined";
104+
if (isSSR) return 0;
105+
106+
const hasScrollbar = document.body.scrollHeight > screen.height;
107+
if (hasScrollbar) {
108+
const scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;
109+
return scrollbarWidth;
110+
}
111+
112+
return 0;
113+
}
114+
89115
overlayClicked(event: MouseEvent): void {
90116
if (event.target === event.currentTarget) {
91117
this.close.emit();

0 commit comments

Comments
 (0)