本篇內(nèi)容主要講解“用CSS-in-JS來做的事情有哪些”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“用CSS-in-JS來做的事情有哪些”吧!
為棗陽等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及棗陽網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、棗陽網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!除了傳統(tǒng)的 CSS,你還可以使用 內(nèi)聯(lián)樣式
和 CSS-in-JS
作為 React 應(yīng)用程序的樣式選項。
對于內(nèi)聯(lián)樣式,你可以將 JavaScript對象傳遞給樣式屬性:
const myStyle = { fontSize: 24, lineHeight: '1.3em', fontWeight: 'bold', }; <span style={myStyle}>Hello World!</p>
然而,并非所有 CSS 特性都受支持。
另一方面,CSS-in-JS 是一種使用 JavaScript來設(shè)置組件樣式的技術(shù)。在解析此 JavaScript時,會生成 CSS(通常作為 <style>
元素)并附加到 DOM 中。
這個功能由第三方庫實現(xiàn)。例如,下面是使用 Aphrodite 實現(xiàn)的上一個示例:
import { StyleSheet, css } from 'aphrodite'; const styles = StyleSheet.create({ myStyle: { fontSize: 24, lineHeight: '1.3em', fontWeight: 'bold', } }); <span className={css(styles.myStyle)}>Hello World!</p>
其他第三方庫推薦:
Emotion
JSS
Radium
Styled-components
我并不完全贊成使用 CSS-in-JS,但我不得不說,其中一些庫增加了對在某些情況下可能會有用的功能支持。
在這篇文章中,我將討論在 CSS-in-JS 中你可以用上面的庫來做的五件事,而我打賭這是你不知道的。
像 styled-components 和 emotion 庫允許您使用 標(biāo)記模板文字 從樣式中創(chuàng)建 React 組件:
import styled from 'styled-components'; // Create a component that renders a <p> element with blue text const BlueText = styled.p` color: blue; `; <BlueText>My blue text</BlueText>
它們也允許你定位于其他樣式組件(像你使用 CSS 選擇器一樣):
const ImportantText = styled.div` font-weight: bold; `; const Text = styled.div` color: gray; ${ImportantText} { font-style: italic; } `; render( <div> <Text> Text in gray <ImportantText>Important text in gray, bold and italic</ImportantText> </Text> <ImportantText>Important text bold</ImportantText> </div> );
這在組合偽類時很有用,例如,在懸停時更改組件的顏色:
const Text = styled.div` color: gray; &:hover ${ImportantText} { color: red; } `;
假設(shè)你已經(jīng)使用 Aphrodite 為你的應(yīng)用程序設(shè)計樣式,現(xiàn)在你需要支持主題。
但問題是 Aphrodite 不能輕易地支持主題。 至少不像 Emotion 那么容易。
不過,這里有兩個項目將 JSS
的核心與 Aphrodite
和 styled-components
相結(jié)合,
aphrodite-jss 和 styled-jss
。
通過這種方式,你可以保留 Aphrodite
(或 styled-components
) 的優(yōu)點,并使用 JSS
的所有特性和 插件
,從 規(guī)則緩存 到 規(guī)則隔離
,以及
主題
,主題包,以下是它提供的高階組件:
ThemeProvider
:通過 context 向 react 樹傳遞主題對象。
withTheme
:允許你接收一個主題對象并作為屬性來更新。
例如:
const blackTheme = { color: 'black', }; const App = () => ( <ThemeProvider theme={blackTheme}> <MyComponent /> </ThemeProvider> );
在 Aphrodite
和主題的案例中,作為另一個例子,你也可以使用 react-with-styles
,它有實現(xiàn) Aphrodite 或 JSS 接口,這樣在定義樣式時就可以訪問主題信息了。
與內(nèi)聯(lián)樣式不同,CSS-in-JS 允許你使用關(guān)鍵幀定義動畫。 例如,這是使用styled-components
做的:
const heightAnimation = keyframes` 0% { height: 0; } 100% { height: 200; } `; const myComponent = styled.div` display: inline-block; width: 200; position: relative; animation-name: ${heightAnimation}; animation-duration: 1.5s; animation-timing-function: ease; `;
但是很多人不知道的是,你可以通過在 animation
屬性中使用多個關(guān)鍵幀對象來鏈接多個動畫。 下面是修改后的兩個動畫的例子:
const heightAnimation = keyframes` 0% { height: 0; } 100% { height: 200; } `; const rotateAnimation = keyframes` 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } `; const myComponent = styled.div` display: inline-block; width: 200; position: relative; animation: ${props => css` ${heightAnimation} 1.5s ease infinite, ${rotateAnimation} 1.5s linear infinite `} `;
Radium
是另一個通過傳遞關(guān)鍵幀對象數(shù)組作為 animationName
屬性值來支持多個 動畫 的庫:
const heightAnimation = Radium.keyframes( { 0% { height: 0; } 100% { height: 200; } }, 'myHeightAnimation', ); const rotateAnimation = Radium.keyframes( { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }, 'myRotateAnimation', ); const styles = { myStyle: { animationName: [heightAnimation, rotateAnimation], animationDuration: '1.5s, 1s', animationIterationCount: 'infinite, infinite', animationTimingFunction: 'ease, linear', display: inline-block; width: 200; position: relative; }, };
CSS 中的一切都是全局的,使用 CSS-in-JS 的目的之一是消除全局樣式定義。
但是,全局樣式的使用有時可能是很有效的,例如,當(dāng)你想對頁面中的每個元素應(yīng)用相同的字體樣式時。
當(dāng)然,你總是可以使用傳統(tǒng)的 CSS,通過 Webpack 導(dǎo)入或在 index.html
文件中聲明它。
但是,如果您真的想在所有樣式中使用 JavaScript,那么有些庫實際上允許您通過 helper
組件或擴展/插件來定義全局樣式。
在 Radium
中,您可以使用 Style 組件來渲染具有全局樣式的樣式元素。 例如:
<Style rules={{ body: { fontFamily: 'Arial, Helvetica, sans-serif' } }} />
將返回:
<style> body { font-family: 'Arial, Helvetica, sans-serif'; } </style>
JSS
使用一個 插件 來編寫全局樣式:
const styles = { '@global': { body: { fontFamily: 'Arial, Helvetica, sans-serif' } } }
在 Aphrodite
中,你可以用 第三方擴展 來做:
import {injectGlobalStyles} from "aphrodite-globals"; injectGlobalStyles({ "body": { fontFamily: 'Arial, Helvetica, sans-serif', } });
或者通過 aphrodit-jss 來使用 JSS
全局插件。
有些庫包含用于測試組件樣式的工具。
Aphrodite
提供了一個沒有文檔說明(至少在寫這篇文章的時候是這樣)的對象
StyleSheetTestUtils
,它僅適用于非生產(chǎn)環(huán)境(process.env.NODE_ENV!=='production'
),有三個方法:
suppressStyleInjection
:它阻止樣式被注入到DOM中,當(dāng)你想要在沒有DOM的情況下測試Aphrodite
組件的輸出時非常有用。
clearBufferAndResumeStyleInjection
:它與 suppressStyleInjection
相反,所以它們應(yīng)該搭配使用。
getBufferedStyles
:它返回尚未刷新的緩沖樣式字符串。
以下是如何使用它們的示例:
import { StyleSheetTestUtils, css } from 'aphrodite'; //... beforeEach(() => { StyleSheetTestUtils.suppressStyleInjection(); }); afterEach(() => { StyleSheetTestUtils.clearBufferAndResumeStyleInjection(); }); test('my test', () => { const sheet = StyleSheet.create({ background: { backgroundColor: 'blue' }, }); css(sheet.background); // buffer will contain something like [ ".background_k554e1{background-color:blue !important;}" ] const buffer = StyleSheetTestUtils.getBufferedStyles(); // ... });
Radium
是另一個例子。它有一個 TestMode 對象,用于在測試期間使用 clearState
,enable
和 disable
方法控制內(nèi)部狀態(tài)和行為。
到此,相信大家對“用CSS-in-JS來做的事情有哪些”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
分享題目:用CSS-in-JS來做的事情有哪些-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://aaarwkj.com/article26/cogijg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、電子商務(wù)、用戶體驗、靜態(tài)網(wǎng)站、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容