[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 ...
随机推荐
- 注解:@SuppressWarning()的用法
@SuppressWarning() 作用:J2SE 提供的一个批注或者注解.该批注的作用是给编译器一条指令,忽略这些警告信息. 常用:unchecked,serial. 1.如果传入多种情况,这几种 ...
- 【Henu ACM Round#14 F】 President and Roads
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 处理出起点到任意点的最短路以及最短路条数=>dis[0][i],cnt[0][i] 然后 把所有的边反向 处理出在反图上终点到 ...
- qmake生成Visual Studio工程
整个Qt在Windows中都可以通过批处理来编译,当编译好Qt后,会生成qmake.exe. 在没有安装Qt-VS-Addin的情况下,如何使用别人提供的Qt *.pro项目文件呢? 使用qmake可 ...
- Linux-----Ubuntu Server安装图形界面
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...
- 79.QT解决迷宫问题(面向过程与面向对象)
面向过程: 创建一个类继承dialog,mydialog,添加两个变量 #ifndef MYDIALOG_H #define MYDIALOG_H #include <QDialog>&g ...
- 突破极限 解决大硬盘上安装Unix新思路
一.问题提出 硬盘越做越大,然我喜欢让我忧.10年前就遇到过在586电脑BIOS不认识超过8.4G容量硬盘的问题,以及Windows Nt操作系统不认大硬盘(容量超过8.4G)的问题,对于Linux ...
- Kinect 开发 —— 面部追踪
SDK1.5中新增了人脸识别类库:Microsoft.Kinect.Toolkit.FaceTracking使得在Kinect中进行人脸识别变得简单,该类库的源代码也在Developer Toolki ...
- 浅谈架构之路:单点登录 SSO
前言:SSO 单点登录 "半吊子"的全栈工程师又来了,技术类的文章才发表了两篇,本来想先将主攻的几个系列都开个头(Nodejs.Java.前端.架构.全栈等等),无奈博客起步太晚, ...
- cocos2dx——lua自己主动和手动绑定
[自己主动绑定] 參考:http://my.oschina.net/skyhacker2/blog/298397 主要是通过引擎自带的tools/tolua,主要过程例如以下: 1.编写好要导出的c+ ...
- Elasticsearch中JAVA API的使用
1.Elasticsearch中Java API的简介 Elasticsearch 的Java API 提供了非常便捷的方法来索引和查询数据等. 通过添加jar包,不需要编写HTTP层的代码就可以开始 ...