JSFiddle - React, Tailwind, and code Playground

by joshmoto

HTML

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.css">
<!-- 
  Bootstrap docs: https://getbootstrap.com/docs
-->

<div class="container py-3">
  
  <!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#ajaxModalPopup">
  Launch ajax demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="ajaxModalPopup" tabindex="-1" aria-labelledby="ajaxModalPopupLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="ajaxModalPopupLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <i class="fa fa-circle-o-notch fa-spin" aria-hidden="true"></i>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

</div>

CSS

.modal-body {
  padding: 2rem;
  font-size: 4rem;
  text-align: center;
}

JavaScript

// ajax modal popup on shown
$('#ajaxModalPopup').on('shown.bs.modal', function (e) {

  // modal body div
  let $modalBody = $('.modal-body',this);
  
  // jquery ajax call
  $.ajax({

    // get json file with emojis 
    url: 'https://api.jsonbin.io/b/5fb3211d5be6ec73e94f6285',
    type: 'GET',
    dataType: 'json',

    // on success response function
    success: function(response) {

      // json response with emojis
      let result = response[0].emojis;

      // log result
      console.log(result);

      // render result html
      $modalBody.html(result);


    },

    // on fail response function
    error: function() {

      // console err
      console.log("json error");

    },


  });
  
// ajax modal popup on hidden
}).on('hidden.bs.modal', function (e) {

  // this modal body div
  let $modalBody = $('.modal-body',this);
  
  // re insert spinner
  $modalBody.html('<i class="fa fa-circle-o-notch fa-spin" aria-hidden="true"></i>');
  
});