I am fairly new to javascript and been playing around with web-components. I wanted to access the CSS color attribute of an element inside the shadow DOM of a web-component. When I use the getComputedStyle() method I am unable to access the style property on running it in the connectedCallback.
Here I am trying to access the color property, on logging the value to the console it shows RGB(0, 0, 0) whereas upon waiting for a millisecond and then logging, the correct value of RGB(0, 128, 0) shows up. Why is this happening?
I presume it is because the styles haven't been computed yet when I am running the function for the first time? What is a more elegant solution to this? How can I wait exactly till the styles have been computed to run my function, instead of some arbitrary time that I am specifying now?
JS
document.addEventListener('DOMContentLoaded',()=>{
class CustomComponent extends HTMLElement{
constructor(){
super();
this.attachShadow({mode:'open'});
const template=document.querySelector('#component');
this.shadowRoot.appendChild(template.content.cloneNode(true));
};
connectedCallback(){
console.log('Element added to DOM');
let text=this.shadowRoot.querySelector('.text');
console.log(getComputedStyle(text).getPropertyValue('color'));
setTimeout(()=>{console.log(getComputedStyle(text).getPropertyValue('color'))},1)
};
};
customElements.define('custom-component',CustomComponent);
});
CSS
.container{
--color-variable:#f2c846;
}
.text{
color:green;
}
HTML
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Random title</title>
<script src='./CustomComponent.js'></script>
</head>
<body>
<template id='component'>
<link rel='stylesheet' href='./CustomComponent.css'/>
<div class=container>
<div class='text'>
Colored Text
</div>
</div>
</template>
<custom-component>
</custom-component>
</body>
</html>