-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the useContext hook instead of withWorkspaceContext HOC
- Loading branch information
Showing
15 changed files
with
27 additions
and
62 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
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
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
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 |
---|---|---|
@@ -1,11 +1,9 @@ | ||
import { compose } from 'redux'; | ||
import { withTranslation } from 'react-i18next'; | ||
import { withWorkspaceContext } from '../contexts/WorkspaceContext'; | ||
import { WorkspaceOptionsMenu } from '../components/WorkspaceOptionsMenu'; | ||
|
||
const enhance = compose( | ||
withTranslation(), | ||
withWorkspaceContext, | ||
); | ||
|
||
export default enhance(WorkspaceOptionsMenu); |
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 |
---|---|---|
@@ -1,36 +1,5 @@ | ||
import { | ||
createContext, useContext, useState, useEffect, forwardRef, | ||
} from 'react'; | ||
import { createContext } from 'react'; | ||
|
||
const WorkspaceContext = createContext({ current: document.body }); | ||
|
||
/** | ||
* @returns HOC that injects the workspace ref into the component | ||
*/ | ||
export const withWorkspaceContext = (Component) => { | ||
/** | ||
* Wrap the component with the context | ||
*/ | ||
function ContextHoc(props, ref) { | ||
const workspaceContext = useContext(WorkspaceContext); | ||
|
||
const [workspaceRef, setWorkspaceRef] = useState(); | ||
|
||
useEffect(() => { | ||
setWorkspaceRef(workspaceContext); | ||
}, [workspaceContext]); | ||
|
||
const passDownProps = { | ||
...props, | ||
...(ref ? { ref } : {}), | ||
}; | ||
|
||
return <Component container={workspaceRef} {...passDownProps} />; | ||
} | ||
|
||
const whatever = forwardRef(ContextHoc); | ||
whatever.displayName = `WithWorkspaceContext(${Component.displayName || Component.name || 'Component'})`; | ||
return whatever; | ||
}; | ||
|
||
export default WorkspaceContext; |