コンテンツへスキップ
言語22
メニュー

ドキュメント / フレームワーク

Astro

UI LoftのアイテムをAstroサイトに追加します。最初にReactとTailwindの追加が必要で、動くアイテムにはクライアントディレクティブが必要です。

バージョン
React
必要なもの
React + Tailwind 4 + shadcn
セットアップ時間
約5分

セットアップ

  1. サイトを作成する

    すでにある場合は省略してください。

    ターミナル
    npm create astro@latest my-site -- --template minimal --install --no-git --yes
    cd my-site
  2. ReactとTailwindを追加する

    Astro標準のコマンドで両方がインストールされ、astro.config.mjsも更新されます。src/styles/global.cssも作られます。

    ターミナル
    npx astro add react tailwind --yes
  3. @エイリアスを追加する

    tsconfig.jsonのcompilerOptionsの中にpathsを追加します。

    tsconfig.json
    {
      "compilerOptions": {
        "paths": {
          "@/*": ["./src/*"]
        }
      }
    }
  4. shadcnをセットアップする

    components.jsonが作られ、src/styles/global.cssにテーマが書き込まれます。-dで初期設定が選ばれます。

    ターミナル
    npx shadcn@latest init -d
  5. アイテムを追加する

    好きなアイテムのページからコマンドをコピーします。ファイルはsrc/components/に追加されます。

    ターミナル
    npx shadcn@latest add https://uiloft.dev/r/glow-button.json
  6. 使う

    スタイルとアイテムを一度だけインポートし、アイテムに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を一度だけインポートしてください。

ほかのフレームワーク