Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

[✨] Allow classes with Slots for selecting with global() #93

Closed
adaliszk opened this issue Feb 19, 2023 · 2 comments
Closed

[✨] Allow classes with Slots for selecting with global() #93

adaliszk opened this issue Feb 19, 2023 · 2 comments
Labels
[STAGE-2] incomplete implementation Remove this label when implementation is complete [STAGE-2] not fully covered by tests yet Remove this label when tests are verified to cover the implementation [STAGE-2] unresolved discussions left Remove this label when all critical discussions are resolved on the issue [STAGE-3] docs changes not added yet Remove this label when the necessary documentation for the feature / change is added [STAGE-3] missing 2 reviews for RFC PRs Remove this label when at least 2 core team members reviewed and approved the RFC implementation

Comments

@adaliszk
Copy link

adaliszk commented Feb 19, 2023

Is your feature request related to a problem?

When building components that rely on slots and Shadow DOM, I used various selectors and tricks with the slot element before; however, the CSS options became pretty limited when using the q:slot and the Slot component.

One trick I have used before is to assign a class to a slot and select the slotted elements only in that:

https://stackblitz.com/edit/lit-classed-slot-selector?file=src%2Fmy-element.ts

import { css, html, LitElement } from 'lit'

export class SlotElementSelector extends LitElement
{
    static styles = css`
        slot.something::slotted(*) {
            color: red;
        }
    `
    
    render() {
        return html`
            <slot class="something" name="something"></slot>
            <slot></slot>
        `
    }
}

However, given how the Slot element only allows name field and it is not an actual element, I cannot do this anymore:

https://stackblitz.com/edit/qwik-classed-slot-selector?file=src%2Froutes%2Findex.tsx

import { Slot, useStylesScoped$, component$ } from '@builder.io/qwik';

export const SlotSelector = component$(() => {
  // language=css
  useStylesScoped$(`
    .something > :global(*) {
      color: red;
    }
  `);

  return (
    <main>
      <Slot class={'something'} name={'something'} />
      <Slot />
    </main>
  );
});

export default component$(() => {
  return (
    <SlotSelector>
      <h1 q:slot="something">I am in the "something" slot</h1>
      <h2>I am in the default slot</h2>
    </SlotSelector>
  );
});
Type '{ class: string; name: string; }' is not assignable to type 'IntrinsicAttributes & { name?: string | undefined; }'.
  Property 'class' does not exist on type 'IntrinsicAttributes & { name?: string | undefined; }'.(2322)

Describe the solution you'd like

Given how the Slot element is not an actual slot, the path I can see would be to allow the class property and automatically attach that to all slotted elements within the particular slot. That way, the :global() could have a valid selector.

<SlotSelector>
    <h1 q:slot="something">I am in the "something" slot</h1>
    <h2>I am in the default slot</h2>
</SlotSelector>

would render as:

<main>
    <h1 q:slot="something" class="⭐️p23nm7-1 something ">I am in the "something" slot</h1>
    <h2>I am in the default slot</h2>
</main>

Describe alternatives you've considered

The workaround is to wrap each slot into an extra parent and use that for selecting slotted elements. While it works, it introduces additional entries in the DOM that should not be needed in the first place. Furthermore, it complicates the necessary CSS as selectors such as :empty would see the host component as filled even if no elements were slotted.

Alternatively, one could drop Slots altogether and use props, but I consider that an anti-pattern.

Additional context

The main reason why this is important for me is that I want to use CSS Grid to align slotted elements when the host element is not empty. When nothing is passed into the slots, the whole component renders nothing, effectively removing itself from a bigger component's layout.

@stackblitz
Copy link

stackblitz bot commented Feb 19, 2023

@adaliszk adaliszk changed the title [✨] Allow classes with Slots for [✨] Allow classes with Slots for selecting with global() Feb 19, 2023
@gioboa
Copy link
Member

gioboa commented Oct 14, 2024

We moved this issue to qwik-evolution repo to create a RFC discussion for this.
Here is our Qwik RFC process thanks.

@gioboa gioboa transferred this issue from QwikDev/qwik Oct 14, 2024
@github-actions github-actions bot added [STAGE-2] incomplete implementation Remove this label when implementation is complete [STAGE-2] not fully covered by tests yet Remove this label when tests are verified to cover the implementation [STAGE-2] unresolved discussions left Remove this label when all critical discussions are resolved on the issue [STAGE-3] docs changes not added yet Remove this label when the necessary documentation for the feature / change is added [STAGE-3] missing 2 reviews for RFC PRs Remove this label when at least 2 core team members reviewed and approved the RFC implementation labels Oct 14, 2024
@QwikDev QwikDev locked and limited conversation to collaborators Oct 14, 2024
@gioboa gioboa converted this issue into discussion #169 Oct 14, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
[STAGE-2] incomplete implementation Remove this label when implementation is complete [STAGE-2] not fully covered by tests yet Remove this label when tests are verified to cover the implementation [STAGE-2] unresolved discussions left Remove this label when all critical discussions are resolved on the issue [STAGE-3] docs changes not added yet Remove this label when the necessary documentation for the feature / change is added [STAGE-3] missing 2 reviews for RFC PRs Remove this label when at least 2 core team members reviewed and approved the RFC implementation
Projects
None yet
Development

No branches or pull requests

2 participants