-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #260 from humanmade/use-post-thumbnail
Add usePostThumbnail hook
- Loading branch information
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# usePostThumbnail | ||
|
||
The `usePostThumbnail` hook allows for easy use and updates of the post thumbnail from within a | ||
|
||
## Usage | ||
|
||
The `usePostThumbnail` hook provides 2 values, and a setter function | ||
|
||
```js | ||
import { usePostThumbnail } from '@humanmade/block-editor-components'; | ||
|
||
function BlockEdit() { | ||
const { postThumbnail, postThumbnailId, setPostThumbnail } = usePostThumbnail(); | ||
|
||
return ( | ||
<> | ||
<MediaUploadCheck> | ||
<MediaUpload | ||
onSelect={ ( media ) => | ||
setPostThumbnail( media.id ) | ||
} | ||
allowedTypes={ ALLOWED_MEDIA_TYPES } | ||
value={ postThumbnailId } | ||
render={ ( { open } ) => ( | ||
<Button onClick={ open }>Open Media Library</Button> | ||
) } | ||
/> | ||
</MediaUploadCheck> | ||
<img alt={ postThumbnail?.alt_text } src={ postThumbnail?.source_url } /> | ||
<> | ||
); | ||
} | ||
``` | ||
## Return | ||
The `usePostThumbnail` hook returns an object with 3 properties: | ||
- `postThumbnail` - The full media object for the post thumbnail (as returned from `select( 'core' ).getMedia()`) | ||
- `postThumbnailId` - The ID of the thumbnail as an integer | ||
- `setPostThumbnail` - A function to set the current post's thumbnail by ID | ||
## Dependencies | ||
The `usePostThumbnail` hook requires the following dependencies, which are expected to be available: | ||
- `@wordpress/data` | ||
- `@wordpress/element` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
useDispatch, | ||
useSelect, | ||
} from '@wordpress/data'; | ||
import { useCallback } from '@wordpress/element'; | ||
|
||
/** | ||
* @typedef {object} postThumbnailUtils | ||
* @property {?object} postThumbnail The post thumbnail object | ||
* @property {?number} postThumbnailId The post thumbnail ID | ||
* @property {Function} setPostThumbnail Function to set the post thumbnail | ||
*/ | ||
|
||
/** | ||
* Get the post thumbnail and a function to set it. | ||
* | ||
* @returns {postThumbnailUtils} The post thumbnail data and setter function | ||
*/ | ||
export default function usePostThumbnail() { | ||
|
||
const { editPost } = useDispatch( 'core/editor' ); | ||
|
||
const postThumbnailId = useSelect( | ||
( select ) => select( 'core/editor' ).getEditedPostAttribute( 'featured_media' ) | ||
); | ||
|
||
const postThumbnail = useSelect( | ||
( select ) => { | ||
if ( ! postThumbnailId ) { | ||
return null; | ||
} | ||
return select( 'core' ).getMedia( postThumbnailId ); | ||
}, | ||
[ postThumbnailId ] | ||
); | ||
|
||
const setPostThumbnail = useCallback( | ||
( mediaId ) => { | ||
// If the image has been removed, we can remove the post thumbnail. | ||
editPost( { featured_media: mediaId } ); | ||
}, | ||
[ editPost ] | ||
); | ||
|
||
return { | ||
postThumbnail, | ||
postThumbnailId, | ||
setPostThumbnail, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters