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

Svelte 5: add :global {...} nested selectors #10815

Closed
Rich-Harris opened this issue Mar 15, 2024 · 0 comments · Fixed by #11276
Closed

Svelte 5: add :global {...} nested selectors #10815

Rich-Harris opened this issue Mar 15, 2024 · 0 comments · Fixed by #11276
Milestone

Comments

@Rich-Harris
Copy link
Member

Describe the problem

Right now the way to write global styles is to wrap each selector in :global(...):

<div>
  <Stuff />
</div>

<style>
  :global(.foo) {
    /* applies to all `.foo` elements on the page */
  }

  :global(.bar) {
    /* applies to all `.bar` elements on the page */
  }

  div :global(.foo) {
    /* applies to all `.foo` elements inside the `<div>` in this component */
  }

  div :global(.bar) {
    /* applies to all `.bar` elements inside the `<div>` in this component */
  }
</style>

Generally, you shouldn't be using pure global selectors much — it's better to put those in a .css file. But it's often useful to have 'scoped global' selectors — it's common to do this sort of thing, for example:

<div class="post">
  {@html post.content}
</div>

<style>
  .post :global(p) {...}
  .post :global(ul) {...}
  .post :global(li) {...}
  .post :global(code) {...}
</style>

Describe the proposed solution

It would be much less irksome to use the recently-added CSS nesting support to do this instead:

<div class="post">
  {@html post.content}
</div>

<style>
  .post :global {
    p {...}
    ul {...}
    li {...}
    code {...}
  }
</style>

Importance

nice to have

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

Successfully merging a pull request may close this issue.

1 participant