使用styled-components實現CSS in JS

来源:https://www.cnblogs.com/xiaohuochai/archive/2018/03/10/8488057.html
-Advertisement-
Play Games

[1]基本用法 [2]組件樣式 [3]擴展樣式 [4]更換標簽 [5]傳遞屬性 [6]attrs函數 [7]動畫 [8]添加類名 ...


前面的話

  使用jsx語法可以實現HTML in JS,使用svgr可以實現svg in JS,使用styled-components可以實現CSS in JS。這樣,使用react開發,就變成了使用JS開發,統一且方便。本文將詳細介紹styled-components的用法

 

基本用法

【安裝】

$ npm install styled-components

  使用非常簡單,下麵的代碼片段展示了 React 項目中使用 styled-components,定義了 Wrapper 和 Button 兩個組件,包含了 html 結構和對應的 css 樣式,從而將樣式和組件之間的 class 映射關係移除

import styled from 'styled-components';
const Wrapper = styled.section`
  margin: 0 auto;
  width: 300px;
  text-align: center;
`;
const Button = styled.button`
  width: 100px;
  color: white;
  background: skyblue;
`;
render(
  <Wrapper>
    <Button>Hello World</Button>
  </Wrapper>
);

 

組件樣式

  如果要為組件設置樣式,則需要使用小括弧語法,而且需要在組件上設置className和children

const Link = ({ className, children }) => (
  <a className={className}>
    {children}
  </a>
)
const StyledLink = styled(Link)`
  color: palevioletred;
  font-weight: bold;
`;
render(
  <div>
    <Link>Unstyled, boring Link</Link>
    <br />
    <StyledLink>Styled, exciting Link</StyledLink>
  </div>
);

  

擴展樣式

  使用擴展樣式,可以基於已經存在的樣式進行擴展

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;
`;

render(
  <div>
    <Button>Normal Button</Button>
    <TomatoButton>Tomato Button</TomatoButton>
  </div>
);

 

更換標簽

  在某些情況下,可以在復用樣式的基礎上更換標簽

const Button = styled.button`
  display: inline-block;
  color: palevioletred;
  font-size: 1em;
  margin: 1em;
  padding: 0.25em 1em;
  border: 2px solid palevioletred;
  border-radius: 3px;
`;
const Link = Button.withComponent('a')
const TomatoLink = Link.extend`
  color: tomato;
  border-color: tomato;
`;
render(
  <div>
    <Button>Normal Button</Button>
    <Link>Normal Link</Link>
    <TomatoLink>Tomato Link</TomatoLink>
  </div>
);

 

傳遞屬性

  通過props可以從父組件向子組件傳遞屬性

const GlassModal = ({ children, className, backgroundColor, padding }) => (
  <Wrap backgroundColor={backgroundColor}>
    <Main padding={padding} className={className}>
      {children}
    </Main>
  </Wrap>
)
export default GlassModal

const Wrap = styled.section`
  background-color: ${props => props.backgroundColor || BLUE_DARK};
`
const Main = styled.main`
  padding: ${props => props.padding || '0'};
  background-color: ${OPACITY_LIGHT};
`
const StyledGlassModal = styled(GlassModal)`
  padding: 20px 10px;
  text-align: center;
`

  或者,基於prop來定製主題

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>
);

 

attrs函數

  通過使用attrs函數,可以用來設置其他屬性

const Input = styled.input.attrs({
  type: 'password',
  margin: props => props.size || '1em',
  padding: props => props.size || '1em'
})`
  color: palevioletred;
  border-radius: 3px;
  margin: ${props => props.margin};
  padding: ${props => props.padding};
`;
render(
  <div>
    <Input placeholder="A small text input" size="1em" />
    <Input placeholder="A bigger text input" size="2em" />
  </div>
);

  或者引入第三方庫的樣式,如activeClassName

const Button = styled.button.attrs({
  className: 'small',
})`
  background: black;
  color: white;
`;

  編譯後的 html 結構如下:

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

 

動畫

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

import { keyframes } from 'styled-components';
const rotate360 = keyframes`
  from {transform: rotate(0deg);}
  to {transform: rotate(360deg);}
`;
const Rotate = styled.div`
  display: inline-block;
  animation: ${rotate360} 2s linear infinite;

`;
render(
  <Rotate>&lt; 

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • IBOutlet或IBAction符號對編譯不產生任何影響,它們只是標記,用於告訴Xcode這些對象可以和UI控制項進行關聯,以便於在編輯Interface Builder上的UI控制項的時候Xcode可以自動提示。 參考資料:《iOS編程指南》 ...
  • 近日,樓主在同一臺手機上,同時安裝同一個游戲的不同渠道包,add install後,提示:Failure [INSTALL_FAILED_DUPLICATE_PERMISSION perm=android.permission.具體許可權 pkg=渠道A游戲包名] 原因: 1、正要安裝的APP的自定義 ...
  • 當我們用RecyclerView時,如果想用某一個特定的版本,怎樣才能知道版本號呢?如果自己的筆記本中用過這個庫,那麼會保存在本地硬碟中。 Android自身依賴包的版本號本地存放路徑: 沒有用過該版本,也沒有關係,google develper中有說明:Support Library的版本號其實都 ...
  • 今天supprt28遇到的問題,由於28還是預覽版,還存在一些bug 都是因為如果程式內出現不同的,support或者其他外部引用庫的多個版本,Gradle在進行合併的時候會使用本地持有的,最高版本的來進行編譯,所以25的support就有可能引用26的東西,就會出現 屬性 merge 錯誤 ,或者 ...
  • 一、Gradle更新問題 Android Studio每次更新版本都會更新Gradle這個插件,而且有時候提示更新,卻一直更新不了,那是因為中國偉大的長城問題。就是下圖,我剛剛更新了,提示更新gradle,卻一直更新不了! 1.1 這有時候會導致停止在Refreshing Gradle Projec ...
  • 這個問題搞得我頭大,並且在查過百度後各位大佬給出的解釋我都不能理解,應該是我太白的原因,希望我寫的能好理解。 下麵文章視窗1、2、3,在代碼里分別為chuangkou、chuangkou1、chuangkou2. 下麵是我創建的一個及其簡易html框架結構: 製作這個網頁需要八個html文件,分別是 ...
  • 總結一下目前在用的前端代碼規範,可作為開發參考 一、基礎規範 開發規範 項目目錄和文件的命名使用小寫字母,避免使用大寫或駝峰,多個單詞以下劃線 _ 分隔 如:my_project/cast_detail.js 目錄有複數意義的時候,使用複數命名 如 scripts images 某些第三方插件可直接 ...
  • 一、簡介 瀏覽器全屏有兩種方式,一種是HTML5新增的requestFullscreen全屏,另一種是摁F11實現的全屏,本文將詳解兩種全屏的特點以及其實現。 二、requestFullscreen全屏 1、判斷是否支持全屏 通過判斷元素的requestFullscreen函數是否存在與docume ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...