Wrap child scss file with parent class
Asked Answered
H

1

5

Is there a way to add a parent class to all rules in a child scss file. By interpreting the child file as if it was already compiled?

Currently, I tried to wrap an @import with a parent class but it doesn't work as expected.

parent.scss

.parent{
   @import 'child';
}

child.scss

.child{
   .dark &{
      color: blue;
   }
}

Actual output

.dark .parent .child {
  color: blue; 
}

Expected output

.parent .dark .child {
  color: blue; 
}

Hume answered 19/5, 2022 at 14:46 Comment(0)
H
7

I finally found a solution using meta.load-css() mixin

child.scss

.child{
   .dark &{
      color: blue;
   }
}

parent.scss

@use "sass:meta";
.parent {
    @include meta.load-css("child");
}

here is a working example: https://github.com/zeckaissue/Wrap-child-scss-file-with-parent-class

Hume answered 17/2, 2023 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.