I am updating a part of a page via a standard this.setState
mechanism. I want to grab a hold of a elements that has been changed on a web page and provide a visual feedback to a user.
Let's say we have a component RichText
that gets a data
props. To render rich text it will delegate render to smaller components like Paragraph
, Header
, BulletPoints
, Text
, etc. The final result is a properly rendered rich text.
Later data
props change (e.g. socket push). As a result of that Paragraph
s can be added, or text changed, or things could move around. I want to provide a visual feedback to a user by simply highlighting HTML nodes that were changed.
In a nutshell I want to achieve what Chrome inspector is showing when you are looking at HTML tree. It blinks DOM changes.
ReactJS knows what was changed. Ideally I would like to get an access to that knowledge.
While smaller Components like Paragraph
could be responsible for highlighting a difference within themselves, I don't think they have enough of a knowledge of the outside world to make it work as expected.
Format (simplified version)
{
content: [{
type: 'Document',
content: [{
type: 'Paragraph',
content: [{
type: 'Text',
text: 'text text'
}, {
type: 'Reference',
content: 'text text'
},
]}, {
type: 'BulletPoints',
content: [{
type: 'ListEntry', content: [{
type: 'Paragraph', content: [{
type: 'Text',
text: 'text text'
}, {
type: 'Reference',
content: 'text text'
}]
}]
}]
}]
My current solution
I have a top level Component that knows how to render the entire Document
by delegating job to other components. I have a live version HOC of it: LiveDocument
that is responsible for a change visualization.
So I capture DOM before setState
and after setState
. Then I am using HtmlTreeWalker to spot a first difference (ignoring certain elements as I walk the tree).