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

Add usePostThumbnail hook #260

Merged
merged 1 commit into from
Feb 27, 2024
Merged
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
49 changes: 49 additions & 0 deletions src/hooks/usePostThumbnail/README.md
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from within a what?


## Usage

The `usePostThumbnail` hook provides 2 values, and a setter function
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs punctuation at the end


```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`
50 changes: 50 additions & 0 deletions src/hooks/usePostThumbnail/index.js
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,
};
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export { default as useActiveBlockStyle } from './hooks/useActiveBlockStyle';
export { default as useBlockStyles } from './hooks/useBlockStyles';
export { default as useDisallowedBlocks } from './hooks/useDisallowedBlocks';
export { default as useMeta } from './hooks/useMeta';
export { default as usePostThumbnail } from './hooks/usePostThumbnail';
export { default as useRenderAppenderWithBlockLimit } from './hooks/useRenderAppenderWithBlockLimit';
export { default as useSelectBlock } from './hooks/useSelectBlock';
export { default as useSetAttribute } from './hooks/useSetAttribute';
Expand Down
Loading