当前位置: 首页 > news >正文

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

带有hover渐变效果 

一、父组件

import LineGradientBox from '../line-gradient-box';
import styles from './index.module.scss';<LineGradientBoxfontSize={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 (<divonClick={onClick}className={clsx(styles.box, className || '')}style={{ minWidth: width, height, lineHeight: height + 'px' }}><div className={styles.bgfff} /><divclassName={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;}
}

相关文章:

  • MongoDB 集合名称映射问题
  • MongoDB索引
  • 【算法】BFS-解决FloodFill问题
  • React项目添加react-quill富文本编辑器,遇到的问题,比如hr标签丢失
  • Apache SeaTunnel:新一代开源、高性能数据集成工具
  • QTextDocument 入门
  • 屏幕适配常见BUG与兼容性问题
  • 7N60-ASEMI无人机专用功率器件7N60
  • 低空经济 WebGIS 无人机配送 | 图扑数字孪生
  • Linux嵌入式系统SQlite3数据库学习笔记
  • 【数据可视化-22】脱发因素探索的可视化分析
  • 基于 WebRTC + Pion + HeyGem.ai 实现可互动的数字人系统
  • 复刻低成本机械臂 SO-ARM100 3D 打印篇
  • LeetCode 1292 元素和小于等于阈值的正方形的最大边长
  • H5付费进群源码 带分销【源码+教程】虚拟货物系统
  • 作为一个创业团队,Neo4j、Nebula Graph、HugeGraph‌、AllegroGraph‌等几款图数据库哪款更合适?
  • 爆改 toxml 组件 支持数据双向绑定 解决数据刷新问题
  • mybatis mapper.xml中使用枚举
  • AVX2与onnx量化加速
  • 【MCP Node.js SDK 全栈进阶指南】中级篇(4):MCP错误处理与日志系统
  • 美团回应京东“二选一”指控:没有任何理由对某平台进行任何限制
  • 市场监管部门完成全国保健食品生产企业体系检查首轮全覆盖
  • 被指违反代理协议遭南航暂停售票资格,去哪儿网:今起恢复
  • 世界读书日|南京图书馆开了首个网络文学主题阅读空间
  • 两名中国公民在墨尔本被海浪卷走,我领馆发文提醒
  • 体坛联播|中国U16女足击败墨西哥,王星昊首获世界冠军