Skip to content

Access SASS variables from css-module in a React component #86

Description

@hdmchl

I have a React component that is using css-modules. Is there a way for me to get access to the SASS variables used in the css-module?

My styles.scss file is using a shared _colors.scss file. My files look something like this (note the comment for use-case):

index.jsx

import styles from './styles';
import React from 'react';
import SomeFooBarComponent from 'irrelevant';

export default class MyComponent extends React.Component {
  render() {
    return (
      <div className={styles.root}>
        <h1>My amazing page</h1>
{/* I want to use a variable here, rather than hard-code the color. 
I want that variable to come from my imported styles file (i.e. through the `styles` object) */}
        <SomeFooBarComponent takesAColorProperty="#00FF00" />
      </div>
    );
  }
}

shared/variables/_colors.scss

$brand-primary: #FF0000;
$brand-secondary: #00FF00;

styles.scss

@import "~shared/variables/colors";

.root {
  color: $brand-primary;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions