rg-dst
v1.2.22
Published
A scalable, reusable React + TypeScript component library with Vite, Tailwind CSS, Storybook, ESLint, and Vitest. This README helps new contributors understand the structure, conventions, and workflows to confidently add or update code and resolve issues.
Readme
RG Design System Template – Developer Guide
A scalable, reusable React + TypeScript component library with Vite, Tailwind CSS, Storybook, ESLint, and Vitest. This README helps new contributors understand the structure, conventions, and workflows to confidently add or update code and resolve issues.
Stack
- React 19, TypeScript 5, Vite 6
- Tailwind CSS 4 (with
@tailwindcss/vite); PostCSS for CSS build - Storybook 8 (React + Vite)
- Vitest 3 + Testing Library + JSDOM
- ESLint 9 (typescript-eslint, react-hooks, react-refresh)
- Rollup for library bundling (ESM + CJS + DTS)
Project Structure
.
├─ src/
│ ├─ assets/
│ │ └─ style/ # Base CSS tokens and utilities (compiled into dist/style.css)
│ ├─ components/
│ │ ├─ <ComponentName>/ # One folder per public component
│ │ │ ├─ index.tsx # Component implementation (export default/named)
│ │ │ ├─ _.style.ts # Styles (e.g., cva, class composition)
│ │ │ ├─ _.types.ts|type.ts # Component props/types
│ │ │ ├─ _.stories.tsx # Storybook stories
│ │ │ └─ _.test.tsx # Unit tests (Vitest + Testing Library)
│ │ ├─ developers/ # Showcase profiles (excluded from coverage)
│ │ └─ ui/ # Internal building blocks (not public API)
│ ├─ hooks/ # Reusable hooks
│ ├─ icons/ # Icon components + types (excluded from coverage)
│ ├─ lib/ # Utilities shared by components
│ ├─ index.ts # Library public exports
│ ├─ index.css # Tailwind entry (built via PostCSS)
│ └─ main.tsx # Local playground entry (not part of build output)
├─ tests/setup.ts # Vitest setup (jsdom, matchers)
├─ vite.config.ts # Vite + Vitest base config (alias '@' → './src')
├─ vitest.config.ts # Test include/exclude and coverage rules
├─ eslint.config.js # ESLint rules and TS integration
├─ postcss.config.mjs # PostCSS pipeline (imports, autoprefixer)
├─ rollup.config.mjs # Library bundle (esm/cjs/dts) for publish
├─ package.json # Scripts, exports, deps, peer deps
├─ dist/ # Build output (ignored in dev)
└─ public/ # Static assets used by Storybook/demoNaming and File Conventions
- Component folders are
PascalCaseand contain the standard set:index.tsx,_.style.ts,_.types.ts(ortype.ts),_.stories.tsx,_.test.tsx. - Internal-only primitives live under
src/components/uiand should not be exported from the library root. - The alias
@resolves to./src(import likeimport { cn } from '@/lib/utils'). - CSS tokens live in
src/assets/style/*.css; the library bundlesdist/style.css(see exports inpackage.json).
Getting Started
Prereqs: Node 18+ (or 20+), PNPM recommended.
Install dependencies:
pnpm installRun the dev playground (Vite):
pnpm devOpen Storybook for component docs:
pnpm storyRun tests:
pnpm testRun tests with coverage:
pnpm coverageLint the repo:
pnpm lintBuild the library (types + CSS + bundles):
pnpm buildFull release build (test → vite build → rollup):
pnpm rollupExports and Consumers
package.json exposes:
- ESM:
dist/index.mjs - CJS:
dist/index.js - Types:
dist/index.d.ts - CSS:
dist/style.css(via subpath export)
Consumers can:
import { Button } from 'rg-dst';
import 'rg-dst/dist/style.css';Adding a New Component
- Scaffold folder:
src/components/<ComponentName>/with files:
index.tsx– component implementation_.types.ts– define prop types; exportComponentNameProps_.style.ts– class builders (e.g., withclass-variance-authority) and helpers_.stories.tsx– at least default and variants stories_.test.tsx– render, states, a11y basics
- Implement component:
- Keep public API small and typed. Prefer controlled props where appropriate.
- Compose classes using
clsx/cva. Do not inline large style strings. - Avoid leaking internal implementation details through props.
- Export from library:
- Re-export from
src/components/index.tsorsrc/index.tsaccording to the existing pattern.
- Document in Storybook:
- Include controls for all public props.
- Add usage notes and edge cases in stories or MDX docs.
- Tests:
- Use
@testing-library/reactwith JSDOM. - Cover critical behaviors, keyboard interactions, and edge states.
- Place snapshots only for minimal structural assurance (avoid brittle snapshots).
- Styles and Tokens:
- Reuse tokens from
src/assets/style/*when possible. - If adding new primitive tokens, update CSS files with clear, scoped variables.
Updating an Existing Component
Search for usages across stories, tests, and
src/components/uiprimitives before changing prop names or behavior.Maintain backward compatibility when feasible. If a breaking change is necessary:
- Adjust stories and tests.
- Update release notes and communicate migration steps in the PR description.
Keep
_.types.tsas the single source of truth for props and update stories/tests accordingly.Performance and a11y:
- Use
React.memo/useMemoonly when it removes real work; avoid premature optimization. - Ensure focus management and ARIA attributes are correct (especially for interactive components).
Testing & Coverage
Vitest is configured with JSDOM and global test APIs. Defaults:
- Includes:
src/**/*.test.{ts,tsx},src/**/*.spec.{ts,tsx} - Excludes:
node_modules,dist,**/*.stories.tsx,**/*.style.ts,**/*.types.ts,src/icons/**/*.{ts,tsx}
Coverage includes src/**/*.{ts,tsx} with further excludes:
- Stories, styles, types, icons
src/components/developers/**tests/**,dist/**,src/main.tsx,src/vite-env.d.ts
Command hints:
pnpm test # run unit tests
pnpm coverage # generate text/json/html coverageLinting & Code Style
ESLint rules are defined in eslint.config.js:
- Typescript-eslint recommended rules
- React hooks rules enforced
@typescript-eslint/no-explicit-anyis disabled where pragmaticreact-refresh/only-export-componentswarned to keep HMR-friendly exports
Guidelines:
- Prefer clear, descriptive names over abbreviations.
- Keep components focused; extract internal UI into
src/components/ui. - Minimal comments; only for non-obvious rationale or caveats.
- Early returns over deep nesting; avoid broad try/catch.
Styles & Theming
- Tailwind via Vite plugin; utility composition preferred.
- Shared tokens and utilities live in
src/assets/style/*.css. - Library CSS is built with
pnpm build:csstodist/style.css. Consumers must import it.
Icons
- Add icons to
src/icons/generalas React components. - Keep icon types in
src/icons/icons.type.ts. - Icons are excluded from test coverage; still add basic validation where necessary.
Aliases
@ → ./src (configured in both vite.config.ts and vitest.config.ts). Example:
import { cn } from '@/lib/utils'Scripts (from package.json)
pnpm dev– Start Vite dev server (with--hostfor LAN testing)pnpm build:css– Buildsrc/index.csstodist/style.cssvia PostCSSpnpm build– TypeScript build + CSS build + Vite buildpnpm rollup– Run tests, Vite build, and Rollup bundle (esm/cjs/dts)pnpm story– Run Storybook on port 6006pnpm build-storybook– Generate static Storybookpnpm test– Run Vitestpnpm coverage– Vitest with coveragepnpm lint– Run ESLint
Publishing Notes
- Ensure
peerDependenciesforreactandreact-domare correct. - Verify
exportsmap andtypesinpackage.json. - Run
pnpm rollupand verifydist/contains.mjs,.js,.d.ts, andstyle.css.
Troubleshooting
Dev server fails to start
- Delete
node_modulesand lockfile, thenpnpm install. - Ensure Node version ≥ 18.
- Delete
Storybook cannot resolve
@alias- Restart Storybook after changes to
vite.config.ts. - Confirm alias is set in both
vite.config.tsand Storybook config (React-Vite preset handles this).
- Restart Storybook after changes to
Tests fail with DOM-related errors
- Ensure
jsdomenvironment is set (it is in config). Checktests/setup.tspath.
- Ensure
Coverage missing for new files
- Confirm files are within
src/and not in an excluded pattern.
- Confirm files are within
Styles not applied in consumer app
- Remind consumers to import
'rg-dst/dist/style.css'.
- Remind consumers to import
Contribution Workflow
- Create a feature branch:
git checkout -b feat/<short-name> - Implement changes with stories and tests.
- Run
pnpm lint && pnpm test && pnpm buildlocally. - Open a PR with:
- Summary of changes
- Screenshots (stories) if visual
- Breaking changes and migration notes
- Request review; address feedback; squash and merge.
Questions or issues? Open an issue with context, repro steps, and environment details.
