Skip to content

Commit

Permalink
feat(react): allow react renderer to assign attributes to react rende…
Browse files Browse the repository at this point in the history
…rer element (ueberdosis#3812)
  • Loading branch information
bdbch committed Mar 3, 2023
1 parent 3702277 commit 6ce5aad
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/ReactNodeViewRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface ReactNodeViewRendererOptions extends NodeViewRendererOptions {
| null
as?: string
className?: string
attrs?: Record<string, string>
}

class ReactNodeView extends NodeView<
Expand Down Expand Up @@ -103,6 +104,7 @@ class ReactNodeView extends NodeView<
props,
as,
className: `node-${this.node.type.name} ${className}`.trim(),
attrs: this.options.attrs,
})
}

Expand Down
8 changes: 8 additions & 0 deletions src/ReactRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface ReactRendererOptions {
props?: Record<string, any>,
as?: string,
className?: string,
attrs?: Record<string, string>,
}

type ComponentType<R, P> =
Expand Down Expand Up @@ -50,6 +51,7 @@ export class ReactRenderer<R = unknown, P = unknown> {
props = {},
as = 'div',
className = '',
attrs,
}: ReactRendererOptions) {
this.id = Math.floor(Math.random() * 0xFFFFFFFF).toString()
this.component = component
Expand All @@ -62,6 +64,12 @@ export class ReactRenderer<R = unknown, P = unknown> {
this.element.classList.add(...className.split(' '))
}

if (attrs) {
Object.keys(attrs).forEach(key => {
this.element.setAttribute(key, attrs[key])
})
}

this.render()
}

Expand Down

0 comments on commit 6ce5aad

Please sign in to comment.