Skip to content

Commit

Permalink
Prevent body scroll when the search modal opens
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Wang <[email protected]>
  • Loading branch information
ruibaby committed Aug 8, 2024
1 parent 833b265 commit be99d3c
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions packages/search-widget/src/search-modal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import resetStyles from '@unocss/reset/tailwind.css?inline';
import { LitElement, css, html, unsafeCSS } from 'lit';
import { LitElement, PropertyValues, css, html, unsafeCSS } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { styleMap } from 'lit/directives/style-map.js';
import { OverlayScrollbars } from 'overlayscrollbars';
Expand Down Expand Up @@ -38,6 +38,18 @@ export class SearchModal extends LitElement {
}, 0);
}

override willUpdate(changedProperties: PropertyValues) {
if (!changedProperties.has('open')) {
return;
}

if (this.open) {
document.body.style.overflow = 'hidden';
} else {
document.body.style.removeProperty('overflow');
}
}

override render() {
return html`<div
class="modal__wrapper"
Expand Down Expand Up @@ -77,8 +89,8 @@ export class SearchModal extends LitElement {
}

override disconnectedCallback() {
window.removeEventListener('keydown', this.handleKeydown);
super.disconnectedCallback();
window.removeEventListener('keydown', this.handleKeydown);
}

static override styles = [
Expand Down

0 comments on commit be99d3c

Please sign in to comment.