-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
778 additions
and
230 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
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
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 |
---|---|---|
|
@@ -83,6 +83,7 @@ | |
"async", | ||
"data fetching", | ||
"data cache", | ||
"api client", | ||
"api", | ||
"normalized cache", | ||
"swr", | ||
|
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 |
---|---|---|
|
@@ -82,6 +82,7 @@ | |
"redux", | ||
"data fetching", | ||
"data cache", | ||
"api client", | ||
"api", | ||
"normalized cache", | ||
"swr", | ||
|
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
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,41 @@ | ||
import React, { useContext, memo } from 'react'; | ||
|
||
import CodeProvider from './CodeProvider'; | ||
import HooksPlayground from '../HooksPlayground'; | ||
import CodeTabContext from './CodeTabContext'; | ||
|
||
// eslint-disable-next-line react/display-name | ||
const DemoPlayground = memo(() => { | ||
const { selectedValue, values } = useContext(CodeTabContext); | ||
|
||
return ( | ||
<> | ||
{values.map(({ value, code }) => ( | ||
<HooksPlayground | ||
groupId="homepage-demo" | ||
row | ||
key={value} | ||
hidden={value !== selectedValue} | ||
> | ||
{code} | ||
</HooksPlayground> | ||
))} | ||
</> | ||
); | ||
}); | ||
|
||
interface Props { | ||
codes: { label: string; value: string; code: string }[]; | ||
} | ||
|
||
export default function CodeEditor({ codes }: Props) { | ||
return ( | ||
<CodeProvider | ||
defaultValue={codes[0].value} | ||
groupId="protocol" | ||
values={codes} | ||
> | ||
<DemoPlayground /> | ||
</CodeProvider> | ||
); | ||
} |
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,50 @@ | ||
import React, { memo, useState } from 'react'; | ||
import useUserPreferencesContext from '@theme/hooks/useUserPreferencesContext'; | ||
|
||
import CodeTabContext from './CodeTabContext'; | ||
|
||
interface Props<V extends { label: string; value: string }[]> { | ||
values: V; | ||
groupId?: string | null; | ||
defaultValue: V[number]['value']; | ||
children: React.ReactNode; | ||
} | ||
|
||
export function CodeProvider<V extends { label: string; value: string }[]>({ | ||
defaultValue, | ||
groupId = null, | ||
values, | ||
children, | ||
}: Props<V>) { | ||
const { tabGroupChoices, setTabGroupChoices } = useUserPreferencesContext(); | ||
const [selectedValue, setLocalSelectedValue] = useState(defaultValue); | ||
|
||
if (groupId != null) { | ||
const choice = tabGroupChoices[groupId]; | ||
if ( | ||
choice != null && | ||
choice !== selectedValue && | ||
values.find(({ value }) => value === choice) | ||
) { | ||
setLocalSelectedValue(choice as any); | ||
} | ||
} | ||
|
||
const setSelectedValue = (selectedTabValue: string) => { | ||
setLocalSelectedValue(selectedTabValue); | ||
|
||
if (groupId != null) { | ||
setTabGroupChoices(groupId, selectedTabValue); | ||
} | ||
}; | ||
|
||
const value = { | ||
selectedValue, | ||
values, | ||
setSelectedValue, | ||
}; | ||
return ( | ||
<CodeTabContext.Provider value={value}>{children}</CodeTabContext.Provider> | ||
); | ||
} | ||
export default memo(CodeProvider); |
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,10 @@ | ||
import { createContext } from 'react'; | ||
|
||
const CodeTabContext = createContext({ | ||
selectedValue: '', | ||
setSelectedValue: (value: string): void => { | ||
throw new Error('No Tab provider'); | ||
}, | ||
values: [], | ||
}); | ||
export default CodeTabContext; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.