久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

淺談styled-components的用法

這篇文章主要介紹了淺談styled-components的用法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

styled components 一種全新的控制樣式的編程方式,它能解決 CSS 全局作用域的問題,而且移除了樣式和組件間的映射關系

import React from 'react';
import styled from 'styled-components';
import { render } from 'react-dom';
 
const Title = styled.h1`
    font-size: 1.5em;
    text-align: center;
    color: palevioletred;
`;
 
class App extends React.Component {
    render() {
        return (
            <Title>Hello world</Title>
        )
    }
}
 
render(
    <App />,
    document.getElementById('app')
);

styled.h1 是一個標簽模板函數

styled.h1 函數返回一個 React Component , styled components 會為這個 React Component 添加一個 class ,該 class 的值為一個隨機字符串。傳給 styled.h1 的模板字符串參數的值實際上是 CSS 語法,這些 CSS 會附加到該 React Component 的 class 中,從而為 React Component 添加樣式

淺談styled-components的用法

二、基于 props 定制主題

const Button = styled.button`
  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;
`;

render(
  <div>
    <Button>Normal</Button>
    <Button primary>Primary</Button>
  </div>
);

我們在組件中傳入的所有 props 都可以在定義組件時獲取到,這樣就可以很容易實現組件主題的定制。如果沒有 styled-components 的情況下,需要使用組件 style 屬性或者定義多個 class 的方式來實現

三、組件樣式繼承

通常在 css 中一般會通過給 class 傳入多個 name 通過空格分隔的方式來復用 class 定義,類似 class="button tomato" 。在 styled-components 中利用了 js 的繼承實現了這種樣式的復用:

const Button = styled.button`
  color: palevioletred;
  font-size: 1em;
  margin: 1em;
  padding: 0.25em 1em;
  border: 2px solid palevioletred;
  border-radius: 3px;
`;

const TomatoButton = Button.extend`
  color: tomato;
  border-color: tomato;
`;

子組件中的屬性會覆蓋父組件中同名的屬性

四、組件內部使用 className

在日常開發(fā)中總會出現覆蓋組件內部樣式的需求,你可能想在 styled-components 中使用 className ,或者在使用第三方組件時。

<Wrapper>
  <h4>Hello Word</h4>
  <div className="detail"></div>
</Wrapper>

五、組件中維護其他屬性

styled-components 同時支持為組件傳入 html 元素的其他屬性,比如為 input 元素指定一個 type 屬性,我們可以使用 attrs 方法來完成

const Password = styled.input.attrs({
  type: 'password',
})`
  color: palevioletred;
  font-size: 1em;
  border: 2px solid palevioletred;
  border-radius: 3px;
`;

在實際開發(fā)中,這個方法還有一個有用處,用來引用第三方類庫的 css 樣式:

const Button = styled.button.attrs({
  className: 'small',
})`
  background: black;
  color: white;
  cursor: pointer;
  margin: 1em;
  padding: 0.25em 1em;
  border: 2px solid black;
  border-radius: 3px;
`;

編譯后的 html 結構如下:

<button class="sc-gPEVay small gYllyG">
  Styled Components
</button>

可以用這種方式來使用在別處定義的 small 樣式,或者單純?yōu)榱俗R別自己定義的 class ,因為正常情況下我們得到的 class 名是不可讀的編碼

六、CSS 動畫支持

styled-components 同樣對 css 動畫中的 @keyframe 做了很好的支持。

import { keyframes } from 'styled-components';
const fadeIn = keyframes`
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
`;

const FadeInButton = styled.button`
  animation: 1s ${fadeIn} ease-out;
`;

七、兼容現在已有的 react components 和 css 框架

styled-components 采用的 css-module 的模式有另外一個好處就是可以很好的與其他的主題庫進行兼容。因為大部分的 css 框架或者 css 主題都是以 className 的方式進行樣式處理的,額外的 className 和主題的 className 并不會有太大的沖突

styled-components 的語法同樣支持對一個 React 組件進行擴展

const StyledDiv = styled(Row)`
  position: relative;
  height: 100%;
  .image img {
    width: 100%;
  }
  .content {
    min-height: 30em;
    overflow: auto;
  }
  .content h2 {
    font-size: 1.8em;
    color: black;
    margin-bottom: 1em;
  }
`;

缺點

不能用 stylelint 檢查你的 Css 代碼

【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業(yè)用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。

相關文檔推薦

主站蜘蛛池模板: 亚洲自拍偷拍视频 | 国产欧美日韩精品一区二区三区 | 亚洲电影在线播放 | 综合色婷婷| 亚洲精品视频一区 | 免费的av| 在线观看www视频 | 九九福利 | 亚洲九九 | 国产视频综合 | 国产精品一区在线观看你懂的 | 亚洲不卡在线观看 | 91久久久久久久久 | 久久久噜噜噜www成人网 | 久草中文在线观看 | 亚洲精品第一 | 久久国产亚洲 | 免费特黄视频 | 一区二区高清 | 青青久久久 | 久久成人免费 | 国产剧情一区 | 天天摸天天看 | www.99热这里只有精品 | 97国产一区二区精品久久呦 | 日韩久久久久久 | 国产一级视频在线播放 | 亚洲精品国产成人 | 狠狠操电影 | 天天干天天干 | 日韩一区在线观看视频 | 亚洲国产精品久久久久久 | 欧美高清hd| 久久精品久久精品 | 精品国产乱码久久久久久1区2区 | 一区二区三区国产精品 | 国产黄色网址在线观看 | 日韩欧美在线一区 | 亚洲国产成人精品女人 | 欧美一级精品片在线看 | 91九色在线观看 |