样式系统中的标题组件

问题描述

如何使用样式系统和样式组件创建通用标题组件?

以下有效,但我所有的标题都使用 h1 标签。

import css from "@styled-system/css"
import styled from "styled-components"
import { color,ColorProps,textAlign,TextAlignProps,variant } from "styled-system"

type Props = {
  level: 1 | 2 | 3 | 4 | 5 | 6
} & TextAlignProps &
  ColorProps

const Css = css({
  fontFamily: "heading",fontWeight: "heading",})

const Variant = variant({
  prop: "level",variants: {
    1: {
      fontSize: 7,lineHeight: 7,},2: {
      fontSize: 6,lineHeight: 6,3: {
      fontSize: 5,lineHeight: 5,4: {
      fontSize: 4,lineHeight: 4,5: {
      fontSize: 3,lineHeight: 3,6: {
      fontSize: 2,lineHeight: 2,})

const Heading = styled("h1")<Props>(Css,Variant,color)

export default Heading

我尝试创建一个 HeadingBase 组件,例如:

type Props = {
  level: 1 | 2 | 3 | 4 | 5 | 6,as: string | undefined
} & TextAlignProps &
  ColorProps

const HeadingBase = ({ level,as: Component = `h${level}`,...props }: Props) => <Component {...props} />

const Heading = styled(HeadingBase)<Props>(Css,color)

但我在 <Component {...props} /> 处收到错误消息,因为它没有用于 TextAlignProps 和 ColorProps 的 API。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)