Skip to content

Commit

Permalink
feat(shadcn): add support for TanStack Start (#6507)
Browse files Browse the repository at this point in the history
* feat(shadcn): add TanStack Start support

* docs(www): add docs for TanStack Start

* chore: changeset
  • Loading branch information
shadcn authored Feb 6, 2025
1 parent f903723 commit 5234c46
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/brave-cheetahs-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"shadcn": minor
---

add support for TanStack Start
6 changes: 6 additions & 0 deletions apps/www/config/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ export const docsConfig: DocsConfig = {
href: "/docs/installation/laravel",
items: [],
},
{
title: "Tanstack Start",
href: "/docs/installation/tanstack",
items: [],
label: "New",
},
{
title: "Gatsby",
href: "/docs/installation/gatsby",
Expand Down
10 changes: 10 additions & 0 deletions apps/www/content/docs/installation/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ description: How to install dependencies and structure your app.
</svg>
<p className="font-medium mt-2">Laravel</p>
</LinkedCard>
<LinkedCard href="/docs/installation/tanstack">
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
className="w-10 h-10"
>
<path d="M6.93 13.688a.343.343 0 0 1 .468.132l.063.106c.48.851.98 1.66 1.5 2.426a35.65 35.65 0 0 0 2.074 2.742.345.345 0 0 1-.039.484l-.074.066c-2.543 2.223-4.191 2.665-4.953 1.333-.746-1.305-.477-3.672.808-7.11a.344.344 0 0 1 .153-.18ZM17.75 16.3a.34.34 0 0 1 .395.27l.02.1c.628 3.286.187 4.93-1.325 4.93-1.48 0-3.36-1.402-5.649-4.203a.327.327 0 0 1-.074-.222c0-.188.156-.34.344-.34h.121a32.984 32.984 0 0 0 2.809-.098c1.07-.086 2.191-.23 3.359-.437zm.871-6.977a.353.353 0 0 1 .445-.21l.102.034c3.262 1.11 4.504 2.332 3.719 3.664-.766 1.305-2.993 2.254-6.684 2.848a.362.362 0 0 1-.238-.047.343.343 0 0 1-.125-.476l.062-.106a34.07 34.07 0 0 0 1.367-2.523c.477-.989.93-2.051 1.352-3.184zM7.797 8.34a.362.362 0 0 1 .238.047.343.343 0 0 1 .125.476l-.062.106a34.088 34.088 0 0 0-1.367 2.523c-.477.988-.93 2.051-1.352 3.184a.353.353 0 0 1-.445.21l-.102-.034C1.57 13.742.328 12.52 1.113 11.188 1.88 9.883 4.106 8.934 7.797 8.34Zm5.281-3.984c2.543-2.223 4.192-2.664 4.953-1.332.746 1.304.477 3.671-.808 7.109a.344.344 0 0 1-.153.18.343.343 0 0 1-.468-.133l-.063-.106a34.64 34.64 0 0 0-1.5-2.426 35.65 35.65 0 0 0-2.074-2.742.345.345 0 0 1 .039-.484ZM7.285 2.274c1.48 0 3.364 1.402 5.649 4.203a.349.349 0 0 1 .078.218.348.348 0 0 1-.348.344l-.117-.004a34.584 34.584 0 0 0-2.809.102 35.54 35.54 0 0 0-3.363.437.343.343 0 0 1-.394-.273l-.02-.098c-.629-3.285-.188-4.93 1.324-4.93Zm2.871 5.812h3.688a.638.638 0 0 1 .55.316l1.848 3.22a.644.644 0 0 1 0 .628l-1.847 3.223a.638.638 0 0 1-.551.316h-3.688a.627.627 0 0 1-.547-.316L7.758 12.25a.644.644 0 0 1 0-.629L9.61 8.402a.627.627 0 0 1 .546-.316Zm3.23.793a.638.638 0 0 1 .552.316l1.39 2.426a.644.644 0 0 1 0 .629l-1.39 2.43a.638.638 0 0 1-.551.316h-2.774a.627.627 0 0 1-.546-.316l-1.395-2.43a.644.644 0 0 1 0-.629l1.395-2.426a.627.627 0 0 1 .546-.316Zm-.491.867h-1.79a.624.624 0 0 0-.546.316l-.899 1.56a.644.644 0 0 0 0 .628l.899 1.563a.632.632 0 0 0 .547.316h1.789a.632.632 0 0 0 .547-.316l.898-1.563a.644.644 0 0 0 0-.629l-.898-1.558a.624.624 0 0 0-.547-.317Zm-.477.828c.227 0 .438.121.547.317l.422.73a.625.625 0 0 1 0 .629l-.422.734a.627.627 0 0 1-.547.317h-.836a.632.632 0 0 1-.547-.317l-.422-.734a.625.625 0 0 1 0-.629l.422-.73a.632.632 0 0 1 .547-.317zm-.418.817a.548.548 0 0 0-.473.273.547.547 0 0 0 0 .547.544.544 0 0 0 .473.27.544.544 0 0 0 .473-.27.547.547 0 0 0 0-.547.548.548 0 0 0-.473-.273Zm-4.422.546h.98M18.98 7.75c.391-1.895.477-3.344.223-4.398-.148-.63-.422-1.137-.84-1.508-.441-.39-1-.582-1.625-.582-1.035 0-2.12.472-3.281 1.367a14.9 14.9 0 0 0-1.473 1.316 1.206 1.206 0 0 0-.136-.144c-1.446-1.285-2.66-2.082-3.7-2.39-.617-.184-1.195-.2-1.722-.024-.559.187-1.004.574-1.317 1.117-.515.894-.652 2.074-.46 3.527.078.59.214 1.235.402 1.934a1.119 1.119 0 0 0-.215.047C3.008 8.62 1.71 9.269.926 10.015c-.465.442-.77.938-.883 1.481-.113.578 0 1.156.312 1.7.516.894 1.465 1.597 2.817 2.155.543.223 1.156.426 1.844.61a1.023 1.023 0 0 0-.07.226c-.391 1.891-.477 3.344-.223 4.395.148.629.425 1.14.84 1.508.44.39 1 .582 1.625.582 1.035 0 2.12-.473 3.28-1.364.477-.37.973-.816 1.489-1.336a1.2 1.2 0 0 0 .195.227c1.446 1.285 2.66 2.082 3.7 2.39.617.184 1.195.2 1.722.024.559-.187 1.004-.574 1.317-1.117.515-.894.652-2.074.46-3.527a14.941 14.941 0 0 0-.425-2.012 1.225 1.225 0 0 0 .238-.047c1.828-.61 3.125-1.258 3.91-2.004.465-.441.77-.937.883-1.48.113-.578 0-1.157-.313-1.7-.515-.894-1.464-1.597-2.816-2.156a14.576 14.576 0 0 0-1.906-.625.865.865 0 0 0 .059-.195z" />
</svg>
<p className="font-medium mt-2">TanStack Start</p>
</LinkedCard>
<LinkedCard href="/docs/installation/gatsby">
<svg
role="img"
Expand Down
181 changes: 181 additions & 0 deletions apps/www/content/docs/installation/tanstack.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
---
title: TanStack Start
description: Install and configure TanStack Start.
---

<Callout>
**Note:** This guide is for TanStack Start and Tailwind v4. If you are using
Tailwind v3, consider the [Basic Starter](#basic-starter) template. **TanStack
Start** works with the **canary** version of `shadcn`.
</Callout>

## TanStack Start + Tailwind v4

<Steps>

### Create project

Start by creating a new TanStack Start project by following the [Build a Project from Scratch](https://tanstack.com/start/latest/docs/framework/react/build-from-scratch) guide on the TanStack Start website.

### Add Tailwind

Install `tailwindcss` and its dependencies.

```bash
npm install tailwindcss @tailwindcss/postcss postcss
```

### Create postcss.config.ts

Create a `postcss.config.ts` file at the root of your project.

```ts title="postcss.config.ts" showLineNumbers
export default {
plugins: {
"@tailwindcss/postcss": {},
},
}
```

### Create `app/styles/app.css`

Create an `app.css` file in the `app/styles` directory and import `tailwindcss`

```css title="app/styles/app.css"
@import "tailwindcss" source("../");
```

### Import `app.css`

```tsx title="app/routes/__root.tsx" showLineNumbers {5,21-26} showLineNumbers
import type { ReactNode } from "react"
import { Outlet, createRootRoute } from "@tanstack/react-router"
import { Meta, Scripts } from "@tanstack/start"

import appCss from "@/styles/app.css?url"

export const Route = createRootRoute({
head: () => ({
meta: [
{
charSet: "utf-8",
},
{
name: "viewport",
content: "width=device-width, initial-scale=1",
},
{
title: "TanStack Start Starter",
},
],
links: [
{
rel: "stylesheet",
href: appCss,
},
],
}),
component: RootComponent,
})
```

### Edit tsconfig.json file

Add the following code to the `tsconfig.json` file to resolve paths.

```ts title="tsconfig.json" showLineNumbers {9-12}
{
"compilerOptions": {
"jsx": "react-jsx",
"moduleResolution": "Bundler",
"module": "ESNext",
"target": "ES2022",
"skipLibCheck": true,
"strictNullChecks": true,
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}
```

### Run the CLI

Run the `shadcn` init command to setup your project:

```bash
npx shadcn@canary init -d
```

This will create a `components.json` file in the root of your project and configure CSS variables inside `app/styles/app.css`.

### That's it

You can now start adding components to your project.

```bash
npx shadcn@canary add button
```

The command above will add the `Button` component to your project. You can then import it like this:

```tsx title="app/routes/index.tsx" showLineNumbers {1,6}
import { Button } from "@/components/ui/button"

function Home() {
const router = useRouter()
const state = Route.useLoaderData()

return (
<div>
<Button>Click me</Button>
</div>
)
}
```

</Steps>

## Basic Starter

The [Basic Starter](https://tanstack.com/start/latest/docs/framework/react/examples/start-basic) template has Tailwind v3 already configured.

<Steps>

### Run the CLI

Run the `shadcn` init command to setup your project:

```bash
npx shadcn@canary init -d
```

This will create a `components.json` file in the root of your project and configure CSS variables inside `app/styles/app.css`.

### That's it

You can now start adding components to your project.

```bash
npx shadcn@canary add button
```

The command above will add the `Button` component to your project. You can then import it like this:

```tsx title="app/routes/index.tsx" showLineNumbers {1,6}
import { Button } from "@/components/ui/button"

function Home() {
const router = useRouter()
const state = Route.useLoaderData()

return (
<div>
<Button>Click me</Button>
</div>
)
}
```

</Steps>
8 changes: 8 additions & 0 deletions packages/shadcn/src/utils/frameworks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ export const FRAMEWORKS = {
tailwind: "https://tailwindcss.com/docs/guides/laravel",
},
},
"tanstack-start": {
name: "tanstack-start",
label: "TanStack Start",
links: {
installation: "https://ui.shadcn.com/docs/installation/tanstack",
tailwind: "https://tailwindcss.com/docs/installation/using-postcss",
},
},
gatsby: {
name: "gatsby",
label: "Gatsby",
Expand Down
13 changes: 12 additions & 1 deletion packages/shadcn/src/utils/get-project-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export async function getProjectInfo(cwd: string): Promise<ProjectInfo | null> {
aliasPrefix,
packageJson,
] = await Promise.all([
fg.glob("**/{next,vite,astro}.config.*|gatsby-config.*|composer.json", {
fg.glob("**/{next,vite,astro,app}.config.*|gatsby-config.*|composer.json", {
cwd,
deep: 3,
ignore: PROJECT_SHARED_IGNORE,
Expand Down Expand Up @@ -116,6 +116,17 @@ export async function getProjectInfo(cwd: string): Promise<ProjectInfo | null> {
return type
}

// TanStack Start.
if (
configFiles.find((file) => file.startsWith("app.config."))?.length &&
Object.keys(packageJson?.dependencies ?? {}).find((dep) =>
dep.startsWith("@tanstack/start")
)
) {
type.framework = FRAMEWORKS["tanstack-start"]
return type
}

// Vite.
// Some Remix templates also have a vite.config.* file.
// We'll assume that it got caught by the Remix check above.
Expand Down

0 comments on commit 5234c46

Please sign in to comment.