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

[docs] No search results for How can we apply CSS styles to nested elements inside the slot #1384

Open
acn-rajesh-paidipati opened this issue Nov 20, 2024 · 4 comments

Comments

@acn-rajesh-paidipati
Copy link

acn-rajesh-paidipati commented Nov 20, 2024

Hi Team,

Could you please help us in applying the CSS to nested elements inside the slot.

Eg:

Render me in a slot

How can we apply the css to span with class redText inside a P tag which is a slot. It would be helpful if you can share some examples. I have already gone through your workarounds, but couldn't execute it.

Workaround 1 - Use Javascript To Style

(1) Access the slotted elements from javascript by using @queryAssignedElements (See https://lit.dev/docs/components/shadow-dom/#query-assigned-nodes)

(2) For each slotted element, parse its dom tree and style the child nodes using javascript (e.g. childelement.style.backgroundColor = '#ff0';)

Workaround 2 - Copy Slotted HTML to Another Div

(1) Access the slotted elements from javascript by using @queryAssignedElements (See https://lit.dev/docs/components/shadow-dom/#query-assigned-nodes)

(2) For every slotted element under consideration, copy-paste its outerHTML to another empty div, placed next to it.

(3) Since these initially empty divs are not from the slots, you can setup static css rules for them.

(4) Empty the slots after the copy pasting into the adjacent divs is complete.

Regards
Rajesh

@sorvell
Copy link
Member

sorvell commented Nov 20, 2024

This is a good question to ask in the Lit Discord channel.

applying the CSS to nested elements inside the slot

This isn't supported in CSS. You can only style slotted elements themselves using :slotted(...). So you have to write some code to do it...

How can we apply the css to span with class redText inside a P tag which is a slot. It would be helpful if you can share some examples.

Here is an example.

Do not use those workarounds, instead if you can't avoid the issue, I recommend copying a style into the parent scope and attempting to scope it so it applies narrowly.

static scopeStyles = css`
  element-name {
    /* whatever makes sense */
  }
`;

connectedCallback() {
  super.connectedCallback();
  const {scopeStyles} = this.constructor as typeof MyElement;
  const {adoptedStyleSheets} = this.getRootNode() as Document;
  if (!adoptedStyleSheets.includes(scopeStyles.styleSheet)) {
    adoptedStyleSheets.push(scopeStyles.styleSheet);
  }
}

@acn-rajesh-paidipati
Copy link
Author

acn-rajesh-paidipati commented Nov 20, 2024 via email

@sorvell
Copy link
Member

sorvell commented Nov 20, 2024

  1. Could this approach have any potential performance implications on the component, especially if used extensively?

I can't guarantee anything, but I wouldn't expect performance issues.

  1. Is it acceptable to inject an entire component's CSS ... we want to ensure there are no significant limitations or drawbacks before proceeding

The main drawback is the need to avoid conflicting with user styling. A more general approach would be to provide a stylesheet along with your elements for users to apply themselves, but if your user base is limited, say to your company, the auto-injection approach may be reasonable.

I'd recommend trying to manually scope the styles to apply inside your elements as much as possible (the example showed a simple approach), and I would also recommend using an @layer to help control the cascade more easily.

@acn-rajesh-paidipati
Copy link
Author

acn-rajesh-paidipati commented Nov 21, 2024 via email

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

No branches or pull requests

2 participants