|
| 1 | +# VideoControl |
| 2 | + |
| 3 | +The `VideoControl` component allows for selecting a single video from the Media Library in a sidebar panel. |
| 4 | +Internally, `VideoControl` is wrapping a [`MediaUpload`](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-editor/src/components/media-upload/index.js) inside a [`BaseControl`](https://github.com/WordPress/gutenberg/blob/trunk/packages/components/src/base-control/index.tsx) component. |
| 5 | + |
| 6 | +|  | |
| 7 | +|------------------------------------------------------------------------------------| |
| 8 | +| _`VideoControl` component._ | |
| 9 | + |
| 10 | +|  | |
| 11 | +|------------------------------------------------------------------------------------| |
| 12 | +| _`VideoControl` component displaying the selected video._ | |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +For a minimum working setup, all you need to do is pass an video ID as `value`, as well as an `onChange` callback that accepts a video object. |
| 17 | + |
| 18 | +```js |
| 19 | +import { VideoControl } from '@humanmade/block-editor-components'; |
| 20 | +import { InspectorControls } from '@wordpress/block-editor'; |
| 21 | +import { PanelBody } from '@wordpress/components'; |
| 22 | + |
| 23 | +function BlockEdit( props ) { |
| 24 | + const { attributes, setAttributes } = props; |
| 25 | + const { videoId } = attributes; |
| 26 | + |
| 27 | + return ( |
| 28 | + <InspectorControls> |
| 29 | + <PanelBody> |
| 30 | + <VideoControl |
| 31 | + value={ videoId } |
| 32 | + onChange={ ( video ) => setAttributes( { videoId: video?.id } ) } |
| 33 | + /> |
| 34 | + </PanelBody> |
| 35 | + </InspectorControls> |
| 36 | + ); |
| 37 | +} |
| 38 | +``` |
| 39 | +
|
| 40 | +You can also customize button texts, as well as the modal title. |
| 41 | +
|
| 42 | +```js |
| 43 | +import { VideoControl } from '@humanmade/block-editor-components'; |
| 44 | +import { InspectorControls } from '@wordpress/block-editor'; |
| 45 | +import { PanelBody } from '@wordpress/components'; |
| 46 | + |
| 47 | +function BlockEdit( props ) { |
| 48 | + const { attributes, setAttributes } = props; |
| 49 | + const { videoId } = attributes; |
| 50 | + |
| 51 | + return ( |
| 52 | + <InspectorControls> |
| 53 | + <PanelBody> |
| 54 | + <VideoControl |
| 55 | + buttonText="Select Video" |
| 56 | + modalTitle="Select Video" |
| 57 | + removeButtonText="Remove Video" |
| 58 | + replaceButtonText="Replace Video" |
| 59 | + value={ videoId } |
| 60 | + onChange={ ( video ) => setAttributes( { videoId: video?.id } ) } |
| 61 | + /> |
| 62 | + </PanelBody> |
| 63 | + </InspectorControls> |
| 64 | + ); |
| 65 | +} |
| 66 | +``` |
| 67 | +
|
| 68 | +Also, all stable props of `BaseControl` are supported. |
| 69 | +
|
| 70 | +## Props |
| 71 | +
|
| 72 | +The `VideoControl` component has custom props `value` and `onChange` for managing the image, as well as `buttonText`, `modalTitle`, `removeButtonText` and `replaceButtonText`. |
| 73 | +Also, it supports all stable props of the `BaseControl` component. |
| 74 | +
|
| 75 | +### `buttonText` |
| 76 | +
|
| 77 | +The button text to display if no image has been selected. |
| 78 | +
|
| 79 | +| Type | Required | Default | |
| 80 | +|--------------------------------------|--------------------------------------|--------------------------------------| |
| 81 | +| `string` | no | `'Select image'` | |
| 82 | +
|
| 83 | +### `modalTitle` |
| 84 | +
|
| 85 | +The modal title. |
| 86 | +
|
| 87 | +| Type | Required | Default | |
| 88 | +|--------------------------------------|--------------------------------------|--------------------------------------| |
| 89 | +| `string` | no | `'Select Video'` | |
| 90 | +
|
| 91 | +### `onChange` |
| 92 | +
|
| 93 | +The callback to use for handling changing the video. |
| 94 | +Please note that `onChange` will receive a video object from the Media Library. |
| 95 | +
|
| 96 | +| Type | Required | Default | |
| 97 | +|--------------------------------------|--------------------------------------|--------------------------------------| |
| 98 | +| `Function` | yes | `undefined` | |
| 99 | +
|
| 100 | +### `removeButtonText` |
| 101 | +
|
| 102 | +The text to display for the remove button. |
| 103 | +
|
| 104 | +| Type | Required | Default | |
| 105 | +|--------------------------------------|--------------------------------------|--------------------------------------| |
| 106 | +| `string` | no | `'Remove Video'` | |
| 107 | +
|
| 108 | +### `replaceButtonText` |
| 109 | +
|
| 110 | +The button text to display if an video has been selected. |
| 111 | +
|
| 112 | +| Type | Required | Default | |
| 113 | +|--------------------------------------|--------------------------------------|--------------------------------------| |
| 114 | +| `string` | no | `'Replace Video` | |
| 115 | +
|
| 116 | +### `size` |
| 117 | +
|
| 118 | +The video size to display in the sidebar. |
| 119 | +
|
| 120 | +| Type | Required | Default | |
| 121 | +|--------------------------------------|--------------------------------------|--------------------------------------| |
| 122 | +| `string` | no | `undefined` | |
| 123 | +
|
| 124 | +### `value` |
| 125 | +
|
| 126 | +A video ID. |
| 127 | +
|
| 128 | +| Type | Required | Default | |
| 129 | +|--------------------------------------|--------------------------------------|--------------------------------------| |
| 130 | +| `number` | yes | `undefined` | |
| 131 | +
|
| 132 | +## Dependencies |
| 133 | +
|
| 134 | +The `VideoControl` component requires the following dependencies, which are expected to be available: |
| 135 | +
|
| 136 | +- `@wordpress/block-editor` |
| 137 | +- `@wordpress/components` |
| 138 | +- `@wordpress/data` |
| 139 | +- `@wordpress/i18n` |
0 commit comments