diff --git a/packages/starlight/src/components/Search.astro b/packages/starlight/src/components/Search.astro --- a/packages/starlight/src/components/Search.astro +++ b/packages/starlight/src/components/Search.astro @@ -40,7 +40,10 @@

{Astro.locals.t('search.devWarning')}

) : ( -
+
+ )}
@@ -75,6 +78,23 @@ const closeBtn = this.querySelector('button[data-close-modal]')!; const dialog = this.querySelector('dialog')!; const dialogFrame = this.querySelector('.dialog-frame')!; + const loadingIndicator = this.querySelector('.search-loading'); + + /** Whether the search UI has finished loading and rendering. */ + let searchReady = import.meta.env.DEV; + let searchInitPromise: Promise | null = null; + + const setSearchLoading = (loading: boolean) => { + loadingIndicator?.toggleAttribute('hidden', !loading); + }; + + const focusSearchInput = () => this.querySelector('input')?.focus(); + + const onSearchReady = () => { + searchReady = true; + setSearchLoading(false); + if (dialog.open) focusSearchInput(); + }; /** Close the modal if a user clicks on a link or outside of the modal. */ const onClick = (event: MouseEvent) => { @@ -89,9 +109,14 @@ }; const openModal = (event?: MouseEvent) => { + if (!searchReady) { + if (!this.querySelector('input')) setSearchLoading(true); + // Start loading the search UI immediately instead of waiting for an idle period. + void initSearch(); + } dialog.showModal(); document.body.toggleAttribute('data-search-modal-open', true); - this.querySelector('input')?.focus(); + focusSearchInput(); event?.stopPropagation(); window.addEventListener('click', onClick); }; @@ -124,29 +149,38 @@ const stripTrailingSlash = (path: string) => path.replace(/(.)\/(#.*)?$/, '$1$2'); const formatURL = shouldStrip ? stripTrailingSlash : (path: string) => path; + /** Load and render the Pagefind search UI. Safe to call multiple times. */ + const initSearch = () => { + if (import.meta.env.DEV) return Promise.resolve(); + if (!searchInitPromise) { + searchInitPromise = (async () => { + // @ts-expect-error — Missing types for @pagefind/default-ui package. + const { PagefindUI } = await import('@pagefind/default-ui'); + new PagefindUI({ + ...pagefindUserConfig, + element: '#starlight__search', + baseUrl: import.meta.env.BASE_URL, + bundlePath: import.meta.env.BASE_URL.replace(/\/$/, '') + '/pagefind/', + showImages: false, + translations, + showSubResults: true, + processResult: (result: { url: string; sub_results: Array<{ url: string }> }) => { + result.url = formatURL(result.url); + result.sub_results = result.sub_results.map((sub_result) => { + sub_result.url = formatURL(sub_result.url); + return sub_result; + }); + }, + }); + onSearchReady(); + })(); + } + return searchInitPromise; + }; + window.addEventListener('DOMContentLoaded', () => { - if (import.meta.env.DEV) return; const onIdle = window.requestIdleCallback || ((cb) => setTimeout(cb, 1)); - onIdle(async () => { - // @ts-expect-error — Missing types for @pagefind/default-ui package. - const { PagefindUI } = await import('@pagefind/default-ui'); - new PagefindUI({ - ...pagefindUserConfig, - element: '#starlight__search', - baseUrl: import.meta.env.BASE_URL, - bundlePath: import.meta.env.BASE_URL.replace(/\/$/, '') + '/pagefind/', - showImages: false, - translations, - showSubResults: true, - processResult: (result: { url: string; sub_results: Array<{ url: string }> }) => { - result.url = formatURL(result.url); - result.sub_results = result.sub_results.map((sub_result) => { - sub_result.url = formatURL(sub_result.url); - return sub_result; - }); - }, - }); - }); + onIdle(() => void initSearch()); }); } } @@ -231,6 +265,39 @@ flex-grow: 1; gap: 1rem; padding: 1rem; + } + + .search-loading { + display: flex; + align-items: center; + justify-content: center; + padding-block: 2rem; + } + + .search-loading[hidden] { + display: none; + } + + .search-loading::after { + content: ''; + width: 1.5rem; + height: 1.5rem; + border: 2px solid var(--sl-color-gray-5); + border-top-color: var(--sl-color-text-accent); + border-radius: 50%; + animation: sl-search-spin 0.8s linear infinite; + } + + @keyframes sl-search-spin { + to { + transform: rotate(360deg); + } + } + + @media (prefers-reduced-motion: reduce) { + .search-loading::after { + animation: none; + } } button[data-close-modal] {