styled-components

1 min read
Table of Contents

styled-components

import React from 'react'
import styled from 'styled-components'

const Button = styled.button<{ primary?: boolean }>`
  ${(props) => (props.primary ? 'background-color: blue' : 'background-color: red')};
  color: white !important;
  padding: 10px 20px;
  border-radius: 5px;
  border: none;
  cursor: pointer;
`

const ErrorButton = styled.button`
  background-color: red;
  color: white;
  padding: 10px 20px;
  border-radius: 5px;
  border: none;
  cursor: pointer;
`
const LinkButton = styled.button`
  background-color: transparent;
  color: blue;
  padding: 10px 20px;
  border-radius: 5px;
  border: none;
  cursor: pointer;
`
const Input = styled.input.attrs<{ defaultValue: number }>((props) => {
  return {
    type: 'number',
    defaultValue: props.defaultValue,
  }
})`
  width: 100px;
  height: 30px;
  border: 1px solid #ccc;
  border-radius: 5px;
  padding: 0 10px;
  text-align: center;
`

export default function StyledComponents() {
  return (
    <div className="flex flex-row gap-2">
      <Button primary>Click me</Button>
      <Button>Click me</Button>
      <ErrorButton>Click me</ErrorButton>
      <LinkButton>Click me</LinkButton>
      <Input defaultValue={1} />
    </div>
  )
}

后记

My avatar

感谢你读到这里。

这座小站更像一份持续维护的“终端笔记”:记录我解决问题的过程,也记录走过的弯路。

如果这篇内容对你有一点点帮助:

  • 点个赞 / 收藏一下,方便你下次回来继续翻
  • 欢迎在评论区补充你的做法(或者指出我的疏漏)
  • 想持续收到更新:可以订阅 RSS(在页面底部)

我们下篇见。


More Posts

评论

评论 (0)

请先登录后再发表评论

加载中...