gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 04:20:03 30 4
gpt4 key购买 nike

我想在 React 应用程序中使用 styled-components 创建一个菜单组件,该组件在同一文件中包含样式,从而使其非常模块化。我正在使用 react-burger-menu 作为菜单。如果我将 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;

现在这当然完全没问题。但是,为了吸取教训并使事情变得更优雅,我想通过将前者传递给 styled 来摆脱 StyledBurgerMenu 内的嵌套 BurgerMenu ()。但是,这会导致汉堡按钮没有样式(根据文档,它透明地覆盖在整个屏幕上,因此我可以单击任意位置打开菜单并查看其他所有样式是否正确)。是否有可能以这种方式设置汉堡按钮的样式,还是我必须使用外部 div?以下是我尝试解决此问题的方法:

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;

谢谢!

最佳答案

Is it possible to style the burger button in this fashion or do I have to use the outer div?

通过将 BurgerMenu 传递给样式化,您没有添加外部 div,样式化组件仅确保将您的 css 添加到文档并将 className 传递给 汉堡菜单。所以为了让它工作,BurgerMenu 必须呈现它的 className 属性。如果这样做,它将在没有外部 div(或任何其他父级)的情况下工作。

根据 https://github.com/negomi/react-burger-menu/blob/95a58dd41e546730f5661dd7ad8deb7296a725ff/src/menuFactory.js#L251它将其 className Prop 分配给内部 div,这对于 styled-components 来说并不理想,但您的样式将应用于该 div。如果您想为该级别或更高级别的其他元素设置样式,您将需要想出一些奇特的选择器或从外部分配它们。

关于javascript - 如何将样式应用于带有样式组件的 react-burger-menu 按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51249812/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com