diff --git a/docs/.vitepress/theme/components/icons/IconItem.vue b/docs/.vitepress/theme/components/icons/IconItem.vue index 775f11367..512488364 100644 --- a/docs/.vitepress/theme/components/icons/IconItem.vue +++ b/docs/.vitepress/theme/components/icons/IconItem.vue @@ -8,6 +8,7 @@ import getSVGIcon from '../../utils/getSVGIcon'; import useConfetti from '../../composables/useConfetti'; import Tooltip from '../base/Tooltip.vue'; import { diamond } from '../../../data/iconNodes'; +import { setLibraryParams } from '../../composables/useExternalLibs'; const downloadText = 'Download!'; const copiedText = 'Copied!'; @@ -76,13 +77,17 @@ async function navigateToIcon(event) { if (props.overlayMode && showOverlay.value) { event.preventDefault(); - window.history.pushState( - {}, - '', - props.externalLibrary - ? `/icons/${props.externalLibrary}/${props.name}` - : `/icons/${props.name}`, + // Keep ?library= so the selection survives opening and closing the drawer. + const url = setLibraryParams( + new URL( + props.externalLibrary + ? `/icons/${props.externalLibrary}/${props.name}` + : `/icons/${props.name}`, + window.location.origin, + ), ); + + window.history.pushState({}, '', url); emit( 'setActiveIcon', props.externalLibrary ? `${props.externalLibrary}:${props.name}` : props.name, diff --git a/docs/.vitepress/theme/components/icons/IconsOverview.vue b/docs/.vitepress/theme/components/icons/IconsOverview.vue index fec0bc487..a684de88b 100644 --- a/docs/.vitepress/theme/components/icons/IconsOverview.vue +++ b/docs/.vitepress/theme/components/icons/IconsOverview.vue @@ -18,6 +18,7 @@ import useSearchPlaceholder from '../../utils/useSearchPlaceholder.ts'; import Icon from '@lucide/vue/src/Icon'; import { listSortDescending } from '~/.vitepress/data/iconNodes'; import { track, trackDebounced } from '../../utils/openpanel'; +import { setLibraryParams } from '../../composables/useExternalLibs'; const ICON_SIZE = 56; const ICON_GRID_GAP = 8; @@ -206,6 +207,8 @@ function handleCloseDrawer() { url.searchParams.set('search', searchQueryDebounced.value); } + setLibraryParams(url); + window.history.pushState({}, '', url); } diff --git a/docs/.vitepress/theme/composables/useExternalLibs.ts b/docs/.vitepress/theme/composables/useExternalLibs.ts index 1469184fe..c0aeb471d 100644 --- a/docs/.vitepress/theme/composables/useExternalLibs.ts +++ b/docs/.vitepress/theme/composables/useExternalLibs.ts @@ -1,4 +1,5 @@ -import { ref, inject, Ref, watch } from 'vue'; +import { ref, inject, Ref, watch, onMounted } from 'vue'; +import { useLocalStorage } from '@vueuse/core'; import { ExternalLibs, IconEntity } from '../types'; export const EXTERNAL_LIBS_CONTEXT = Symbol('externalLibs'); @@ -6,11 +7,11 @@ export const EXTERNAL_LIBS_CONTEXT = Symbol('externalLibs'); type ExternalIconNodes = Partial>; interface ExternalLibContext { - selectedLibs: Ref<[ExternalLibs]>; + selectedLibs: Ref; externalIconNodes: Ref; } -export const externalLibContext = { +export const externalLibContext: ExternalLibContext = { selectedLibs: ref([]), externalIconNodes: ref({}), }; @@ -19,41 +20,104 @@ const externalLibIconNodesAPI = { lab: `${import.meta.env.DEV ? 'http://localhost:3000' : ''}/api/lab/icon-details`, }; -export function useExternalLibs(): ExternalLibContext { - const context = inject(EXTERNAL_LIBS_CONTEXT); +// URL query parameter for preselecting external libraries, e.g. /icons/?library=lab +// Repeat it for several libraries: ?library=lab&library=other +const URL_PARAM = 'library'; - watch(context?.selectedLibs, async (selectedLibs) => { - const savedIconNodes = { ...context?.externalIconNodes.value }; - const newExternalIconNodes: ExternalIconNodes = {}; +const storedLibs = useLocalStorage('external-libs', [], { initOnMounted: false }); - try { - for (const lib of selectedLibs) { - if (savedIconNodes[lib]) { - newExternalIconNodes[lib] = savedIconNodes[lib]; - } else { - const response = await fetch(externalLibIconNodesAPI[lib]); - const iconDetails = await response.json(); +const isExternalLib = (value: string): value is ExternalLibs => + Object.keys(externalLibIconNodesAPI).includes(value); - if (iconDetails) { - newExternalIconNodes[lib] = Object.values(iconDetails).map((iconEntity: IconEntity) => { - return { - ...iconEntity, - externalLibrary: lib, - }; - }); - } +/** + * Writes the current library selection into `url` as `?library=` params, replacing any + * existing ones. Use it when building URLs for history.pushState so the param survives. + */ +export const setLibraryParams = (url: URL, libs = externalLibContext.selectedLibs.value) => { + url.searchParams.delete(URL_PARAM); + + for (const lib of libs) { + url.searchParams.append(URL_PARAM, lib); + } + + return url; +}; + +// Keep the URL shareable and remember the selection for the next visit. +const persistSelection = (libs: ExternalLibs[]) => { + storedLibs.value = libs; + + const url = setLibraryParams(new URL(window.location.href), libs); + window.history.replaceState(window.history.state, '', url); +}; + +// `?library=` wins (shared links, Back/Forward); otherwise use the last selection. +const syncSelection = () => { + const { selectedLibs } = externalLibContext; + const libsFromUrl = new URLSearchParams(window.location.search) + .getAll(URL_PARAM) + .filter(isExternalLib); + const libs = libsFromUrl.length + ? [...new Set(libsFromUrl)] + : storedLibs.value.filter(isExternalLib); + + if (String(libs) !== String(selectedLibs.value)) { + selectedLibs.value = libs; + } + + // Also when nothing changed, to restore the param on URLs that dropped it. + persistSelection(libs); +}; + +const fetchExternalIconNodes = async (selectedLibs: ExternalLibs[]) => { + const { externalIconNodes } = externalLibContext; + const savedIconNodes = { ...externalIconNodes.value }; + const newExternalIconNodes: ExternalIconNodes = {}; + + try { + for (const lib of selectedLibs) { + if (savedIconNodes[lib]) { + newExternalIconNodes[lib] = savedIconNodes[lib]; + } else { + const response = await fetch(externalLibIconNodesAPI[lib]); + const iconDetails = await response.json(); + + if (iconDetails) { + newExternalIconNodes[lib] = Object.values(iconDetails).map((iconEntity: IconEntity) => { + return { + ...iconEntity, + externalLibrary: lib, + }; + }); } } - - context.externalIconNodes.value = newExternalIconNodes; - } catch (error) { - console.error(error); } - }); + + externalIconNodes.value = newExternalIconNodes; + } catch (error) { + console.error(error); + } +}; + +// Registered once at module level so they outlive any single component. The selection only +// changes in the browser, so the watchers never run during SSR. +watch(externalLibContext.selectedLibs, persistSelection); +watch(externalLibContext.selectedLibs, fetchExternalIconNodes); + +if (typeof window !== 'undefined') { + window.addEventListener('popstate', syncSelection); +} + +export function useExternalLibs(): ExternalLibContext { + const context = inject(EXTERNAL_LIBS_CONTEXT); if (!context) { throw new Error('useExternalLibs must be used with externalLibs context'); } + // Apply the URL/stored selection after hydration to avoid an SSR mismatch on the checkbox. + // Runs on every mount so client-side navigation to a URL carrying the param reapplies it. + onMounted(syncSelection); + return context; }