Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When modal is open, turn off body scrolling #3453

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/javascript/blacklight-frontend/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ import ModalForm from 'blacklight-frontend/modalForm'
const Modal = (() => {
const modal = {}

// bootstrap class that will stop body scrolling when modal is open
const bootstrapBodyClassOpen = "modal-open";

// a Bootstrap modal div that should be already on the page hidden
modal.modalSelector = '#blacklight-modal';

Expand Down Expand Up @@ -168,6 +171,12 @@ const Modal = (() => {
dom.dispatchEvent(e)

dom.close()

// Turn body scrolling back to what it was
document.body.style["overflow"] = modal.originalBodyOverflow;
document.body.style["padding-right"] = modal.originalBodyPaddingRight;
modal.originalBodyOverflow = undefined;
modal.originalBodyPaddingRight = undefined;
}

modal.show = function(el) {
Expand All @@ -179,6 +188,12 @@ const Modal = (() => {
dom.dispatchEvent(e)

dom.showModal()

// Turn off body scrolling
modal.originalBodyOverflow = document.body.style['overflow'];
modal.originalBodyPaddingRight = document.body.style['padding-right'];
document.body.style["overflow"] = "hidden"
document.body.style["padding-right"] = "0px"
}

modal.target = function() {
Expand Down