Coder Social home page Coder Social logo

hyoban / unocss-preset-shadcn Goto Github PK

View Code? Open in Web Editor NEW
161.0 2.0 4.0 691 KB

Use shadcn ui with UnoCSS

Home Page: https://unocss-preset-shadcn.vercel.app

License: MIT License

TypeScript 74.88% JavaScript 0.26% HTML 2.26% CSS 22.61%
shadcn-ui unocss unocss-preset shadcn-vue shadcn-svelte

unocss-preset-shadcn's Introduction

unocss-preset-shadcn

npm version npm downloads bundle JSDocs License

Use shadcn/ui or shadcn-vue or shadcn-svelte 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. 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",
    }),
  ],
  // 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 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.
  2. copy utils.ts into your project at src/lib
  3. create components.json 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.

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"
  }
}

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 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  avatar

unocss-preset-shadcn's Issues

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

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.

[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?

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)($|\?)/,
      ],
    },
  },
})

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.