Created
March 19, 2019 10:22
-
-
Save kmelve/681b21df26c5d5dabdba6cb633880d20 to your computer and use it in GitHub Desktop.
Simple implementation of a youtube embed field with a preview for Sanity.io
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 React from 'react'; | |
| import getYouTubeID from 'get-youtube-id'; | |
| const YouTubePreview = ({ value }) => { | |
| const id = getYouTubeID(value.url); | |
| const url = `https://www.youtube.com/embed/${id}`; | |
| if (!id) { | |
| return <div>Missing YouTube URL</div>; | |
| } | |
| return ( | |
| <iframe | |
| title="YouTube Preview" | |
| width="560" | |
| height="315" | |
| src={url} | |
| frameBorder="0" | |
| allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" | |
| /> | |
| ); | |
| }; | |
| export default { | |
| name: 'youtube', | |
| type: 'object', | |
| title: 'YouTube Embed', | |
| fields: [ | |
| { | |
| name: 'url', | |
| type: 'url', | |
| title: 'URL', | |
| }, | |
| ], | |
| preview: { | |
| select: { | |
| url: 'url', | |
| }, | |
| component: YouTubePreview, | |
| }, | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment