question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optionally scope imported CSS files to component level

See original GitHub issue

Describe the problem

Currently, when stylesheets are imported within either a script or style block within a Svelte component their styles are applied globally.

<style>
    @import 'carbon-components-svelte/css/g10.css';
</style>

This is a sensible default behaviour. Some design system libraries etc. need to touch things like the body and html elements.

However, I think this can also be problematic and a limitation in some cases. For example, if you are producing a component library you often want to make sure that all of your styles are encapsulated and do not wreak havoc with the parent webpage/app styling.

Describe the proposed solution

A solution for this currently exists in Vue, where you can do something like:

<style scoped src="@/assets/css/main.css"></style>

…and it wil scope the contents of the CSS file. This is very ergonomic.

I want to be clear, the extent of this request is in relation to imported external CSS files. In researching this issue, I came across some rather in-depth debates as regards providing a scoped/unscoped modifier for the style block. I am not trying to rehash this debate here and I’m not wedded to this particular approach, I’m just using it as an example of an equivalent functionality in another popular framework.

Futhermore, given that Svelte does not wrap components in a generic div in the generated HTML I can see reasons why this may be difficult to implement. But given the potential benefits for users I thought I should ask the question at least.

Alternatives considered

  1. Don’t implement it: In this case, I think the current behaviour could at least be better documented. Given that most people associate Svelte with effective style encapsulation, this behaviour with imported CSS can come as a surprise to people, certainly it took me a minute to work it out.
  2. Perhaps this can be solved in userland with a preprocessor? I tried to see if I could get this to work with PostCSS but it’s proved beyond my skills so far.

Importance

would make my life easier

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:8
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Conduitrycommented, Jan 12, 2022

This would have to be some sort of preprocessor, because the core compiler just takes a single string as input and doesn’t look at the filesystem at all.

0reactions
rodochcommented, Nov 22, 2022

This is a great solution - many thanks! I think that closes this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

import - CSS: Cascading Style Sheets - MDN Web Docs
The @import CSS at-rule is used to import style rules from other valid stylesheets. An @import rule must be defined at the top...
Read more >
Basic Features: Built-in CSS Support - Next.js
This behavior makes CSS Modules the ideal way to include component-level CSS. CSS Module files can be imported anywhere in your application.
Read more >
Component scoped css modules in React Js - LearnBestCoding
CSS modules enable CSS classes to scope to the component level. It allows us to solve CSS class name clashes by generating dynamic...
Read more >
How to make React CSS import component-scoped?
However, the CSS is imported in the <header> section and stays global-scope. I was expecting CSS to be: Component-scoped in a way that...
Read more >
Manage Custom Component, CSS, and Module Imports
You can import resources such as custom CSS files, modules, and components to create "declarative" references to imported resources.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found