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.

babel-preset-css-prop does not work with Storybook

See original GitHub issue
  • emotion version: 10.0.9
  • react version: 16.8.3
  • storybook version 5.0.6

Relevant code:

// babel.config.js
module.exports = {
  presets: [
    'react-app',
    '@emotion/babel-preset-css-prop',
  ],
  plugins: [
    'emotion',
  ],
};
// Button.jsx
import React from 'react';
import { css } from '@emotion/core';

const Button = props => (
  <button
    css={css`
      background-color: blue;

      &:hover {
        background-color: lightblue;
      }
    `}
  >
    {'Hello'}
  </button>
);

export default Button;

What you did:

Bootstrap a new Storybook instance:

npx -p @storybook/cli sb init --type react

What happened:

Output is <button css="[object Object]">Hello</button>

Problem description:

The css prop is not transformed. It does, however, work when using /** @jsx jsx */ pragma.

Per https://storybook.js.org/docs/configurations/custom-babel-config/ Storybook uses the app’s babel config so the note about custom configs should not apply.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:8
  • Comments:9

github_iconTop GitHub Comments

5reactions
LukeAskewcommented, Apr 25, 2019

Played around a bit with these solutions and realized that Storybook only supports loading .storybook/.babelrc and not babel.config.js.

It only loads from .storybook (not root, where most babel configs are located), and only supports .babelrc.

If you drop a .babelrc file in .storybook/ then it works.

🤦‍♂️

3reactions
iamyokicommented, Sep 16, 2021

.storybook/main.js

module.exports = {
  ...,
  babel: async (options) => ({
    ...options,
    presets: [...options.presets, '@emotion/babel-preset-css-prop'],
  }),
}

https://storybook.js.org/docs/react/configure/babel#gatsby-focus-wrapper

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't get emotion's css prop working inside storybook #7540
Describe the bug I can't seem to get emotion's css prop to work inside of Storybook. Seems like emotion is now the dominant...
Read more >
Use Emotion CSS Prop in Storybook - Duncan Leung
I was running into an issue where my Storybook webpack.config.js was not correctly parsing and evaluating Emotion styles passed via the css prop...
Read more >
emotion/babel-preset-css-prop
A Babel preset to automatically enable Emotion's css prop when using the classic ... is not compatible with @babel/plugin-transform-react-inline-elements .
Read more >
@emotion/babel-preset-css-prop examples - CodeSandbox
Learn how to use @emotion/babel-preset-css-prop by viewing and forking example apps that make use of @emotion/babel-preset-css-prop on CodeSandbox.
Read more >
Configure Storybook
The main.js configuration file is a preset and, as such, has a powerful interface, ... babelModeV7, Enables the new Babel configuration mode 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