挑战用React封装100个组件【001】

项目地址
https://github.com/hismeyy/react-component-100

组件描述

组件适用于需要展示图文信息的场景,比如产品介绍、用户卡片或任何带有标题、描述和可选图片的内容展示

样式展示

代码展示

InfoCard.tsx
js 复制代码
import './InfoCard.css'

interface InfoCardProps {
    title: string;
    description: string;
    imgSrc: string;
    imgAlt?: string;
}

const InfoCard = ({ 
    title, 
    description, 
    imgSrc, 
    imgAlt = title
}: InfoCardProps) => {
    return (
        <div className='info-card'>
            <div className='img'>
                {imgSrc && <img src={imgSrc} alt={imgAlt} />}
            </div>
            <div className="info">
                <h6>{title}</h6>
                <p>{description}</p>
            </div>
        </div>
    )
}

export default InfoCard
InfoCard.css
js 复制代码
.info-card {
    box-sizing: border-box;
    width: 240px;
    height: 80px;
    border-radius: 10px;
    background-color: #F5F5F5;
    box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.1);
    display: flex;
    justify-content: left;
    align-items: center;
    padding: 5px 5px 5px 10px;
    cursor: pointer;
    transition: all 0.3s ease;
    transform-origin: center;
}

.info-card .img {
    width: 60px;
    height: 60px;
    border-radius: 6px;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: transform 0.3s ease;
}

.info-card .img img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.info-card .info{
    margin-left: 5px;
    height: 60px;
}

.info-card .info h6 {
    all: unset;
    display: block;
    width: 160px;
    font-size: 14px;
    font-weight: bold;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: color 0.3s ease;
}

.info-card .info p {
    all: unset;
    display: -webkit-box;
    width: 160px;
    margin-top: 10px;
    font-size: 12px;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
    word-wrap: break-word;
    line-height: 1.3;
    transition: color 0.3s ease;
    line-clamp: 2;
    box-orient: vertical;
}

.info-card:hover {
    transform: translateY(-2px) scale(1.02);
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
    background-color: #ffffff;
}

.info-card:hover .img {
    transform: scale(1.05);
}

.info-card:hover .info h6 {
    color: #f08a5d;
}

.info-card:hover .info p {
    color: #666;
}

使用

App.tsx
js 复制代码
import './App.css'
import InfoCard from './components/card/infoCard01/InfoCard'

function App() {
  return (
    <>
      <InfoCard
        title="React 18 新特性"
        description="React 18 带来了令人兴奋的新特性,包括自动批处理、Transitions API 和 Suspense 的改进。这些更新显著提升了应用性能和用户体验。"
        imgSrc="https://cdn1.iconfinder.com/data/icons/programing-development-8/24/react_logo-512.png"
      />
    </>
  )
}

export default App
相关推荐
抱抱宝37 分钟前
Agent-study项目教程(03):手写 Mini-ReAct Agent(不依赖框架)
javascript·人工智能·gpt·react.js·prompt·agent
想要成为糕糕手6 小时前
🚀 在浏览器里跑 DeepSeek-R1?WebGPU 端侧推理实战(六)—— 完结篇:消息渲染与流式收尾
react.js·typescript·llm
YIAN7 小时前
从 Hash 底层原理到 React Router v6 实战:我学会了什么?
前端·react.js·vue-router
懒人wsh7 小时前
axios 封装那点破事:401 无感刷新踩过的坑
前端框架
阿懂在掘金8 小时前
同一份弹窗我重构了三次:从 v-model 地狱到路由式调用,终于治好了模板臃肿
前端·vue.js·前端框架
YUS云生8 小时前
大模型学习·第46天:Agent智能体核心概念与ReAct框架
前端·学习·react.js
To_OC8 小时前
后端接口还没交付,前端如何独立把整套业务跑通
前端·react.js·全栈
程序员跑路9 小时前
Barrel Export、Jest、Babel 与依赖图
react.js
l1m0_10 小时前
告别反复修改Prompt:AI生成React应用并落地开发的实战复盘
前端·react.js·ui·ai·设计
xexpertS10 小时前
前端工程转型实践:从 Ember 迁移到 React,提升构建速度与研发效能
前端·react.js·前端框架