Skip to content
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

Add toolbar dropdown component for use with blocks #3904

Merged
merged 4 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the arguments are a bit generic and the documentation don't specify what they actually do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think this change is enough? 6a94e1e

*/
const ToolbarDropdown = ( {
options,
optionsLabel,
icon,
value,
onChange,
} ) => {
const selectedOption = options.find( ( option ) => value === option.value );

return (
<Dropdown
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we change the border to have a block color as the rest of the dropdown menus?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Fixed here: bbdf53c

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