Automatically open single fSelect facet dropdowns when a selection is active

<?php

// Automatically keep the fSelect facet drowndown open when a selection was made.
// Keeps the fSelect facets dropdown open on page reload when a selection is in the URL.
// Intended for pages with one fSelect facet. If you have multiple fSelect facets on the page, use this snippet:
// https://gist.facetwp.com/gist/automatically-open-multiple-fselect-facet-dropdowns-when-a-selection-is-active/

add_action( 'wp_footer', function() {
  ?>
    <script>
      (function($) {

        document.addEventListener('facetwp-loaded', function() {
          const facet = 'my_fselect_facet'; // Change to your fSelect facet name
          let selected = FWP.facets[facet];
          if (selected.length) {
            $(`.facetwp-facet-${facet} .facetwp-dropdown`).nodes[0].fselect.open();
          }
        });

      })(fUtil);
    </script>
  <?php
}, 100 );