Giter VIP home page Giter VIP logo

unocss-preset-shadcn's Introduction

unocss-preset-shadcn

npm version npm downloads bundle JSDocs License

Use shadcn/ui or shadcn-vue or shadcn-svelte or SolidUI with UnoCSS.

  1. Based on fisand/unocss-preset-shadcn
  2. Theme can be easily customized

Starter

You can easily start a new project without manually setting up the project.

Usage

Follow the official guide to set up shadcn/ui, shadcn-vue, or shadcn-svelte, or SolidUI. Replace the step to set up Tailwind CSS with UnoCSS.

Then install unocss-preset-shadcn and unocss-preset-animations, and update your unocss.config.ts:

ni -D unocss-preset-animations unocss-preset-shadcn
// unocss.config.ts
import { defineConfig, presetUno } from 'unocss'
import presetAnimations from 'unocss-preset-animations'
import { presetShadcn } from 'unocss-preset-shadcn'

export default defineConfig({
  presets: [
    presetUno(),
    presetAnimations(),
    presetShadcn({
      color: 'red',
      // With default setting for SolidUI, you need to set the darkSelector option.
      darkSelector: '[data-kb-theme="dark"]',
    }),
  ],
  // By default, `.ts` and `.js` files are NOT extracted.
  // If you want to extract them, use the following configuration.
  // It's necessary to add the following configuration if you use shadcn-vue or shadcn-svelte.
  content: {
    pipeline: {
      include: [
        // the default
        /\.(vue|svelte|[jt]sx|mdx?|astro|elm|php|phtml|html)($|\?)/,
        // include js/ts files
        'src/**/*.{js,ts}',
      ],
    },
  },
})

Important

Do not run npx shadcn-ui@latest init or npx shadcn-vue@latest init or npx shadcn-svelte@latest init or npx solidui-cli@latest init to initialize your project.

  1. ni lucide-react class-variance-authority clsx tailwind-merge
    • ni lucide-vue-next radix-vue class-variance-authority clsx tailwind-merge for shadcn-vue.
    • ni lucide-svelte tailwind-variants clsx tailwind-merge for shadcn-svelte.
    • ni tailwindcss-animate class-variance-authority clsx tailwind-merge for SolidUI.
  2. copy utils.ts into your project at src/lib
  3. create components.json or ui.config.json (for SolidUI) in your project root and modify as needed
  4. npx shadcn-ui@latest add button
    • npx shadcn-vue@latest add button for shadcn-vue.
    • npx shadcn-svelte@latest add button for shadcn-svelte.
    • npx solidui-cli@latest add button for SolidUI.

Warning

If you encounter problems adjusting animation property, this may be because tailwind-animate has duplicate rules about animation and transition. You can refer to Migration Guide from Animations Preset for UnoCSS to solve this problem.

Code to copy

utils.ts: this file usually under src/lib folder.

import type { ClassValue } from 'clsx'
import { clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs))
}

components.json: this file should under your project root.

React + shadcn-ui

{
  "$schema": "https://ui.shadcn.com/schema.json",
  "style": "default",
  "tailwind": {
    "config": "tailwind.config.js",
    "css": "styles/global.css",
    "baseColor": "neutral",
    "cssVariables": true,
    "prefix": ""
  },
  "rsc": false,
  "tsx": true,
  "aliases": {
    "components": "@/components",
    "utils": "@/lib/utils",
    "ui": "@/components/ui"
  }
}

Vue + shadcn-vue

{
  "style": "default",
  "typescript": true,
  "tailwind": {
    "config": "tailwind.config.js",
    "css": "src/assets/index.css",
    "baseColor": "neutral",
    "cssVariables": true
  },
  "framework": "vite",
  "aliases": {
    "components": "@/components",
    "utils": "@/lib/utils"
  }
}

Svelte + shadcn-svelte

{
  "$schema": "https://shadcn-svelte.com/schema.json",
  "style": "default",
  "tailwind": {
    "config": "tailwind.config.js",
    "css": "src/app.pcss",
    "baseColor": "neutral"
  },
  "aliases": {
    "components": "$lib/components",
    "utils": "$lib/utils"
  }
}

ui.config.json: this file should under your project root. This file is for SolidUI.

{
  "tsx": true,
  "componentDir": "./src/components/ui",
  "tailwind": {
    "config": "tailwind.config.js",
    "css": "src/App.css"
  },
  "aliases": {
    "path": "~/*"
  }
}

Dynamic Theme

Preview the demo.

If you want to use a dynamic theme, you can pass an array of theme objects to presetShadcn:

import { defineConfig, presetUno, UserConfig } from 'unocss'
import presetAnimations from 'unocss-preset-animations'
import { builtinColors, presetShadcn } from 'unocss-preset-shadcn'

export default defineConfig({
  presets: [
    presetUno(),
    presetAnimations(),
    presetShadcn(builtinColors.map((c) => ({ color: c }))),
  ],
})

Add a theme sync script to your index.html. To dynamically change the theme, you can create a theme switch component.

See also

unocss-preset-shadcn's People

Contributors

hyoban avatar kainstar avatar namesmt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

unocss-preset-shadcn's Issues

Classes not working on dev server

Describe the bug

After I started the local server I encountered that the styles do not work.

Reproduction

https://stackblitz.com/edit/nuxt-starter-475whq?file=components.json,app.vue,package.json,nuxt.config.ts

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (6) x64 Intel(R) Core(TM) i5-9400 CPU @ 2.90GHz
    Memory: 4.70 GB / 15.92 GB
  Binaries:
    Node: 20.11.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.21 - C:\Program Files\nodejs\yarn.CMD
    npm: 10.2.4 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.14.3 - C:\Program Files\nodejs\pnpm.CMD
  Browsers:
    Edge: Chromium (126.0.2592.81)
    Internet Explorer: 11.0.19041.4355

Used Package Manager

pnpm

Validations

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.

Solidjs support?

Clear and concise description of the problem

Curious if its possible to use this with solidjs?

Suggested solution

If so could we add this to the readme?

Validations

Uncaught TypeError: c.toUpperCase is not a function

I encountered this error while trying to use shadcn-vue and unocss.

Uncaught TypeError: c.toUpperCase is not a function
    at chunk-U4IHFRBV.js:58:50
    at String.replace (<anonymous>)
    at chunk-U4IHFRBV.js:58:14
    at chunk-U4IHFRBV.js:53:33
    at setFullProps (chunk-U4IHFRBV.js:5584:49)
    at initProps (chunk-U4IHFRBV.js:5454:3)
    at setupComponent (chunk-U4IHFRBV.js:9019:3)
    at mountComponent (chunk-U4IHFRBV.js:7352:7)
    at processComponent (chunk-U4IHFRBV.js:7318:9)
    at patch (chunk-U4IHFRBV.js:6784:11)

The project where this issue occurred is inconvenient to troubleshoot, but I have reproduced this error through another method.
Just enable variant group transformer for unocss, and the same error will appear.

// unocss.config.ts
import {
  defineConfig,
  presetUno,
  transformerVariantGroup,
} from 'unocss'
import presetAnimations from "unocss-preset-animations"
import { presetShadcn } from "unocss-preset-shadcn"

export default defineConfig({
  presets: [presetUno(), presetAnimations(), presetShadcn()],
  transformers: [transformerVariantGroup()],
  content: {
    pipeline: {
      include: [
        /\.(vue|svelte|[jt]s|[jt]sx|mdx?|astro|elm|php|phtml|html)($|\?)/,
      ],
    },
  },
})

[QUESTION] Vue Compatibility

Cool preset! I tried to use this with a Vue 3 (Vite) app, but could not get it to work. When adding components, these are added as .tsx. Is there a guide to get this working with Vue components?

Clarify 'Code to copy'

Additional context

It's hard for a beginner to understand where to put Code to copy. Perhaps refactoring to express where exactly that code needs to be copied to would help.

Add support for custom theming with css variables in a css file

Clear and concise description of the problem

Hello there,

First of all i want to thank you for this, its a game changer for someone like me who loves both unocss and shadcn/ui.

I would love to propose for more flexibility in the usage of this preset. When using shadcn/ui with tailwind, you get the flexibility of customizing your theme colors and values using the css variables provided to you through the tailwindcss file which is undoubtedly very useful.

I wonder if we could also have this preset give room for such customization.

Suggested solution

Here are some possible solutions.

1. Passing custom variable values to the preset

presetShadcn({
  theme: {
        default: {
          background: "0 0% 100%",
          // and so on....
        },
        dark: {
          background: "0 0% 100%",
          // and so on....
        }
        cherry: {
          background: "0 0% 100%",
          // and so on....
        }
   }
})

2. Switching to custom theme

We could have some sort of property that lets the preset know that we want to use custom theme values.
This will then let you set your values in a css file, without loading the inbuilt ones

presetShadcn({
   customThemeVars: true
})
::root{
  ---background: 255 255% 100%;
 // and so on....
}


.dark{
  ---background: 0 0% 100%;
 // and so on....
}

Alternative

No response

Additional context

No response

Validations

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.