Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ One way to ensure all dependencies are loaded is to use the [`@wordpress/depende
- [`useActiveBlockStyle`](src/hooks/useActiveBlockStyle)
- [`useBlockStyles`](src/hooks/useBlockStyles)
- [`useDisallowedBlocks`](src/hooks/useDisallowedBlocks)
- [`useMediaSrc`](src/hooks/useMediaSrc)
- [`useMeta`](src/hooks/useMeta)
- [`useRenderAppenderWithBlockLimit`](src/hooks/useRenderAppenderWithBlockLimit)
- [`useSelectBlock`](src/hooks/useSelectBlock)
Expand Down
45 changes: 45 additions & 0 deletions src/hooks/useMediaSrc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# useMediaSrc

The `useMediaSrc` hook allows for the easy retrieval of source media URLs from an ID.

## Usage

The `useMediaSrc` hook takes an ID and returns a URL or undefined.

```js
import { useMediaSrc } from '@humanmade/block-editor-components';

function BlockEdit( { attributes, setAttributes } ) {
const {
videoId
} = attributes;
const videoUrl = useMediaSrc( videoId );

return (
<>
<MediaUploadCheck>
<MediaUpload
onSelect={ ( media ) =>
setAttributes( { videoId: media.id } )
}
value={ videoId }
render={ ( { open } ) => (
<Button onClick={ open }>Open Media Library</Button>
) }
/>
</MediaUploadCheck>
<video src={ videoUrl } />
<>
);
}
```

## Return

The `useMediaSrc` hook returns either a URL string or undefined.

## Dependencies

The `useMediaSrc` hook requires the following dependencies, which are expected to be available:

- `@wordpress/data`
21 changes: 21 additions & 0 deletions src/hooks/useMediaSrc/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useSelect } from '@wordpress/data';

/**
* Returns the original source URL of media if available, or undefined, given an ID.
*
* @param {number} id attachment ID.
* @returns {string|undefined} Media source URL or undefined.
*/
export default function useMediaSrc( id ) {
const url = useSelect(
( select ) => {
const media = select( 'core' ).getMedia( id, { context: 'view' } );
if ( ! media ) {
return undefined;
}
return media.source_url;
},
[ id ]
);
return url;
}
2 changes: 1 addition & 1 deletion src/hooks/usePostThumbnail/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function usePostThumbnail() {

const setPostThumbnail = useCallback(
( mediaId ) => {
// If the image has been removed, we can remove the post thumbnail.
// If the image has been removed, we can remove the post thumbnail.
editPost( { featured_media: mediaId } );
},
[ editPost ]
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export {
export { default as useActiveBlockStyle } from './hooks/useActiveBlockStyle';
export { default as useBlockStyles } from './hooks/useBlockStyles';
export { default as useDisallowedBlocks } from './hooks/useDisallowedBlocks';
export { default as useMediaSrc } from './hooks/useMediaSrc';
export { default as useMeta } from './hooks/useMeta';
export { default as usePostThumbnail } from './hooks/usePostThumbnail';
export { default as useRenderAppenderWithBlockLimit } from './hooks/useRenderAppenderWithBlockLimit';
Expand Down
Loading