Issue with serializer not applying styles to list items
15 replies
Last updated: Apr 14, 2020
C
Hello, I'm trying to add some styles to my list items, but my serializer is not working. Not sure why, any one spot the error? My serializer:
Full serializer at
https://gist.github.com/AndreasJacobsen/de9a9eb192c8face517f9fd21313f974 , right now I'm trying to add the classname liststyle to a all ul elements in a block content component
listItem: props => { switch (props.node.listItem) { case "bullet": { return <ul className="liststyle">{props.children}</ul> } case "number": { return <ol>{props.children}</ol> } default: { return <li>{props.children}</li> } } },
https://gist.github.com/AndreasJacobsen/de9a9eb192c8face517f9fd21313f974 , right now I'm trying to add the classname liststyle to a all ul elements in a block content component
Apr 14, 2020, 7:39 PM
M
Hi Andreas, does it work without the class? The code looks correct to me but you could try defining the components separately and replacing with something like . Perhaps it doesn’t like the quotation marks in the current approach, although that shouldn’t matter.
<ul className="liststyle">{props.children}</ul>
<UlItem>{props.children}</UlItem>
Apr 14, 2020, 8:03 PM
C
I'm getting "ULItem is not defined", so it seems to be ish working, but I cannot get the className, it's just an empty className
Apr 14, 2020, 8:08 PM
C
if I change it to say blockquote it stays as a ul without a css class, so the serializer is not hitting my bullet point list. Even thoug the graphQl tells me this is of type bullet
My sanity blockContent file has the following for lists
My sanity blockContent file has the following for lists
lists: [
{ title: 'Bullet', value: 'bullet' },
{ title: 'Numbered', value: 'numbered' },
],
Apr 14, 2020, 8:10 PM
M
The GraphQL output and
blockContentcontent for lists look OK to me. Does it work if you remove all other serializers and just leave the
listItemone?
Apr 14, 2020, 8:15 PM
C
does not work with only listItems either
Apr 14, 2020, 8:18 PM
M
Just to check a silly little thing: if you change to
<ul className="liststyle">{props.children}</ul>
<p>{props.children}</p>, are you seeing any change?
Apr 14, 2020, 8:19 PM
C
no, not seing any change at all
I'm calling it like this
with an import of course
I'm calling it like this
<StatementBlock blocks={data.sanityAccessibilityStatement._rawStatement} />
import StatementBlock from "../components/Sanity/blockContent"
Apr 14, 2020, 8:21 PM
M
Hmm I’m having a hunch. I think you’re missing a list renderer. Here’s a basic example just to confirm it’s doing something:
const ListRenderer = ({ children }) => { return <ul>{children}</ul>; }; const ListItemRenderer = ({ children }) => { return <li>{children}</li>; }; const serializers = { ..., list: ListRenderer, listItem: ListItemRenderer, ... }
Apr 14, 2020, 8:27 PM
C
Still the same error, I updated the gist with the code you so generously provided https://gist.github.com/AndreasJacobsen/de9a9eb192c8face517f9fd21313f974
Apr 14, 2020, 8:31 PM
C
but I also want to support numbered lists (edit, I can just make a switch case inside the listRender I realized)
Edit2: I updated the gist to contain more files, I just don't understand why this is not working. feel free to reply tomorrow instead though!
Edit2: I updated the gist to contain more files, I just don't understand why this is not working. feel free to reply tomorrow instead though!
Apr 14, 2020, 8:32 PM
M
Just tested the above code locally and can confirm it’s working, also with a class name on the
ulelement. Are the other serializers working for you?
Apr 14, 2020, 8:38 PM
C
no, I updated the gist to also include the file where I'm using the blockContent compontent, I can add/remove classnames the titles and the other elements defined over listItem
Apr 14, 2020, 8:39 PM
M
Nasty 🤓 It doesn’t give any errors because the missing bracket is added at the end, but it’s enough to make it all fail. There’s one closing bracket missing to close off
Try this:
typesand there’s one extra bracket at the very end of
serializersif you do close off
typesbefore.
Try this:
const serializers = { types: { block(props) { switch (props.node.style) { case "h1": return <h1 className="title is-1 introTitle">{props.children}</h1> case "h2": return <h2 className="title is-2 introTitle">{props.children}</h2> case "h3": return <h3 className="title is-3 introTitle">{props.children}</h3> case "h4": return <h4 className="title is-4 introTitle">{props.children}</h4> case "blockquote": return <blockquote>{props.children}</blockquote> default: return <p>{props.children}</p> } } }, list: ListRenderer, listItem: ListItemRenderer, figure(props) { return <Figure image={props.node} /> }, marks: { internalLink: ({ mark, children }) => { const { slug = {} } = mark const href = `/${slug.current}` return <Link to={href}>{children}</Link> }, externalLink: ({ mark, children }) => { const { blank, href } = mark return blank ? ( <a href={href} target="_blank" rel="noopener noreferrer"> {children} </a> ) : ( <a href={href}>{children}</a> ) } } }
Apr 14, 2020, 8:55 PM
C
It works :party_parrot: :party_parrot: :party_parrot: :party_parrot:Thank you Peter, you are amazing
Apr 14, 2020, 8:56 PM
C
I may have over estimated my switch case abilities, do you know how I can write a switch case for the list items?
Edit: using children.props.node.listItem I get an error stating "Cannot read property 'node' of underfined
const ListRenderer = ({ children }) => {
switch (children.props.node.listItem) {
case "bullet":
return <ul _className_="liststyle">{children}</ul>
default:
return (
<ol>
{children}
</ol>
)
}
}WIth this all items are returned as <ol> lists (default), even the ones that are supposed to be unordered lists
Edit: using children.props.node.listItem I get an error stating "Cannot read property 'node' of underfined
Apr 14, 2020, 9:30 PM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.