首页 > 其他分享 >在React中,怎么用tailwind css(就叫顺丰吧 :D 。。。)封装Button组件

在React中,怎么用tailwind css(就叫顺丰吧 :D 。。。)封装Button组件

时间:2023-01-24 18:56:02浏览次数:64  
标签:lg bg tailwind tw Button focus React hover shadow

我的目的

想用 tailwind css 来快速封装Button组件,而不是从更大型的UI库导入一个Button组件(那样就太大材小用)。

几个工具

开抄

//Button.tsx

import React, { ReactElement, ReactNode } from "react";
import { ButtonType, ButtonSize, buttonSize, buttonType } from "./theme";

interface Props {
  children?: ReactNode;
  size: buttonSize;
  type: buttonType;
}

export default function Button({ children, size, type }: Props): ReactElement {
  const classNames = `${ButtonType[type]} ${ButtonSize[size]}`;
  return <button className={classNames}>{children}</button>;
}

//theme.ts 东西有点多,因为是用血轮眼复制过来的。。。

export const ButtonType = {
  primary:
    "tw-inline-block tw-px-6 tw-py-2.5 tw-bg-blue-600 tw-text-white tw-font-medium  tw-leading-tight tw-uppercase tw-rounded tw-shadow-md hover:tw-bg-blue-700 hover:tw-shadow-lg focus:tw-bg-blue-700 focus:tw-shadow-lg focus:tw-outline-none focus:tw-ring-0 active:tw-bg-blue-800 active:tw-shadow-lg tw-transition tw-duration-150 tw-ease-in-out ",
  secondary:
    "tw-inline-block tw-px-6 tw-py-2.5 tw-bg-purple-600 tw-text-white tw-font-medium  tw-leading-tight tw-uppercase tw-rounded tw-shadow-md hover:tw-bg-purple-700 hover:tw-shadow-lg focus:tw-bg-purple-700 focus:tw-shadow-lg focus:tw-outline-none focus:tw-ring-0 active:tw-bg-purple-800 active:tw-shadow-lg tw-transition tw-duration-150 tw-ease-in-out",
  basic:
    "tw-inline-block tw-px-6 tw-py-2.5 tw-bg-green-500 tw-text-white tw-font-medium  tw-leading-tight tw-uppercase tw-rounded tw-shadow-md hover:tw-bg-green-600 hover:tw-shadow-lg focus:tw-bg-green-600 focus:tw-shadow-lg focus:tw-outline-none focus:tw-ring-0 active:tw-bg-green-700 active:tw-shadow-lg tw-transition tw-duration-150 tw-ease-in-out",
  delete:
    "tw-inline-block tw-px-6 tw-py-2.5 tw-bg-red-600 tw-text-white tw-font-medium  tw-leading-tight tw-uppercase tw-rounded tw-shadow-md hover:tw-bg-red-700 hover:tw-shadow-lg focus:tw-bg-red-700 focus:tw-shadow-lg focus:tw-outline-none focus:tw-ring-0 active:tw-bg-red-800 active:tw-shadow-lg tw-transition tw-duration-150 tw-ease-in-out",
};

export const ButtonSize = {
  sm: "tw-py-2 tw-px-4 tw-text-xs",
  lg: "tw-py-3 tw-px-6 tw-text-lg ",
};

export type buttonSize = keyof typeof ButtonSize;
export type buttonType = keyof typeof ButtonType;

就这样,喵。

标签:lg,bg,tailwind,tw,Button,focus,React,hover,shadow
From: https://www.cnblogs.com/nulixuexipython/p/17065213.html

相关文章

  • [SolidJS] Build a simple version of reactivity
    letcontext=[]functioncleanup(observer){for(constdepofobserver.dependencies){dep.delete(observer)}}functionsubscribe(observer,subscr......
  • 编写简单的button配合input实现上传文件操作
    <template> <button>导入文件<inputtype="file"@change="fileChange"accept=".*":disable="disable"/></button></template><script>d......
  • Unity2D:Update()下 Input.GetKey/GetKeyDown/GetButtonDown失效
    代码如下usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassPauseMenu:MonoBehaviour{publicGameObjectPauseMe......
  • vue3+vite+tailwind+element配置,el-button被覆盖样式
    生产环境下1、vite.config.ts生产环境代码分文件夹存放element自动导入下,解决el-button开发环境下被tailwind覆盖样式base:process.env.NODE_ENV==='production'?......
  • react table ts 示例
    interfaceColumnDetails{[key:string]:string}constdata=useMemo<ColumnDetails[]>(()=>[{col1:'Hello',col2:'World',},......
  • 如何实现在react现有项目中嵌入Blazor?
    如何实现在react现有项目中嵌入Blazor?目前官方只提供了angular和react俩种示例所以本教程只将react教程思路讲解:首先在现有react项目中我们可能某些组件是在Blazor中完......
  • 使用 IDEA 工具打开vue/react/node项目
    1.IDEA在官网上下载社区免费版的https://www.jetbrains.com/zh-cn/idea/download/#section=mac下载完成后,一直点击next安装即可2.安装完成后点击open打开gitcl......
  • React Spring 学习笔记
    ReactSpring学习笔记炎灸纹舞2020年09月15日15:04 ·  阅读5261简介( 官方文档 )react-spring 是 React 上的一个动画库。它基于弹簧物理原理实现,......
  • react,vue中的key有什么作用?(key的内部原理)
    1.虚拟DOM中的key的作用:key是虚拟dom对象的标识,当状态中的数据发生变化时,vue会根据新数据生成新的虚拟dom,随后vue进行新的虚拟dom与旧的虚拟dom的差异比较。2.比较规则(1......
  • react ts
     前提你需要准备好node.js版本不低于6.14.8和 git文章内容比较长(保姆级别教程),全是干货,请耐心看完通过create-react-app脚手架搭建项目1.第一步    注:......