Issue with rendering bulleted lists in portable text using react-portable-text package
23 replies
Last updated: Apr 20, 2021
C
Afternoon! I am struggling to parse lists via portable text. Im using the react-portable-text package and seem to be able to add custom classNames to h2's without any issues. However all of the bulleted list items in my unordered list are rendering to the dom as p tags and I cant seem to get the serializer to impact them. Any tips are appreciated! https://jmp.sh/S4NZ2qn
Apr 20, 2021, 1:05 AM
A
Can’t say for sure but one thing comes to mind — you don’t have any serialisers for
ulor
olso you effectively only serialising list items
Apr 20, 2021, 1:13 AM
C
Thanks, I was just playing with the idea of add a UL serializer. The odd thing is that they arent being picked up because each list item is being output as a P tag and there is no actual UL being output (even without any serializers).
Apr 20, 2021, 1:15 AM
C
Apr 20, 2021, 1:16 AM
A
What if you set your
liserialiser to something crazy?
h1
Apr 20, 2021, 1:16 AM
A
will it use the h1 then?
Apr 20, 2021, 1:16 AM
C
No, it doesnt seem to change anything
Apr 20, 2021, 1:17 AM
G
Does your list actually have a value of
liin Sanity? Usually you’ll serialize
bulletor
number. https://www.sanity.io/docs/block-type#lists-59b2751e6a05
Apr 20, 2021, 1:17 AM
A
I had a problem once with pasting a list from google docs…bullets were there but it wasn’t actually a list. Can this be a problem?
Apr 20, 2021, 1:19 AM
C
You might be on to something... I used the built in little bullet option to create the list and this is what is being output in the inspect: https://jmp.sh/fWfg4iK
Apr 20, 2021, 1:19 AM
C
I was basically just following the docs here to change the li's: https://www.sanity.io/plugins/react-portable-text
Apr 20, 2021, 1:20 AM
C
user A
Does that mean that you generally would select the items in the serializer with "bullet"Apr 20, 2021, 1:23 AM
G
Yes, something like:
const serializers = { lists: { bullet: ({ children }) => <li style={{ color: 'red' }}>{children}</li> } }
Apr 20, 2021, 1:25 AM
A
This indeed peculiar. The types of “list items” are blocks…
Apr 20, 2021, 1:25 AM
A
But the docs clearly refer to
uland
li. As well as the code itself all the way down to the internal hyperscript package
Apr 20, 2021, 1:26 AM
A
got it…
react-portable-textis a third party plugin
Apr 20, 2021, 1:28 AM
A
https://github.com/sanity-io/block-content-to-react is the “official” one
Apr 20, 2021, 1:28 AM
A
and the official one has
listand
listItemserialisers 🤷
Apr 20, 2021, 1:29 AM
C
Interesting, I also saw a note that you will want to use the "raw" selectors in the graphql query. Will try this now combined with that serializer to see if it helps.
Apr 20, 2021, 1:30 AM
A
I went through the code and it doesn’t make sense to me that list items are
_type: "block"
Apr 20, 2021, 1:35 AM
A
The official library expects it to be
_type: "list"
Apr 20, 2021, 1:36 AM
A
But I might be missing something. I’d trust
user A
more than me to be honest:)Apr 20, 2021, 1:36 AM
C
Thanks for the help! I had tried the official plugin but switched to react-portable-text after quite a bit of fighting with it. Turns out that in both cases (block-content-to-react and react-portable-text) you needed to query the _raw version of the portable text in graphql for it to renders lists instead of paragraphs. This ultimately was the key for this to work: https://www.sanity.io/plugins/react-portable-text
Apr 20, 2021, 2:07 AM
G
Great to hear you got this working, Chad! 🙌
Apr 20, 2021, 2:14 AM
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.