Skip to content

Commit

Permalink
feat(shadcn-ui): add support for jsx (#834)
Browse files Browse the repository at this point in the history
  • Loading branch information
shadcn committed Jul 4, 2023
1 parent f78a4aa commit edc653c
Show file tree
Hide file tree
Showing 28 changed files with 666 additions and 56 deletions.
5 changes: 5 additions & 0 deletions .changeset/spicy-games-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"shadcn-ui": minor
---

add support for jsx
5 changes: 5 additions & 0 deletions apps/www/config/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ export const docsConfig: DocsConfig = {
href: "/docs/changelog",
items: [],
},
{
title: "About",
href: "/docs/about",
items: [],
},
],
},
{
Expand Down
20 changes: 20 additions & 0 deletions apps/www/content/docs/about.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: About
description: Powered by amazing open source projects.
---

## About

[ui.shadcn.com](https://ui.shadcn.com) is a project by [shadcn](https://shadcn.com).

## Credits

- [Radix UI](https://radix-ui.com) - For the primitives.
- [Vercel](https://vercel.com) - Where I host all my projects.
- [Shu Ding](https://shud.in) - The typography style is adapted from his work on Nextra.
- [Cal](https://cal.com) - Where I copied the styles for the first component: the `Button`.
- [cmdk](https://cmdk.paco.me) - For the `<Command />` component.

## License

MIT © [shadcn](https://shadcn.com)
42 changes: 42 additions & 0 deletions apps/www/content/docs/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,48 @@ description: Latest updates and announcements.
toc: false
---

## July 2023 - JavaScript

This project and the components are written in TypeScript. We recommend using TypeScript for your project as well.

However we provide a JavaScript version of the components as well. The JavaScript version is available via the [cli](/docs/cli).

```txt
Would you like to use TypeScript (recommended)? no
```

To opt-out of TypeScript, you can use the `tsx` flag in your `components.json` file.

```json {10} title="components.json"
{
"style": "default",
"tailwind": {
"config": "tailwind.config.js",
"css": "src/app/globals.css",
"baseColor": "zinc",
"cssVariables": true
},
"rsc": false,
"tsx": false,
"aliases": {
"utils": "~/lib/utils",
"components": "~/components"
}
}
```

To configure import aliases, you can use the following `jsconfig.json`:

```json {4} title="jsconfig.json"
{
"compilerOptions": {
"paths": {
"@/*": ["./*"]
}
}
}
```

## June 2023 - New CLI, Styles and more

I have a lot of updates to share with you today:
Expand Down
8 changes: 0 additions & 8 deletions apps/www/content/docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,3 @@ But hey, let me know if you do. I'd love to see what you build.
</AccordionItem>

</Accordion>

## Credits

- [Radix UI](https://radix-ui.com) - For the primitives.
- [Vercel](https://vercel.com) - Where I host all my projects.
- [Shu Ding](https://shud.in) - The typography style is adapted from his work on Nextra.
- [Cal](https://cal.com) - Where I copied the styles for the first component: the `Button`.
- [cmdk](https://cmdk.paco.me) - For the `<Command />` component.
1 change: 1 addition & 0 deletions apps/www/content/docs/installation/astro.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ npx shadcn-ui@latest init
You will be asked a few questions to configure `components.json`:

```txt showLineNumbers
Would you like to use TypeScript (recommended)? no / yes
Which style would you like to use? › Default
Which color would you like to use as base color? › Slate
Where is your global CSS file? › › ./src/styles/globals.css
Expand Down
1 change: 1 addition & 0 deletions apps/www/content/docs/installation/gatsby.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ npx shadcn-ui@latest init
You will be asked a few questions to configure `components.json`:

```txt showLineNumbers
Would you like to use TypeScript (recommended)? no / yes
Which style would you like to use? › Default
Which color would you like to use as base color? › Slate
Where is your global CSS file? › › ./src/styles/globals.css
Expand Down
42 changes: 39 additions & 3 deletions apps/www/content/docs/installation/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ title: Installation
description: How to install dependencies and structure your app.
---

<div className="grid sm:grid-cols-2 gap-4 sm:gap-6">
## Frameworks

<div className="grid sm:grid-cols-2 gap-4 mt-8 sm:gap-6">
<LinkedCard href="/docs/installation/next">
<svg
role="img"
Expand Down Expand Up @@ -87,6 +89,40 @@ description: How to install dependencies and structure your app.
</LinkedCard>
</div>

## Other frameworks
## TypeScript

This project and the components are written in TypeScript. We recommend using TypeScript for your project as well.

However we provide a JavaScript version of the components as well. The JavaScript version is available via the [cli](/docs/cli).

To opt-out of TypeScript, you can use the `tsx` flag in your `components.json` file.

```json {10} title="components.json"
{
"style": "default",
"tailwind": {
"config": "tailwind.config.js",
"css": "src/app/globals.css",
"baseColor": "zinc",
"cssVariables": true
},
"rsc": false,
"tsx": false,
"aliases": {
"utils": "~/lib/utils",
"components": "~/components"
}
}
```

To configure import aliases, you can use the following `jsconfig.json`:

I'm looking for help writing guides for other frameworks. Help me write those guides by [opening an PR](https://github.com/shadcn/ui).
```json {4} title="jsconfig.json"
{
"compilerOptions": {
"paths": {
"@/*": ["./*"]
}
}
}
```
1 change: 1 addition & 0 deletions apps/www/content/docs/installation/next.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ npx shadcn-ui@latest init
You will be asked a few questions to configure `components.json`:

```txt showLineNumbers
Would you like to use TypeScript (recommended)? no / yes
Which style would you like to use? › Default
Which color would you like to use as base color? › Slate
Where is your global CSS file? › › app/globals.css
Expand Down
1 change: 1 addition & 0 deletions apps/www/content/docs/installation/remix.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ npx shadcn-ui@latest init
You will be asked a few questions to configure `components.json`:

```txt showLineNumbers
Would you like to use TypeScript (recommended)? no / yes
Which style would you like to use? › Default
Which color would you like to use as base color? › Slate
Where is your global CSS file? › app/tailwind.css
Expand Down
1 change: 1 addition & 0 deletions apps/www/content/docs/installation/vite.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ npx shadcn-ui@latest init
You will be asked a few questions to configure `components.json`:

```txt showLineNumbers
Would you like to use TypeScript (recommended)? no / yes
Which style would you like to use? › Default
Which color would you like to use as base color? › Slate
Where is your global CSS file? › › src/index.css
Expand Down
3 changes: 3 additions & 0 deletions apps/www/public/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"rsc": {
"type": "boolean"
},
"tsx": {
"type": "boolean"
},
"aliases": {
"type": "object",
"properties": {
Expand Down
7 changes: 7 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,30 @@
},
"dependencies": {
"@antfu/ni": "^0.21.4",
"@babel/core": "^7.22.1",
"@babel/parser": "^7.22.6",
"@babel/plugin-transform-typescript": "^7.22.5",
"chalk": "5.2.0",
"commander": "^10.0.0",
"cosmiconfig": "^8.1.3",
"diff": "^5.1.0",
"execa": "^7.0.0",
"fs-extra": "^11.1.0",
"https-proxy-agent": "^6.2.0",
"lodash.template": "^4.5.0",
"node-fetch": "^3.3.0",
"ora": "^6.1.2",
"prompts": "^2.4.2",
"recast": "^0.23.2",
"ts-morph": "^18.0.0",
"tsconfig-paths": "^4.2.0",
"zod": "^3.20.2"
},
"devDependencies": {
"@types/babel__core": "^7.20.1",
"@types/diff": "^5.0.3",
"@types/fs-extra": "^11.0.1",
"@types/lodash.template": "^4.5.1",
"@types/prompts": "^2.4.2",
"rimraf": "^4.1.3",
"tsup": "^6.6.3",
Expand Down
6 changes: 5 additions & 1 deletion packages/cli/src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export const add = new Command()
}

for (const file of item.files) {
const filePath = path.resolve(targetDir, file.name)
let filePath = path.resolve(targetDir, file.name)

// Run transformers.
const content = await transform({
Expand All @@ -153,6 +153,10 @@ export const add = new Command()
baseColor,
})

if (!config.tsx) {
filePath = filePath.replace(/\.tsx$/, ".jsx")
}

await fs.writeFile(filePath, content)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ async function diffComponent(
baseColor,
})

const patch = diffLines(registryContent, fileContent)
const patch = diffLines(registryContent as string, fileContent)
if (patch.length > 1) {
changes.push({
file: file.name,
Expand Down
27 changes: 21 additions & 6 deletions packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { existsSync, promises as fs } from "fs"
import path from "path"
import {
DEFAULT_COMPONENTS,
DEFAULT_TAILWIND_BASE_COLOR,
DEFAULT_TAILWIND_CONFIG,
DEFAULT_TAILWIND_CSS,
DEFAULT_UTILS,
Expand All @@ -23,6 +22,7 @@ import * as templates from "@/src/utils/templates"
import chalk from "chalk"
import { Command } from "commander"
import { execa } from "execa"
import template from "lodash.template"
import ora from "ora"
import prompts from "prompts"
import * as z from "zod"
Expand Down Expand Up @@ -86,6 +86,16 @@ export async function promptForConfig(
const baseColors = await getRegistryBaseColors()

const options = await prompts([
{
type: "toggle",
name: "typescript",
message: `Would you like to use ${highlight(
"TypeScript"
)} (recommended)?`,
initial: defaultConfig?.tsx ?? true,
active: "yes",
inactive: "no",
},
{
type: "select",
name: "style",
Expand Down Expand Up @@ -115,7 +125,9 @@ export async function promptForConfig(
{
type: "toggle",
name: "tailwindCssVariables",
message: `Do you want to use ${highlight("CSS variables")} for colors?`,
message: `Would you like to use ${highlight(
"CSS variables"
)} for colors?`,
initial: defaultConfig?.tailwind.cssVariables ?? true,
active: "yes",
inactive: "no",
Expand Down Expand Up @@ -158,6 +170,7 @@ export async function promptForConfig(
cssVariables: options.tailwindCssVariables,
},
rsc: options.rsc,
tsx: options.typescript,
aliases: {
utils: options.utils,
components: options.components,
Expand Down Expand Up @@ -213,12 +226,14 @@ export async function runInit(cwd: string, config: Config) {
}
}

const extension = config.tsx ? "ts" : "js"

// Write tailwind config.
await fs.writeFile(
config.resolvedPaths.tailwindConfig,
config.tailwind.cssVariables
? templates.TAILWIND_CONFIG_WITH_VARIABLES
: templates.TAILWIND_CONFIG,
? template(templates.TAILWIND_CONFIG_WITH_VARIABLES)({ extension })
: template(templates.TAILWIND_CONFIG)({ extension }),
"utf8"
)

Expand All @@ -236,8 +251,8 @@ export async function runInit(cwd: string, config: Config) {

// Write cn file.
await fs.writeFile(
`${config.resolvedPaths.utils}.ts`,
templates.UTILS,
`${config.resolvedPaths.utils}.${extension}`,
extension === "ts" ? templates.UTILS : templates.UTILS_JS,
"utf8"
)

Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/utils/get-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const rawConfigSchema = z
$schema: z.string().optional(),
style: z.string(),
rsc: z.coerce.boolean().default(false),
tsx: z.coerce.boolean().default(true),
tailwind: z.object({
config: z.string(),
css: z.string(),
Expand Down Expand Up @@ -89,6 +90,6 @@ export async function getRawConfig(cwd: string): Promise<RawConfig | null> {

return rawConfigSchema.parse(configResult.config)
} catch (error) {
throw new Error(`Invald configuration found in ${cwd}/components.json.`)
throw new Error(`Invalid configuration found in ${cwd}/components.json.`)
}
}
Loading

1 comment on commit edc653c

@vercel
Copy link

@vercel vercel bot commented on edc653c Jul 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ui – ./apps/www

ui-git-main-shadcn-pro.vercel.app
example-playground.vercel.app
ui.shadcn.com
ui-shadcn-pro.vercel.app

Please sign in to comment.