-
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 block: use toolbar for heading level control #20246
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a57a17d
Heading block: use toolbar only for level control.
ZebulanStanphill fa0dab7
Fix the heading toolbar for RN mobile.
SergioEstevao 0b58a7e
Fix Safari horizontal scrollbar issue.
ZebulanStanphill bb405cc
Simplify to use ToolbarButton.
ZebulanStanphill e0b2a1b
Workaround for Firefox/Safari button focus issue.
ZebulanStanphill File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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,14 @@ | ||
// Remove padding in heading level control popover since the toolbar buttons already have padding. | ||
.block-library-heading-level-dropdown .components-popover__content { | ||
padding: 0; | ||
// TODO: Find a less hardcoded way of doing this. `max-content` works on | ||
// Chromium, but it results in a scrollbar on Safari, and isn't supported | ||
// at all in IE11. | ||
min-width: 230px; | ||
} | ||
|
||
// The dropdown already has a border, so we can remove the one on the heading | ||
// level toolbar. | ||
.block-library-heading-level-toolbar { | ||
border: none; | ||
} |
112 changes: 112 additions & 0 deletions
112
packages/block-library/src/heading/heading-level-dropdown.js
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,112 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
Dropdown, | ||
Toolbar, | ||
ToolbarButton, | ||
ToolbarGroup, | ||
} from '@wordpress/components'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { DOWN } from '@wordpress/keycodes'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import HeadingLevelIcon from './heading-level-icon'; | ||
|
||
const HEADING_LEVELS = [ 1, 2, 3, 4, 5, 6 ]; | ||
|
||
const POPOVER_PROPS = { | ||
className: 'block-library-heading-level-dropdown', | ||
isAlternate: true, | ||
}; | ||
|
||
/** @typedef {import('@wordpress/element').WPComponent} WPComponent */ | ||
|
||
/** | ||
* HeadingLevelDropdown props. | ||
* | ||
* @typedef WPHeadingLevelDropdownProps | ||
* | ||
* @property {number} selectedLevel The chosen heading level. | ||
* @property {(newValue:number)=>any} onChange Callback to run when | ||
* toolbar value is changed. | ||
*/ | ||
|
||
/** | ||
* Dropdown for selecting a heading level (1 through 6). | ||
* | ||
* @param {WPHeadingLevelDropdownProps} props Component props. | ||
* | ||
* @return {WPComponent} The toolbar. | ||
*/ | ||
export default function HeadingLevelDropdown( { selectedLevel, onChange } ) { | ||
return ( | ||
<Dropdown | ||
popoverProps={ POPOVER_PROPS } | ||
renderToggle={ ( { onToggle, isOpen } ) => { | ||
const openOnArrowDown = ( event ) => { | ||
if ( ! isOpen && event.keyCode === DOWN ) { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
onToggle(); | ||
} | ||
}; | ||
|
||
return ( | ||
<ToolbarButton | ||
aria-expanded={ isOpen } | ||
aria-haspopup="true" | ||
icon={ <HeadingLevelIcon level={ selectedLevel } /> } | ||
label={ __( 'Change heading level' ) } | ||
onClick={ onToggle } | ||
onKeyDown={ openOnArrowDown } | ||
showTooltip | ||
/> | ||
); | ||
} } | ||
renderContent={ () => ( | ||
<Toolbar | ||
className="block-library-heading-level-toolbar" | ||
__experimentalAccessibilityLabel={ __( | ||
'Change heading level' | ||
) } | ||
> | ||
<ToolbarGroup | ||
isCollapsed={ false } | ||
controls={ HEADING_LEVELS.map( ( targetLevel ) => { | ||
const isActive = targetLevel === selectedLevel; | ||
return { | ||
icon: ( | ||
<HeadingLevelIcon | ||
level={ targetLevel } | ||
isPressed={ isActive } | ||
/> | ||
), | ||
title: sprintf( | ||
// translators: %s: heading level e.g: "1", "2", "3" | ||
__( 'Heading %d' ), | ||
targetLevel | ||
), | ||
isActive, | ||
onClick() { | ||
onChange( targetLevel ); | ||
}, | ||
// Temporary workaround for macOS Firefox/Safari issue | ||
// where clicking buttons in the heading level toolbar | ||
// doesn't work. | ||
// TODO: Replace this with a more general solution. | ||
// https://github.com/WordPress/gutenberg/pull/20246#pullrequestreview-417338057 | ||
onMouseDown( event ) { | ||
event.preventDefault(); | ||
event.currentTarget.focus(); | ||
}, | ||
}; | ||
} ) } | ||
/> | ||
</Toolbar> | ||
) } | ||
/> | ||
); | ||
} |
63 changes: 63 additions & 0 deletions
63
packages/block-library/src/heading/heading-level-dropdown.native.js
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,63 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { DropdownMenu } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import HeadingLevelIcon from './heading-level-icon'; | ||
|
||
const HEADING_LEVELS = [ 1, 2, 3, 4, 5, 6 ]; | ||
|
||
/** @typedef {import('@wordpress/element').WPComponent} WPComponent */ | ||
|
||
/** | ||
* HeadingLevelDropdown props. | ||
* | ||
* @typedef WPHeadingLevelDropdownProps | ||
* | ||
* @property {number} selectedLevel The chosen heading level. | ||
* @property {(newValue:number)=>any} onChange Callback to run when | ||
* toolbar value is changed. | ||
*/ | ||
|
||
/** | ||
* Dropdown for selecting a heading level (1 through 6). | ||
* | ||
* @param {WPHeadingLevelDropdownProps} props Component props. | ||
* | ||
* @return {WPComponent} The toolbar. | ||
*/ | ||
export default function HeadingLevelDropdown( { selectedLevel, onChange } ) { | ||
const createLevelControl = ( | ||
targetLevel, | ||
currentLevel, | ||
onChangeCallback | ||
) => { | ||
const isActive = targetLevel === currentLevel; | ||
return { | ||
icon: ( | ||
<HeadingLevelIcon | ||
level={ targetLevel } | ||
isPressed={ isActive } | ||
/> | ||
), | ||
// translators: %s: heading level e.g: "1", "2", "3" | ||
title: sprintf( __( 'Heading %d' ), targetLevel ), | ||
isActive, | ||
onClick: () => onChangeCallback( targetLevel ), | ||
}; | ||
}; | ||
|
||
return ( | ||
<DropdownMenu | ||
icon={ <HeadingLevelIcon level={ selectedLevel } /> } | ||
controls={ HEADING_LEVELS.map( ( index ) => | ||
createLevelControl( index, selectedLevel, onChange ) | ||
) } | ||
label={ __( 'Change heading level' ) } | ||
/> | ||
); | ||
} |
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
is this how we mark optional types, I thought it was more
boolean=
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.
I'm using the
?xxx
format because these docs said to use it for nullable types.