-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(v2): support syncing tab choices (#2366)
* feat(v2): Support syncing tab choices * Move docs changes to website/docs * Do not import entire React in TabGroupChoiceContext * Store only one tab choice according to discussion in PR * Remove leftover logging code during debugging * Put storage value in separate const outside the hook-level * Use an array to keep track of different tab groups * Revert back to using `groupId` * Update markdown-features.mdx * Update markdown-features.mdx Co-authored-by: Yangshun Tay <[email protected]>
- Loading branch information
1 parent
da0a61d
commit 55a50d3
Showing
8 changed files
with
268 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/docusaurus-theme-classic/src/theme/TabGroupChoiceContext.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {createContext} from 'react'; | ||
|
||
const TabGroupChoiceContext = createContext({ | ||
tabGroupChoices: {}, | ||
setTabGroupChoices: () => {}, | ||
}); | ||
|
||
export default TabGroupChoiceContext; |
24 changes: 24 additions & 0 deletions
24
packages/docusaurus-theme-classic/src/theme/TabGroupChoiceProvider/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from 'react'; | ||
|
||
import useTabGroupChoice from '@theme/hooks/useTabGroupChoice'; | ||
import TabGroupChoiceContext from '@theme/TabGroupChoiceContext'; | ||
|
||
function TabGroupChoiceProvider(props) { | ||
const {tabGroupChoices, setTabGroupChoices} = useTabGroupChoice(); | ||
|
||
return ( | ||
<TabGroupChoiceContext.Provider | ||
value={{tabGroupChoices, setTabGroupChoices}}> | ||
{props.children} | ||
</TabGroupChoiceContext.Provider> | ||
); | ||
} | ||
|
||
export default TabGroupChoiceProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/docusaurus-theme-classic/src/theme/hooks/useTabGroupChoice.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {useState, useCallback, useEffect} from 'react'; | ||
|
||
const TAB_CHOICE_PREFIX = 'docusaurus.tab.'; | ||
|
||
const useTabGroupChoice = () => { | ||
const [tabGroupChoices, setChoices] = useState({}); | ||
const setChoiceSyncWithLocalStorage = useCallback((groupId, newChoice) => { | ||
try { | ||
localStorage.setItem(`${TAB_CHOICE_PREFIX}${groupId}`, newChoice); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
try { | ||
const localStorageChoices = {}; | ||
for (let i = 0; i < localStorage.length; i += 1) { | ||
const storageKey = localStorage.key(i); | ||
if (storageKey.startsWith(TAB_CHOICE_PREFIX)) { | ||
const groupId = storageKey.substring(TAB_CHOICE_PREFIX.length); | ||
localStorageChoices[groupId] = localStorage.getItem(storageKey); | ||
} | ||
} | ||
setChoices(localStorageChoices); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}, []); | ||
|
||
return { | ||
tabGroupChoices, | ||
setTabGroupChoices: (groupId, newChoice) => { | ||
setChoices(oldChoices => ({...oldChoices, [groupId]: newChoice})); | ||
setChoiceSyncWithLocalStorage(groupId, newChoice); | ||
}, | ||
}; | ||
}; | ||
|
||
export default useTabGroupChoice; |
16 changes: 16 additions & 0 deletions
16
packages/docusaurus-theme-classic/src/theme/hooks/useTabGroupChoiceContext.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {useContext} from 'react'; | ||
|
||
import TabGroupChoiceContext from '@theme/TabGroupChoiceContext'; | ||
|
||
function useTabGroupChoiceContext() { | ||
return useContext(TabGroupChoiceContext); | ||
} | ||
|
||
export default useTabGroupChoiceContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters