react组件之间如何使用接收到的className(封装一个按钮案例)

带有hover渐变效果 

一、父组件

import LineGradientBox from '../line-gradient-box';
import styles from './index.module.scss';

 <LineGradientBox
  fontSize={20}
  className={styles.btn_height}
  text='Sign up'
  width="100%"
  onClick={() => {
    navigate('/sign-up');
  }}
/>

 需要传递样式在父组件中先定义好(sitepx函数用法):

.btn_height {
    height: sitepx(50);
}

二、子组件

import clsx from 'clsx';
import styles from './index.module.scss';
const LineGradientBox: React.FC<{
  text: string;
  width: string | number;
  height?: number;
  fontSize?: number;
  className?: string;
  onClick?: () => void;
  beforeTextIcon?: React.ReactNode;
}> = ({ text, width, height, fontSize, className, beforeTextIcon, onClick }) => {
  return (
    <div
      onClick={onClick}
      className={clsx(styles.box, className || '')}
      style={{ minWidth: width, height, lineHeight: height + 'px' }}
    >
      <div className={styles.bgfff} />

      <div
        className={styles.box_content}
        style={{ ...(fontSize ? { fontSize: fontSize + 'px' } : {}) }}
      >
        {beforeTextIcon}
        {text}
      </div>
    </div>
  );
};

export default LineGradientBox;

子组件样式:

.box {
  text-align: center;
  border-radius: 6px;
  overflow: hidden;
  background: $line-gradient-init-color;
  padding: 1px;
  position: relative;
  cursor: pointer;
  padding: 0 10px;

  &_content {
    position: relative;
    z-index: 2;
    width: 100%;
    height: 100%;
    background: $line-gradient-init-color;
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;

    display: flex;
    justify-content: center;
    align-items: center;
    & path {
      stroke: linear-gradient(90deg, #8057ff 0%, #936bff 50%, #b892ff 100%) !important;
    }
  }

  &:hover {
    color: var(--color-primary);
    background: $line-gradient-hover-color;

    .box_content {
      background: $line-gradient-hover-color;
      background-clip: text;
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
    }
  }


  .bgfff {
    position: absolute;
    z-index: 1;
    border-radius: 5px;
    top: 1px;
    left: 1px;
    width: calc(100% - 2px);
    height: calc(100% - 2px);
    background-color: #fff;
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值