[React] Create component variations in React with styled-components and "extend"
In this lesson, we extend the styles of a base button component to create multiple variations of buttons, using "extend". We can then modify the base styles in one place, and have all button types updated.
import React from "react";
import styled from "styled-components"; const Button = styled.button`
background: ${props => props.theme.base};
color: white;
font-size: 1rem;
padding: .75rem 2rem;
box-shadow: 3px 5px rgba(, , , 0.1);
cursor: pointer;
border: none;
border-radius: 4px;
margin: .5rem;
transition: all .1s;
&:hover {
transform: translateY(1px);
box-shadow: 2px 3px rgba(, , , 0.15);
}
`; const ButtonDanger = Button.extend`background: ${props => props.theme.danger};`;
const ButtonGradient = Button.extend`
background: ${props => props.theme.gradient};
`; /* ============================== */
/* ===== the main component ===== */
/* ============================== */
const App = () =>
<div>
<Button>Basic button</Button>
<ButtonDanger>Watch out now!</ButtonDanger>
<ButtonGradient>Gradient Button!</ButtonGradient>
</div>; export default App;
theme:
import React from "react";
import ReactDOM from "react-dom";
import App from "./App"; import { ThemeProvider, injectGlobal } from "styled-components"; injectGlobal`
body {
margin: ;
padding: 2rem;
font-family: -apple-system,
BlinkMacSystemFont,
"Segoe UI",
Roboto,
Helvetica,
Arial,
sans-serif,
"Apple Color Emoji",
"Segoe UI Emoji",
"Segoe UI Symbol";
}
`; const theme = {
base: "#a04ed9",
danger: "tomato",
gradient: `background-color: #00DBDE; background-image: linear-gradient(225deg, #00DBDE %, #FC00FF %);`
}; ReactDOM.render(
<ThemeProvider theme={theme}>
<App />
</ThemeProvider>,
document.getElementById("root")
);
[React] Create component variations in React with styled-components and "extend"的更多相关文章
- [React] Create & Deploy a Universal React App using Zeit Next
In this lesson, we'll use next to create a universal React application with no configuration. We'll ...
- [React] Update Component State in React With Ramda Lenses
In this lesson, we'll refactor a React component to use Ramda lenses to update our component state. ...
- [React] Preventing extra re-rendering with function component by using React.memo and useCallback
Got the idea form this lesson. Not sure whether it is the ncessary, no othere better way to handle i ...
- 利用 Create React Native App 快速创建 React Native 应用
本文介绍的 Create-React-Native-App 是非常 Awesome 的工具,而其背后的 Expo 整个平台也让笔者感觉非常的不错.笔者目前公司是采用 APICloud 进行移动应用开发 ...
- Ch03 React/JSX/Component 簡介
Facebook 本身有提供 Test Utilities,但由于不够好用,所以目前主流开发社群比较倾向使用 Airbnb 团队开发的 enzyme,其可以与市面上常见的测试工具(Mocha.Karm ...
- A Bite Of React(2) Component, Props and State
component component:用户自己定义的元素 const element = <Welcome name="Sara" />; class Welcome ...
- react slot component with args
react slot component with args how to pass args to react props child component https://codesandbox.i ...
- react hooks & component will unmount & useEffect & clear up
react hooks & component will unmount & useEffect & clear up useEffect & return === u ...
- [React] Create a Persistent Reference to a Value Using React useRef Hook
The useRef is a hook for creating values that persist across renders. In this lesson we'll learn how ...
随机推荐
- Perl初学笔记
标量数据 标量:数字和字符串. 数字:Perl不存在整形,全部是double类型.整形常量会被自动转换为浮点型. Perl数字字面量(直接量):+-和小数点是非必须的,e代表10的多少次方.例如:-1 ...
- SpringBoot 结合 Thymeleaf 进行页面的跳转
1.引入thymeleaf依赖 <!--thymeleaf--> <dependency> <groupId>org.springframework.boot< ...
- 利用js 获取ip和地址
1.引用第三方js<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script> 2. I ...
- 改动UINavigationBar (导航栏)上NavigationBarItem 的字体大小和颜色的用法
//创建一个左边button UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"<" ...
- 一次失败的PHP扩展开发之旅
一次失败的PHP扩展开发之旅 By warezhou 2014.11.19 缘起 经过不断的持续迭代.我们部门的协程版网络框架(CoSvrFrame)最终出炉了!这本来是件喜大普奔的事情.可是随着新业 ...
- deep-in-es6(一)
一 迭代器和for-of循环 以前的一些遍历数组: function c(n) { console.log(n); } 方法一: for(let i = 0;i < arr.length;i++ ...
- Ansible学习记录一:Linux下部署
0.Ansible介绍 Ansible 是一个简单的自动化运维管理工具,可以用来自动化部署应用.配置.编排 task(持续交付.无宕机更新等),采用 paramiko 协议库(fabric 也使用这个 ...
- 通过Gradle来下载依赖的jar包
前段时间在看Gradle文章方向其比ANT.Maven真的要简单很多,特别是在下载jar依赖方面下面就是用gradle写的一个jar包依赖下载的小例子: (1)建立build.gradle文件 (2) ...
- Linux下的一个高速跳转到上N层文件夹的简单方法
编辑文件 vim .bashrc (使改动在当前用户下有效) 或者 vim /etc/profile (须要在root用户下运行,使改动在全部用户下有效) 在文件结尾加入别名 alias cd1=' ...
- 生成CPU使用率 sin 曲线 控制cpu使用率 编程之美
入职Oracle 以后想着把之前写过的<编程之美>中控制CPU使用率曲线的程序再写一边, 可是总是由于入职须要学习的东西太多, 没有时间. 程序早就写好了. 最终有机会贴出来了.o(∩∩) ...