Repository navigation
Move HeadinglevelDropdown to its own component #46003
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
Changes from all commits
a5fb915
753e2a8
939bd29
165ad2f
d906d56
787d080
6c62d43
5691977
1ba3734
a2d48fa
5639693
48a7a10
98dbcde
9a4bad3
3724097
d331504
1fba40d
d397580
dcf2f38
62b1de0
c2d4377
33f32da
a146a80
13476cf
ed7000d
b71e4df
46b117a
a50c982
ec4278f
2716599
563f933
95866c5
b674dc1
b78b136
de0e2c9
ce5c979
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # Heading Level Dropdown | ||
|
|
||
| `<HeadingLevelDropdown>` Adds a dropdown for selecting H1-H6 and paragraph HTML tags from the block toolbar. | ||
| Uses `<ToolbarDropdownMenu>`. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```jsx | ||
|
|
||
| import { BlockControls, HeadingLevelDropdown } from '@wordpress/block-editor'; | ||
|
|
||
| const HEADING_LEVELS = [ 0, 1, 2, 3, 4, 5, 6 ]; | ||
|
|
||
| const MyHeadingLevelToolbar = () => ( | ||
| <BlockControls group="block"> | ||
| <HeadingLevelDropdown | ||
| options={ HEADING_LEVELS } | ||
| value={ tag } | ||
| onChange={ ( newTag ) => | ||
| setAttributes( { tag: newTag } ) | ||
| } | ||
| /> | ||
| </BlockControls> | ||
| ); | ||
| ``` | ||
|
|
||
| ### Props | ||
|
|
||
| #### options | ||
|
|
||
| The list of available HTML tags, passed from the block. | ||
|
|
||
| - Type: `Object` | ||
| - Required: no | ||
|
|
||
| #### value | ||
|
|
||
| The chosen HTML tag. | ||
|
|
||
| - Type: `string` | ||
| - Required: no | ||
|
|
||
| #### onChange | ||
|
|
||
| Callback to run when toolbar value is changed. | ||
|
|
||
| - Type: `string` | ||
| - Required: yes | ||
|
|
||
| ## Related components | ||
|
|
||
| Block Editor components are components that can be used to compose the UI of your block editor. Thus, they can only be used under a [`BlockEditorProvider`](https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/provider/README.md) in the components tree. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { | ||
| headingLevel1, | ||
| headingLevel2, | ||
| headingLevel3, | ||
| headingLevel4, | ||
| headingLevel5, | ||
| headingLevel6, | ||
| paragraph, | ||
| } from '@wordpress/icons'; | ||
|
|
||
| /** @typedef {import('@wordpress/element').WPComponent} WPComponent */ | ||
|
|
||
| /** | ||
| * HeadingLevelIcon props. | ||
| * | ||
| * @typedef WPHeadingLevelIconProps | ||
| * | ||
| * @property {number} level The heading level to show an icon for. | ||
| */ | ||
|
|
||
| const LEVEL_TO_PATH = { | ||
| 0: paragraph, | ||
| 1: headingLevel1, | ||
| 2: headingLevel2, | ||
| 3: headingLevel3, | ||
| 4: headingLevel4, | ||
| 5: headingLevel5, | ||
| 6: headingLevel6, | ||
| }; | ||
|
|
||
| /** | ||
| * Heading level icon. | ||
| * | ||
| * @param {WPHeadingLevelIconProps} props Component props. | ||
| * | ||
| * @return {?WPComponent} The icon. | ||
| */ | ||
| export default function HeadingLevelIcon( { level } ) { | ||
| return LEVEL_TO_PATH[ level ] ?? null; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ import { | |
| InspectorControls, | ||
| useBlockProps, | ||
| PlainText, | ||
| HeadingLevelDropdown, | ||
| privateApis as blockEditorPrivateApis, | ||
| } from '@wordpress/block-editor'; | ||
| import { ToggleControl, TextControl, PanelBody } from '@wordpress/components'; | ||
|
|
@@ -22,7 +23,6 @@ import { useEntityProp } from '@wordpress/core-data'; | |
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import HeadingLevelDropdown from '../heading/heading-level-dropdown'; | ||
| import { useCanEditEntity } from '../utils/hooks'; | ||
| import { unlock } from '../lock-unlock'; | ||
|
|
||
|
|
@@ -34,7 +34,7 @@ export default function PostTitleEdit( { | |
| context: { postType, postId, queryId }, | ||
| insertBlocksAfter, | ||
| } ) { | ||
| const TagName = 0 === level ? 'p' : 'h' + level; | ||
|
Contributor
Author
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. I noticed that this code seems to have been adapted for allowing the post title to be a paragraph, but I don't remember this being implemented? It was only implemented for the site title. Because of the discussions in this issue #30549 |
||
| const TagName = 'h' + level; | ||
| const isDescendentOfQueryLoop = Number.isFinite( queryId ); | ||
| /** | ||
| * Hack: useCanEditEntity may trigger an OPTIONS request to the REST API via the canUser resolver. | ||
|
|
@@ -120,7 +120,7 @@ export default function PostTitleEdit( { | |
| { blockEditingMode === 'default' && ( | ||
| <BlockControls group="block"> | ||
| <HeadingLevelDropdown | ||
| selectedLevel={ level } | ||
| value={ level } | ||
| onChange={ ( newLevel ) => | ||
| setAttributes( { level: newLevel } ) | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.