基本用法
import styled, { css } from 'styled-components'
const { Item: BreadcrumbItem } = Breadcrumb
// 定义样式
const StyledPointer = css`
cursor: pointer;
`
// 定义组件
const StyledH1 = styled.div`
font-size: 20px;
font-weight: 600;
`
// 修改组件
const StyledBreadcrumbItem = styled(BreadcrumbItem)`
font-size: 18px; // 会覆盖 BreadcrumbItem font-size属性
`
// 动态样式写法一:根据组件是否存在对应属性
// 如果 StyledBreadcrumbItem 组件存在 pointer 属性,则会添加 StyledPointer 样式
const StyledBreadcrumbItem = styled(BreadcrumbItem)`
font-weight: 400;
${props => props.pointer && StyledPointer};
${props => props.primary && css`
background: white;
`}
`
<StyledBreadcrumbItem pointer>123</StyledBreadcrumbItem>
// 动态样式写法二:对象模式,根据组件对应属性的属性值
// 根据 StyledBreadcrumbItem 组件传递的cursor属性值 动态设置 cursor样式
const StyledBreadcrumbItem = styled(BreadcrumbItem)(props => ({
fontWeight: 400,
cursor: props.cursor,
}))
<StyledBreadcrumbItem cursor="pointer">123</StyledBreadcrumbItem>
设置图片
// 静态图片
const StyledArrow = Styled.div`
position: relative;
top: 10px;
width: 27px;
height: 22px;
background-image: url(${IMG_ARROW});
background-size: 100% 100%;
`
// 动态图片
// 写法一
const StyledImageBox = styled.div`
width: 275px;
height: 275px;
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
position: relative;
background-image: url(${props => props.assetsImg})
`
// 写法二
const StyledImageBox = styled.div(props => ({
width: '275px',
height: '275px',
borderBottomLeftRadius: '4px',
borderBottomRightRadius: '4px',
position: 'relative',
backgroundImage: `url(${props.assetsImg})`,
}))
<StyledImageBox assetsImg={detailData.fundImg}></StyledImageBox>