@tpzdsp/next-toolkit 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +594 -0
- package/package.json +133 -0
- package/src/assets/fonts/gds-bold-w-v2.woff +0 -0
- package/src/assets/fonts/gds-bold-w2-v2.woff2 +0 -0
- package/src/assets/fonts/gds-light-w-v2.woff +0 -0
- package/src/assets/fonts/gds-light-w2-v2.woff2 +0 -0
- package/src/assets/images/defra-logo.svg +51 -0
- package/src/assets/images/ea-logo.svg +58 -0
- package/src/assets/images/ogl.svg +1 -0
- package/src/assets/styles/globals.css +68 -0
- package/src/assets/styles/index.ts +7 -0
- package/src/components/Button/Button.stories.tsx +41 -0
- package/src/components/Button/Button.test.tsx +55 -0
- package/src/components/Button/Button.tsx +44 -0
- package/src/components/Card/Card.stories.tsx +35 -0
- package/src/components/Card/Card.test.tsx +12 -0
- package/src/components/Card/Card.tsx +19 -0
- package/src/components/ErrorText/ErrorText.stories.tsx +34 -0
- package/src/components/ErrorText/ErrorText.test.tsx +35 -0
- package/src/components/ErrorText/ErrorText.tsx +17 -0
- package/src/components/Heading/Heading.stories.tsx +21 -0
- package/src/components/Heading/Heading.test.tsx +24 -0
- package/src/components/Heading/Heading.tsx +21 -0
- package/src/components/Hint/Hint.stories.tsx +40 -0
- package/src/components/Hint/Hint.test.tsx +35 -0
- package/src/components/Hint/Hint.tsx +13 -0
- package/src/components/Paragraph/Paragraph.stories.tsx +30 -0
- package/src/components/Paragraph/Paragraph.test.tsx +12 -0
- package/src/components/Paragraph/Paragraph.tsx +7 -0
- package/src/components/container/Container.tsx +38 -0
- package/src/components/dropdown/DropdownMenu.test.tsx +213 -0
- package/src/components/dropdown/DropdownMenu.tsx +106 -0
- package/src/components/dropdown/useDropdownMenu.ts +245 -0
- package/src/components/images/DefraLogo.tsx +64 -0
- package/src/components/images/EaLogo.tsx +81 -0
- package/src/components/images/OglLogo.tsx +18 -0
- package/src/components/index.ts +36 -0
- package/src/components/layout/footer/Copyright.tsx +14 -0
- package/src/components/layout/footer/Footer.tsx +26 -0
- package/src/components/layout/footer/Licence.tsx +19 -0
- package/src/components/layout/footer/MetaLinks.tsx +36 -0
- package/src/components/layout/header/Header.tsx +89 -0
- package/src/components/layout/header/HeaderAuthClient.tsx +32 -0
- package/src/components/layout/header/HeaderNavClient.tsx +64 -0
- package/src/components/link/ExternalLink.tsx +29 -0
- package/src/components/link/Link.tsx +26 -0
- package/src/components/link/NextLinkWrapper.tsx +66 -0
- package/src/components/theme/ThemeProvider.tsx +30 -0
- package/src/contexts/ThemeContext.tsx +72 -0
- package/src/contexts/index.ts +5 -0
- package/src/hooks/index.ts +6 -0
- package/src/hooks/useDebounce.ts +18 -0
- package/src/hooks/useLocalStorage.ts +57 -0
- package/src/index.ts +8 -0
- package/src/types.ts +99 -0
- package/src/utils/auth.ts +19 -0
- package/src/utils/constants.ts +3 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/renderers.tsx +68 -0
- package/src/utils/utils.ts +63 -0
- package/src/vite-env.d.ts +17 -0
package/README.md
ADDED
|
@@ -0,0 +1,594 @@
|
|
|
1
|
+
# Next.js Component Library
|
|
2
|
+
|
|
3
|
+
A modern, reusable React component library designed specifically for Next.js applications. This library provides a collection of client and server components, hooks, contexts, and utilities with full TypeScript support.
|
|
4
|
+
|
|
5
|
+
## ๐ Prerequisites
|
|
6
|
+
|
|
7
|
+
- **Node.js**: 22.17.1 (managed with `asdf`)
|
|
8
|
+
- **Package Manager**: Yarn (preferred) or npm
|
|
9
|
+
- **Next.js**: 13+ (peer dependency)
|
|
10
|
+
- **React**: 18+ (peer dependency)
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- ๐ **Next.js Optimized**: Built specifically for Next.js with support for both client and server components
|
|
15
|
+
- ๐จ **Tailwind CSS v3**: Styled with Tailwind CSS and includes `tailwind-merge` for efficient class merging
|
|
16
|
+
- ๐ฆ **Multiple Export Patterns**: Separate exports for client, server, and shared utilities
|
|
17
|
+
- ๐งช **Fully Tested**: Comprehensive test coverage with Vitest and React Testing Library
|
|
18
|
+
- ๐ **TypeScript First**: Full TypeScript support with proper type definitions
|
|
19
|
+
- โก **Fast Development**: Built with Vite for lightning-fast development and builds
|
|
20
|
+
- ๐ฏ **Tree Shakeable**: Optimized for tree-shaking to reduce bundle size
|
|
21
|
+
- ๐ **Source Distribution**: Distributes raw TypeScript files for optimal bundling
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
yarn add @your-org/nextjs-library
|
|
27
|
+
# or
|
|
28
|
+
npm install @your-org/nextjs-library
|
|
29
|
+
# or
|
|
30
|
+
pnpm add @your-org/nextjs-library
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
### Basic Import
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
// Import everything
|
|
39
|
+
import { Button, Card, useLocalStorage } from '@your-org/nextjs-library';
|
|
40
|
+
|
|
41
|
+
// Import styles (choose one method)
|
|
42
|
+
import '@your-org/nextjs-library/styles';
|
|
43
|
+
// OR if the above doesn't work:
|
|
44
|
+
// import '@your-org/nextjs-library/src/assets/styles/globals.css';
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Selective Imports (Recommended)
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
// Import only client components
|
|
51
|
+
import { Button, NextLinkWrapper } from '@your-org/nextjs-library/client';
|
|
52
|
+
|
|
53
|
+
// Import only server components
|
|
54
|
+
import { Card, Container } from '@your-org/nextjs-library/server';
|
|
55
|
+
|
|
56
|
+
// Import only types
|
|
57
|
+
import type { ButtonProps, ApiResponse, FormFieldProps } from '@your-org/nextjs-library/types';
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Example Usage
|
|
61
|
+
|
|
62
|
+
```tsx
|
|
63
|
+
import { Button } from '@your-org/nextjs-library/client';
|
|
64
|
+
import { Card } from '@your-org/nextjs-library/server';
|
|
65
|
+
import '@your-org/nextjs-library/styles'; // or the src path if this doesn't work
|
|
66
|
+
|
|
67
|
+
export default function MyPage() {
|
|
68
|
+
return (
|
|
69
|
+
<Card title="Welcome" description="Get started with our components">
|
|
70
|
+
<Button variant="primary" size="lg">
|
|
71
|
+
Get Started
|
|
72
|
+
</Button>
|
|
73
|
+
</Card>
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Using Exported Types
|
|
79
|
+
|
|
80
|
+
```tsx
|
|
81
|
+
import type {
|
|
82
|
+
ButtonProps,
|
|
83
|
+
ButtonVariant,
|
|
84
|
+
CardProps,
|
|
85
|
+
BaseProps,
|
|
86
|
+
ThemeContextValue,
|
|
87
|
+
} from '@your-org/nextjs-library';
|
|
88
|
+
|
|
89
|
+
// Use library types in your own components
|
|
90
|
+
interface MyCustomButtonProps extends ButtonProps {
|
|
91
|
+
icon?: React.ReactNode;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Use type unions for consistency
|
|
95
|
+
const handleVariantChange = (variant: ButtonVariant) => {
|
|
96
|
+
console.log('Variant changed to:', variant);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// Extend base props for your own components
|
|
100
|
+
interface MyComponentProps extends BaseProps {
|
|
101
|
+
customProp: string;
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Available Components
|
|
106
|
+
|
|
107
|
+
### Client Components
|
|
108
|
+
|
|
109
|
+
- **Button**: Interactive button with multiple variants and loading states
|
|
110
|
+
- **NextLinkWrapper**: Enhanced Next.js Link wrapper with external link handling
|
|
111
|
+
- **ThemeProvider**: Theme context provider for dark/light mode
|
|
112
|
+
|
|
113
|
+
### Server Components
|
|
114
|
+
|
|
115
|
+
- **Card**: Flexible card component with title and description
|
|
116
|
+
- **Container**: Responsive container with multiple size options
|
|
117
|
+
|
|
118
|
+
### Hooks
|
|
119
|
+
|
|
120
|
+
- **useLocalStorage**: localStorage hook with SSR support
|
|
121
|
+
- **useDebounce**: Debounce values and functions
|
|
122
|
+
|
|
123
|
+
### Utilities
|
|
124
|
+
|
|
125
|
+
- **cn()**: Tailwind class merging utility using tailwind-merge
|
|
126
|
+
- **debounce()**: Function debouncing utility
|
|
127
|
+
- **formatBytes()**: Human-readable byte formatting
|
|
128
|
+
|
|
129
|
+
### Types & Interfaces
|
|
130
|
+
|
|
131
|
+
- **Component Props**: `ButtonProps`, `CardProps`, `ContainerProps`, etc.
|
|
132
|
+
- **Common Types**: `BaseProps`, `Variant`, `Size`, `Status`, `Theme`
|
|
133
|
+
- **Form Types**: `FormFieldProps`, `ChangeHandler`, `SubmitHandler`
|
|
134
|
+
- **API Types**: `ApiResponse<T>`, `PaginatedResponse<T>`, `PaginationMeta`
|
|
135
|
+
- **Utility Types**: `PropsWithRequired<T, K>`, `PropsWithOptional<T, K>`
|
|
136
|
+
|
|
137
|
+
## ๐๏ธ Project Structure
|
|
138
|
+
|
|
139
|
+
```
|
|
140
|
+
src/
|
|
141
|
+
โโโ components/
|
|
142
|
+
โ โโโ client/ # Client components ('use client' directive)
|
|
143
|
+
โ โ โโโ Button/
|
|
144
|
+
โ โ โ โโโ Button.tsx
|
|
145
|
+
โ โ โ โโโ Button.test.tsx
|
|
146
|
+
โ โ โโโ NextLinkWrapper/
|
|
147
|
+
โ โ โโโ ThemeProvider/
|
|
148
|
+
โ โโโ server/ # Server components (SSR compatible)
|
|
149
|
+
โ โโโ Card/
|
|
150
|
+
โ โ โโโ Card.tsx
|
|
151
|
+
โ โ โโโ Card.test.tsx (when created)
|
|
152
|
+
โ โโโ Container/
|
|
153
|
+
โโโ hooks/ # React hooks (client-side only)
|
|
154
|
+
โ โโโ useLocalStorage.ts
|
|
155
|
+
โ โโโ useDebounce.ts
|
|
156
|
+
โโโ contexts/ # React contexts (client-side only)
|
|
157
|
+
โ โโโ ThemeContext.tsx
|
|
158
|
+
โโโ utils/ # Utilities (client & server compatible)
|
|
159
|
+
โ โโโ index.ts
|
|
160
|
+
โโโ assets/ # CSS, images, and static assets
|
|
161
|
+
โ โโโ styles/
|
|
162
|
+
โ โโโ images/
|
|
163
|
+
โโโ types.ts # TypeScript type definitions
|
|
164
|
+
โโโ index.ts # Main library exports
|
|
165
|
+
|
|
166
|
+
vitest.setup.ts # Test configuration (top-level)
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## ๐งญ Architecture Decisions
|
|
170
|
+
|
|
171
|
+
### Client vs Server Components
|
|
172
|
+
|
|
173
|
+
- **Client Components** (`src/components/client/`): Include `'use client';` directive, used for interactive features, hooks, and state management
|
|
174
|
+
- **Server Components** (`src/components/server/`): No directive needed, can be server-side rendered, used for static content and layouts
|
|
175
|
+
|
|
176
|
+
### Source Distribution Strategy
|
|
177
|
+
|
|
178
|
+
This library distributes **raw TypeScript/TSX files** instead of compiled bundles:
|
|
179
|
+
|
|
180
|
+
- โ
Better tree-shaking in consuming applications
|
|
181
|
+
- โ
Consuming apps control the build process and browser targets
|
|
182
|
+
- โ
Smaller final bundle sizes
|
|
183
|
+
- โ
No build step needed during development
|
|
184
|
+
|
|
185
|
+
### Styling Approach
|
|
186
|
+
|
|
187
|
+
- **Tailwind CSS v3** for utility-first styling
|
|
188
|
+
- **tailwind-merge** (`cn()` utility) for intelligent class merging
|
|
189
|
+
- Component-specific CSS classes in `assets/styles/globals.css`
|
|
190
|
+
|
|
191
|
+
## Development
|
|
192
|
+
|
|
193
|
+
```bash
|
|
194
|
+
# Install dependencies
|
|
195
|
+
yarn install
|
|
196
|
+
|
|
197
|
+
# Start development server
|
|
198
|
+
yarn dev
|
|
199
|
+
|
|
200
|
+
# Run tests
|
|
201
|
+
yarn test
|
|
202
|
+
|
|
203
|
+
# Run tests with UI
|
|
204
|
+
yarn test:ui
|
|
205
|
+
|
|
206
|
+
# Build the library (TypeScript check only)
|
|
207
|
+
yarn build
|
|
208
|
+
|
|
209
|
+
# Build distribution files (if needed)
|
|
210
|
+
yarn build:dist
|
|
211
|
+
|
|
212
|
+
# Preview the built library
|
|
213
|
+
yarn preview
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
## Local Development with Yalc
|
|
217
|
+
|
|
218
|
+
This library works perfectly with `yalc` for local testing before publishing to npm. Since we distribute source files, changes are reflected immediately.
|
|
219
|
+
|
|
220
|
+
### Setup Yalc (one-time)
|
|
221
|
+
|
|
222
|
+
```bash
|
|
223
|
+
# Install yalc globally
|
|
224
|
+
npm install -g yalc
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### Publishing to Yalc Store
|
|
228
|
+
|
|
229
|
+
```bash
|
|
230
|
+
# In this library directory
|
|
231
|
+
yarn yalc:publish
|
|
232
|
+
# or
|
|
233
|
+
yalc publish
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### Using in Your Next.js App
|
|
237
|
+
|
|
238
|
+
```bash
|
|
239
|
+
# In your Next.js app directory
|
|
240
|
+
yalc add @your-org/nextjs-library
|
|
241
|
+
|
|
242
|
+
# Install dependencies (yalc creates a symlink-like structure)
|
|
243
|
+
yarn install
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
### Your Next.js App Configuration
|
|
247
|
+
|
|
248
|
+
Add to your `next.config.js`:
|
|
249
|
+
|
|
250
|
+
```js
|
|
251
|
+
/** @type {import('next').NextConfig} */
|
|
252
|
+
const nextConfig = {
|
|
253
|
+
transpilePackages: ['@your-org/nextjs-library'],
|
|
254
|
+
// Enable if you want faster refresh during development
|
|
255
|
+
experimental: {
|
|
256
|
+
externalDir: true,
|
|
257
|
+
},
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
module.exports = nextConfig;
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
### Development Workflow
|
|
264
|
+
|
|
265
|
+
```bash
|
|
266
|
+
# 1. Make changes to the library
|
|
267
|
+
# 2. Push changes to apps using the library
|
|
268
|
+
yarn yalc:push
|
|
269
|
+
# or
|
|
270
|
+
yalc push
|
|
271
|
+
|
|
272
|
+
# 3. Your Next.js app will automatically pick up the changes!
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
### Removing from Test App
|
|
276
|
+
|
|
277
|
+
```bash
|
|
278
|
+
# In your Next.js app directory
|
|
279
|
+
yalc remove @your-org/nextjs-library
|
|
280
|
+
yarn install
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
### Benefits with Source Distribution
|
|
284
|
+
|
|
285
|
+
- โ
**Instant Changes**: Since we distribute source files, changes appear immediately
|
|
286
|
+
- โ
**Full TypeScript**: Complete type checking and IntelliSense
|
|
287
|
+
- โ
**Tree Shaking**: Your app's bundler handles optimization
|
|
288
|
+
- โ
**Hot Reload**: Works with Next.js development server
|
|
289
|
+
- โ
**No Build Step**: No need to rebuild the library for every change
|
|
290
|
+
|
|
291
|
+
## ๐ Quick Yalc Workflow
|
|
292
|
+
|
|
293
|
+
### Library Development (this repo):
|
|
294
|
+
|
|
295
|
+
```bash
|
|
296
|
+
# First time setup
|
|
297
|
+
yalc publish
|
|
298
|
+
|
|
299
|
+
# After making changes
|
|
300
|
+
yalc push # Pushes to all connected apps
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
### In Your Next.js Test App:
|
|
304
|
+
|
|
305
|
+
```bash
|
|
306
|
+
# Add the library
|
|
307
|
+
yalc add @your-org/nextjs-library
|
|
308
|
+
yarn install
|
|
309
|
+
|
|
310
|
+
# Configure next.config.js (transpilePackages: ['@your-org/nextjs-library'])
|
|
311
|
+
|
|
312
|
+
# Remove when done testing
|
|
313
|
+
yalc remove @your-org/nextjs-library
|
|
314
|
+
yarn install
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
## ๐ ๏ธ Adding New Components
|
|
318
|
+
|
|
319
|
+
### Client Component Template
|
|
320
|
+
|
|
321
|
+
```tsx
|
|
322
|
+
// src/components/client/MyComponent/MyComponent.tsx
|
|
323
|
+
'use client';
|
|
324
|
+
|
|
325
|
+
import React from 'react';
|
|
326
|
+
import { cn } from '../../../utils';
|
|
327
|
+
|
|
328
|
+
export interface MyComponentProps {
|
|
329
|
+
children: React.ReactNode;
|
|
330
|
+
className?: string;
|
|
331
|
+
// Add your props here
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
export const MyComponent: React.FC<MyComponentProps> = ({ children, className, ...props }) => {
|
|
335
|
+
return (
|
|
336
|
+
<div className={cn('base-classes', className)} {...props}>
|
|
337
|
+
{children}
|
|
338
|
+
</div>
|
|
339
|
+
);
|
|
340
|
+
};
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### Server Component Template
|
|
344
|
+
|
|
345
|
+
```tsx
|
|
346
|
+
// src/components/server/MyComponent/MyComponent.tsx
|
|
347
|
+
import React from 'react';
|
|
348
|
+
import { cn } from '../../../utils';
|
|
349
|
+
|
|
350
|
+
export interface MyComponentProps {
|
|
351
|
+
children: React.ReactNode;
|
|
352
|
+
className?: string;
|
|
353
|
+
// Add your props here
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
export const MyComponent: React.FC<MyComponentProps> = ({ children, className, ...props }) => {
|
|
357
|
+
return (
|
|
358
|
+
<div className={cn('base-classes', className)} {...props}>
|
|
359
|
+
{children}
|
|
360
|
+
</div>
|
|
361
|
+
);
|
|
362
|
+
};
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
### Don't Forget To:
|
|
366
|
+
|
|
367
|
+
1. Add exports to the appropriate `index.ts` file
|
|
368
|
+
2. Create tests co-located with your component (e.g., `MyComponent.test.tsx`)
|
|
369
|
+
3. Update TypeScript types if needed
|
|
370
|
+
4. Test with `yalc` in a real Next.js app
|
|
371
|
+
|
|
372
|
+
## ๐ Troubleshooting
|
|
373
|
+
|
|
374
|
+
### Common Issues
|
|
375
|
+
|
|
376
|
+
**"Cannot resolve module" errors:**
|
|
377
|
+
|
|
378
|
+
```bash
|
|
379
|
+
# Make sure transpilePackages is configured in next.config.js
|
|
380
|
+
transpilePackages: ['@your-org/nextjs-library']
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
**TypeScript errors in consuming app:**
|
|
384
|
+
|
|
385
|
+
```bash
|
|
386
|
+
# Ensure your tsconfig.json includes:
|
|
387
|
+
{
|
|
388
|
+
"compilerOptions": {
|
|
389
|
+
"moduleResolution": "bundler", // or "node16"
|
|
390
|
+
"allowSyntheticDefaultImports": true,
|
|
391
|
+
"esModuleInterop": true
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
**Tailwind styles not working:**
|
|
397
|
+
|
|
398
|
+
```bash
|
|
399
|
+
# Import the library styles in your app:
|
|
400
|
+
import '@your-org/nextjs-library/styles';
|
|
401
|
+
|
|
402
|
+
# Or alternatively, import the CSS file directly:
|
|
403
|
+
import '@your-org/nextjs-library/src/assets/styles/globals.css';
|
|
404
|
+
|
|
405
|
+
# And include the library in your tailwind.config.js content:
|
|
406
|
+
content: [
|
|
407
|
+
// your paths...
|
|
408
|
+
'./node_modules/@your-org/nextjs-library/src/**/*.{js,ts,jsx,tsx}',
|
|
409
|
+
]
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
**yalc issues:**
|
|
413
|
+
|
|
414
|
+
```bash
|
|
415
|
+
# Reset yalc completely
|
|
416
|
+
yalc remove --all
|
|
417
|
+
yalc publish
|
|
418
|
+
yalc add @your-org/nextjs-library
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
## Testing
|
|
422
|
+
|
|
423
|
+
This library uses Vitest and React Testing Library for testing:
|
|
424
|
+
|
|
425
|
+
```bash
|
|
426
|
+
# Run all tests
|
|
427
|
+
yarn test
|
|
428
|
+
|
|
429
|
+
# Run tests in watch mode
|
|
430
|
+
yarn test:watch
|
|
431
|
+
|
|
432
|
+
# Run tests with coverage
|
|
433
|
+
yarn test:coverage
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
## Build Configuration
|
|
437
|
+
|
|
438
|
+
The library distributes **raw TypeScript/TSX source files** rather than pre-built bundles. This approach offers several benefits:
|
|
439
|
+
|
|
440
|
+
- **Better Tree-Shaking**: Your bundler can eliminate unused code more effectively
|
|
441
|
+
- **Optimal Bundling**: Your build process optimizes the code for your specific use case
|
|
442
|
+
- **Smaller Bundle Size**: No duplicate dependencies or unnecessary polyfills
|
|
443
|
+
- **Modern Output**: Your bundler can target your specific browser requirements
|
|
444
|
+
|
|
445
|
+
### Requirements for Consuming Apps
|
|
446
|
+
|
|
447
|
+
Your Next.js application needs to be configured to transpile the library:
|
|
448
|
+
|
|
449
|
+
```js
|
|
450
|
+
// next.config.js
|
|
451
|
+
/** @type {import('next').NextConfig} */
|
|
452
|
+
const nextConfig = {
|
|
453
|
+
transpilePackages: ['@your-org/nextjs-library'],
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
module.exports = nextConfig;
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
## Tailwind CSS Integration
|
|
460
|
+
|
|
461
|
+
To use the library's Tailwind styles in your Next.js project:
|
|
462
|
+
|
|
463
|
+
1. Import the library's Tailwind config:
|
|
464
|
+
|
|
465
|
+
```js
|
|
466
|
+
// tailwind.config.js
|
|
467
|
+
import libraryConfig from '@your-org/nextjs-library/tailwind';
|
|
468
|
+
|
|
469
|
+
export default {
|
|
470
|
+
...libraryConfig,
|
|
471
|
+
content: [
|
|
472
|
+
...libraryConfig.content,
|
|
473
|
+
'./pages/**/*.{js,ts,jsx,tsx,mdx}',
|
|
474
|
+
'./components/**/*.{js,ts,jsx,tsx,mdx}',
|
|
475
|
+
'./app/**/*.{js,ts,jsx,tsx,mdx}',
|
|
476
|
+
],
|
|
477
|
+
};
|
|
478
|
+
```
|
|
479
|
+
|
|
480
|
+
2. Import the library styles:
|
|
481
|
+
|
|
482
|
+
```js
|
|
483
|
+
// pages/_app.js or app/layout.js or globals.css
|
|
484
|
+
import '@your-org/nextjs-library/styles';
|
|
485
|
+
|
|
486
|
+
// Alternative method (if the above doesn't work):
|
|
487
|
+
import '@your-org/nextjs-library/src/assets/styles/globals.css';
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
## Contributing
|
|
491
|
+
|
|
492
|
+
### Getting Started
|
|
493
|
+
|
|
494
|
+
1. **Install asdf and set Node.js version:**
|
|
495
|
+
|
|
496
|
+
```bash
|
|
497
|
+
# Install asdf if you haven't already
|
|
498
|
+
# Then install the required Node.js version
|
|
499
|
+
asdf install nodejs 22.17.1
|
|
500
|
+
asdf local nodejs 22.17.1
|
|
501
|
+
```
|
|
502
|
+
|
|
503
|
+
2. **Install dependencies:**
|
|
504
|
+
|
|
505
|
+
```bash
|
|
506
|
+
yarn install
|
|
507
|
+
```
|
|
508
|
+
|
|
509
|
+
3. **Run tests to ensure everything works:**
|
|
510
|
+
```bash
|
|
511
|
+
yarn test
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
### Development Workflow
|
|
515
|
+
|
|
516
|
+
1. Fork the repository
|
|
517
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
518
|
+
3. Make your changes following our component templates above
|
|
519
|
+
4. Add tests for your changes (`src/test/YourComponent.test.tsx`)
|
|
520
|
+
5. Run the test suite (`yarn test`)
|
|
521
|
+
6. Test with yalc in a real Next.js app
|
|
522
|
+
7. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
|
523
|
+
8. Push to the branch (`git push origin feature/amazing-feature`)
|
|
524
|
+
9. Open a Pull Request
|
|
525
|
+
|
|
526
|
+
### Code Style
|
|
527
|
+
|
|
528
|
+
- Use TypeScript for all files
|
|
529
|
+
- Follow the existing component patterns
|
|
530
|
+
- Use `cn()` utility for className merging
|
|
531
|
+
- Include proper TypeScript interfaces
|
|
532
|
+
- Add tests for new components/functions
|
|
533
|
+
- Document props and functions with JSDoc comments
|
|
534
|
+
|
|
535
|
+
## License
|
|
536
|
+
|
|
537
|
+
MIT License - see the [LICENSE](LICENSE) file for details.
|
|
538
|
+
|
|
539
|
+
```js
|
|
540
|
+
export default tseslint.config([
|
|
541
|
+
globalIgnores(['dist']),
|
|
542
|
+
{
|
|
543
|
+
files: ['**/*.{ts,tsx}'],
|
|
544
|
+
extends: [
|
|
545
|
+
// Other configs...
|
|
546
|
+
|
|
547
|
+
// Remove tseslint.configs.recommended and replace with this
|
|
548
|
+
...tseslint.configs.recommendedTypeChecked,
|
|
549
|
+
// Alternatively, use this for stricter rules
|
|
550
|
+
...tseslint.configs.strictTypeChecked,
|
|
551
|
+
// Optionally, add this for stylistic rules
|
|
552
|
+
...tseslint.configs.stylisticTypeChecked,
|
|
553
|
+
|
|
554
|
+
// Other configs...
|
|
555
|
+
],
|
|
556
|
+
languageOptions: {
|
|
557
|
+
parserOptions: {
|
|
558
|
+
project: ['./tsconfig.node.json', './tsconfig.app.json'],
|
|
559
|
+
tsconfigRootDir: import.meta.dirname,
|
|
560
|
+
},
|
|
561
|
+
// other options...
|
|
562
|
+
},
|
|
563
|
+
},
|
|
564
|
+
]);
|
|
565
|
+
```
|
|
566
|
+
|
|
567
|
+
You can also install [eslint-plugin-react-x](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x) and [eslint-plugin-react-dom](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-dom) for React-specific lint rules:
|
|
568
|
+
|
|
569
|
+
```js
|
|
570
|
+
// eslint.config.js
|
|
571
|
+
import reactX from 'eslint-plugin-react-x';
|
|
572
|
+
import reactDom from 'eslint-plugin-react-dom';
|
|
573
|
+
|
|
574
|
+
export default tseslint.config([
|
|
575
|
+
globalIgnores(['dist']),
|
|
576
|
+
{
|
|
577
|
+
files: ['**/*.{ts,tsx}'],
|
|
578
|
+
extends: [
|
|
579
|
+
// Other configs...
|
|
580
|
+
// Enable lint rules for React
|
|
581
|
+
reactX.configs['recommended-typescript'],
|
|
582
|
+
// Enable lint rules for React DOM
|
|
583
|
+
reactDom.configs.recommended,
|
|
584
|
+
],
|
|
585
|
+
languageOptions: {
|
|
586
|
+
parserOptions: {
|
|
587
|
+
project: ['./tsconfig.node.json', './tsconfig.app.json'],
|
|
588
|
+
tsconfigRootDir: import.meta.dirname,
|
|
589
|
+
},
|
|
590
|
+
// other options...
|
|
591
|
+
},
|
|
592
|
+
},
|
|
593
|
+
]);
|
|
594
|
+
```
|