Comments (4)
Thank you very much for the contribution.
from react-portabletext.
Where are you running into issues? A lot of my projects use Next.js and Sanity and I haven't had any problems with the documentation. The only challenging part I think would be building a custom block for YouTube videos. Have you read this article on it?
from react-portabletext.
Please share your example I managed to render videos but with the obsolete package
@sanity/block-content-to-react The guide you are giving me is obsolete. I have problems customizing images and links.
from react-portabletext.
I just threw something together. There may or may not be a better way to do this, but this is super simple. The article is a perfectly fine starting point. I'm using Studio v3, but I don't recall any major differences in this process if you're on v2.
Sanity
First, create your schema.
export default defineType({
name: 'video',
title: 'Video',
type: 'object',
fields: [
{
name: 'videoId',
title: 'Video ID',
type: 'string',
},
// ...other information you want to collect (title, video options, etc.)
],
});
Then add it to your main schema export like you would for any others.
No example, this is standard Sanity use.
Now add the video type to the array for your block content.
{
name: 'description',
title: 'Description',
type: 'array',
of: [{ type: 'block' }, { type: 'video' }],
},
Your video object will appear in the block content pane.
Get the ID from your YouTube video and add it to a new video block.
And that's all you need to do from a Sanity standpoint. If that's confusing, you should go back through their docs since that's the very basics.
Next.js and @portabletext/react
I haven't experimented with custom component keys until now--so if I can do it, so can you! :)
Update the PortableText components object to show a custom YouTube embed component
I just installed react-youtube
for this:
-
npm i react-youtube
-
Fetch your block content resource from Sanity using whichever method you choose, and pass it to
PortableText
. -
Add YouTube component to types object. The key should match the name of the object type we previously created--in this case
video
. The{value}
parameter is the data from that object, so we can easily access the video ID we stored in thevideoId
field.
import YouTube from 'react-youtube';
// ...
<PortableText
value={blockContentFetchedFromSanity}
components={{
marks: { ... },
blocks: { ... },
// What we're interested in
types: {
video: ({value}) => <YouTube videoId={value.videoId} />
}
}}
/>
// ...
Celebrate
You should now see the video on your front end!
Let me know if you're confused about any of this, and I'll be happy to point you in the direction of resources.
from react-portabletext.
Related Issues (20)
- Build Fail due to ReferenceError: createElement is not defined HOT 6
- unknownBlockStyle is not working / does nothing
- Children prop no longer available in custom block content HOT 1
- Marks Not Rendering HOT 1
- Named export 'LIST_NEST_MODE_HTML' not found. HOT 2
- typescript example blog rendering of audio player and code block HOT 1
- Can't do custom components HOT 3
- Cannot render elements with `type` of `text` or `span` using `types` HOT 4
- Can't get item position a list HOT 1
- Dependency Dashboard
- Anchored Headings for Portable Text HOT 2
- Version 2.0.1 breaks backwards compatibility HOT 3
- Can't Get Component to work HOT 1
- ReactServerComponentsError when using with Dynamic Routing in Next.js 13.4 App Router configuration HOT 1
- Nested Lists? HOT 2
- react-portable-text has no property 'toPlainText" HOT 4
- PortableText Typescript Error HOT 5
- Cannot render embedded image HOT 2
- [Feature request] Add a component wrapper prop
- Issues using with Nextjs and tailwind HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-portabletext.