-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Heading block: use toolbar exclusively for heading level control (#20246
) * Heading block: use toolbar only for level control. * Fix the heading toolbar for RN mobile. Fix lint errors. Change the native side to use a dropDown menu directly. * Fix Safari horizontal scrollbar issue. * Simplify to use ToolbarButton. * Workaround for Firefox/Safari button focus issue. Co-authored-by: Sergio Estevao <sergioestevao@gmail.com>
- Loading branch information
1 parent
3c9deb4
commit 47ae410
Showing
9 changed files
with
225 additions
and
91 deletions.
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