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

Reset layouts still inherit their parents' styles #3622

Closed
GauBen opened this issue Jan 29, 2022 · 4 comments
Closed

Reset layouts still inherit their parents' styles #3622

GauBen opened this issue Jan 29, 2022 · 4 comments

Comments

@GauBen
Copy link
Contributor

GauBen commented Jan 29, 2022

Describe the bug

__layout.reset.svelte does not reset the style stack

Reproduction

/src/routes/__layout.svelte

<h1>Root layout</h1>
<slot/>

<style>
  :global(html) {
    background: skyblue
  }
</style>

/src/routes/nested/__layout(.reset)?.svelte

<h1>Nested layout</h1>
<slot/>

Without .reset:
image

With .reset:
image
(It should not have the blue background)

Logs

No response

System Info

devDependencies:
  '@sveltejs/adapter-auto': 1.0.0-next.17
  '@sveltejs/kit': 1.0.0-next.250_svelte@3.46.3
  svelte: 3.46.3

Severity

annoyance

Additional Information

I'm not sure if this is the intended behavior, but it seems inconsistent

@babichjacob
Copy link
Member

Reason in #1356 (comment)

Is this only when using client side navigation (e.x. by clicking links to go from a page using the root layout to one using the nested layout), or do you still see the unexpected background color after refreshing the page?

@GauBen
Copy link
Contributor Author

GauBen commented Jan 30, 2022

Both, that's why I considered it not being a bug but a feature

@Rich-Harris
Copy link
Member

This is a consequence of how styles work in SvelteKit — if you import a component, its CSS will be applied to the page, regardless of whether it is rendered or not (and it won't be removed when the component is destroyed), because the contents of <style> are ultimately turned into a <link rel="stylesheet">, as if you had an explicit import './styles.css' declaration. You could definitely make the case that this is a bug, but changing that behaviour would make performance worse in the general case.

Styles from the root layout will therefore always apply, because the root layout is always imported eagerly (along with the root error component). You'll have to work around it by avoiding global body styles there.

Realistically this would be very difficult to change, so I'm going to close this issue. (Note that layout resets have been replaced by named layouts.)

@GauBen
Copy link
Contributor Author

GauBen commented Apr 25, 2022

This makes sense, thanks for answering

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

No branches or pull requests

3 participants