Skip to content

Commit 3779bc5

Browse files
JonghakseoHiCreatripPatrykKuniczak
authored
Adding shadcn guide to README.md (Jonghakseo#739)
Co-authored-by: Jonghakseo <jonghak.seo@creatrip.com> Co-authored-by: Patryk Kuniczak <p.kuniczak@gmail.com>
1 parent a41db0a commit 3779bc5

File tree

5 files changed

+406
-52
lines changed

5 files changed

+406
-52
lines changed

packages/ui/README.md

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,249 @@ Modify the `tailwind.config.ts` file to make global style changes to the package
104104
## Modifying the css variable of the UI library
105105

106106
Modify the css variable in the `ui/lib/global.css` code to change the css variable of the package.
107+
108+
## Guide for Adding shadcn to the UI Package
109+
110+
You can refer to the this [manual guide](https://ui.shadcn.com/docs/installation/manual)
111+
112+
1. Create components.json in packages/ui
113+
114+
Create a file named `components.json` in the `packages/ui` directory with the following content:
115+
116+
```json
117+
{
118+
"$schema": "https://ui.shadcn.com/schema.json",
119+
"style": "default",
120+
"rsc": false,
121+
"tsx": true,
122+
"tailwind": {
123+
"config": "tailwind.config.ts",
124+
"css": "lib/global.css",
125+
"baseColor": "neutral",
126+
"cssVariables": true,
127+
"prefix": ""
128+
},
129+
"aliases": {
130+
"components": "@/lib/components",
131+
"utils": "@/lib/utils",
132+
"ui": "@/lib/components/ui",
133+
"lib": "@/lib"
134+
}
135+
}
136+
```
137+
138+
2. Install dependencies
139+
140+
Run the following command from the root of your project:
141+
142+
```shell
143+
pnpm add tailwindcss-animate class-variance-authority -F ui
144+
```
145+
146+
3. Edit `withUI.ts` in `lib` folder
147+
148+
This configuration file is from the manual guide. You can refer to the manual guide to modify the configuration file. ([`Configure tailwind.config.js`](https://ui.shadcn.com/docs/installation/manual))
149+
150+
```ts
151+
import deepmerge from 'deepmerge';
152+
import type { Config } from 'tailwindcss/types/config';
153+
import { fontFamily } from 'tailwindcss/defaultTheme';
154+
import tailwindAnimate from 'tailwindcss-animate';
155+
156+
export function withUI(tailwindConfig: Config): Config {
157+
return deepmerge(
158+
shadcnConfig,
159+
deepmerge(tailwindConfig, {
160+
content: ['./node_modules/@extension/ui/lib/**/*.{tsx,ts,js,jsx}'],
161+
}),
162+
);
163+
}
164+
165+
const shadcnConfig = {
166+
darkMode: ['class'],
167+
theme: {
168+
container: {
169+
center: true,
170+
padding: '2rem',
171+
screens: {
172+
'2xl': '1400px',
173+
},
174+
},
175+
extend: {
176+
colors: {
177+
border: 'hsl(var(--border))',
178+
input: 'hsl(var(--input))',
179+
ring: 'hsl(var(--ring))',
180+
background: 'hsl(var(--background))',
181+
foreground: 'hsl(var(--foreground))',
182+
primary: {
183+
DEFAULT: 'hsl(var(--primary))',
184+
foreground: 'hsl(var(--primary-foreground))',
185+
},
186+
secondary: {
187+
DEFAULT: 'hsl(var(--secondary))',
188+
foreground: 'hsl(var(--secondary-foreground))',
189+
},
190+
destructive: {
191+
DEFAULT: 'hsl(var(--destructive))',
192+
foreground: 'hsl(var(--destructive-foreground))',
193+
},
194+
muted: {
195+
DEFAULT: 'hsl(var(--muted))',
196+
foreground: 'hsl(var(--muted-foreground))',
197+
},
198+
accent: {
199+
DEFAULT: 'hsl(var(--accent))',
200+
foreground: 'hsl(var(--accent-foreground))',
201+
},
202+
popover: {
203+
DEFAULT: 'hsl(var(--popover))',
204+
foreground: 'hsl(var(--popover-foreground))',
205+
},
206+
card: {
207+
DEFAULT: 'hsl(var(--card))',
208+
foreground: 'hsl(var(--card-foreground))',
209+
},
210+
},
211+
borderRadius: {
212+
lg: `var(--radius)`,
213+
md: `calc(var(--radius) - 2px)`,
214+
sm: 'calc(var(--radius) - 4px)',
215+
},
216+
fontFamily: {
217+
sans: ['var(--font-sans)', ...fontFamily.sans],
218+
},
219+
keyframes: {
220+
'accordion-down': {
221+
from: { height: '0' },
222+
to: { height: 'var(--radix-accordion-content-height)' },
223+
},
224+
'accordion-up': {
225+
from: { height: 'var(--radix-accordion-content-height)' },
226+
to: { height: '0' },
227+
},
228+
},
229+
animation: {
230+
'accordion-down': 'accordion-down 0.2s ease-out',
231+
'accordion-up': 'accordion-up 0.2s ease-out',
232+
},
233+
},
234+
},
235+
plugins: [tailwindAnimate],
236+
};
237+
```
238+
239+
4. Edit `global.css` in `lib` folder
240+
241+
This configuration also comes from the manual guide. You can refer to the manual guide to modify the configuration file. ([`Configure styles`](https://ui.shadcn.com/docs/installation/manual))
242+
243+
```css
244+
@tailwind base;
245+
@tailwind components;
246+
@tailwind utilities;
247+
248+
@layer base {
249+
:root {
250+
--background: 0 0% 100%;
251+
--foreground: 222.2 47.4% 11.2%;
252+
253+
--muted: 210 40% 96.1%;
254+
--muted-foreground: 215.4 16.3% 46.9%;
255+
256+
--popover: 0 0% 100%;
257+
--popover-foreground: 222.2 47.4% 11.2%;
258+
259+
--border: 214.3 31.8% 91.4%;
260+
--input: 214.3 31.8% 91.4%;
261+
262+
--card: 0 0% 100%;
263+
--card-foreground: 222.2 47.4% 11.2%;
264+
265+
--primary: 222.2 47.4% 11.2%;
266+
--primary-foreground: 210 40% 98%;
267+
268+
--secondary: 210 40% 96.1%;
269+
--secondary-foreground: 222.2 47.4% 11.2%;
270+
271+
--accent: 210 40% 96.1%;
272+
--accent-foreground: 222.2 47.4% 11.2%;
273+
274+
--destructive: 0 100% 50%;
275+
--destructive-foreground: 210 40% 98%;
276+
277+
--ring: 215 20.2% 65.1%;
278+
279+
--radius: 0.5rem;
280+
}
281+
282+
.dark {
283+
--background: 224 71% 4%;
284+
--foreground: 213 31% 91%;
285+
286+
--muted: 223 47% 11%;
287+
--muted-foreground: 215.4 16.3% 56.9%;
288+
289+
--accent: 216 34% 17%;
290+
--accent-foreground: 210 40% 98%;
291+
292+
--popover: 224 71% 4%;
293+
--popover-foreground: 215 20.2% 65.1%;
294+
295+
--border: 216 34% 17%;
296+
--input: 216 34% 17%;
297+
298+
--card: 224 71% 4%;
299+
--card-foreground: 213 31% 91%;
300+
301+
--primary: 210 40% 98%;
302+
--primary-foreground: 222.2 47.4% 1.2%;
303+
304+
--secondary: 222.2 47.4% 11.2%;
305+
--secondary-foreground: 210 40% 98%;
306+
307+
--destructive: 0 63% 31%;
308+
--destructive-foreground: 210 40% 98%;
309+
310+
--ring: 216 34% 17%;
311+
312+
--radius: 0.5rem;
313+
}
314+
}
315+
316+
@layer base {
317+
* {
318+
@apply border-border;
319+
}
320+
body {
321+
@apply bg-background text-foreground;
322+
font-feature-settings: "rlig" 1, "calt" 1;
323+
}
324+
}
325+
```
326+
327+
5. Add shadcn components
328+
329+
Finally, run this command from the root of your project to add the button component:
330+
331+
```shell
332+
pnpm dlx shadcn@latest add button -c ./packages/ui
333+
```
334+
335+
This will add the shadcn button component to your UI package.
336+
337+
Remember to adjust any paths or package names if your project structure differs from the assumed layout in this guide.
338+
339+
6. Export components
340+
341+
Make the `index.ts` file in the `components/ui` directory export the button component:
342+
343+
```ts
344+
export * from './button';
345+
```
346+
347+
Edit the `index.ts` file in the `packages/ui` directory to export the shadcn ui component:
348+
349+
```ts
350+
// export * from './lib/components'; // remove this line: duplicated button component
351+
export * from './lib/components/ui';
352+
```

packages/ui/build.mjs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'node:fs';
2+
import { replaceTscAliasPaths } from 'tsc-alias';
23
import { resolve } from 'node:path';
34
import esbuild from 'esbuild';
45

@@ -15,4 +16,17 @@ const buildOptions = {
1516
};
1617

1718
await esbuild.build(buildOptions);
19+
20+
/**
21+
* Post build paths resolve since ESBuild only natively
22+
* support paths resolution for bundling scenario
23+
* @url https://github.com/evanw/esbuild/issues/394#issuecomment-1537247216
24+
*/
25+
await replaceTscAliasPaths({
26+
configFile: 'tsconfig.json',
27+
watch: false,
28+
outDir: 'dist',
29+
declarationDir: 'dist',
30+
});
31+
1832
fs.copyFileSync(resolve('lib', 'global.css'), resolve('dist', 'global.css'));

packages/ui/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
},
2525
"devDependencies": {
2626
"@extension/tsconfig": "workspace:*",
27-
"deepmerge": "^4.3.1"
27+
"deepmerge": "^4.3.1",
28+
"tsc-alias": "^1.8.10"
2829
},
2930
"dependencies": {
3031
"clsx": "^2.1.1",

packages/ui/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
"compilerOptions": {
44
"outDir": "dist",
55
"baseUrl": ".",
6-
"types": ["chrome"]
6+
"types": ["chrome"],
7+
"paths": {
8+
"@/*": ["./*"]
9+
}
710
},
811
"include": ["index.ts", "lib"]
912
}

0 commit comments

Comments
 (0)