Sub topic 11
Shared UI Components and Design Systems
This chapter assumes you know React components, props, and TypeScript. It also assumes you have the feature-based layout from earlier chapters, including the @/ path alias.
What belongs in shared UI
The shared/ui folder holds components that know nothing about your product. A Button, TextInput, or Modal fits. A component that mentions orders, carts, or users does not.
Three rules keep the folder clean:
- Shared UI never imports from
featuresorapp. - Shared UI makes no API calls and uses no domain types.
- Everything it needs arrives through props.
These rules make the dependency direction one-way, so changing a feature can never break a shared component.
A small shared component
A good shared component wraps a native element, adds a few named options, and passes everything else through.
// src/shared/ui/Button.tsx
import type { ButtonHTMLAttributes } from "react";
type ButtonProps = ButtonHTMLAttributes<HTMLButtonElement> & {
variant?: "primary" | "secondary" | "danger";
};
export function Button({
variant = "primary",
type = "button",
className = "",
...rest
}: ButtonProps) {
return (
<button
type={type}
className={`btn btn-${variant} ${className}`.trim()}
{...rest}
/>
);
}The default type="button" stops the button from submitting a surrounding form by accident. Spreading ...rest means onClick, disabled, and aria-* attributes work without extra code.
A small index file gives the folder one import path:
// src/shared/ui/index.ts
export { Button } from "./Button";A feature then imports it with import { Button } from "@/shared/ui";.
Design tokens
A design system is a set of shared components plus the rules behind them. The rules are usually stored as design tokens. These are named values for colors, spacing, and type sizes. CSS variables are a simple way to store them:
:root {
--color-primary: #1d4ed8;
--color-danger: #b91c1c;
--space-2: 0.5rem;
}
.btn-primary {
background: var(--color-primary);
padding: var(--space-2);
}When a brand color changes, you edit one line instead of searching every component.
Deciding what to share
Use this table when you are unsure where a component goes.
| Component | Location | Reason |
|---|---|---|
| `Button` | `shared/ui` | No domain knowledge |
| `CartList` | `features/cart` | Knows about cart items |
| `OrderStatusBadge` | `features/orders` | Encodes order statuses |
| `formatDate` | `shared/lib` | Pure helper, not UI |
Common errors
- Sharing too early. Wait until a second or third feature needs the component. A wrong abstraction costs more than a small duplicate.
- Domain leakage. If a shared component accepts an
Orderprop, it belongs in the orders feature. Pass plain values such aslabelandtoneinstead. - Boolean flag growth. A component with
isLarge,isRound, andisGhostis hard to reason about. Use a union such asvariantorsize, or acceptchildrenfor composition. - Stale copies. Two near-identical buttons in different features drift apart. Move the common one into
shared/uiand keep only real differences local.
If several apps need the same components, move shared/ui into a workspace package. A later chapter covers that step.
Exercise
A teammate adds OrderStatusBadge to shared/ui because it renders a colored label. It imports the Order type from @/features/orders. What is wrong, and how would you fix it?
Show answer
Shared UI must not import from features, so this breaks the one-way dependency rule. The component also encodes order-specific statuses. Move it to features/orders/components. If you want a reusable colored label, create a generic Badge in shared/ui that takes label and tone props, and have OrderStatusBadge map order statuses to those props.