Last active
September 30, 2024 09:03
-
-
Save anova/50e7c97815332bb4301e60507f1e21b2 to your computer and use it in GitHub Desktop.
Custom media upload component for Wordpress Gutenberg.
This file contains hidden or 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
import { MediaUpload } from '@wordpress/block-editor'; | |
import { Button } from '@wordpress/components'; | |
function CustomMediaUpload(props){ | |
return <MediaUpload | |
onSelect={ media => { props.onMediaSelected(media.url) } } | |
type="image" | |
value={ props.mediaUrl } | |
render={ | |
({open}) => { | |
if(props.mediaUrl) { | |
return <> | |
<img src={props.mediaUrl} /> | |
<hr/> | |
<Button onClick={ () => { props.onMediaSelected(null) }}>Remove Image</Button> | |
</>; | |
} | |
return <Button onClick={open}>Select Image</Button>; | |
} | |
}/>; | |
} | |
export { CustomMediaUpload }; | |
/* | |
Usage: | |
import { CustomMediaUpload } from 'custom-media-upload' | |
<CustomMediaUpload | |
mediaUrl={attributes.mediaUrl} | |
onMediaSelected={ ( value ) => { setAttributes({'mediaUrl': value}) } } | |
/> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment