【发布时间】:2018-12-17 09:20:49
【问题描述】:
我想在 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() 来摆脱BurgerMenu 在StyledBurgerMenu 内的嵌套。但是,这会导致汉堡按钮没有样式(根据文档,它透明地覆盖在整个屏幕上,因此我可以单击任意位置以打开菜单并查看其他所有内容的样式是否正确)。是否可以以这种方式设置汉堡按钮的样式,还是必须使用外部 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;
谢谢!
【问题讨论】:
-
如果
<BurgerMenu/>还不是样式化组件的实例,则必须包装它。没办法。
标签: javascript reactjs styled-components