npx shadcn-ui@latest init
例えばボタンをインストールする場合は次のコマンドを実行します
npx shadcn-ui@latest add button
shadcnはnpmパッケージとして提供されてません。 つまり依存関係としてインストールする必要はありません。 直接指定したディレクトリにインストールされるという形になります
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}
vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';
// https://vitejs.dev/config/
export default defineConfig({
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'), // '@' は 'src' フォルダを指すように設定
},
},
plugins: [react()],
});
以下のように呼び出して使用します
import './App.css';
import { Button } from '@/components/ui/button';
function App() {
return (
<div>
<Button>test</Button>
</div>
);
}
export default App;
以下のように変更してからインストールコマンドを実行すると新しい場所にインストールされます。 (古いコンポーネントを手動で削除します。)
components.json
"aliases": {
"components": "@/components/shadcn",
"utils": "@/components/shadcn/lib/utils"
}
import '../src/index.css'; // Tailwind CSS のスタイルシートへのパス