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"的更多相关文章

  1. [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 ...

  2. [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. ...

  3. [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 ...

  4. 利用 Create React Native App 快速创建 React Native 应用

    本文介绍的 Create-React-Native-App 是非常 Awesome 的工具,而其背后的 Expo 整个平台也让笔者感觉非常的不错.笔者目前公司是采用 APICloud 进行移动应用开发 ...

  5. Ch03 React/JSX/Component 簡介

    Facebook 本身有提供 Test Utilities,但由于不够好用,所以目前主流开发社群比较倾向使用 Airbnb 团队开发的 enzyme,其可以与市面上常见的测试工具(Mocha.Karm ...

  6. A Bite Of React(2) Component, Props and State

    component component:用户自己定义的元素 const element = <Welcome name="Sara" />; class Welcome ...

  7. react slot component with args

    react slot component with args how to pass args to react props child component https://codesandbox.i ...

  8. react hooks & component will unmount & useEffect & clear up

    react hooks & component will unmount & useEffect & clear up useEffect & return === u ...

  9. [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 ...

随机推荐

  1. 【Docker自定制镜像之Dockerfile】

    镜像的定制,就是定制每一层所添加的配置.文件,如果可以把每一层修改.安装.构建.操作的命令都写入到一个脚本中,用脚本来构建.定制镜像,这个脚本就是Dockerfile Dockerfile是一个文本文 ...

  2. img下面的留白解决

    在做网页的时候经常会出现一个令人困惑的现象.那就是行内元素和块级元素之间会出现“留白”.就是块级元素中明明只有一个行内元素,但行内元素却不会铺满块级元素.像这个例子: “留白”出现的原因 行内元素默认 ...

  3. SFML学习纪要

    工作需要,近段粗浅看了一下SFML,记录一下! 一.浅见概述 SFML,simple and Fast mulitmedia Library官方网站:http://www.sfml-dev.org/i ...

  4. Yum数据库错误

    使用yum时提示数据库错误: /var/lib/rpm... open... db4 error from db->close:... 解决办法: 1.删除/var/lib/rpm目录下的__d ...

  5. PostgreSQL递归查询实现树状结构查询

    在Postgresql的使用过程中发现了一个非常有意思的功能,就是对于须要相似于树状结构的结果能够使用递归查询实现.比方说我们经常使用的公司部门这样的数据结构.一般我们设计表结构的时候都是相似以下的S ...

  6. 在OEL 5.4 32bit上使用yum install命令遇到的问题

    在OEL 5.4 32bit上使用yum install命令遇到的问题 [root@localhost yum.repos.d]# yum install elfutils-libelf-devel- ...

  7. iOS项目开发实战——学会使用TableView列表控件(二)

    要在iOS开发中使用TableView列表控件,不仅能够直接使用TableViewController作为整个主界面,并且还能够使用TableView控件来实现.使用TableView能够进行很多其它 ...

  8. JAVA多态学习1

    多态–概念 所谓多态.就是指一个引用(类型)在不同情况下的多种状态. 也能够理解成:多态是指通过指向父类的指针,来调用在不同子类中实现的方法. 实现多态有两种方式:1.继承.2.接口 这一次我们先来演 ...

  9. Java Swing设置主窗体位置居中方法

    01.第一种方法 int windowWidth = frame.getWidth(); //获得窗体宽  int windowHeight = frame.getHeight(); //获得窗体高 ...

  10. Codeforces 472D

    看官方题解提供的是最小生成树,怎么也想不明确.you can guess and prove it! 看了好几个人的代码.感觉实现思路全都不一样,不得不佩服cf题目想法的多样性 以下说说我自己的理解, ...