-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
* | ||
* @example | ||
* ```jsx | ||
|
@@ -56,16 +56,17 @@ export function useHeading( | |
'Heading' | ||
); | ||
|
||
const as = asProp || `h${ level }`; | ||
const as = ( asProp || `h${ level }` ) as keyof JSX.IntrinsicElements; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Necessary to avoid a TypeScript error when passing this computed |
||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix a TypeScript error where the expected value of |
||
} | ||
|
||
const textProps = useText( { | ||
|
There was a problem hiding this comment.
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 oflevel
is2