DEV Community

Thom
Thom

Posted on

3 1

Next Experience Framework: select a shadow DOM node in your actionHandlers

I had the task to write a component where I render a d3js graph. D3.js is a JavaScript library to manipulate documents based on data you provide.

So my goal was, to create the hole graph when the component is rendered. For that we can use some component lifecycle actions. I used the 'COMPONENT_DOM_READY' to achieve my goal.

First we need to import the action type like:

import {createCustomElement, actionTypes} from '@servicenow/ui-core';
const { COMPONENT_DOM_READY } = actionTypes;
Enter fullscreen mode Exit fullscreen mode

Then we can go to our createCustomElement function and add the actionHandlers. We can now create the action handler for the COMPONENT_DOM_READY lifecycle action and with the effects we are also getting the host object.

Let's imagine I have a tag in my view and now I can use the d3.select and the host.shadowRoot.querySelector("svg") to get the right shadow DOM node to do my d3js magic.

actionHandlers: {
    [COMPONENT_DOM_READY]: {
        effect({ state, host }) {
                   const svg = d3.select(host.shadowRoot.querySelector("svg"));
      }
   }
}
Enter fullscreen mode Exit fullscreen mode

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (1)

Collapse
 
freeraga profile image
Raga

Hi Tom,

Thanks for your post, it was very helpful.
I have a following question, I struggle to use the CSS for CDN FontAwesome. After the component is rendered, it seems that the CSS are out of reach. Can you confirm ? I guess you were faced with the same issue with d3 that needs al lot of css to be prettified !

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay