-
Notifications
You must be signed in to change notification settings - Fork 14
Description
In our app we need to know the dimensions of a specific HTML node after it's inserted into the native DOM. React provides Life Cycle Methods (specifically the componentDidUpdate) to support this.
In LumiGuide@bf9a35e @roelvandijk and I made an initial implementation for this.
We don't think we have the right design yet but at the moment it works for us. So see this as a way to start the design discussion.
API
- We added the event handler registration function:
Text.Blaze.Event.onDomDidUpdate :: (DomNode -> act) -> Attribute actDomNodeis a partial representation of the actual DOM node that was updated:
data DomNode =
DomNode
{ domNodeClassName :: !T.Text
, domNodeId :: !T.Text
, domNodeTagName :: !T.Text
, domNodeBoundingClientRect :: !DomRect
} deriving (Eq, Show)
data DomRect =
DomRect
{ domRectBottom :: !Int
, domRectHeight :: !Int
, domRectLeft :: !Int
, domRectRight :: !Int
, domRectTop :: !Int
, domRectWidth :: !Int
} deriving (Eq, Show)I'm not sure this representation is ideal since we are deciding for the user which properties of the actual DOM node we put in DomNode. It might be better to have:
Text.Blaze.Event.onDomDidUpdate :: (JsObject DOMNode_ -> act) -> Attribute actso that the user can query (and manipulate!) the actual DOM node. This querying and manipulation does have to be performed in IO so users are required to use tell [...] for this.
- Applying the action resulting from
onDomDidUpdatecauses anotheronDomDidUpdateto fire. This causes an infinite loop. To solve this we would like the user to have control over when the virtual DOM gets updated. For this we added theshouldUpdateDomtransition function:
shouldUpdateDom :: Bool -> TransitionM s aThis directly corresponds to shouldComponentUpdate.
When the user calls: shouldUpdateDom False in his transition (and isn't followed by a shouldUpdateDom True) the virtual DOM won't get updated.
Implementation
When the user calls onDomDidUpdate like:
someNode ! onDomDidUpdate handler $ ...someNode will gain the attribute data-life-cycle-id=<fresh life-cycle id> and we remember the "<fresh life-cycle id> -> handler" association. This happens in Text.Blaze.Renderer.ReactJS.
When we receive an update event for the root blaze-react component we loop over all the "life-cycle id -> handler" associations, lookup the DOM node with the corresponding life-cycle id, construct a DomNode and apply the handler to it.
Since blaze-react is using a single component to represent the entire app it means that this component will receive all update events. It would be better to give the user access to the component API so that he can control the depth of each component.
We modified TransitionM to be:
type TransitionM state action = WriterT [IO action] (WriterT (Last Bool) (State state)) ()The Last Bool records the desired shouldUpdateDom behavior.
We went with a nested WriterT implementation instead of a single WriterT layer with a tuple so that we didn't need to modify existing calls to tell in client code (we have quite a few of them).