@wordpress/components
Version:
UI components for WordPress.
78 lines (77 loc) • 2.31 kB
JavaScript
import clsx from "clsx";
import { forwardRef, useContext } from "@wordpress/element";
import Button from "../../button/index.mjs";
import ToolbarItem from "../toolbar-item/index.mjs";
import ToolbarContext from "../toolbar-context/index.mjs";
import ToolbarButtonContainer from "./toolbar-button-container.mjs";
import { jsx } from "react/jsx-runtime";
// packages/components/src/toolbar/toolbar-button/index.tsx
function useDeprecatedProps({ isDisabled, ...otherProps }) {
return {
disabled: isDisabled,
...otherProps
};
}
function UnforwardedToolbarButton(props, ref) {
const { children, className, containerClassName, extraProps, isActive, title, ...restProps } = useDeprecatedProps(props);
if (!useContext(ToolbarContext)) return /* @__PURE__ */ jsx(ToolbarButtonContainer, {
className: containerClassName,
children: /* @__PURE__ */ jsx(Button, {
ref,
icon: restProps.icon,
size: "compact",
label: title,
shortcut: restProps.shortcut,
"data-subscript": restProps.subscript,
onClick: (event) => {
event.stopPropagation();
if (restProps.onClick) restProps.onClick(event);
},
className: clsx("components-toolbar__control", className),
isPressed: isActive,
accessibleWhenDisabled: true,
"data-toolbar-item": true,
...extraProps,
...restProps,
children
})
});
return /* @__PURE__ */ jsx(ToolbarItem, {
className: clsx("components-toolbar-button", className),
...extraProps,
...restProps,
ref,
children: (toolbarItemProps) => /* @__PURE__ */ jsx(Button, {
size: "compact",
label: title,
isPressed: isActive,
...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 { pencil } from '@wordpress/icons';
*
* function MyToolbar() {
* return (
* <Toolbar label="Options">
* <ToolbarButton
* icon={ pencil }
* label="Edit"
* onClick={ () => alert( 'Editing' ) }
* />
* </Toolbar>
* );
* }
* ```
*/
const ToolbarButton = forwardRef(UnforwardedToolbarButton);
ToolbarButton.displayName = "ToolbarButton";
export { ToolbarButton, ToolbarButton as default };
//# sourceMappingURL=index.mjs.map