3

I have a custom component which is using another component within it but slotted. The component is set up like this:

<div class="nys-modal">
  <!-- rest of component -->
  <div class="nys-modal_footer">
    <slot name="actions"></slot>
  </div>
</div>

and so the implementation is

<nys-modal>
  <!-- other content not in slot -->
  <div slot="actions">
    <nys-button label="Action 1"></nys-button>
    <nys-button label="Action 2"></nys-button>
  </div>
</nys-modal>

I have a css variable --_nys-button-width set up which I can override in the nys-modal.styles file but it only works if the <nys-button> is set up in the <nys-modal>, not when it is passed in through the slot. I want to override the --_nys-button-width to be 100% but something like the following doesn't work:

::slotted([slot="actions"]) nys-button {
  --_nys-button-width: 100%;
} 

This is only an issue on the slot. I can easily manipulate --_nys-button-width (or any of my other css vars) for the nys-button in the nys-modal component that is not a slot with:

nys-button {
  --_nys-button-width: 100%;
}
2
  • Slotted content is reflected, see long SO answer: stackoverflow.com/questions/61626493/… Commented Oct 2 at 8:59
  • The issue you're encountering depends on the fact that CSS variables flow down the DOM tree (i.e. from host to children), but not up or sideways. ::slotted() selectors only apply to the slotted element itself, not its children. Commented Oct 9 at 19:30

1 Answer 1

0

Try to set the CSS variable on the slotted element itself. That means applying the variable to the <div slot="actions">:

::slotted([slot="actions"]) {
  --_nys-button-width: 100%;
}

Then, inside your nys-button, make sure it inherits the variable:

nys-button {
  width: var(--_nys-button-width, auto);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.