Skip to content

Commit 0a6f6e3

Browse files
authored
docs: remember selected package manager 😎 (#7837)
1 parent bc835b0 commit 0a6f6e3

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

‎packages/docs/src/components/header/header.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
ThemeToggle,
2424
} from '../theme-toggle/theme-toggle';
2525
import { SearchIcon } from '../docsearch/icons/SearchIcon';
26+
import { getPkgManagerPreference } from '../package-manager-tabs';
2627

2728
export const SearchButton = component$<PropsOf<'button'>>(({ ...props }) => {
2829
return (
@@ -46,6 +47,7 @@ export const Header = component$(() => {
4647
const pathname = useLocation().url.pathname;
4748

4849
useVisibleTask$(() => {
50+
globalStore.pkgManager = getPkgManagerPreference();
4951
globalStore.theme = getColorPreference();
5052
return colorSchemeChangeListener((isDark) => {
5153
globalStore.theme = isDark ? 'dark' : 'light';

‎packages/docs/src/components/package-manager-tabs/index.tsx‎

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,51 @@
1-
import { Slot, component$, useContext, useSignal, $, type PropsOf } from '@builder.io/qwik';
1+
import {
2+
Slot,
3+
component$,
4+
useContext,
5+
useSignal,
6+
$,
7+
type PropsOf,
8+
isBrowser,
9+
} from '@builder.io/qwik';
210
import { Tabs } from '@qwik-ui/headless';
311
import { GlobalStore } from '~/context';
412

513
const pkgManagers = ['pnpm', 'npm', 'yarn', 'bun'] as const;
6-
type PkgManagers = (typeof pkgManagers)[number];
14+
export type PkgManagers = (typeof pkgManagers)[number];
15+
16+
const pkgManagerStorageKey = 'pkg-manager-preference';
17+
18+
const setPreference = (value: PkgManagers) => {
19+
if (isBrowser) {
20+
localStorage.setItem(pkgManagerStorageKey, value);
21+
}
22+
};
23+
24+
export const getPkgManagerPreference = () => {
25+
try {
26+
return (localStorage.getItem(pkgManagerStorageKey) || 'pnpm') as PkgManagers;
27+
} catch (err) {
28+
return 'pnpm';
29+
}
30+
};
731

832
export default component$(() => {
933
const globalStore = useContext(GlobalStore);
10-
const selectedPkgManagersSig = useSignal<PkgManagers>('pnpm');
11-
1234
const activeClass = `${globalStore.theme === 'light' ? 'bg-gray-300 text-black' : 'bg-slate-800 text-white'}`;
1335

1436
return (
1537
<Tabs.Root
16-
selectedTabId={selectedPkgManagersSig.value}
38+
selectedTabId={globalStore.pkgManager}
1739
onSelectedTabIdChange$={(pkgManager) => {
18-
selectedPkgManagersSig.value = pkgManager as PkgManagers;
40+
const value = pkgManager as PkgManagers;
41+
globalStore.pkgManager = value;
42+
setPreference(value);
1943
}}
2044
>
2145
<Tabs.List>
2246
<Tabs.Tab
2347
tabId="pnpm"
24-
class={`px-4 pt-2 rounded-md ${selectedPkgManagersSig.value === 'pnpm' ? `font-bold ${activeClass}` : ''}`}
48+
class={`px-4 pt-2 rounded-md ${globalStore.pkgManager === 'pnpm' ? `font-bold ${activeClass}` : ''}`}
2549
>
2650
<span class="inline-flex items-center gap-x-2">
2751
<PnpmIcon />
@@ -30,7 +54,7 @@ export default component$(() => {
3054
</Tabs.Tab>
3155
<Tabs.Tab
3256
tabId="npm"
33-
class={`px-4 pt-2 rounded-md ${selectedPkgManagersSig.value === 'npm' ? `font-bold ${activeClass}` : ''}`}
57+
class={`px-4 pt-2 rounded-md ${globalStore.pkgManager === 'npm' ? `font-bold ${activeClass}` : ''}`}
3458
>
3559
<span class="inline-flex items-center gap-x-2">
3660
<NpmIcon />
@@ -39,7 +63,7 @@ export default component$(() => {
3963
</Tabs.Tab>
4064
<Tabs.Tab
4165
tabId="yarn"
42-
class={`px-4 pt-2 rounded-md ${selectedPkgManagersSig.value === 'yarn' ? `font-bold ${activeClass}` : ''}`}
66+
class={`px-4 pt-2 rounded-md ${globalStore.pkgManager === 'yarn' ? `font-bold ${activeClass}` : ''}`}
4367
>
4468
<span class="inline-flex items-center gap-x-2">
4569
<YarnIcon />
@@ -48,7 +72,7 @@ export default component$(() => {
4872
</Tabs.Tab>
4973
<Tabs.Tab
5074
tabId="bun"
51-
class={`px-4 pt-2 rounded-md ${selectedPkgManagersSig.value === 'bun' ? `font-bold ${activeClass}` : ''}`}
75+
class={`px-4 pt-2 rounded-md ${globalStore.pkgManager === 'bun' ? `font-bold ${activeClass}` : ''}`}
5276
>
5377
<span class="inline-flex items-center gap-x-2">
5478
<BunIcon />
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { createContextId } from '@builder.io/qwik';
22
import type { ThemePreference } from './components/theme-toggle/theme-toggle';
3+
import type { PkgManagers } from './components/package-manager-tabs';
34

45
export interface SiteStore {
56
headerMenuOpen: boolean;
67
sideMenuOpen: boolean;
78
theme: ThemePreference | 'auto';
9+
pkgManager: PkgManagers;
810
}
911

1012
export const GlobalStore = createContextId<SiteStore>('site-store');

0 commit comments

Comments
 (0)