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.

CSS in header.css causes incorrect rendering of component

See original GitHub issue

Describe the bug

I’m using Chakra UI to render a menu. Each MenuItem component is rendered as a button element. In a menu of two items, the second item is misaligned since it’s given a left margin by the CSS rule:

button + button {
    margin-left: 10px;
}

from header.css, which appears to come from Storybook. Is there a way to avoid this CSS being included? Apologies if I’ve misunderstood something!

Actual:

image

Expected:

image

To Reproduce

Steps to reproduce the behavior:

  1. Create a fresh React application using Create React App: npx create-react-app app; cd app

  2. Install Chakra UI: npm install @chakra-ui/react framer-motion @emotion/react @emotion/styled

  3. Install Storybook: npx sb init

  4. Fix Chakra UI / Storybook integration by updating .storybook/main.js:

    const path = require("path");
    
    const toPath = (_path) => path.join(process.cwd(), _path);
    
    module.exports = {
      "stories": [
        "../src/**/*.stories.mdx",
        "../src/**/*.stories.@(js|jsx|ts|tsx)"
      ],
      "addons": [
        "@storybook/addon-links",
        "@storybook/addon-essentials",
        "@storybook/preset-create-react-app"
      ],
      webpackFinal: async (config) => {
        return {
          ...config,
          resolve: {
    	...config.resolve,
    	alias: {
    	  ...config.resolve.alias,
    	  "@emotion/core": toPath("node_modules/@emotion/react"),
    	  "emotion-theming": toPath("node_modules/@emotion/react"),
    	},
          },
        }
      },
    }
    
    
  5. Create a story with a menu:

    import React from "react";
    
    import { Button, ChakraProvider, Menu, MenuButton, MenuItem, MenuList } from "@chakra-ui/react";
    
    function ExampleMenu() {
        return (
    	<ChakraProvider>
    	    <Menu>
    	        <MenuButton as={Button}>
    	            Actions
    	        </MenuButton>
    	        <MenuList>
    	            <MenuItem>Download</MenuItem>
    	            <MenuItem>Create a Copy</MenuItem>
    	            <MenuItem>Mark as Draft</MenuItem>
    	            <MenuItem>Delete</MenuItem>
    	            <MenuItem>Attend a Workshop</MenuItem>
    	        </MenuList>
    	    </Menu>
    	</ChakraProvider>
        );
    }
    
    export default {
        title: "ExampleMenu",
        component: ExampleMenu,
    };
    
    export const BrokenMenu = () => (
        <ExampleMenu />
    );
    
    

Expected behavior

Buttons are correctly aligned.

System

Environment Info:

  System:
    OS: Linux 4.19 Debian GNU/Linux 10 (buster) 10 (buster)
    CPU: (12) x64 AMD Ryzen 5 2600 Six-Core Processor
  Binaries:
    Node: 12.18.3 - ~/bin/node
    Yarn: 1.22.4 - ~/bin/yarn
    npm: 6.14.6 - ~/bin/npm
  Browsers:
    Chrome: 89.0.4389.72
    Firefox: 86.0
  npmPackages:
    @storybook/addon-actions: ^6.1.21 => 6.1.21 
    @storybook/addon-essentials: ^6.1.21 => 6.1.21 
    @storybook/addon-links: ^6.1.21 => 6.1.21 
    @storybook/node-logger: ^6.1.21 => 6.1.21 
    @storybook/preset-create-react-app: ^3.1.6 => 3.1.6 
    @storybook/react: ^6.1.21 => 6.1.21 

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
davidpfahlercommented, May 6, 2021

@ndelangen Thanks! I didn’t realize the examples could mess with the rest of the components / styles, but makes sense.

1reaction
ndelangencommented, Mar 17, 2021

Just delete header.css, it’s just to help people get started, it’s not required to keep.

Read more comments on GitHub >

github_iconTop Results From Across the Web

css file not applying style component in - Stack Overflow
A CSS File can't be rendered literally, just Apply style to some ... actually located relative to Header.js so the import path is...
Read more >
CSS element incorrect rendering | Firefox Support Forum
The CSS element, below, is not rendered properly in the current version of FireFox for Windows. I first noticed it with the introduction...
Read more >
Content Jumping (and How To Avoid It) | CSS-Tricks
A web page isn't locked in stone just because it has rendered visually. Media assets, like images, can come in and cause the...
Read more >
Tackling Render Blocking CSS for a Fast Rendering Website
This may cause a delay between the time the HTML has been downloaded and the time the page renders on the screen. The...
Read more >
Solving the React Error: Not Picking Up CSS Style | Pluralsight
Here, the CSS file is saved outside the src folder, so the compiler failed to import it. To make this code work, you...
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