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

CssSyntax error: Selector "&:hover" is not pure (pure selectors must contain at least one local class or id) #10475

Closed
andrasna opened this issue Feb 9, 2020 · 9 comments · Fixed by #62644

Comments

@andrasna
Copy link

andrasna commented Feb 9, 2020

The error

./pages/index.module.css (./node_modules/css-loader/dist/cjs.js??ref--5-oneOf-2-1!./node_modules/postcss-loader/src??__nextjs_postcss!./pages/index.module.css)
CssSyntax error: Selector "&:hover" is not pure (pure selectors must contain at least one local class or id) (4:3)


color: pink;

  &:hover {
    color: purple; 
}

To Reproduce

https://github.com/andrasna/hello-next-css-module-issue

The above repository is based on following the examples in the documentation:

https://nextjs.org/docs/basic-features/built-in-css-support#adding-component-level-css

Expected behavior

As far as I understand, the above selector does contain a "pure selector", so it should work. However, apparently, since hover is nested in this example, it isn't recognized as a "pure selector".

@jamesmosier
Copy link
Contributor

As far as I know, you cannot nest like that in CSS modules.

This example from the css modules docs shows an example in which they are using :hover but not nesting, which would lead me to believe this is not possible and you must stick to regular CSS synax.

@matamatanot
Copy link
Contributor

@andrasna
https://nextjs.org/docs/advanced-features/customizing-postcss-config
Create postcss.config.json or postcss.config.js

{
  "plugins": [
    "postcss-flexbugs-fixes",
    [
      "postcss-preset-env",
      {
        "autoprefixer": {
          "flexbox": "no-2009"
        },
        "stage": 0,
        "features": {
          "custom-properties": false,
          "nesting-rules": true  // Enable nesting
        }
      }
    ]
  ]
}

@timneutkens
Copy link
Member

Exactly as per #10475 (comment)

@remy
Copy link
Contributor

remy commented Jun 19, 2020

@timneutkens just checking, but the Next docs say it supports postcss, and I'm using postcss exclusively to get nested support (as css does everything else I need), but in the particular combination of Next + Postcss + Nesting + CSS Modules - does this mean nested CSS (via postcss) isn't supported/doesn't work?

@SalahAdDin
Copy link

@andrasna
https://nextjs.org/docs/advanced-features/customizing-postcss-config
Create postcss.config.json or postcss.config.js

{
  "plugins": [
    "postcss-flexbugs-fixes",
    [
      "postcss-preset-env",
      {
        "autoprefixer": {
          "flexbox": "no-2009"
        },
        "stage": 0,
        "features": {
          "custom-properties": false,
          "nesting-rules": true  // Enable nesting
        }
      }
    ]
  ]
}

it didn't work for me.

@andrasna
Copy link
Author

@SalahAdDin

Hi,

https://github.com/andrasna/hello-next-css-module-issue

I have unarchived and updated the repository with the issue, where I think the above code is a solution.

@LordZardeck
Copy link

I have nested selectors working with css modules with storybook, but as soon as I run it through next.js it fails, which makes me think this issue was closed incorrectly. To be fair, I had to finesse it so that the css rules applied to sass as well, but still it works. Wondering why I can't get it to work in Next.js. If it helps, here is my storybook config that works:

module.exports = {
    stories: [
        // Paths to the story files
        '../stories/**/*.stories.mdx',
        '../stories/**/*.stories.@(js|jsx|ts|tsx)'
    ],
    addons: [
        '@storybook/addon-links',
        '@storybook/addon-essentials',
        {
            name: '@storybook/addon-postcss',
            options: {
                postcssLoaderOptions: {
                    implementation: require('postcss')
                }
            }
        },
        {
            name: '@storybook/preset-scss',
            options: {
                cssLoaderOptions: {
                    modules: true,
                }
            }
        },
    ],
    webpackFinal: async (config) => {
        // Default rule for images /\.(svg|ico|jpg|jpeg|png|gif|eot|otf|webp|ttf|woff|woff2|cur|ani|pdf)(\?.*)?$/
        const fileLoaderRule = config.module.rules.find(rule => rule.test && rule.test.test('.svg'));
        fileLoaderRule.exclude = /\.svg$/;

        config.module.rules.push({
            test: /\.svg$/,
            enforce: 'pre',
            use: ['@svgr/webpack'],
        });

        const cssRule = config.module.rules.find(rule => rule.test && rule.test.test('.css'));
        const sassRule = config.module.rules.find(rule => rule.test && rule.test.test('.scss'));
        const postCSSLoader = cssRule.use.find(config => config.loader && config.loader.indexOf('postcss-loader') > 0);

        sassRule.use.push(postCSSLoader);

        return config;
    },
};

@equinusocio
Copy link

equinusocio commented Oct 25, 2021

Same issue here, im using postcss-extend-rule which breaks nextjs. It works on storybook but not nextjs

> 1 | %IndeterminateMixin {
    |                               ^
  2 |   content: '';
  3 |   position: absolute;
wait  - compiling...
error - ./node_modules/react-components/progress/progress.module.css:1:1
Syntax error: Selector "%IndeterminateMixin" is not pure (pure selectors must contain at least one local class or id)

@balazsorban44
Copy link
Member

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.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 27, 2022
samcx added a commit that referenced this issue Feb 29, 2024
today ~83% of all browsers support css nesting:
https://caniuse.com/css-nesting

![CSS Nesting Browser
Support](https://github.com/css-modules/postcss-modules-local-by-default/assets/4113649/141f8dce-a8bd-4df4-b2bd-210252189711)
https://caniuse.com/css-nesting  
https://developer.mozilla.org/en-US/docs/Web/CSS/Nesting_selector

therefore this pr upgrades postcss-modules-local-by-default which fixes
a bug in
css-modules/postcss-modules-local-by-default#64)

-  `.foo { &:hover { a_value: some-value; } }` is pure
-  `.foo { html &:hover { a_value: some-value; } }` is pure
-  `.foo { &:global(.bar) { a_value: some-value; } }` is pure
-  `:global(.foo) { &:hover { a_value: some-value; } }` is **not** pure


upgrading the package will allow using css nestings with or **without**
postcss compilation

it fixes the following error:

```
CssSyntaxError: postcss-modules-local-by-default: <css input>:1:8: Selector "&:hover" is not pure (pure selectors must contain at least one local class or id)
```

Fixes #55053
Fixes #33734
Fixes #10475

Co-authored-by: Sam Ko <sam@vercel.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants