AJK
AJK

Reputation: 435

Bootstrap: How to swipe refresh full screen modal on mobile

I'm using Bootstrap 5 full screen modal and trying to figure out how to enable refreshing on mobile by swiping down like you normally would to refresh a page.

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal modal-blur fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-fullscreen">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="modal-title fs-5" id="exampleModalLabel">Modal title</h1>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

Upvotes: 0

Views: 615

Answers (1)

Jonas Weinhardt
Jonas Weinhardt

Reputation: 1046

The reason why the pull to refresh is disabled on mobile when a modal is shown is because bootstrap adds overflow: hidden to the body element of the page when a modal is shown. A really simple way to prevent this behaviour can be accomplished with the following javascript code:


    //Get the modal by its id
    let modal = document.getElementById("exampleModal");
    
    //Subscribe to the "shown" event of the modal
    modal.addEventListener("shown.bs.modal", () => {
          //Remove the overflow: hidden on the body
          document.body.style.overflow = null;
    });

The bootstrap documentation states the following:

Modals are built with HTML, CSS, and JavaScript. They’re positioned over everything else in the document and remove scroll from the <body> so that modal content scrolls instead.

So be careful when you are using this solution because it may come with some side effects depending on your use case.

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal modal-blur fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-fullscreen">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="modal-title fs-5" id="exampleModalLabel">Modal title</h1>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

<script>
//Get the modal by its id
let modal = document.getElementById("exampleModal");

//Subscribe to the "shown" event of the modal
modal.addEventListener("shown.bs.modal", () => {
      //Remove the overflow: hidden on the body
      document.body.style.overflow = null;
});
</script>

Upvotes: 0

Related Questions