-
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.
Add
NavigatorLink
and NavigatorBackLink
, replace usages across co…
…debase
- Loading branch information
Showing
12 changed files
with
260 additions
and
76 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export { default as NavigatorProvider } from './navigator-provider'; | ||
export { default as NavigatorScreen } from './navigator-screen'; | ||
export { default as NavigatorLink } from './navigator-link'; | ||
export { default as NavigatorBackLink } from './navigator-back-link'; | ||
export { default as useNavigator } from './use-navigator'; |
19 changes: 19 additions & 0 deletions
19
packages/components/src/navigator/navigator-back-link/README.md
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,19 @@ | ||
# `NavigatorBackLink` | ||
|
||
<div class="callout callout-alert"> | ||
This feature is still experimental. “Experimental” means this is an early implementation subject to drastic and breaking changes. | ||
</div> | ||
|
||
**TODO** | ||
|
||
The `Navigator*` family of components is _not_ opinionated in terms of UI, and can be composed with any UI components to navigate between the nested screens. | ||
|
||
## Usage | ||
|
||
**TODO** | ||
|
||
## Props | ||
|
||
The component accepts the following props: | ||
|
||
**TODO** |
65 changes: 65 additions & 0 deletions
65
packages/components/src/navigator/navigator-back-link/component.tsx
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 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
// eslint-disable-next-line no-restricted-imports | ||
import type { Ref } from 'react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useCallback } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
contextConnect, | ||
useContextSystem, | ||
WordPressComponentProps, | ||
} from '../../ui/context'; | ||
import { View } from '../../view'; | ||
import useNavigator from '../use-navigator'; | ||
import type { NavigatorBackLinkProps } from '../types'; | ||
|
||
function NavigatorBackLink( | ||
props: WordPressComponentProps< NavigatorBackLinkProps, 'button' >, | ||
forwardedRef: Ref< any > | ||
) { | ||
const { | ||
children, | ||
onClick, | ||
as = 'button', | ||
...otherProps | ||
} = useContextSystem( props, 'NavigatorBackLink' ); | ||
|
||
const { pop } = useNavigator(); | ||
const handleClick: React.MouseEventHandler< HTMLElement > = useCallback( | ||
( e ) => { | ||
e.preventDefault(); | ||
pop(); | ||
onClick?.( e ); | ||
}, | ||
[ pop, onClick ] | ||
); | ||
|
||
return ( | ||
<View | ||
as={ as } | ||
ref={ forwardedRef } | ||
onClick={ handleClick } | ||
{ ...otherProps } | ||
> | ||
{ children } | ||
</View> | ||
); | ||
} | ||
|
||
/** | ||
* TODO: add example | ||
*/ | ||
const ConnectedNavigatorBackLink = contextConnect( | ||
NavigatorBackLink, | ||
'NavigatorBackLink' | ||
); | ||
|
||
export default ConnectedNavigatorBackLink; |
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 @@ | ||
export { default } from './component'; |
19 changes: 19 additions & 0 deletions
19
packages/components/src/navigator/navigator-link/README.md
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,19 @@ | ||
# `NavigatorLink` | ||
|
||
<div class="callout callout-alert"> | ||
This feature is still experimental. “Experimental” means this is an early implementation subject to drastic and breaking changes. | ||
</div> | ||
|
||
**TODO** | ||
|
||
The `Navigator*` family of components is _not_ opinionated in terms of UI, and can be composed with any UI components to navigate between the nested screens. | ||
|
||
## Usage | ||
|
||
**TODO** | ||
|
||
## Props | ||
|
||
The component accepts the following props: | ||
|
||
**TODO** |
72 changes: 72 additions & 0 deletions
72
packages/components/src/navigator/navigator-link/component.tsx
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,72 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
// eslint-disable-next-line no-restricted-imports | ||
import type { Ref } from 'react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useCallback } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
contextConnect, | ||
useContextSystem, | ||
WordPressComponentProps, | ||
} from '../../ui/context'; | ||
import { View } from '../../view'; | ||
import useNavigator from '../use-navigator'; | ||
import type { NavigatorLinkProps } from '../types'; | ||
|
||
const defaultAttributeName = 'data-navigator-focusable-id'; | ||
const defaultSelectorFactory = ( attrName: string, attrValue: string ) => | ||
`[${ attrName }="${ attrValue }"]`; | ||
|
||
function NavigatorLink( | ||
props: WordPressComponentProps< NavigatorLinkProps, 'a' >, | ||
forwardedRef: Ref< any > | ||
) { | ||
const { | ||
path, | ||
children, | ||
onClick, | ||
as = 'button', | ||
attributeName = defaultAttributeName, | ||
selectorFactory = defaultSelectorFactory, | ||
...otherProps | ||
} = useContextSystem( props, 'NavigatorLink' ); | ||
|
||
const { push } = useNavigator(); | ||
const focusTargetSelector = selectorFactory( attributeName, path ); | ||
const handleClick: React.MouseEventHandler< HTMLElement > = useCallback( | ||
( e ) => { | ||
e.preventDefault(); | ||
push( path, { focusTargetSelector } ); | ||
onClick?.( e ); | ||
}, | ||
[ push, selectorFactory, onClick ] | ||
); | ||
|
||
const linkProps = { [ attributeName ]: path, path, ...otherProps }; | ||
|
||
return ( | ||
<View | ||
as={ as } | ||
ref={ forwardedRef } | ||
onClick={ handleClick } | ||
{ ...linkProps } | ||
> | ||
{ children } | ||
</View> | ||
); | ||
} | ||
|
||
/** | ||
* TODO: add example | ||
*/ | ||
const ConnectedNavigatorLink = contextConnect( NavigatorLink, 'NavigatorLink' ); | ||
|
||
export default ConnectedNavigatorLink; |
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 @@ | ||
export { default } from './component'; |
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
Oops, something went wrong.