Skip to content

Commit

Permalink
Merge pull request #3453 from projectblacklight/modal_turn_off_body_s…
Browse files Browse the repository at this point in the history
…crolling

When modal is open, turn off body scrolling
  • Loading branch information
jrochkind authored Dec 5, 2024
2 parents 6b1d698 + edc608d commit a753cd9
Showing 1 changed file with 15 additions and 0 deletions.
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 @@ -170,6 +173,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 @@ -181,6 +190,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

0 comments on commit a753cd9

Please sign in to comment.