The MediaToolbar
component is a React component designed for handling media-related toolbar actions, such as replacing, adding, and removing media. It is specifically designed for integration with the WordPress Block Editor. It can be used with the <MediaPicker />
component
import { MediaToolbar } from '@digitalsilk/block-editor-components';
const YourComponent = (props) => {
const { attributes, setAttributes } = props;
const { attachmentId, mediaType } = attributes;
const handleMediaSelect = (selectedMedia) => {
// Handle the selected media data
setAttributes({ attachmentId: selectedMedia?.id ? selectedMedia.id : 0 });
};
const handleMediaRemove = (selectedMedia) => {
// Handle the selected media remove
setAttributes({ attachmentId: 0 });
};
return (
<MediaToolbar
id={attachmentId}
mediaType={mediaType}
onSelect={handleMediaSelect}
onRemove={handleMediaRemove}
/>
);
};
The Attachment ID.
A function callback to handle media selection. It is triggered when the user selects media.
A function callback to handle media removal. It is triggered when the user chooses to remove media.
A string representing the type of media. Valid values are 'image'
or 'video'
.
A boolean indicating whether multiple media selection is allowed.
<MediaToolbar
id={123}
onSelect={(selectedMedia) => {
// Handle media selection logic
}}
onRemove={() => {
// Handle media removal logic
}}
mediaType="image"
multiple={false}
/>
"attributes": {
"attachmentId": {
"type": "number",
"default": 0
},
"mediaType": {
"type": "string",
"default": "image"
}
}