Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Heading: refactor away from the createComponent function, fix TS errors #34921

Merged
merged 1 commit into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions packages/components/src/heading/component.ts

This file was deleted.

37 changes: 37 additions & 0 deletions packages/components/src/heading/component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* External dependencies
*/
// eslint-disable-next-line no-restricted-imports
import type { Ref } from 'react';

/**
* Internal dependencies
*/
import { contextConnect, WordPressComponentProps } from '../ui/context';
import { View } from '../view';
import { useHeading, HeadingProps } from './hook';

function Heading(
props: WordPressComponentProps< HeadingProps, 'h1' >,
forwardedRef: Ref< any >
) {
const headerProps = useHeading( props );

return <View { ...headerProps } ref={ forwardedRef } />;
}

/**
* `Heading` renders headings and titles using the library's typography system.
*
* @example
* ```jsx
* import { Heading } from `@wordpress/components`
*
* function Example() {
* return <Heading>Code is Poetry</Heading>;
* }
* ```
*/
const ConnectedHeading = contextConnect( Heading, 'Heading' );

export default ConnectedHeading;
9 changes: 5 additions & 4 deletions packages/components/src/heading/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface HeadingProps extends Omit< TextProps, 'size' > {
/**
* `Heading` will typically render the sizes `1`, `2`, `3`, `4`, `5`, or `6`, which map to `h1`-`h6`.
*
* @default 3
* @default 2
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the useHeading hook (defined in this same file), the default value of level is 2

*
* @example
* ```jsx
Expand Down Expand Up @@ -56,16 +56,17 @@ export function useHeading(
'Heading'
);

const as = asProp || `h${ level }`;
const as = ( asProp || `h${ level }` ) as keyof JSX.IntrinsicElements;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Necessary to avoid a TypeScript error when passing this computed as prop to the View component


const a11yProps: {
role?: string;
'aria-level'?: string | number;
'aria-level'?: number;
} = {};
if ( typeof as === 'string' && as[ 0 ] !== 'h' ) {
// if not a semantic `h` element, add a11y props:
a11yProps.role = 'heading';
a11yProps[ 'aria-level' ] = level;
a11yProps[ 'aria-level' ] =
typeof level === 'string' ? parseInt( level ) : level;
Comment on lines +63 to +69
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix a TypeScript error where the expected value of aria-level can only be number | undefined (and not string)

}

const textProps = useText( {
Expand Down