Skip to content

Commit

Permalink
Add toolbar dropdown component
Browse files Browse the repository at this point in the history
  • Loading branch information
renatho committed Jan 19, 2021
1 parent a04787b commit 3154c0c
Show file tree
Hide file tree
Showing 4 changed files with 91 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';
78 changes: 78 additions & 0 deletions assets/blocks/editor-components/toolbar-dropdown/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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 value.
* @param {Function} props.onChange Change function.
*/
const ToolbarDropdown = ( {
options,
optionsLabel,
icon,
value,
onChange,
} ) => {
const selectedOption = options.find( ( option ) => value === option.value );

return (
<Dropdown
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 3154c0c

Please sign in to comment.