Skip to content

Commit 89a0579

Browse files
authored
add docs for @ctablex/core (#5)
1 parent 2ea6cb3 commit 89a0579

28 files changed

Lines changed: 3382 additions & 1 deletion

packages/ctablex-core/README.md

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
# @ctablex/core
2+
3+
Core building blocks for composable, context-based React components using the **micro-context pattern**.
4+
5+
## What is Micro-Context?
6+
7+
**Micro-context** is a pattern for passing data through localized React Context instead of props. Unlike traditional context patterns that span entire applications, micro-context creates small, scoped providers within component subtrees for fine-grained data flow.
8+
9+
This enables:
10+
11+
- **Reusable components** that work anywhere without knowing the data source
12+
- **Flexible composition** of data transformers and renderers
13+
- **No prop drilling** through intermediate components
14+
- **Immutable children** for better performance optimization
15+
16+
Read more: [Micro-Context Pattern](./docs/MICRO-CONTEXT.md)
17+
18+
## Installation
19+
20+
```bash
21+
npm install @ctablex/core
22+
```
23+
24+
## Quick Start
25+
26+
```tsx
27+
import { ContentProvider, FieldContent, DefaultContent } from '@ctablex/core';
28+
29+
type User = {
30+
name: string;
31+
email: string;
32+
};
33+
34+
const user: User = {
35+
name: 'Alice',
36+
email: 'alice@example.com',
37+
};
38+
39+
// Provide data via context
40+
<ContentProvider value={user}>
41+
{/* Access fields without props */}
42+
<FieldContent field="name">
43+
<DefaultContent />
44+
</FieldContent>
45+
</ContentProvider>;
46+
// Renders: Alice
47+
```
48+
49+
## Core Concepts
50+
51+
### ContentProvider & useContent
52+
53+
The foundation of micro-context:
54+
55+
```tsx
56+
import { ContentProvider, useContent } from '@ctablex/core';
57+
58+
// Provide data
59+
<ContentProvider value={someData}>
60+
<MyComponent />
61+
</ContentProvider>;
62+
63+
// Consume data
64+
function MyComponent() {
65+
const data = useContent();
66+
return <div>{data}</div>;
67+
}
68+
```
69+
70+
Read more: [ContentContext - ContentProvider & useContent](./docs/ContentContext.md)
71+
72+
### Content Components
73+
74+
Transform and access data through context:
75+
76+
```tsx
77+
import {
78+
ContentValue,
79+
FieldContent,
80+
ArrayContent,
81+
ObjectContent,
82+
NullableContent,
83+
DefaultContent,
84+
KeyContent,
85+
} from '@ctablex/core';
86+
87+
// Access nested paths
88+
<ContentValue accessor="user.address.city">
89+
<DefaultContent />
90+
</ContentValue>
91+
92+
// Access object fields
93+
<FieldContent field="price">
94+
<DefaultContent />
95+
</FieldContent>
96+
97+
// Iterate arrays
98+
<ContentProvider value={users}>
99+
<ArrayContent getKey="id">
100+
<FieldContent field="name">
101+
<DefaultContent />
102+
</FieldContent>
103+
</ArrayContent>
104+
</ContentProvider>
105+
106+
// Iterate objects
107+
<ContentProvider value={product}>
108+
<ObjectContent join=", ">
109+
<KeyContent />: <DefaultContent />
110+
</ObjectContent>
111+
</ContentProvider>
112+
113+
// Handle null/undefined
114+
<FieldContent field="email">
115+
<NullableContent nullContent="No email">
116+
<DefaultContent />
117+
</NullableContent>
118+
</FieldContent>
119+
```
120+
121+
Read more: [Contents](./docs/Contents.md), [ArrayContent](./docs/ArrayContent.md), [ObjectContent](./docs/ObjectContent.md)
122+
123+
### Type-Safe Accessors
124+
125+
Extract values with strong TypeScript support:
126+
127+
```tsx
128+
import { access } from '@ctablex/core';
129+
130+
type User = {
131+
profile: {
132+
name: string;
133+
};
134+
};
135+
136+
const user: User = { profile: { name: 'Bob' } };
137+
138+
// Path accessor with autocomplete
139+
const name = access(user, 'profile.name'); // ✓ Type-safe + autocomplete
140+
141+
// Function accessor
142+
const value = access(user, (u) => u.profile.name); // ✓ Type inference + auto type safety
143+
```
144+
145+
Read more: [Accessors](./docs/Accessors.md)
146+
147+
## Key Features
148+
149+
### Reusable Renderers
150+
151+
Components work anywhere without knowing the data source:
152+
153+
```tsx
154+
function PriceDisplay() {
155+
const price = useContent<number>();
156+
return <span>${price.toFixed(2)}</span>;
157+
}
158+
159+
// Works in any context that provides a number
160+
<ContentProvider value={99.99}>
161+
<PriceDisplay />
162+
</ContentProvider>;
163+
```
164+
165+
### Default Children and Open for Customization
166+
167+
Components provide sensible defaults while remaining customizable:
168+
169+
```tsx
170+
// Simple - uses DefaultContent
171+
<FieldContent field="price" />
172+
173+
// Custom rendering
174+
<FieldContent field="price">
175+
<PriceDisplay />
176+
</FieldContent>
177+
```
178+
179+
### Performance Optimization
180+
181+
Immutable children enable powerful memoization:
182+
183+
```tsx
184+
const content = (
185+
<ArrayContent>
186+
<FieldContent field="name">
187+
<DefaultContent />
188+
</FieldContent>
189+
</ArrayContent>
190+
);
191+
192+
function ProductList() {
193+
return content; // Same reference every render
194+
}
195+
```
196+
197+
## Type Safety Limitations
198+
199+
⚠️ Micro-context provides weak type safety. Generic types must be manually specified and cannot be validated across context boundaries. See [MICRO-CONTEXT.md - Weak Type Safety](./docs/MICRO-CONTEXT.md#weak-type-safety) for details.
200+
201+
## Documentation
202+
203+
For detailed documentation, common patterns, and pitfalls, see:
204+
205+
- **[Documentation Guide](./docs/README.md)** - Start here for a complete overview
206+
- **[Micro-Context Pattern](./docs/MICRO-CONTEXT.md)** - Understand the core concept
207+
- **[ContentContext](./docs/ContentContext.md)** - ContentProvider, useContent, ContentContext
208+
- **[Contents](./docs/Contents.md)** - ContentValue, FieldContent, NullableContent, DefaultContent
209+
- **[ArrayContent](./docs/ArrayContent.md)** - Array iteration components
210+
- **[ObjectContent](./docs/ObjectContent.md)** - Object iteration components
211+
- **[Accessors](./docs/Accessors.md)** - Type-safe value extraction
212+
213+
## License
214+
215+
MIT
216+
217+
## Related Packages
218+
219+
- **[@ctablex/table](https://www.npmjs.com/package/@ctablex/table)** - Composable table components built on @ctablex/core

0 commit comments

Comments
 (0)