feat(theme-classic): store selected tab in query string. (#8225)

Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
Closes https://github.com/facebook/docusaurus/issues/7008
This commit is contained in:
mturoci 2022-12-09 17:46:09 +01:00 committed by GitHub
parent eb710af1b8
commit 5c09dbfc3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 329 additions and 111 deletions

View file

@ -19,6 +19,8 @@ import {ReactContextError} from '../utils/reactUtils';
const TAB_CHOICE_PREFIX = 'docusaurus.tab.';
type ContextValue = {
/** A boolean that tells if choices have already been restored from storage */
readonly ready: boolean;
/** A map from `groupId` to the `value` of the saved choice. */
readonly tabGroupChoices: {readonly [groupId: string]: string};
/** Set the new choice value of a group. */
@ -28,6 +30,7 @@ type ContextValue = {
const Context = React.createContext<ContextValue | undefined>(undefined);
function useContextValue(): ContextValue {
const [ready, setReady] = useState(false);
const [tabGroupChoices, setChoices] = useState<{
readonly [groupId: string]: string;
}>({});
@ -51,6 +54,7 @@ function useContextValue(): ContextValue {
} catch (err) {
console.error(err);
}
setReady(true);
}, []);
const setTabGroupChoices = useCallback(
@ -62,8 +66,8 @@ function useContextValue(): ContextValue {
);
return useMemo(
() => ({tabGroupChoices, setTabGroupChoices}),
[tabGroupChoices, setTabGroupChoices],
() => ({ready, tabGroupChoices, setTabGroupChoices}),
[ready, tabGroupChoices, setTabGroupChoices],
);
}