直到最近,这还不是React世界中解决的问题。
我是ElementalUI(一个React组件库)的维护者之一,我们在过去的6-12个月里一直在尝试所有不同的样式化方法。 (!)你的名字,我们已经尝试过了。 (我在我的ReactNL keynote和他们分解的地方谈到了我对一些最流行的库的使用经验)
问题是,目前的样式库都没有内置的主题支持。你可以用一种非常好用的,非用户友好的方式来做到这一点,但是当你发布一个组件时,这不是你想要的,对吗?
这就是为什么我们建造了styled-components
。 styled-components
有一些有趣的属性,其中一个是将主题直接内置到库中,使其成为构建可分发组件的完美选择!
下面是简短的解释,但我鼓励你通过我们的documentation解释一切!
这是什么styled-components
的基本用法是这样的:
import React from 'react';
import styled from 'styled-components';
// Create a <Wrapper> react component that renders a <section> with
// some padding and a papayawhip background
const Wrapper = styled.section`
padding: 4em;
background: papayawhip;
`;
这个变量,Wrapper
,现在反应的组分可以渲染:
// Use them like any other React component – except they're styled!
<Wrapper>
<Title>Hello World, this is my first styled component!</Title>
</Wrapper>
(如果你点击图像,你会得到一个现场操场)
当您将插值函数传递给带标签的模板文字时,我们将传递给该组件的属性传递给您。这意味着你可以适应道具:
import styled from 'styled-components';
const Button = styled.button`
/* Adapt the colors based on primary prop */
background: ${props => props.primary ? 'palevioletred' : 'white'};
color: ${props => props.primary ? 'white' : 'palevioletred'};
font-size: 1em;
margin: 1em;
padding: 0.25em 1em;
border: 2px solid palevioletred;
border-radius: 3px;
`;
在这里,我们创建了一个Button
组件,你可以做primary
像任何其他阵营组成:
<Button>Normal</Button>
<Button primary>Primary</Button>
现在来主题方面。我们出口一个名为ThemeProvider
组件,您可以通过一个theme
并包装您的应用程序(或应用程序的部分):该ThemeProvider
内
import { ThemeProvider } from 'styled-components';
const theme = {
main: 'mediumseagreen',
};
ReactDOM.render(
<ThemeProvider theme={theme}>
<MyApp />
</ThemeProvider>,
myElem
);
现在,任何风格的组成部分,无论多么深刻的感谢背景下,将把这个theme
注入道具。
这是一个主题化Button
会是什么样子:
import styled from 'styled-components';
const Button = styled.button`
/* Color the background and border with theme.main */
background: ${props => props.theme.main};
border: 2px solid ${props => props.theme.main};
/* …more styles here… */
`;
现在你Button
会把它被传递的theme
并改变它基于这样的造型! (您也可以通过defaultProps
提供默认设置)
这就是styled-components
的要点,以及它如何帮助构建可分发的组件!
我们目前有WIP doc about writing third-party component libraries,我鼓励您查看,当然,正常的文档也是一个很好的阅读。我们试图涵盖所有的基础,所以如果你看到任何你不喜欢的或者你认为失踪的东西,请马上告诉我们,我们将讨论!
如果您有任何其他关于styled-components
的问题,请随时在此回复或在Twitter上与我们联系。 (@mxstbr)
你如何定制你自己的自定义组件? 只需使用div包装? – MoeSattler
是的,我使用每个较大组件的大量样式化组件。类似这样的: const Btn =风格。按钮' 风格:这里; ' const的按钮=(道具)=> { 回报( {props.text} ); } –
mxstbr
如何解决组件中样式的实际耦合问题。例如,让我们说我的所有按钮现在都需要5px的半径,我必须进入所有按钮并手动更改它们。或者我读你的帖子是错的?或媒体查询,你如何使用它们? –