Skip to content

fix: pass props down to styles - #522

Merged
danielroe merged 6 commits into
nuxt-modules:mainfrom
concisionuk:main
Oct 17, 2022
Merged

fix: pass props down to styles#522
danielroe merged 6 commits into
nuxt-modules:mainfrom
concisionuk:main

Conversation

@aadgrant

Copy link
Copy Markdown
Contributor

I needed props passed down to my Styles elements and Styles serializers (e.g. p, h2, h3, h4, blockquote, etc.) so I've tweaked this in Sanity Content.

Example use case - previously the below blocks would not render a class="hello-world" on <blockquote />

const blocks = [
  {
    _key: '03ed74fca82b',
    _type: 'block',
    class: 'hello-world',
    children: [],
    markDefs: [],
    style: 'blockquote'
  }
]

<blockquote class="hello-world"></blockquote>

I largely copied the code for the existing render() function, only tweak being to filter out markDefs from props.

markDefs aren't required or passed down to the Element - if we wanted to change this, this code could be used:

const props = Object.fromEntries(Object.entries(item).filter(([key]) => key !== '_type').map(
    ([key, value]) => {
      if (key === '_key') return ['key', value || null]
      **if (key === 'markDefs') return ['markDefs', value?.length ? value : null]**
      if (!isElement || validAttrs.includes(key)) return [key, value]
      return []
    },
  ))

@netlify

netlify Bot commented Oct 12, 2022

Copy link
Copy Markdown

Deploy Preview for nuxt-sanity-module canceled.

Name Link
🔨 Latest commit 2fd90af
🔍 Latest deploy log https://app.netlify.com/sites/nuxt-sanity-module/deploys/634c22dab9b3560007ba881c

@danielroe

Copy link
Copy Markdown
Collaborator

Would you add some snapshot tests to cover these changes? 🙏

@aadgrant

aadgrant commented Oct 12, 2022

Copy link
Copy Markdown
Contributor Author

Added tests, hope these are suitable!

Also added 'class' to validAttrs - not sure if this was excluded for any reason, but it means <p class="hello-world"> can render when a class is provided within the Sanity Blocks.

Comment on lines +111 to 125
function renderStyle (item: Block, serializers: Required<Serializers>, children?: () => Children) {
const { style, listItem } = item
const serializer = serializers.styles[style]
const isElement = typeof serializer === 'string'
const props = Object.fromEntries(Object.entries(item).filter(([key]) => key !== '_type' && key !== 'markDefs' && key !== 'children').map(([key, value]) => {
if (key === '_key')
return ['key', value || null]
if (!isElement || validAttrs.includes(key))
return [key, value]
return []
}))

if (!listItem && style && serializer) {
return h(serializer as any, props, isVue2 ? children?.() : { default: children })
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function renderStyle (item: Block, serializers: Required<Serializers>, children?: () => Children) {
const { style, listItem } = item
const serializer = serializers.styles[style]
const isElement = typeof serializer === 'string'
const props = Object.fromEntries(Object.entries(item).filter(([key]) => key !== '_type' && key !== 'markDefs' && key !== 'children').map(([key, value]) => {
if (key === '_key')
return ['key', value || null]
if (!isElement || validAttrs.includes(key))
return [key, value]
return []
}))
if (!listItem && style && serializer) {
return h(serializer as any, props, isVue2 ? children?.() : { default: children })
}
function renderStyle (item: Block, serializers: Required<Serializers>, children?: () => Children) {
const serializer = item.style && serializers.styles[item.style]
const isElement = typeof serializer === 'string'
const props = Object.fromEntries(
Object.entries(item)
.filter(([key]) => key !== '_type' && key !== 'markDefs' && key !== 'children')
.map(([key, value]) => {
if (key === '_key') return ['key', value || null]
if (!isElement || validAttrs.includes(key)) return [key, value]
return []
}),
)
if (!item.listItem && item.style && serializer) {
return h(serializer as any, props, isVue2 ? children?.() : { default: children })
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next time, please do tick the box to allow maintainers to commit changes directly to the PR branch. I'm remaking this PR ~> #523

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, didn't realise that was a thing, will do in the future! :)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem at all. Also very happy for you to pull in my commits on the other branch into this one so you get proper credit ❤️

@aadgrant aadgrant Oct 16, 2022

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I think I've managed to do that! 😅

Comment thread src/runtime/components/sanity-content.ts Outdated

@danielroe danielroe left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! ❤️

@danielroe danielroe changed the title Pass props down to styles (e.g. p, h3, h4, blockquote, etc.) fix: pass props down to styles Oct 17, 2022
@danielroe
danielroe merged commit 93c48f4 into nuxt-modules:main Oct 17, 2022
This was referenced Aug 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants