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

[Bug]: Compiled CSS is missing vendor prefixes, when Next.js does add them #23234

Closed
sawyerh opened this issue Jun 27, 2023 · 4 comments
Closed

Comments

@sawyerh
Copy link

sawyerh commented Jun 27, 2023

Describe the bug

My project's CSS includes modern CSS syntax, such as the mask property. This property requires vendor prefixes (e.g. -webkit-mask) in order to properly render in Chrome.

When using the @storybook/nextjs framework, the CSS mask property is not being automatically prefixed in the processed CSS. However, Next.js does this for you, so this means the CSS shown in Storybook is somewhat broken compared to when the page is viewed

In Next.js

CleanShot 2023-06-27 at 13 40 12@2x

In Storybook

CleanShot 2023-06-27 at 13 41 27@2x

To Reproduce

  1. I've created a repo with a minimum reproduction of this issue: https://github.com/sawyerh/storybook-css-prefixing-bug
  2. Run the Storybook in Chrome

System

Environment Info:

  System:
    OS: macOS 13.4.1
    CPU: (8) arm64 Apple M1
  Binaries:
    Node: 18.12.1 - ~/.asdf/installs/nodejs/18.12.1/bin/node
    Yarn: 1.22.19 - ~/.asdf/installs/nodejs/18.12.1/bin/yarn
    npm: 8.19.2 - ~/.asdf/plugins/nodejs/shims/npm
  Browsers:
    Chrome: 114.0.5735.133
    Safari: 16.5.1
  npmPackages:
    @storybook/nextjs: ^7.0.24 => 7.0.24
    @storybook/react: ^7.0.24 => 7.0.24


### Additional context

_No response_
@brandon-lent
Copy link

following

@sookmax
Copy link
Contributor

sookmax commented Jun 28, 2023

@sawyerh Hey, it seems like next.js uses some sophisticated postcss plugin (postcss-preset-env) to determine whether they should add vendor prefixes depending on browserslist you specified in your package.json.

Well fortunately under the hood, postcss-preset-env uses autoprefixer postcss plugin, and @storybook/nextjs's webpack is already configured to use postcss-loader when processing css files, you can manually install postcss and autoprefixer (e.g. npm install -D postcss autoprefixer) in your project and add postcss.config.js file at the project root like below:

postcss.config.js

module.exports = {
  plugins: {
    autoprefixer: {},
  },
};

postcss.config.js will be picked up and processed by postcss-loader in @storybook/nextjs, which will result in vender prefixes being automatically added to your css properties whenever you run the storybook dev server.

Screenshot 2023-06-28 at 9 34 12 PM

I've also added a PR to your reproduction repository (link), so see if it works correctly.

Cheers.

@sawyerh
Copy link
Author

sawyerh commented Jun 28, 2023

@sookmax Thanks for the quick response! It looks like adding the postcss.config.js does fix this in Storybook. For my project, the presence of postcss-preset-env in package.json was sufficent, and I didn't have to add autoprefixer as a direct dependency.

@ushakovfserg
Copy link

You need to be careful.
Adding postcss.config.js in project root disables default next.js internal postcss config.
According to docs it is better to set the same as internal and modify it.

I created postcss.config.js like this

const MODERN_BROWSERSLIST_TARGET = require('next/dist/shared/lib/modern-browserslist-target');

/**
 * Default configuration for PostCSS in Next.js.
 * https://nextjs.org/docs/pages/building-your-application/configuring/post-css#customizing-plugins
 *
 * Required for correct CSS in storybook, because @storybook/nextjs doesn't support it.
 */
module.exports = {
  plugins: {
    'postcss-flexbugs-fixes': {},
    'postcss-preset-env': {
      browsers: MODERN_BROWSERSLIST_TARGET,
      autoprefixer: {
        flexbox: 'no-2009',
      },
      stage: 3,
      features: {
        'custom-properties': false,
      },
    },
  },
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests

5 participants