Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need access to the component's host CSSStyleDeclaration via this.style. #3999

Open
sgesin opened this issue Feb 15, 2024 · 6 comments · May be fixed by #4044
Open

Need access to the component's host CSSStyleDeclaration via this.style. #3999

sgesin opened this issue Feb 15, 2024 · 6 comments · May be fixed by #4044

Comments

@sgesin
Copy link

sgesin commented Feb 15, 2024

Description

This is more common with Light DOM components but is something that can happen with Shadow DOM components depending on how you try to access the host's CSSStyleDeclaration.

I noticed that when using Shadow DOM components, I could do things like add custom CSS properties to the host by doing something like:

const hostStyle = this.template.host.style;
hostStyle.setProperty('--myProperty', 'red');

It seemed natural to be able to do something similar for Light DOM components like:

this.style.setProperty('--myProperty', 'red');

However I found that this.style is always undefined in the Light DOM component. This is also preventing me from being able to get the value of a custom CSS property:

this.style.getPropertyValue('--myProperty');

Steps to Reproduce

This playground helps reproduce the issue:
https://stackblitz.com/edit/salesforce-lwc-ndqrwq?file=src%2Fmodules%2Fx%2Fapp%2Fapp.html

It also includes a suggested workaround which is to get to the host element via refs or children:

const hostStyle = this.children[0].parentElement.style;

This will only work after renderedCallback.

Expected Results

We should be able to access the host's CSSStyleDeclaration via this.style.

Actual Results

The value of this.style is always undefined.

Browsers Affected

All

Version

  • LWC: 3.6.0, 6.1.1 (present in both versions)

Possible Solution

Additional context/Screenshots

@nolanlawson nolanlawson added enhancement Up for grabs Issues that are relatively small, self-contained, and ready for implementation labels Feb 15, 2024
@le-incroyable1-dev
Copy link

Hi @nolanlawson and everyone, I'm trying to work on this issue, I've worked with LWC before but it's the first time I'm trying to contribute here. Could anyone guide me about how I would go about this?

@nolanlawson
Copy link
Contributor

Thanks for your interest @le-incroyable1-dev! I'd recommend looking at how we implemented tagName: #3642

This was a similar scenario where we were just missing a mapping from this.<propName> to element.<propName>.

@le-incroyable1-dev
Copy link

I keep getting this build error in the engine server, can't understand what the error message means, hope someone can help!

image

@nolanlawson
Copy link
Contributor

@le-incroyable1-dev Any new API added to the renderer.ts in engine-core must also be added to the same renderer in engine-dom and engine-server. 🙂

@le-incroyable1-dev
Copy link

@nolanlawson thanks for all the help. I've raised a PR for this, please get it reviewed and let me know about any problems!

@nolanlawson nolanlawson removed the Up for grabs Issues that are relatively small, self-contained, and ready for implementation label Apr 11, 2024
@nolanlawson
Copy link
Contributor

No longer up-for-grabs since this is slated for the v7 release ^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants