@wordpress/components
Version:
UI components for WordPress.
96 lines (86 loc) • 2.57 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import { createElement } from "@wordpress/element";
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useContext, forwardRef } from '@wordpress/element';
/**
* Internal dependencies
*/
import Button from '../../button';
import ToolbarItem from '../toolbar-item';
import ToolbarContext from '../toolbar-context';
import ToolbarButtonContainer from './toolbar-button-container';
function UnforwardedToolbarButton(_ref, ref) {
let {
children,
className,
containerClassName,
extraProps,
isActive,
isDisabled,
title,
...props
} = _ref;
const accessibleToolbarState = useContext(ToolbarContext);
if (!accessibleToolbarState) {
return createElement(ToolbarButtonContainer, {
className: containerClassName
}, createElement(Button, _extends({
ref: ref,
icon: props.icon,
label: title,
shortcut: props.shortcut,
"data-subscript": props.subscript,
onClick: event => {
event.stopPropagation(); // TODO: Possible bug; maybe use onClick instead of props.onClick.
if (props.onClick) {
props.onClick(event);
}
},
className: classnames('components-toolbar__control', className),
isPressed: isActive,
disabled: isDisabled,
"data-toolbar-item": true
}, extraProps, props), children));
} // ToobarItem will pass all props to the render prop child, which will pass
// all props to Button. This means that ToolbarButton has the same API as
// Button.
return createElement(ToolbarItem, _extends({
className: classnames('components-toolbar-button', className)
}, extraProps, props, {
ref: ref
}), toolbarItemProps => createElement(Button, _extends({
label: title,
isPressed: isActive,
disabled: isDisabled
}, toolbarItemProps), children));
}
/**
* ToolbarButton can be used to add actions to a toolbar, usually inside a Toolbar
* or ToolbarGroup when used to create general interfaces.
*
* ```jsx
* import { Toolbar, ToolbarButton } from '@wordpress/components';
* import { edit } from '@wordpress/icons';
*
* function MyToolbar() {
* return (
* <Toolbar label="Options">
* <ToolbarButton
* icon={ edit }
* label="Edit"
* onClick={ () => alert( 'Editing' ) }
* />
* </Toolbar>
* );
* }
* ```
*/
export const ToolbarButton = forwardRef(UnforwardedToolbarButton);
export default ToolbarButton;
//# sourceMappingURL=index.js.map