diff --git a/packages/components/CHANGELOG.md b/packages/components/CHANGELOG.md index 12c454459a198..de726e6106e9b 100644 --- a/packages/components/CHANGELOG.md +++ b/packages/components/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Internal + +- `Guide`: Convert to TypeScript ([#47493](https://github.com/WordPress/gutenberg/pull/47493)). + ## 23.5.0 (2023-03-01) ### Enhancements diff --git a/packages/components/src/guide/README.md b/packages/components/src/guide/README.md index 061332234cdbc..01b1182338005 100644 --- a/packages/components/src/guide/README.md +++ b/packages/components/src/guide/README.md @@ -37,20 +37,6 @@ function MyTutorial() { The component accepts the following props: -### onFinish - -A function which is called when the guide is finished. The guide is finished when the modal is closed or when the user clicks _Finish_ on the last page of the guide. - -- Type: `function` -- Required: Yes - -### pages - -A list of objects describing each page in the guide. Each object **must** contain a `'content'` property and may optionally contain a `'image'` property. - -- Type: `array` -- Required: Yes - ### className A custom class to add to the modal. @@ -62,7 +48,7 @@ A custom class to add to the modal. This property is used as the modal's accessibility label. It is required for accessibility reasons. -- Type: `String` +- Type: `string` - Required: Yes ### finishButtonText @@ -71,3 +57,19 @@ Use this to customize the label of the _Finish_ button shown at the end of the g - Type: `string` - Required: No +- Default: `'Finish'` + +### onFinish + +A function which is called when the guide is finished. The guide is finished when the modal is closed or when the user clicks _Finish_ on the last page of the guide. + +- Type: `( event?: KeyboardEvent< HTMLDivElement > | SyntheticEvent ) => void` +- Required: Yes + +### pages + +A list of objects describing each page in the guide. Each object **must** contain a `'content'` property and may optionally contain a `'image'` property. + +- Type: `{ content: ReactNode; image?: ReactNode; }[]` +- Required: No +- Default: `[]` diff --git a/packages/components/src/guide/icons.js b/packages/components/src/guide/icons.tsx similarity index 76% rename from packages/components/src/guide/icons.js rename to packages/components/src/guide/icons.tsx index 1f9713d6fdd8c..1c47180ae0b11 100644 --- a/packages/components/src/guide/icons.js +++ b/packages/components/src/guide/icons.tsx @@ -3,7 +3,7 @@ */ import { SVG, Circle } from '@wordpress/primitives'; -export const PageControlIcon = ( { isSelected } ) => ( +export const PageControlIcon = ( { isSelected }: { isSelected: boolean } ) => ( setIsOpen( false ) } + * pages={ [ + * { + * content:

Welcome to the ACME Store!

, + * }, + * { + * image: , + * content: ( + *

+ * Click Add to Cart to buy a product. + *

+ * ), + * }, + * ] } + * /> + * ); + * } + * ``` + */ +function Guide( { children, className, contentLabel, - finishButtonText, + finishButtonText = __( 'Finish' ), onFinish, pages = [], -} ) { - const guideContainer = useRef(); +}: GuideProps ) { + const guideContainer = useRef< HTMLDivElement >( null ); const [ currentPage, setCurrentPage ] = useState( 0 ); useEffect( () => { @@ -42,12 +75,17 @@ export default function Guide( { // Each time we change the current page, start from the first element of the page. // This also solves any focus loss that can happen. if ( guideContainer.current ) { - focus.tabbable.find( guideContainer.current )?.[ 0 ]?.focus(); + ( + focus.tabbable.find( guideContainer.current ) as HTMLElement[] + )[ 0 ]?.focus(); } }, [ currentPage ] ); if ( Children.count( children ) ) { - pages = Children.map( children, ( child ) => ( { content: child } ) ); + pages = + Children.map( children, ( child ) => ( { + content: child, + } ) ) ?? []; } const canGoBack = currentPage > 0; @@ -124,7 +162,7 @@ export default function Guide( { className="components-guide__finish-button" onClick={ onFinish } > - { finishButtonText || __( 'Finish' ) } + { finishButtonText } ) } @@ -132,3 +170,5 @@ export default function Guide( { ); } + +export default Guide; diff --git a/packages/components/src/guide/page-control.js b/packages/components/src/guide/page-control.tsx similarity index 93% rename from packages/components/src/guide/page-control.js rename to packages/components/src/guide/page-control.tsx index 41547c18326df..6e5951923ab38 100644 --- a/packages/components/src/guide/page-control.js +++ b/packages/components/src/guide/page-control.tsx @@ -8,12 +8,13 @@ import { __, sprintf } from '@wordpress/i18n'; */ import Button from '../button'; import { PageControlIcon } from './icons'; +import type { PageControlProps } from './types'; export default function PageControl( { currentPage, numberOfPages, setCurrentPage, -} ) { +}: PageControlProps ) { return (