Skip to content

Commit

Permalink
Add example usage for MediaPlaceholder (#13389)
Browse files Browse the repository at this point in the history
* Add example usage for MediaPlaceholder

Adds an example of how to use the MediaPlaceholder and setting
the url for an image attribute.

Fixes #13377

* Fixed code style
  • Loading branch information
mkaz authored Jan 21, 2019
1 parent e1e02af commit 6d5a537
Showing 1 changed file with 37 additions and 13 deletions.
50 changes: 37 additions & 13 deletions packages/editor/src/components/media-placeholder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,31 @@ MediaPlaceholder

`MediaPlaceholder` is a React component used to render either the media associated with a block, or an editing interface to replace the media for a block.

## Setup
## Usage

An example usage which sets the URL of the selected image to `theImage` attributes.

```
const { MediaPlaceholder } = wp.editor;
...
edit: ( { attributes, setAttributes } ) {
const mediaPlaceholder = <MediaPlaceholder
onSelect = {
( el ) => {
setAttributes( { theImage: el.url } );
}
}
allowedTypes = { [ 'image' ] }
multiple = { false }
labels = { { title: 'The Image' } }
/>;
...
}
```

## Extend

It includes a `wp.hooks` filter `editor.MediaPlaceholder` that enables developers to replace or extend it.

Expand All @@ -12,18 +36,18 @@ _Example:_
Replace implementation of the placeholder:

```js
function replaceMediaPlaceholder() {
return function() {
return wp.element.createElement(
'div',
{},
'The replacement contents or components.'
);
}
}

wp.hooks.addFilter(
'editor.MediaPlaceholder',
function replaceMediaPlaceholder() {
return function() {
return wp.element.createElement(
'div',
{},
'The replacement contents or components.'
);
}
}

wp.hooks.addFilter(
'editor.MediaPlaceholder',
'my-plugin/replace-media-placeholder',
replaceMediaPlaceholder
);
Expand Down

0 comments on commit 6d5a537

Please sign in to comment.