@emotion/styled中文文檔總結(jié)

原文鏈接:https://emotion.sh/docs/styled

styled是一種創(chuàng)建帶有樣式的React組件的方法

1.寫一個(gè)帶樣式的組件

語法:styled.元素名`樣式`

import styled from '@emotion/styled'
const Button = styled.button`
  color: turquoise;
`
render(<Button>This my button component.</Button>)

2.通過參數(shù)控制樣式

import styled from '@emotion/styled'
import {Fragment} from 'react';
const Button = styled.button`
  color: ${props =>
    props.primary ? 'hotpink' : 'turquoise'};
`
<Fragment>
  <Button primary>This is a primary button.</Button>
  <Button>This is a primary button.</Button>
</Fragment>

3.通過傳className創(chuàng)建組件

語法:styled( ({className}) => (<p className={className}>text</
p>) )`樣式`
分析:相當(dāng)于把樣式通過className傳遞給了元素

import styled from '@emotion/styled'
const Basic = ({ className }) => (
  <div className={className}>Some text</div>
)
const Fancy = styled(Basic)`
  color: hotpink;
`
render(<Fancy />)

4.創(chuàng)建與某個(gè)組件相同的樣式

語法:樣式組件.withComponent('元素')

import styled from '@emotion/styled'
const Section = styled.section`
  background: #333;
  color: #fff;
`
// Aside樣式跟Section樣式相同
const Aside = Section.withComponent('aside')
render(
  <div>
    <Section>This is a section</Section>
    <Aside>This is an aside</Aside>
  </div>
)

5.嵌套寫法 - ${子組件}

語法:父組件 = styled.元素`${子組件} {樣式}`

import styled from '@emotion/styled'
const Child = styled.div`
  color: red;
`
const Parent = styled.div`
  ${Child} {
    color: green;
  }
`
render(
  <div>
    <Parent>
      <Child>Green because I am inside a Parent</Child>
    </Parent>
    <Child>Red because I am not inside a Parent</Child>
  </div>
)

6.嵌套寫法 - 對(duì)象(鍵值對(duì))

語法:父組件 = styled.元素(
{
[子組件]: {樣式}}
)

import styled from '@emotion/styled'
const Child = styled.div({
  color: 'red'
})
const Parent = styled.div({
  [Child]: {
    color: 'green'
  }
})
render(
  <div>
    <Parent>
      <Child>green</Child>
    </Parent>
    <Child>red</Child>
  </div>
)

7.對(duì)象樣式

import styled from '@emotion/styled'
const H1 = styled.h1(
  {
    fontSize: 20
  },
  props => ({ color: props.color,  width:props.width })
)
render(<H1 color="lightgreen" width="200px">This is lightgreen.</H1>)

8.

import isPropValid from '@emotion/is-prop-valid'
import styled from '@emotion/styled'

const H1 = styled('h1', {
  shouldForwardProp: prop =>
    isPropValid(prop) && prop !== 'color'
})(props => ({
  color: 'hotpink'
}))

render(<H1 color="lightgreen">This is lightgreen.</H1>)

9.動(dòng)態(tài)樣式

import styled from '@emotion/styled'
import { css } from '@emotion/core'
const dynamicStyle = props =>
  css`
    color: ${props.color};
  `
const Container = styled.div`
  ${dynamicStyle};
`
render(
  <Container color="lightgreen">
    This is lightgreen.
  </Container>
)

10.as prop

要使用樣式化組件中的樣式但要更改呈現(xiàn)的元素赂摆,可以使用as prop执解。

import styled from '@emotion/styled'

const Button = styled.button`
  color: hotpink;
`
render(
  <Button
    as="a"
    
  >
    Emotion on GitHub
  </Button>
)

11.嵌套元素樣式寫法

import styled from '@emotion/styled'
const Example = styled('span')`
  color: lightgreen;
  & > a {
    color: hotpink;
  }
`
render(
  <Example>
    This is <a>nested</a>.
  </Example>
)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子宪赶,更是在濱河造成了極大的恐慌晒哄,老刑警劉巖毁习,帶你破解...
    沈念sama閱讀 218,122評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件牍鞠,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡爹土,警方通過查閱死者的電腦和手機(jī)甥雕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來胀茵,“玉大人社露,你說我怎么就攤上這事∏砟铮” “怎么了峭弟?”我有些...
    開封第一講書人閱讀 164,491評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長轨奄。 經(jīng)常有香客問我孟害,道長,這世上最難降的妖魔是什么挪拟? 我笑而不...
    開封第一講書人閱讀 58,636評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮击你,結(jié)果婚禮上玉组,老公的妹妹穿的比我還像新娘。我一直安慰自己丁侄,他們只是感情好惯雳,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,676評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著鸿摇,像睡著了一般石景。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上拙吉,一...
    開封第一講書人閱讀 51,541評(píng)論 1 305
  • 那天潮孽,我揣著相機(jī)與錄音,去河邊找鬼筷黔。 笑死往史,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的佛舱。 我是一名探鬼主播椎例,決...
    沈念sama閱讀 40,292評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼挨决,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了订歪?” 一聲冷哼從身側(cè)響起脖祈,我...
    開封第一講書人閱讀 39,211評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎刷晋,沒想到半個(gè)月后撒犀,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,655評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡掏秩,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,846評(píng)論 3 336
  • 正文 我和宋清朗相戀三年或舞,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蒙幻。...
    茶點(diǎn)故事閱讀 39,965評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡映凳,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出邮破,到底是詐尸還是另有隱情诈豌,我是刑警寧澤,帶...
    沈念sama閱讀 35,684評(píng)論 5 347
  • 正文 年R本政府宣布抒和,位于F島的核電站矫渔,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏摧莽。R本人自食惡果不足惜庙洼,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,295評(píng)論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望镊辕。 院中可真熱鬧油够,春花似錦、人聲如沸征懈。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽卖哎。三九已至鬼悠,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間亏娜,已是汗流浹背焕窝。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留照藻,地道東北人袜啃。 一個(gè)月前我還...
    沈念sama閱讀 48,126評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像幸缕,于是被迫代替她去往敵國和親群发。 傳聞我的和親對(duì)象是個(gè)殘疾皇子晰韵,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,914評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容