aboutsummaryrefslogtreecommitdiff
path: root/website/src/components/Search.astro
blob: cceeed34bbb500406500d3d745abe916dfaa057a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
---
import StarlightSearch from '@astrojs/starlight/components/Search.astro';
import { docsVersion } from '../docs-path';

const version = docsVersion(Astro.url.pathname);
---

<version-search data-version={version}>
  <StarlightSearch />
</version-search>

<script>
  class VersionSearch extends HTMLElement {
    observer?: MutationObserver;

    connectedCallback() {
      const version = this.dataset.version;
      if (!version) return;

      const selectVersion = () => {
        const checkbox = this.querySelector<HTMLInputElement>(
          `.pagefind-ui__filter-checkbox[name="version"][value="${version}"]`,
        );
        if (!checkbox) return false;
        if (!checkbox.checked) checkbox.click();
        this.observer?.disconnect();
        return true;
      };

      if (selectVersion()) return;
      // Starlight does not expose its PagefindUI instance, so wait for Pagefind's filter control.
      this.observer = new MutationObserver(selectVersion);
      this.observer.observe(this, { childList: true, subtree: true });
    }

    disconnectedCallback() {
      this.observer?.disconnect();
    }
  }

  customElements.define('version-search', VersionSearch);
</script>

<style>
  version-search {
    display: contents;
  }

  version-search :global(.pagefind-ui__filter-panel) {
    display: none;
  }
</style>