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. JAVA学习(一)——基本语法

    tips:前端开发写惯了弱类型语言,再来学强类型语言,真的是被各种修饰符.类型声明给整的云里雾里,而且java语法和javascript语言差别还是很大的,所以做好笔记,把一些需要注意的地方记下来是非 ...

  2. BZOJ2668: [cqoi2012]交换棋子(费用流)

    Description 有一个n行m列的黑白棋盘,你每次可以交换两个相邻格子(相邻是指有公共边或公共顶点)中的棋子,最终达到目标状态.要求第i行第j列的格子只能参与mi,j次交换. Input 第一行 ...

  3. Oracle Database Sample Schemas

    本文在Creative Commons许可证下发布 最近在钻研Oracle 11gR2,写SQL缺乏Demo表,研究他家的官方资料时发现一块甲骨文已经给我们准备Sample Schemas.比如说SC ...

  4. 【TC SRM 718 DIV 2 B】Reconstruct Graph

    [Link]: [Description] 给你两个括号序列; 让你把这两个括号序列合并起来 (得按顺序合并) 使得组成的新的序列为合法序列; 即每个括号都能匹配; 问有多少种合并的方法; [Solu ...

  5. 【hiho39】二分·归并排序之逆序对

    近期申请了微软的暑假实习,4号就要在线笔试了,在线測试系统用的是http://hihocoder.com/,今天试手做了一道题. [题目] 原题链接:http://hihocoder.com/cont ...

  6. 线段树 hdu3642 Get The Treasury

    不得不说,这是一题很经典的体积并.. 然而还是debug了2个多小时... 首先思路:按z的大小排序. 然后相当于扫描面一样,,从体积的最下方向上方扫描,遇到这个面 就将相应的两条线增加到set中,或 ...

  7. 10.cocos2d坐标系

    一.笛卡儿坐标系 OpenGl坐标系为笛卡儿右手系.x向右,y向上,z向外.在cocos2d-lua中坐标系原点在屏幕的左下角,x向右,y向上,z则是指的zorder(层级). 二.世界坐标系,本地坐 ...

  8. POJ 3038 贪心(multiset)

    题意: 思路: 1. 贪心 我们考虑肯定是走最近的最合适 想象自己是一个黑一日游的司机: 1.如果有乘客要上车,那么就让他上,收钱! 2.如果超载了,把距目的地最远的几个乘客踢下去,退钱. 3.行驶到 ...

  9. 秀秀SolarWinds为网络工程师准备的工具

    SolarWinds Engineer's Toolset部分功能截图 650) this.width=650;" onclick='window.open("http://blo ...

  10. C/C++(函数)

    函数 函数三要素:函数名,参数,返回值 重点研究函数的输入输出 随机数函数 //产生一组随机数 #include<stdio.h> #include<stdlib.h> #in ...