Reload masonry layout after filtering

<?php

// Retrigger/reload masonry layout after facet filtering
// Works for https://masonry.desandro.com

// If you get overlapping images etc. use the imagesLoaded check like this:
// https://gist.facetwp.com/gist/reload-masonry-layout-after-filtering-with-imagesloaded/

add_action( 'wp_head', function() { ?>

    <script>
      (function($) {
        $(document).on('facetwp-loaded', function() {
          if (FWP.loaded) {
            $('.facetwp-template').masonry('reloadItems').masonry('layout');
          }
        });
      })(jQuery);
    </script>

<?php }, 100 );