Skip to content

Commit

Permalink
Merge pull request #3904 from Automattic/add/toolbar-dropdown
Browse files Browse the repository at this point in the history
Add toolbar dropdown component for the editor
  • Loading branch information
renatho authored Jan 21, 2021
2 parents e6a8336 + 0a7c48b commit dfadb70
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions assets/blocks/editor-components/styles.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import './toolbar-dropdown/styles';
80 changes: 80 additions & 0 deletions assets/blocks/editor-components/toolbar-dropdown/index.js
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 assets/blocks/editor-components/toolbar-dropdown/styles.scss
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;
}
}
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const files = [
'data-port/import.js',
'data-port/export.js',
'data-port/style.scss',
'blocks/editor-components/styles.scss',
'blocks/course-outline/frontend.js',
'blocks/sensei-single-course-blocks.js',
'blocks/single-course.scss',
Expand Down

0 comments on commit dfadb70

Please sign in to comment.