0

I'm creating a custom directive in Angular. I have a SCSS file with variables, like the image below, which I would like to import to my directive (.directive.ts). How can I do that?

variables.scss

chip.directive.ts

Thanks

1
  • 1
    would your mind taking the code from the pic and pasting it as text into your question? Commented Jan 13, 2022 at 22:52

1 Answer 1

2
  1. Write them to custom CSS variables (you can use @each rule for lists):
:root {
  --bs-light: #f1f2f3;
  --bs-dark: #3e3e3c;
}
  1. Use this code to read variables from the document:
export enum ScssVariables {
    Light = "light",
    Dark = "dark",
}

@Directive({
    selector: "[test-attr]",
})
export class TestDirective {
    public styles: { [key in ScssVariables]: string | null } = {
        light: null,
        dark: null,
    };

    constructor() {
        const styles = window.getComputedStyle(document.body);
        Object.keys(this.styles).forEach((key: ScssVariables) => {
            this.styles[key] = styles.getPropertyValue("--bs-" + key);
        })
        console.log(this.styles);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.