简体   繁体   English

如何将样式应用于带有样式组件的react-burger-menu按钮?

[英]How to apply styles to react-burger-menu button with styled-components?

I would like to use styled-components in a React app to create a menu component that contains the styles in the same file thus making it very modular. 我想在React应用程序中使用styled-components来创建一个菜单组件,该菜单组件在同一文件中包含样式,从而使其具有很高的模块化性。 I'm using react-burger-menu for the menu. 我在react-burger-menu使用react-burger-menu menu。 Everything works correctly if I wrap BurgerMenu in a styled div like so (styles copied from react-burger-menu README.md): 如果将BurgerMenu包裹在这样的样式div一切都将正常工作(从react-burger-menu README.md复制的样式):

import React from 'react';
import { slide as BurgerMenu } from 'react-burger-menu';
import styled from 'styled-components';


const StyledBurgerMenu = styled.div`
  /* Position and sizing of burger button */
  .bm-burger-button {
    position: fixed;
    width: 36px;
    height: 30px;
    left: 36px;
    top: 36px;
  }

  /* Color/shape of burger icon bars */
  .bm-burger-bars {
    background: #373a47;
  }

  /* Position and sizing of clickable cross button */
  .bm-cross-button {
    height: 24px;
    width: 24px;
  }

  /* Color/shape of close button cross */
  .bm-cross {
    background: #bdc3c7;
  }

  /* General sidebar styles */
  .bm-menu {
    background: #373a47;
    padding: 2.5em 1.5em 0;
    font-size: 1.15em;
  }

  /* Morph shape necessary with bubble or elastic */
  .bm-morph-shape {
    fill: #373a47;
  }

  /* Wrapper for item list */
  .bm-item-list {
    color: #b8b7ad;
    padding: 0.8em;
  }

  /* Individual item */
  .bm-item {
    display: inline-block;
  }

  /* Styling of overlay */
  .bm-overlay {
    background: rgba(0, 0, 0, 0.3);
  }
`;


export class Menu extends React.Component {
  showSettings(event) {
    event.preventDefault();
  }

  render() {
    return (
      <StyledBurgerMenu>
        <BurgerMenu>
          <a id="home" className="menu-item" href="/">Home</a>
          <a id="about" className="menu-item" href="/about">About</a>
        </BurgerMenu>
      </StyledBurgerMenu>
    );
  }
}

export default Menu;

Now this is of course totally okay. 现在,这当然完全可以。 However, to learn a lesson and make things a bit more elegant, I'd like to get rid of nesting BurgerMenu inside of StyledBurgerMenu by passing the former to styled() . 但是,要学习一课并使事情变得更优雅,我想通过将前者传递给styled()来摆脱在BurgerMenu内嵌套BurgerMenuStyledBurgerMenu However, this leads to the burger button being not styled (as per the docs, it's overlaid transparently across all of the screen, so I can click anywhere to open the menu and see that everything else is styled correctly). 但是,这会导致汉堡按钮的样式没有设置(根据文档,它在整个屏幕上都透明覆盖,因此我可以单击任意位置以打开菜单,看看其他所有样式均正确设置)。 Is it possible to style the burger button in this fashion or do I have to use the outer div ? 是否可以以这种方式设置汉堡按钮的样式,或者我必须使用外部div Here is how I've tried to solve this: 这是我尝试解决此问题的方法:

import React from 'react';
import { slide as BurgerMenu } from 'react-burger-menu';
import styled from 'styled-components';


const StyledBurgerMenu = styled(BurgerMenu)`
  /* Position and sizing of burger button */
  .bm-burger-button {
    position: fixed;
    width: 36px;
    height: 30px;
    left: 36px;
    top: 36px;
  }

  /* Color/shape of burger icon bars */
  .bm-burger-bars {
    background: #373a47;
  }

  /* Position and sizing of clickable cross button */
  .bm-cross-button {
    height: 24px;
    width: 24px;
  }

  /* Color/shape of close button cross */
  .bm-cross {
    background: #bdc3c7;
  }

  /* General sidebar styles */
  .bm-menu {
    background: #373a47;
    padding: 2.5em 1.5em 0;
    font-size: 1.15em;
  }

  /* Morph shape necessary with bubble or elastic */
  .bm-morph-shape {
    fill: #373a47;
  }

  /* Wrapper for item list */
  .bm-item-list {
    color: #b8b7ad;
    padding: 0.8em;
  }

  /* Individual item */
  .bm-item {
    display: inline-block;
  }

  /* Styling of overlay */
  .bm-overlay {
    background: rgba(0, 0, 0, 0.3);
  }
`;


export class Menu extends React.Component {
  showSettings(event) {
    event.preventDefault();
  }

  render() {
    return (
      <StyledBurgerMenu>
        <a id="home" className="menu-item" href="/">Home</a>
        <a id="about" className="menu-item" href="/about">About</a>
      </StyledBurgerMenu>
    );
  }
}

export default Menu;

Thanks! 谢谢!

Is it possible to style the burger button in this fashion or do I have to use the outer div? 是否可以以这种方式设置汉堡按钮的样式,或者我必须使用外部div?

By passing your BurgerMenu to styled, you are not adding an outer div, the styled component only ensures your css is added to the document and passes the className to BurgerMenu . 通过将BurgerMenu传递给样式,您无需添加外部div,样式化组件仅确保将CSS添加到文档并将className传递给BurgerMenu So in order for this to work, BurgerMenu has to render its className prop. 因此,为了使其正常工作, BurgerMenu必须呈现其className BurgerMenu If it does that it will work without an outer div (or any other parent). 如果这样做,它将在没有外部div(或任何其他父对象)的情况下工作。

According to https://github.com/negomi/react-burger-menu/blob/95a58dd41e546730f5661dd7ad8deb7296a725ff/src/menuFactory.js#L251 it assigns its className prop to an inner div, which is not ideal for styled-components but your styles will apply to that div . 根据https://github.com/negomi/react-burger-menu/blob/95a58dd41e546730f5661dd7ad8deb7296a725ff/src/menuFactory.js#L251的定义 ,它将className分配给内部div,这对styled-components并不理想,但您的样式很理想将适用于该div If you want to style the other elements at or above that level, you will either need to come up with some fancy selectors or assign them from outside. 如果要在该级别或更高级别设置其他元素的样式,则需要提供一些精美的选择器或从外部进行分配。

This is not possible because of the way styled-components work 由于样式化组件的工作方式,这是不可能的

The styling is being applied to a child div, not the outer div: Link to code in react-burger-menu 样式将应用于子div,而不是外部div: 链接到react-burger-menu中的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM