-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Navigator
: refactor to TypeScript (#35214)
- Loading branch information
Showing
14 changed files
with
330 additions
and
126 deletions.
There are no files selected for viewing
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createContext } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { NavigatorContext as NavigatorContextType } from './types'; | ||
|
||
const initialContextValue: NavigatorContextType = [ {}, () => {} ]; | ||
export const NavigatorContext = createContext( initialContextValue ); |
File renamed without changes.
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
21 changes: 0 additions & 21 deletions
21
packages/components/src/navigator/navigator-provider/component.js
This file was deleted.
Oops, something went wrong.
96 changes: 96 additions & 0 deletions
96
packages/components/src/navigator/navigator-provider/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,96 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
// eslint-disable-next-line no-restricted-imports | ||
import type { Ref } from 'react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
contextConnect, | ||
useContextSystem, | ||
WordPressComponentProps, | ||
} from '../../ui/context'; | ||
import { View } from '../../view'; | ||
import { NavigatorContext } from '../context'; | ||
import type { NavigatorProviderProps, NavigatorPath } from '../types'; | ||
|
||
function NavigatorProvider( | ||
props: WordPressComponentProps< NavigatorProviderProps, 'div' >, | ||
forwardedRef: Ref< any > | ||
) { | ||
const { initialPath, children, ...otherProps } = useContextSystem( | ||
props, | ||
'NavigatorProvider' | ||
); | ||
|
||
const [ path, setPath ] = useState< NavigatorPath >( { | ||
path: initialPath, | ||
} ); | ||
|
||
return ( | ||
<View ref={ forwardedRef } { ...otherProps }> | ||
<NavigatorContext.Provider value={ [ path, setPath ] }> | ||
{ children } | ||
</NavigatorContext.Provider> | ||
</View> | ||
); | ||
} | ||
|
||
/** | ||
* The `NavigatorProvider` component allows rendering nested panels or menus (via the `NavigatorScreen` component) and navigate between these different states (via the `useNavigator` hook). | ||
* The Global Styles sidebar is an example of this. 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. | ||
* | ||
* @example | ||
* ```jsx | ||
* import { | ||
* __experimentalNavigatorProvider as NavigatorProvider, | ||
* __experimentalNavigatorScreen as NavigatorScreen, | ||
* __experimentalUseNavigator as useNavigator, | ||
* } from '@wordpress/components'; | ||
* | ||
* function NavigatorButton( { | ||
* path, | ||
* isBack = false, | ||
* ...props | ||
* } ) { | ||
* const navigator = useNavigator(); | ||
* return ( | ||
* <Button | ||
* onClick={ () => navigator.push( path, { isBack } ) } | ||
* { ...props } | ||
* /> | ||
* ); | ||
* } | ||
* | ||
* const MyNavigation = () => ( | ||
* <NavigatorProvider initialPath="/"> | ||
* <NavigatorScreen path="/"> | ||
* <p>This is the home screen.</p> | ||
* <NavigatorButton isPrimary path="/child"> | ||
* Navigate to child screen. | ||
* </NavigatorButton> | ||
* </NavigatorScreen> | ||
* | ||
* <NavigatorScreen path="/child"> | ||
* <p>This is the child screen.</p> | ||
* <NavigatorButton isPrimary path="/" isBack> | ||
* Go back | ||
* </NavigatorButton> | ||
* </NavigatorScreen> | ||
* </NavigatorProvider> | ||
* ); | ||
* ``` | ||
*/ | ||
const ConnectedNavigatorProvider = contextConnect( | ||
NavigatorProvider, | ||
'NavigatorProvider' | ||
); | ||
|
||
export default ConnectedNavigatorProvider; |
File renamed without changes.
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
92 changes: 0 additions & 92 deletions
92
packages/components/src/navigator/navigator-screen/component.js
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.