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

Passed down class contains no css-rules and is recognized by compiler as unused, "Unused CSS selector" warning in console #7763

Closed
evgenii-minkov opened this issue Aug 6, 2022 · 5 comments

Comments

@evgenii-minkov
Copy link

Describe the bug

When composing svelte components, a class passed from parent-component to child-component as a prop is

  • successfully passed as a prop
  • contains no css-rules
  • recognized as unused by compiler

image

Reproduction

basic, no-code reproduction

manual reproduction

  1. follow the "getting started" guide at https://kit.svelte.dev/docs/introduction
  2. write 2 svelte-components
/** child-component, child.svelte */
<script>
	let className;
	export { className as class };
</script>

<div class={ className }>some content</div>

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/** parent-component, parent.svelte */
<script>
	import Child from "./child.svelte";
</script>

<style>
    .bg-red { background-color: red; }
    .bg-blue { background-color: blue; }
</style>

<div>
    <Child class="bg-red"/>
    <Child class="bg-blue"/>
</div>
  1. Use these components on some page
/** routes/index.svelte */
<script>
    import Parent from "../components/parent.svelte";
</script>

<Parent/>
  1. Notice that
  • neither passed class is applied
  • both passed classes are present in DOM
  • both passed classes lack css-rules
  • your IDE warns you about classes "bg-red", "bg-blue" being unused

Logs

No response

System Info

System:
    OS: macOS 12.5
    CPU: (10) arm64 Apple M1 Pro
    Memory: 514.44 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
Binaries:
    Node: 18.4.0 - ~/.nvm/versions/node/v18.4.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.4.0/bin/yarn
    npm: 8.12.1 - ~/.nvm/versions/node/v18.4.0/bin/npm
Browsers:
    Chrome: 104.0.5112.79
    Safari: 15.6
npmPackages:
    svelte: ^3.49.0 => 3.49.0

Severity

blocking all usage of svelte

@evgenii-minkov
Copy link
Author

And an additional note:

i tried wrapping a child component in a div as a workaround, and then styling the child through the wrapper-div, like this

<script>
    import Child from "./child.svelte";
</script>

<style>
    .wrapper > div { 
       background-color: red;
    }
</style>

<div class="wrapper"><Child/></div>

and it didn't work - environment's behavior is the same :-(

@baseballyama
Copy link
Member

You cannot specify a class to Svelte component.
See documentation for style scopes.

https://svelte.dev/docs#component-format-style

Also, maybe CSS variables helps you.
https://svelte.dev/docs#template-syntax-component-directives---style-props

@evgenii-minkov
Copy link
Author

hello, sir

ty for responding so quickly

i'll look deeper into theses materials you linked

however, i'm very confused - this example i linked in the "Reproduction" section describes exactly the behavior i wanted to implement:

  • style described in a parent component
  • className defined as a prop in a child component
  • class passed to a child through parent
    and it doesn't work in the docs' REPL either

so, i don't know what conclusions to make? is the example outdated? has it always been wrong? or maybe i misunderstood it and "Passing down classes to component" in example's title means smth else entirely?

@evgenii-minkov
Copy link
Author

oh, i think i figured it out

the example from REPL is NOT an official svelte-team example

it's a random hosted code that Google finds for a "svelte pass class to component" search query

again, ty for your time

@evgenii-minkov
Copy link
Author

for anyone stumbling on this issue

one possible way to implement styling of a child component in svelte is this

aka

/** parent.svelte */
<script>import Child from "..."</script>

<style>
    .parent > :global(.child) {
        ... your styles
    }
</style>

<div class="parent">
    <Child class="child"/>
</div>

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

2 participants