-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3904 from Automattic/add/toolbar-dropdown
Add toolbar dropdown component for the editor
- Loading branch information
Showing
4 changed files
with
93 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@import './toolbar-dropdown/styles'; |
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,80 @@ | ||
import { | ||
Button, | ||
Dropdown, | ||
MenuGroup, | ||
MenuItem, | ||
NavigableMenu, | ||
} from '@wordpress/components'; | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* @typedef {Object} DropdownOption | ||
* | ||
* @property {string} label Option label. | ||
* @property {string} value Option value. | ||
*/ | ||
/** | ||
* Dropdown for the editor toolbar. | ||
* | ||
* @param {Object} props | ||
* @param {DropdownOption[]} props.options Dropdown options. | ||
* @param {string} [props.optionsLabel] Options label. | ||
* @param {Object} props.icon Icon for the toolbar. | ||
* @param {string} props.value Current dropdown value. | ||
* @param {Function} props.onChange Dropdown change callback, which receive | ||
* the new value as argument. | ||
*/ | ||
const ToolbarDropdown = ( { | ||
options, | ||
optionsLabel, | ||
icon, | ||
value, | ||
onChange, | ||
} ) => { | ||
const selectedOption = options.find( ( option ) => value === option.value ); | ||
|
||
return ( | ||
<Dropdown | ||
popoverProps={ { isAlternate: true } } | ||
renderToggle={ ( { isOpen, onToggle } ) => ( | ||
<Button | ||
onClick={ onToggle } | ||
icon={ icon } | ||
aria-expanded={ isOpen } | ||
aria-haspopup="true" | ||
> | ||
{ selectedOption.label } | ||
</Button> | ||
) } | ||
renderContent={ ( { onClose } ) => ( | ||
<NavigableMenu role="menu" stopNavigationEvents> | ||
<MenuGroup label={ optionsLabel }> | ||
{ options.map( ( option ) => { | ||
const isSelected = | ||
option.value === selectedOption.value; | ||
|
||
return ( | ||
<MenuItem | ||
key={ option.value } | ||
role="menuitemradio" | ||
isSelected={ isSelected } | ||
className={ classnames( { | ||
'sensei-toolbar-dropdown-item-checked': isSelected, | ||
} ) } | ||
onClick={ () => { | ||
onChange( option.value ); | ||
onClose(); | ||
} } | ||
> | ||
{ option.label } | ||
</MenuItem> | ||
); | ||
} ) } | ||
</MenuGroup> | ||
</NavigableMenu> | ||
) } | ||
/> | ||
); | ||
}; | ||
|
||
export default ToolbarDropdown; |
11 changes: 11 additions & 0 deletions
11
assets/blocks/editor-components/toolbar-dropdown/styles.scss
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,11 @@ | ||
.sensei-toolbar-dropdown-item-checked { | ||
position: relative; | ||
padding-right: 30px; | ||
&::after { | ||
font-family: dashicons; | ||
content: "\f147"; | ||
font-size: 20px; | ||
position: absolute; | ||
right: 0; | ||
} | ||
} |
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