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

Feature: Allow passing custom labels to MediaToolbar component #334

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
20 changes: 17 additions & 3 deletions components/media-toolbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ interface MediaToolbarProps {
* The ID of the media, in this case the image.
*/
id: number;

/**
* Labels for the buttons.
*/
labels?: {
replace?: string;
remove?: string;
add?: string;
};
}

/*
Expand All @@ -40,6 +49,11 @@ export const MediaToolbar: React.FC<MediaToolbarProps> = ({
onRemove,
isOptional = false,
id,
labels = {
add: __('Add Image', '10up-block-components'),
remove: __('Remove Image', '10up-block-components'),
replace: __('Replace Image', '10up-block-components'),
},
fabiankaegy marked this conversation as resolved.
Show resolved Hide resolved
}) => {
const hasImage = !!id;
const { media } = useMedia(id);
Expand All @@ -51,11 +65,11 @@ export const MediaToolbar: React.FC<MediaToolbarProps> = ({
<MediaReplaceFlow
mediaUrl={media?.source_url}
onSelect={onSelect}
name={__('Replace Image', '10up-block-components')}
name={labels.replace}
/>
{!!isOptional && (
<ToolbarButton onClick={onRemove}>
{__('Remove Image', '10up-block-components')}
{labels.remove}
</ToolbarButton>
)}
</>
Expand All @@ -65,7 +79,7 @@ export const MediaToolbar: React.FC<MediaToolbarProps> = ({
onSelect={onSelect}
render={({ open }) => (
<ToolbarButton onClick={open}>
{__('Add Image', '10up-block-components')}
{labels.add}
</ToolbarButton>
)}
/>
Expand Down
9 changes: 8 additions & 1 deletion components/media-toolbar/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The MediaToolbar component is build to quickly add a complete media editing expe
## Usage

```js
import { __ } from '@wordpress/i18n';
import { BlockControls } from '@wordpress/block-editor';
import { MediaToolbar } from '@10up/block-components';

Expand All @@ -28,6 +29,11 @@ function BlockEdit(props) {
id={ imageId }
onSelect={ handleImageSelect }
onRemove={ handleImageRemove }
labels={{
add: __('Custom add label'),
remove: __('Custom remove label'),
replace: __('Custom replace label'),
}}
/>
</BlockControls>
...
Expand All @@ -43,4 +49,5 @@ function BlockEdit(props) {
| `id` | `number` | `null` | image id |
| `onSelect` | `Function` | `null` | Callback that gets called with the new image when one is selected |
| `onRemove` | `Function` | `null` | Callback that gets called when the remove image button is clicked |
| `isOptional` | `boolean` | `false` | Wether or not the Remove Image button should be shown |
| `isOptional` | `boolean` | `false` | Whether or not the Remove Image button should be shown |
| `labels` | `object` | `{}` | Custom labels prop. Used to override default labels for better UX when using different media types. Allows the sub properties `add`, `remove`, and `replace`. |
18 changes: 18 additions & 0 deletions example/src/blocks/custom-media-example/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "example/custom-media-example",
"apiVersion": 3,
"title": "Custom Media Example",
"description": "Example Block to show the custom media toolbar in usage",
"icon": "smiley",
"category": "common",
"example": {},
"supports": {
"html": false
},
"attributes": {
"imageId": {
"type": "number"
}
},
"editorScript": "file:./index.ts"
}
56 changes: 56 additions & 0 deletions example/src/blocks/custom-media-example/edit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React from 'react';
import { __ } from '@wordpress/i18n';
import { BlockControls, useBlockProps } from '@wordpress/block-editor';

import { Image, MediaToolbar } from '@10up/block-components';

export function BlockEdit(props) {
const {
attributes,
setAttributes
} = props;

const { imageId, focalPoint } = attributes;
const blockProps = useBlockProps();

const handleImageSelection = value => {
setAttributes({imageId: value.id })
};
const removeImage = () => setAttributes({imageId: null});

const handleFocalPointChange = value => {
setAttributes({focalPoint: value})
};

return (
<>
<BlockControls>
<MediaToolbar
id={imageId}
onSelect={ handleImageSelection }
isOptional={true}
onRemove={removeImage}
labels={{
replace: __('Replace your gif'),
remove: __('Remove the gif'),
add: __('Add a gif')
}}
/>
</BlockControls>
<div {...blockProps}>
<h2>Hello World!</h2>
<Image
id={imageId}
size="large"
onSelect={handleImageSelection}
className="example-image"
allowedTypes={['image/gif']}
labels={{
title: __('Select a gif image'),
instructions: __('Upload a gif or pick one from your media library.')
}}
/>
</div>
</>
)
}
10 changes: 10 additions & 0 deletions example/src/blocks/custom-media-example/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { registerBlockType } from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';

import { BlockEdit } from './edit';
import metadata from './block.json';

registerBlockType( metadata, {
edit: BlockEdit,
save: () => null
} );
Loading