ドキュメント / フレームワーク
Astro
UI LoftのアイテムをAstroサイトに追加します。最初にReactとTailwindの追加が必要で、動くアイテムにはクライアントディレクティブが必要です。
- バージョン
- React
- 必要なもの
- React + Tailwind 4 + shadcn
- セットアップ時間
- 約5分
セットアップ
サイトを作成する
すでにある場合は省略してください。
ターミナルnpm create astro@latest my-site -- --template minimal --install --no-git --yes cd my-siteReactとTailwindを追加する
Astro標準のコマンドで両方がインストールされ、
astro.config.mjsも更新されます。src/styles/global.cssも作られます。ターミナルnpx astro add react tailwind --yes@エイリアスを追加する
tsconfig.jsonのcompilerOptionsの中にpathsを追加します。tsconfig.json{ "compilerOptions": { "paths": { "@/*": ["./src/*"] } } }shadcnをセットアップする
components.jsonが作られ、src/styles/global.cssにテーマが書き込まれます。-dで初期設定が選ばれます。ターミナルnpx shadcn@latest init -dアイテムを追加する
好きなアイテムのページからコマンドをコピーします。ファイルは
src/components/に追加されます。ターミナルnpx shadcn@latest add https://uiloft.dev/r/glow-button.json使う
スタイルとアイテムを一度だけインポートし、アイテムに
client:loadを付けてブラウザで動くようにします。src/pages/index.astro--- import "@/styles/global.css" import { GlowButton } from "@/components/glow-button" --- <html lang="en"> <body> <GlowButton client:load>Get started</GlowButton> </body> </html>
知っておくと便利
- クライアントディレクティブがないと、AstroはアイテムをプレーンなHTMLとして出力します。表示はされますが、動いたり反応したりはしません。
client:loadの代わりにclient:visibleを使うと、アイテムがスクロールで見えるまで待ちます。- レイアウトがある場合は、各ページではなくレイアウトで
@/styles/global.cssを一度だけインポートしてください。