This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
1 parent
0db0724
commit af22ce0
Showing
5 changed files
with
144 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import React from 'react' | ||
import { useButtons } from './button-bar-context' | ||
|
||
const ButtonToolBar = () => { | ||
const buttons = useButtons() | ||
return <>{buttons.map((button) => button)}</> | ||
} | ||
|
||
export default ButtonToolBar |
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,36 @@ | ||
import React, { useState } from 'react' | ||
|
||
type Props = { | ||
children: React.ReactNode | ||
} | ||
|
||
type ButtonUpdater = (buttons: React.ReactNode[]) => void | ||
|
||
const ButtonBarStateContext = React.createContext<React.ReactNode[]>([]) | ||
const ButtonBarUpdateContext = React.createContext<ButtonUpdater>(() => {}) | ||
|
||
function ButtonBarProvider(props: Props) { | ||
const { children } = props | ||
const [state, setState] = useState<React.ReactNode[]>([]) | ||
return ( | ||
<ButtonBarStateContext.Provider value={state}> | ||
<ButtonBarUpdateContext.Provider value={setState}>{children}</ButtonBarUpdateContext.Provider> | ||
</ButtonBarStateContext.Provider> | ||
) | ||
} | ||
function useButtons() { | ||
const context = React.useContext(ButtonBarStateContext) | ||
if (context === undefined) { | ||
throw new Error('useCountState must be used within a CountProvider') | ||
} | ||
return context | ||
} | ||
function useButtonToolbarSetter() { | ||
const context = React.useContext(ButtonBarUpdateContext) | ||
if (context === undefined) { | ||
throw new Error('useCountDispatch must be used within a CountProvider') | ||
} | ||
return context | ||
} | ||
|
||
export { ButtonBarProvider, useButtons, useButtonToolbarSetter } |
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