Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update ReactRenderer to include CSS class #2166

Closed
wants to merge 1 commit into from
Closed

Conversation

kukagg
Copy link

@kukagg kukagg commented Nov 15, 2021

Includes extension name to root react-renderer node so that styling can be done directly.

@netlify
Copy link

netlify bot commented Nov 15, 2021

✔️ Deploy Preview for tiptap-embed ready!

🔨 Explore the source changes: 8423d8f

🔍 Inspect the deploy log: https://app.netlify.com/sites/tiptap-embed/deploys/6192112090e17f00077afab0

😎 Browse the preview: https://deploy-preview-2166--tiptap-embed.netlify.app

@philippkuehn
Copy link
Contributor

Not sure about this one. You can add a class to your component within ReactRenderer, right?

@kukagg
Copy link
Author

kukagg commented Nov 16, 2021

Thanks!

Yeah, @philippkuehn we can do that but the tree looks like .react-renderer > .custom-component. So the node that has .react-renderer can’t be modified via CSS. My use case is that I have two .react-renderer components and the visuals should be conditionally modified with:

.first-component + .next-component {
  margin-top: -3rem;
}

in this structure:

<div class="react-renderer first-component"></div>
<div class="react-renderer next-component"></div>

This CSS above doesn’t work right now because going up the tree in CSS is not possible so this CSS below doesn’t have access to the .next-component:

.react-renderer > .first-component + ?

as structure is:

<div class="react-renderer">
  <div class="first-component"></div>
</div>
<div class="react-renderer">
  <div class="next-component"></div>
</div>

It’s possible to do it programmatically but not ideal, thus this PR.

Let me know if this makes sense.

@hanspagel
Copy link
Contributor

Thanks for the PR!

The solution feels somehow wrong to me. If we’ll add support for it, it should be configureable and maybe even off by default.

Another nitpick: We use lowerCamelCase for extension names, so the class names would end up like react-renderer taskItem, which I couldn’t take. 😅

@kukagg
Copy link
Author

kukagg commented Nov 16, 2021

Ok, that’s fair @hanspagel. What about adding extension name as a data-extension attr then?

So it would look like:

<div class="react-renderer" data-extension="taskItem" />

@thatsjonsense
Copy link
Contributor

We're currently using a decoration to achieve this as a workaround, but I think it would be easier and more performant if Tiptap did this out of the box. Here's our custom plugin for reference:

Extension.create({
  addProseMirrorPlugins() {
    return [
      new Plugin({
        props: {
          decorations: ({ doc }) => {
            const decorations: Decoration[] = []
            const decorate = (node, pos) => {
              decorations.push(
                Decoration.node(pos, pos + node.nodeSize, {
                  class: `node-${node.type.name}`,
                })
              )
            }
            doc.descendants(decorate)
            return DecorationSet.create(doc, decorations)
          },
        },
      }),
    ]
  },
})

@philippkuehn
Copy link
Contributor

Thanks for this PR but instead of adding the extension name to the ReactRenderer I added a className option. Within ReactNodeViewRenderer I use this option to add a class name based on the extension name.

@kukagg
Copy link
Author

kukagg commented Nov 18, 2021

Awesome, thanks @philippkuehn

andrewlu0 pushed a commit to trybaseplate/tiptap that referenced this pull request Oct 20, 2023
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.

4 participants