-
Notifications
You must be signed in to change notification settings - Fork 4.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
edb432c
commit 0731c22
Showing
30 changed files
with
1,352 additions
and
127 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,14 @@ | ||
export { default as Navigator } from './navigator'; | ||
|
||
export { default as NavigatorBack } from './navigator-back'; | ||
export { default as NavigatorButton } from './navigator-button'; | ||
export { default as NavigatorLink } from './navigator-link'; | ||
export { default as NavigatorRouter } from './navigator-router'; | ||
export { default as NavigatorScreen } from './navigator-screen'; | ||
export { default as NavigatorScreens } from './navigator-screens'; | ||
export { default as NavigatorSwitch } from './navigator-switch'; | ||
|
||
export { default as withNavigator } from './with-navigator'; | ||
|
||
export * from './navigator-context'; | ||
export * from './navigator-hooks'; |
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,21 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import Button from '../button'; | ||
import { contextConnect, useContextSystem } from '../ui/context'; | ||
import { useHistory } from './router'; | ||
|
||
function NavigatorBack( props, forwardedRef ) { | ||
const { ...otherProps } = useContextSystem( props, 'NavigatorBack' ); | ||
const history = useHistory(); | ||
|
||
return ( | ||
<Button | ||
{ ...otherProps } | ||
onClick={ history.goBack } | ||
ref={ forwardedRef } | ||
/> | ||
); | ||
} | ||
|
||
export default contextConnect( NavigatorBack, 'NavigatorBack' ); |
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,65 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import Button from '../button'; | ||
import { contextConnect, useContextSystem } from '../ui/context'; | ||
import { useHistory } from './router'; | ||
|
||
function NavigatorButton( props, forwardedRef ) { | ||
/* eslint-disable no-unused-vars */ | ||
const { | ||
as, | ||
children, | ||
exact, | ||
href, | ||
isBack, | ||
isPlain, | ||
params, | ||
showArrow, | ||
to, | ||
...otherProps | ||
} = useContextSystem( props, 'NavigatorButton' ); | ||
/* eslint-enable no-unused-vars */ | ||
|
||
const history = useHistory(); | ||
|
||
const handleOnClick = ( event ) => { | ||
event.preventDefault(); | ||
|
||
if ( isBack ) { | ||
if ( to ) { | ||
history.push( to, { isBack: true } ); | ||
} else { | ||
history.goBack(); | ||
} | ||
} else { | ||
history.push( to ); | ||
} | ||
}; | ||
|
||
if ( ! to ) { | ||
return ( | ||
<Button | ||
href={ href || '#' } | ||
ref={ forwardedRef } | ||
{ ...otherProps } | ||
onClick={ handleOnClick } | ||
> | ||
{ children } | ||
</Button> | ||
); | ||
} | ||
|
||
return ( | ||
<Button | ||
{ ...otherProps } | ||
exact={ exact } | ||
onClick={ handleOnClick } | ||
ref={ forwardedRef } | ||
> | ||
{ children } | ||
</Button> | ||
); | ||
} | ||
|
||
export default contextConnect( NavigatorButton, 'NavigatorButton' ); |
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,7 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createContext, useContext } from '@wordpress/element'; | ||
|
||
export const NavigatorContext = createContext( {} ); | ||
export const useNavigatorContext = () => useContext( NavigatorContext ); |
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 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useHistory, useLocation, useParams, useRouteMatch } from './router'; | ||
|
||
export const useNavigatorHistory = useHistory; | ||
export const useNavigatorLocation = useLocation; | ||
export const useNavigatorParams = useParams; | ||
export const useNavigatorRouteMatch = useRouteMatch; |
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,105 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classNames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Children } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import * as styles from './navigator-styles'; | ||
import { | ||
contextConnect, | ||
ContextSystemProvider, | ||
hasConnectNamespace, | ||
useContextSystem, | ||
} from '../ui/context'; | ||
import { NavLink, useHistory } from './router'; | ||
|
||
function NavigatorLink( props, forwardedRef ) { | ||
/* eslint-disable no-unused-vars */ | ||
const { | ||
as, | ||
children, | ||
className, | ||
exact, | ||
href, | ||
isBack, | ||
isPlain, | ||
params, | ||
showArrow, | ||
to, | ||
...otherProps | ||
} = useContextSystem( props, 'NavigatorLink' ); | ||
/* eslint-enable no-unused-vars */ | ||
|
||
const history = useHistory(); | ||
const [ child ] = Children.toArray( children ); | ||
const isWrappingMenuItem = hasConnectNamespace( child, [ 'MenuItem' ] ); | ||
|
||
const isLink = !! to || !! href; | ||
|
||
const classes = classNames( { | ||
[ styles.menuItemLink ]: isWrappingMenuItem, | ||
className, | ||
} ); | ||
|
||
const handleOnClick = ( event ) => { | ||
if ( isBack ) { | ||
event.preventDefault(); | ||
if ( to ) { | ||
history.push( to, { isBack: true } ); | ||
} else { | ||
history.goBack(); | ||
} | ||
} | ||
}; | ||
|
||
const content = ( | ||
<ContextSystemProvider | ||
value={ { | ||
MenuItem: { | ||
isBack, | ||
showArrow: isLink || showArrow, | ||
tabIndex: -1, | ||
}, | ||
} } | ||
> | ||
{ children } | ||
</ContextSystemProvider> | ||
); | ||
|
||
if ( ! to ) { | ||
return ( | ||
<a | ||
href={ href || '#' } | ||
ref={ forwardedRef } | ||
{ ...otherProps } | ||
className={ classes } | ||
onClick={ handleOnClick } | ||
> | ||
{ content } | ||
</a> | ||
); | ||
} | ||
|
||
return ( | ||
<NavLink | ||
{ ...otherProps } | ||
activeClassName="is-active" | ||
className={ classes } | ||
exact={ exact } | ||
onClick={ handleOnClick } | ||
ref={ forwardedRef } | ||
to={ to } | ||
> | ||
{ content } | ||
</NavLink> | ||
); | ||
} | ||
|
||
export default contextConnect( NavigatorLink, 'NavigatorLink' ); |
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,34 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { memo, useEffect } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useNavigatorHistory } from './navigator-hooks'; | ||
import { MemoryRouter } from './router'; | ||
|
||
function NavigatorRouter( { children, initialPath } ) { | ||
const history = useNavigatorHistory(); | ||
// Would only exist if nested within another <Navigator /> | ||
const location = history?.location; | ||
|
||
// Redirect on load | ||
useEffect( () => {} ); | ||
|
||
// No parent router | ||
if ( ! location ) { | ||
const initialEntry = initialPath ? [ initialPath ] : undefined; | ||
|
||
return ( | ||
<MemoryRouter initialEntries={ initialEntry }> | ||
{ children } | ||
</MemoryRouter> | ||
); | ||
} | ||
|
||
return children; | ||
} | ||
|
||
export default memo( NavigatorRouter ); |
Oops, something went wrong.