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.

Working with SASS modules and SASS imports for components

See original GitHub issue

After reading some different methods of utilizing CSS modules within Nextjs pages, I’m wanting to know if the same concept can be applied to components but with SASS. This is one of the examples I’m referencing to:

https://github.com/zeit/next.js/tree/canary/examples/with-next-sass

What I’m trying to achieve is something like this with the following file structure:

Reusable components are in a folder structure so both .js and .sass files are in the same area.

├── pages
│   ├── index.js
├── components
│   ├── Button
│   │   ├── index.js
│   │   ├── index.sass

Import components as usual…

// pages/index.js

import Button from 'components/Button'

...

<Button 
  className='book'
  text="I'm a button"
  href='/about'
/>

Import styles from the component’s sass file, then pass the styles as properties with styles.btn

// components/Button/index.js

// Libraries
import React, { Component } from 'react'
import Link from 'next/link'

// Styles
import styles from './index.sass'

export default class Button extends Component {
    constructor(props) {
        super(props)
    }

    componentDidMount() {

    }

    render() {
        return (
            this.props.href
                ? (
                    <a className={styles.btn + this.props.className} target='_blank' href={this.props.href}>
                        {this.props.text}
                    </a>
                ) : (
                    <Link href={this.props.link}>
                        <a className={styles.btn + this.props.className}>
                            {this.props.text}
                        </a>
                    </Link>
                )
        )
    }
}




Button’s styling.

// components/Button/index.sass

.btn
  color: red

My next.config.js setup:

const path = require('path')
const glob = require('glob')
const withSASS = require('@zeit/next-sass')

module.exports = withSASS({
  cssModules: true,
  distDir: 'public',

  exportPathMap: function () {
    return {
      "/": { page: "/" },
      "/about": { page: "/about" },
    }
  }, 

  webpack: (config, { dev }) => {
    return config
  }
})

The markup for the page renders, but styles aren’t being applied and the class name has a weird string with it (book is the class passed in the Button properties):

<a class="EGxqjOug9lYuZ5fOMdFUbbook" target="_blank" href="/about">I'm a button</a>

With all of this, I have a couple of questions concerning this setup:

  1. Is what I’m trying to achieve even possible to do at the moment?
  2. Is this issue depending on how my next.config.js file is setup?
  3. How would I be able to import other sass files from different directories into the index.sass of my component? For example, if I want to import breakpoint mixins into my button component, will that cause any issues with modules?
  • I have searched the issues of this repository and believe that this is not a duplicate.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mgiraldocommented, Apr 13, 2018

@timneutkens the _document.js part bit me too. Maybe add a heading to that part saying With or without CSS modules or something? The reader’s eyes gravitate to headings. Or maybe under each subheading (eg: right under “With CSS modules”) mention “Create your _document.js. Then…” because yo do repeat the “Create a config/index” files parts.

0reactions
balazsorban44commented, Jan 31, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sass: @import
Sass extends CSS's @import rule with the ability to import Sass and CSS stylesheets, providing access to mixins, functions, and variables and combining ......
Read more >
The New SASS Module System: Out with @import, In with @use
The new @use rule is the replacement for @import which allows other stylesheets to use its variables, functions, and mixins. What makes the...
Read more >
Stop using @import with Sass | @use and @forward explained
A quick note, if you are using node- sass, it is out of date and @use and @forward, as well as their entire...
Read more >
CSS in React: 5 ways w/ sass + styled components + modules
We will learn about css scoping with css modules as well as how to incorporate SASS / SCSS as well as master css...
Read more >
Introducing Sass Modules - CSS-Tricks
Sass just launched a major new feature you might recognize from other languages: a module system. This is a big step forward for...
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