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

When using sass, how to import sass variables? #1619

Closed
grundmanise opened this issue Feb 22, 2017 · 13 comments
Closed

When using sass, how to import sass variables? #1619

grundmanise opened this issue Feb 22, 2017 · 13 comments

Comments

@grundmanise
Copy link

Hi, first of all thanks for such a great react development setup & build automation tool.

When using create-react-app with node-sass. How to expose sass variables to my project? Can't get my head around..

My folder structure for the reference:

└───src
    ├───app
    │    └───variables.scss // here are all my vars
    ├───components
    │   └───Header // there is Header.scss in this folder, where I need to use my variables
    ├───helpers
    ├───public
    └───utils
@viankakrisna
Copy link
Contributor

viankakrisna commented Feb 22, 2017

one way to do it is just import any scss file from your root index.scss

so rather than placing import './Header.css'; in Header.js
You do

@import "./app/variables.scss";
@import "./components/Header/Header.scss";

in index.scss

and import './index.css' in index.js

@grundmanise
Copy link
Author

grundmanise commented Feb 23, 2017

@viankakrisna thanks, this works! :)

@gaearon
Copy link
Contributor

gaearon commented Feb 24, 2017

Couldn't you also @import "../../variables.scss"; or equivalent from any component's Sass file that needs them?

@gaearon
Copy link
Contributor

gaearon commented Feb 24, 2017

Closing with 67d0d49.

@gaearon gaearon closed this as completed Feb 24, 2017
@viankakrisna
Copy link
Contributor

viankakrisna commented Feb 24, 2017

As always, @gaearon comments is an eye opener 👍 @grundmanise please use this instead of my suggestion. It's more inline with component based structure.

@woniesong92
Copy link

woniesong92 commented May 15, 2017

@gaearon do you have a suggestion to use absolute paths for sass imports? For example, when trying to import src/shared/variables.scss from a deeply nested component, there are a lot of ../../../. Related: #1712 (for importing JS files)

@gaearon
Copy link
Contributor

gaearon commented May 15, 2017

No, I don’t know how to solve this aside from moving them into an npm package and then using ~packagename/file.css.

@brianbuie
Copy link

You can use sass-resources-loader to load global variables into every sass file before output. In addition to the other style loaders for scss files in your webpack config:
{ loader: 'sass-resources-loader', options: { resources: './client/scss/resources/**/*.scss' }}
This will load all files in the client/scss/resources directory and subdirectories into scope for all processed scss files. As long as those files don't directly generate css, you won't get any repeated code in your output css.

@bengrunfeld
Copy link

Getting hit with an error when trying to use @import inside of a SASS file in a create-react-app app. SASS has been set up as the docs suggest. Here is a link to the offending file.

Error Message

{
  "status": 1,
  "file": "/Users/ben/Desktop/Work/code/indio-form-builder/src/components/Tab/tab.scss",
  "line": 3,
  "column": 1,
  "message": "media query expression must begin with '('",
  "formatted": "Error: media query expression must begin with '('\n        on line 3 of src/components/Tab/tab.scss\n>> .active a {\n   ^\n"
}

My code

@import "./constants.scss"

.active a {
  background-color: $tabColor;
}

@amackintosh
Copy link

amackintosh commented Oct 31, 2017

You can also load the styles into a root component and pass them down as props, but maybe that is just a React Native strategy, I'm not certain.

I will just mention it anyway for the more savage yet creative individuals. It can facilitate theming your app by swapping out variables at the root level, perhaps based on user-preferences.

@hom-bahrani
Copy link

hom-bahrani commented Feb 6, 2018

@woniesong92 npm package seems to work for me, also have very deeply nested files

@transiient
Copy link

@bengrunfeld you're missing a semicolon on your import. Damn things.

@m-salamon
Copy link

m-salamon commented Nov 14, 2018

Any new suggestions?
I'm using Sass in a Create React App project. I have a master.scss file with global var's, I would like to use those var's globally... (I don't want to @import "master" in every scss file ), Any other ideas how to make it globally available ?

@lock lock bot locked and limited conversation to collaborators Jan 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants