Target element is not rendered
You will see the browser api throw an Target element is not rendered
exception when you attempt to commitStyles
on an element that is not rendered in the DOM.
Most likely the element you're attempting to set commitStyles
on has a CSS rule that makes the element not rendered.
IE display: none
You guard against the execption by checking if the element is rendered.
This answer goes into more detail on sensible ways to check if an element is rendered.
Example/simulation
In general you could check if the element is rendered right before calling commitStyles
const divElem1 = document.querySelector(".item");
const toggleClass = () => {
divElem1.classList.toggle("display-none");
}
function isHidden(el) {
return (el.offsetParent === null)
}
let isSafeguarding = true;
const toggleSafeguard = (e) => {
isSafeguarding = !isSafeguarding
e.target.textContent = `safeguard is: ${isSafeguarding?'ON':'OFF'}`
}
document.body.addEventListener("mousemove", (evt) => {
const anim1 = divElem1.animate({
transform: `translate(${evt.clientX}px, ${evt.clientY}px)`
}, {
duration: 500,
fill: "forwards"
});
if (!isSafeguarding || !isHidden(divElem1)) {
anim1.commitStyles();
}
});
.item {
padding: 50px;
display: inline-block;
border: 2px solid navy;
background-color: azure;
position:absolute;
}
.item.display-none {
display: none;
}
body {
background-color: PeachPuff;
width: 100vw;
height: 100vh;
}
button {
margin: 6px;
}
<div class="item one">moves to pointer</div>
<div>
<div>Hide the element and turn off the safeguard to see an error.</div>
<button onClick="toggleClass()">toggle display:none</button>
<button onClick="toggleSafeguard(event)">safeguard is: ON</button>
</div>
Closing notes:
commitStyles
is relatively new. Adoption in mayor browsers started around 2020.
This might be why still little documentation is available from the community.
In a sense you're pionering the documentation for future google searches for this specific error .
If you fancy reading the chromium source
you can see the exact place and condition for the error to be thrown.
If you fancy reading the spec, there's an entry specific to commit styles