Skip to content

Commit de83067

Browse files
authored
[docs-infra] Sort package manager automatically (#46897)
1 parent a100519 commit de83067

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

packages/mui-docs/src/HighlightedCodeWithTabs/HighlightedCodeWithTabs.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import { Tab as TabBase } from '@mui/base/Tab';
77
import useLocalStorageState from '@mui/utils/useLocalStorageState';
88
import { HighlightedCode } from '../HighlightedCode';
99

10+
const PACKAGE_MANAGER_ORDER = new Map(
11+
['npm', 'pnpm', 'yarn'].map((manager, index) => [manager, index]),
12+
);
13+
1014
export const CodeTabList = styled(TabsListBase)<{
1115
ownerState: { mounted: boolean; contained?: boolean };
1216
}>(({ theme }) => ({
@@ -294,7 +298,16 @@ export function HighlightedCodeWithTabs(
294298
} & Record<string, any>,
295299
) {
296300
const { tabs, storageKey } = props;
297-
const availableTabs = React.useMemo(() => tabs.map(({ tab }) => tab), [tabs]);
301+
const availableTabs = React.useMemo(() => {
302+
const result = tabs.map(({ tab }) => tab);
303+
if (storageKey === 'package-manager') {
304+
result.sort(
305+
(a, b) =>
306+
(PACKAGE_MANAGER_ORDER.get(a) ?? Infinity) - (PACKAGE_MANAGER_ORDER.get(b) ?? Infinity),
307+
);
308+
}
309+
return result;
310+
}, [storageKey, tabs]);
298311
const [activeTab, setActiveTab] = useLocalStorageState(storageKey ?? null, availableTabs[0]);
299312
// During hydration, activeTab is null, default to first value.
300313
const defaultizedActiveTab = activeTab ?? availableTabs[0];

0 commit comments

Comments
 (0)