Skip to content

Instantly share code, notes, and snippets.

@kmelve
Created March 19, 2019 10:22
Show Gist options
  • Select an option

  • Save kmelve/681b21df26c5d5dabdba6cb633880d20 to your computer and use it in GitHub Desktop.

Select an option

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
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