daisyUI怎么解决TailwindCSS堆砌class问题

    daisyUI概述

    daisyUI是一个可定制的TailwindCSS的组件库,目前(发文日期)在GitHub中已经有12.8k的star数量。

    它与现在常用的ElementUI或者AntDesign不同,它提供了一些类名,类似于Bootstrap,想要拿来即用的组件需要自己进行封装。

    daisyUI官网中介绍了daisyUI的5个特点:

    • Tailwind CSS 的插件

    • 快速开发

    • 更纯净的 HTML

    • 深度自定义、可定制主题

    • 纯净 CSS、支持任意框架

    丰富的资源

    daisyUI内置了29款主题,开发出的网站可以直接使用这些主题

    除了这内置的29款主题,还支持自定义,可以参考。

    daisyUI目前一共内置了47个组件,如下图:

    daisyUI怎么解决TailwindCSS堆砌class问题

    对比TailwindCSS

    daisyUI虽然是TailwindCSS插件,但是却有一点“反”TailwindCSS的意思,如果我们想要实现一个按钮,使用TailwindCSS的写法如下:

    <a class="inline-block px-4 py-3    text-sm font-semibold text-center    text-white uppercase transition    duration-200 ease-in-out bg-indigo-600     rounded-md cursor-pointer    hover:bg-indigo-700">Button</a>

    而使用daisyUI的代码如下:

    <a class="btn btn-primary">Button</a>

    上面的代码实现了了一个按钮,可以明显看后者比前者更节省代码。

    顽强的适用性

    daisy是一个纯净的CSS组件,它几乎适用于所有的前端开发场景,我在官网截了个图

    daisyUI怎么解决TailwindCSS堆砌class问题

    可以看到目前已经支持全部的前端开发场景。

    快速上手

    现在我们使用Vue3+TS+Vite3创建要给项目,并在项目中应用daisyUI,首先就是通过Vite创建一个Vu3+TS的项目,创建过程如下:

    这里我使用pnpm作为包管理工具,项目安装过程如下:

    pnpm create vite# project name -> daisyui-demo# select a framework -> vue# select a variant -> vue+tscd daisyui-demopnpm installcode . # 使用VScode打开项目

    安装完成之后我们在项目中安装一下TailwindCSS,daisyUI的使用需要TailwindCSS;过程如下:

    pnpm install -D tailwindcss postcss au较好refixer# 生成配置文件pnpm tailwindcss init -p

    修改一下tailwind.config配置文件,修改成下面这样的。

    // tailwind.config.cjs/** @type {import('tailwindcss').Config} */module.exports = {  content: ['./index.html', './src/**/*.{vue,js,ts,jsx,tsx}'],  theme: {    extend: {},  },  plugins: [],}

    然后创建一个css文件,引入TailwindCSS的内容

    // src/index.css@tailwind base;@tailwind components;@tailwind utilities;

    然后在main.ts中引入这个css:

    import { createApp } from 'vue'import './style.css'// 引入前面添加的cssimport './index.css'import App from './App.vue'createApp(App).mount('#app')

    到这为止我们就安装好了我们TailwindCSS,现在来安装下daisyUI,过程如下:

    pnpm i daisyui

    然后在修改一下tailwind.config配置文件,修改成下面这样的。

    /** @type {import('tailwindcss').Config} */module.exports = {  content: ['./index.html', './src/**/*.{vue,js,ts,jsx,tsx}'],  theme: {    extend: {},  },  plugins: [require('daisyui')],}

    就是把刚安装的daisyUI引入一下呀

    现在这个就写完了,我们去App.vue中简单的使用一下这个UI组件。实例代码如下:

    <script setup lang="ts">const handleChangeTheme = () => {  const html = document.getElementsByTagName('html')[0]  const darkTheme = html.dataset.theme  if (darkTheme === 'dark') {    html.dataset.theme = 'light'  } else {    html.dataset.theme = 'dark'  }}</script><template>  <div class="button-container flex justify-center my-10">    <button class="btn">Button</button>    <button class="btn btn-primary">Button</button>    <button class="btn btn-secondary">Button</button>    <button class="btn btn-accent">Button</button>    <button class="btn btn-ghost">Button</button>    <button class="btn btn-link">Button</button>  </div>  <div class="flex justify-center my-10">    <label class="swap swap-rotate">      <!-- this hidden checkbox controls the state -->      <input type="checkbox" @click="handleChangeTheme" />      <!-- sun icon -->      <svg        class="swap-on fill-current w-10 h-10"        xmlns="http://www.w3.org/2000/svg"        viewBox="0 0 24 24"      >        <path          d="M5.64,17l-.71.71a1,1,0,0,0,0,1.41,1,1,0,0,0,1.41,0l.71-.71A1,1,0,0,0,5.64,17ZM5,12a1,1,0,0,0-1-1H3a1,1,0,0,0,0,2H4A1,1,0,0,0,5,12Zm7-7a1,1,0,0,0,1-1V3a1,1,0,0,0-2,0V4A1,1,0,0,0,12,5ZM5.64,7.05a1,1,0,0,0,.7.29,1,1,0,0,0,.71-.29,1,1,0,0,0,0-1.41l-.71-.71A1,1,0,0,0,4.93,6.34Zm12,.29a1,1,0,0,0,.7-.29l.71-.71a1,1,0,1,0-1.41-1.41L17,5.64a1,1,0,0,0,0,1.41A1,1,0,0,0,17.66,7.34ZM21,11H20a1,1,0,0,0,0,2h2a1,1,0,0,0,0-2Zm-9,8a1,1,0,0,0-1,1v1a1,1,0,0,0,2,0V20A1,1,0,0,0,12,19ZM18.36,17A1,1,0,0,0,17,18.36l.71.71a1,1,0,0,0,1.41,0,1,1,0,0,0,0-1.41ZM12,6.5A5.5,5.5,0,1,0,17.5,12,5.51,5.51,0,0,0,12,6.5Zm0,9A3.5,3.5,0,1,1,15.5,12,3.5,3.5,0,0,1,12,15.5Z"        />      </svg>      <!-- moon icon -->      <svg        class="swap-off fill-current w-10 h-10"        xmlns="http://www.w3.org/2000/svg"        viewBox="0 0 24 24"      >        <path          d="M21.64,13a1,1,0,0,0-1.05-.14,8.05,8.05,0,0,1-3.37.73A8.15,8.15,0,0,1,9.08,5.49a8.59,8.59,0,0,1,.25-2A1,1,0,0,0,8,2.36,10.14,10.14,0,1,0,22,14.05,1,1,0,0,0,21.64,13Zm-9.5,6.69A8.14,8.14,0,0,1,7.08,5.22v.27A10.15,10.15,0,0,0,17.22,15.63a9.79,9.79,0,0,0,2.1-.22A8.11,8.11,0,0,1,12.14,19.73Z"        />      </svg>    </label>  </div>  <div class="flex justify-center">    <div class="alert alert-success shadow-lg w-96">      <div>        <svg          xmlns="http://www.w3.org/2000/svg"          class="stroke-current flex-shrink-0 h-6 w-6"          fill="none"          viewBox="0 0 24 24"        >          <path            stroke-linecap="round"            stroke-linejoin="round"            stroke-width="2"            d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"          />        </svg>        <span>Your purchase has been confirmed!</span>      </div>    </div>  </div></template><style scoped></style>

    自定义主题

    daisyUI有一个好玩的特性就是允许自定义主题,而且非常简单,只需要在tailwind.config.js中添加一个daisyui的配置项,增加一个themes的数组即可,实例代码如下:

    /** @type {import('tailwindcss').Config} */module.exports = {  content: ['./index.html', './src/**/*.{vue,js,ts,jsx,tsx}'],  theme: {    extend: {},  },  plugins: [require('daisyui')],  daisyui: {    themes: [      {        // key为自定义主题名称        'ywz-theme': {          primary: '#fcaec1',          secondary: '#64c619',          accent: '#6fedac',          neutral: '#281E29',          info: '#83C2E2',          success: '#1BC55F',          warning: '#F0AA28',          error: '#E24B4B',          'base-100': '#EFEEF2',          '--rounded-box': '1rem', // border radius rounded-box utility class, used in card and other large boxes          '--rounded-btn': '0.5rem', // border radius rounded-btn utility class, used in buttons and similar element          '--rounded-badge': '1.9rem', // border radius rounded-badge utility class, used in badges and similar          '--animation-btn': '0.25s', // duration of animation when you click on button          '--animation-input': '0.2s', // duration of animation for inputs like checkbox, toggle, radio, etc          '--btn-text-case': 'uppercase', // set default text transform for buttons          '--btn-focus-scale': '0.95', // scale transform of button when you focus on it          '--border-btn': '1px', // border width of buttons          '--tab-border': '1px', // border width of tabs          '--tab-radius': '0.5rem', // border radius of tabs        },      },    ],  },}

    可以配置daisyUI提供的主题生成器,可以轻松实现自定义主题,自定义主题实用如下:

    daisyUI怎么解决TailwindCSS堆砌class问题

    封装一个button组件

    daisyUI是基于原始的CSS实现的,基本上所有的组件只有样式没有功能,现在就简单的封装一个button组件再来学习一下daisyUI。

    首先定义一下目录结构,如下所示:

    monorepo-demo├── src│   ├── components│   │   ├── button│   │   │   ├── index.ts│   │   │   ├── src│   │   │   │   └── YwzButton.vue├── pnpm-lock.yaml└── package.json

    其中src/components/button/index.ts的内容如下:

    import YwzButton from './src/YwzButton.vue'export { YwzButton }

    这里就执行了导入导出操作

    主要代码如下:

    <script setup lang="ts">import { computed } from 'vue'interface Props {  disabled?: boolean  type?:'primary' | 'secondary' | 'accent' | 'info' | 'success' | 'warning' | 'error' | 'ghost' | 'link'  outline?: boolean  size?: 'lg' | 'md' | 'sm' | 'sx'}const props = defineProps<Props>()const classComputed = computed(() => {  const classList = []  props.disabled && classList.push('btn-disabled')  props.type && classList.push('btn-' + props.type)  props.outline && classList.push('btn-outline')  props.size && classList.push('btn-' + props.size)  return classList})</script><template>  <button class="btn" :class="classComputed">    <!-- 默认插槽 -->    <slot></slot>  </button></template><style scoped></style>

    使用这个组件也很简单,代码如下:

    <script setup lang="ts">import YwzButton from './components/button/src/YwzButton.vue'</script><template>  <div class="button-container flex justify-center my-10">    <YwzButton type="error" outline size="lg">按钮</YwzButton>  </div></template><style scoped></style>

    到此,关于“daisyUI怎么解决TailwindCSS堆砌class问题”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

    文章标题:daisyUI怎么解决TailwindCSS堆砌class问题,发布者:亿速云,转载请注明出处:https://worktile.com/kb/p/21432

    (0)
    打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
    亿速云亿速云认证作者
    上一篇 2022年8月30日 下午4:27
    下一篇 2022年8月30日 下午11:14

    相关推荐

    • windows office365怎么卸载干净

      office365卸载干净步骤: 1、左键你的开始菜单,找到设置,如图。 2、点击进入到应用中。 3、在应用与功能里找到和office365。 4、点击卸载。 6、将保留我的的数据和使用习惯取消勾选。 7、此时点击继续卸载,就会将残留文件也移除了。 到此,关于“windows office365怎么…

      2022年9月21日
      38200
    • Spring框架基于注解怎么开发CRUD

      1. Maven坐标 <!– https://mvnrepository.com/artifact/org.springframework/spring-webmvc –><dependency> <groupId>org.springframework&lt…

      2022年8月27日
      12400
    • 如何做好项目管理

      做好项目管理有两个必须具备的核心技能:1、SMART原则;2、PDCA循环;除此以外,在项目进行的过程中我们还有:明确且达成共识的项目目标、有效的规划控制、制定项目标准及流程、合理的资源管理、建立完善的交流管理体系、利用有效的管理手段、激励等8个必须注意的点。 一、做好项目管理必须具备的两大核心技能…

      2022年3月19日
      25400
    • windows会声会影导出视频mp4格式的方法

      会声会影导出视频mp4格式的方法 1、首先点击上方的“共享”。 2、点击右侧的“MPE4”。 3、我们可以在配置文件中更改视频具体参数。 4、然后修改文件名和渲染解码方式。 5、最后点击右边的文件夹选择保存路径,点击“开始”开始导出。 6、等待渲染完成导出视频就可以了。 以上就是关于“windows…

      2022年9月15日
      16300
    • HTML可不可以美化网页

      HTML不可以美化网页。HTML是用来定义网页内容的,例如标题、正文、图像等,它是无法美化网页的;美化网页需要使用CSS,CSS是样式语言,主要用来控制网页的外观,例如颜色、字体、背景等。CSS能够对网页中的对象的位置排版进行像素级的精确控制,支持几乎所有的字体字号样式,拥有对网页对象和模型样式编辑…

      2022年9月24日
      23700
    • Python代码智能感知类型标注与特殊注释实例分析

      一、代码智能感知 想必大部分现代的集成开发环境(IDE)都有代码智能感知功能吧! 智能感知(IntelliSense),就是在我们写代码的时候,代码编辑器自动弹出我们代码中需要补全的部分,而这些补全的部分就是代码编辑器通过智能感知得到的,最重要的是,代码编辑器智能地感知补全的部分是通过代码中的变量的…

      2022年9月22日
      19500
    • temp文件夹全删光应该怎么做

      temp文件夹能全部删光;temp文件是系统中的临时文件夹,用于收藏浏览网页的临时文件、编辑文件等等,文件中的内容都可以删除,有些删不掉是因为有的程序正在使用,可以把后台的所有程序都关掉,即可删除文件夹中的指定内容了。 temp文件夹能不能全删光 能全删光。 temp(临时文件夹temporary)…

      2022年9月13日
      5.2K00
    • 电脑0xc0000011如何解决

      0xc0000011修复方法: 方法一: 1、首先我们需要查看是否是软件问题,开机时按住“F8”直到进入系统修复界面。 2、接着在其中选择“最后一次正确的配置”回车进入。 3、如果我们无法进入系统,那么说明就不是安装的软件或驱动等问题。 方法二: 1、这时候就需要采用系统盘进行修复了。 2、如果我们…

      2022年9月18日
      22800
    • Webug靶场任意文件下载漏洞怎么复现

      漏洞简述: 一些网站由于业务需求,可能提供文件查看或者下载的功能,如果对用户查看或者下载的文件不做限制,那么恶意用户可以可以查看或者下载一些敏感文件,比如配置信息、源码文件等 漏洞成因: 存在读取文件的函数 读取文件的路径用户可控且未校验或校验不严格 输出了文件内容 漏洞危害: 下载服务器任意文件,…

      2022年9月22日
      21000
    • imazing下载应用出错如何解决

      imazing下载应用出错解决方法 1、下载应用出错,有可能是imazing软件问题,可以在本站下载一个imazing进行尝试。 2、还可能是因为需要下载的软件问题,例如需要下载的软件依旧下架就无法正常下载了。 3、也可能是因为我们没有使用正确的应用下载方法,或者没有在App Store中获取过,下…

      2022年9月15日
      32000
    站长微信
    站长微信
    电话联系

    400-800-1024

    工作日9:30-21:00在线

    分享本页
    返回顶部