Forms and shadowDOM
Improve the interaction with the forms and accessibility of your components.
It is normal that we use the technique of dispatching events from the component to communicate states and more, but when using forms this changes since the events inside the shadowDOM do not interact with the form outside of it, example:
1
<form>
2
<my-input>
3
#shador-root
4
<input/>
5
</my-input>
6
</form>
Copied!
To solve this we will have to nest an input tag in the lightDOM of our component, in order to reflect the logic of this to the form. Atomico facilitates this hybrid interaction between lightDOM and shadowDOM with the @atomico/hooks/use-render hook that allows executing a second render that works from the lightDOM, example:
1
import { useRender } from "@atomico/hooks/use-render";
2
3
function myInput(){
4
// render LightDOM
5
useRender(()=><input/>);
6
7
// render ShadowDOM
8
return <host shadowDom>
9
<slot/>
10
</host>
11
}
Copied!
With this you have gained full control over the existing input tag in the lightDOM, allowing you to apply styles using the ::slotted selector, example:
1
myInput.styles = css`
2
:slotted(input){
3
border: 1px solid black;
4
height: 40px;
5
}
6
`;
Copied!
We have created an input tag that allows you to interact directly with the form, this technique is applicable with all the tags that interact with the form, such as button, input, textarea and others.
Last modified 5mo ago
Copy link
Edit on GitHub