|
| 1 | +--- |
| 2 | +title: "Component Override Example for JSON-DOC TypeScript" |
| 3 | +author: "Abreham <abreham@textcortex.com> and Claude Code" |
| 4 | +date: "2025-01-05" |
| 5 | +--- |
| 6 | + |
| 7 | +# Component Override Example |
| 8 | + |
| 9 | +This demonstrates how users can now override specific block components in the JSON-DOC renderer. |
| 10 | + |
| 11 | +## Usage Example |
| 12 | + |
| 13 | +```typescript |
| 14 | +import { |
| 15 | + JsonDocRenderer, |
| 16 | + ParagraphBlockRenderer, |
| 17 | + HeadingBlockRenderer |
| 18 | +} from 'jsondoc/renderer'; |
| 19 | + |
| 20 | +// Example: Override paragraph with custom className |
| 21 | +const CustomParagraph = (props) => ( |
| 22 | + <ParagraphBlockRenderer |
| 23 | + {...props} |
| 24 | + className="my-custom-paragraph-style" |
| 25 | + /> |
| 26 | +); |
| 27 | + |
| 28 | +// Example: Override heading with wrapper div and custom styling |
| 29 | +const CustomHeading = (props) => ( |
| 30 | + <div className="my-heading-wrapper"> |
| 31 | + <HeadingBlockRenderer |
| 32 | + {...props} |
| 33 | + className="my-custom-heading" |
| 34 | + style={{ color: 'blue' }} |
| 35 | + /> |
| 36 | + </div> |
| 37 | +); |
| 38 | + |
| 39 | +// Usage |
| 40 | +<JsonDocRenderer |
| 41 | + page={jsonDocPage} |
| 42 | + components={{ |
| 43 | + paragraph: CustomParagraph, |
| 44 | + heading_1: CustomHeading, |
| 45 | + heading_2: CustomHeading, |
| 46 | + heading_3: CustomHeading |
| 47 | + }} |
| 48 | +/> |
| 49 | +``` |
| 50 | + |
| 51 | +## What This Enables |
| 52 | + |
| 53 | +1. **Custom Styling**: Users can add their own CSS classes and styles |
| 54 | +2. **Wrapper Components**: Add additional markup around blocks |
| 55 | +3. **Custom Logic**: Add click handlers, analytics, etc. |
| 56 | +4. **Design System Integration**: Easy integration with Tailwind, styled-components, etc. |
| 57 | +5. **Progressive Enhancement**: Override only what you need, keep defaults for the rest |
| 58 | + |
| 59 | +## Example with Tailwind CSS |
| 60 | + |
| 61 | +```typescript |
| 62 | +const TailwindParagraph = (props) => ( |
| 63 | + <ParagraphBlockRenderer |
| 64 | + {...props} |
| 65 | + className="prose prose-lg text-gray-700 leading-relaxed" |
| 66 | + /> |
| 67 | +); |
| 68 | + |
| 69 | +const TailwindHeading = (props) => ( |
| 70 | + <HeadingBlockRenderer |
| 71 | + {...props} |
| 72 | + className="font-bold text-gray-900 border-b border-gray-200 pb-2" |
| 73 | + /> |
| 74 | +); |
| 75 | +``` |
| 76 | + |
| 77 | +## Example with Styled Components |
| 78 | + |
| 79 | +```typescript |
| 80 | +const StyledParagraph = styled(ParagraphBlockRenderer)` |
| 81 | + font-family: "Inter", sans-serif; |
| 82 | + line-height: 1.6; |
| 83 | + color: ${(props) => props.theme.colors.text}; |
| 84 | +`; |
| 85 | + |
| 86 | +const StyledHeading = styled(HeadingBlockRenderer)` |
| 87 | + font-family: "Playfair Display", serif; |
| 88 | + color: ${(props) => props.theme.colors.primary}; |
| 89 | + border-bottom: 2px solid ${(props) => props.theme.colors.accent}; |
| 90 | +`; |
| 91 | +``` |
| 92 | + |
| 93 | +## Benefits of This Approach |
| 94 | + |
| 95 | +- **Verbose but Explicit**: Clear what each override does |
| 96 | +- **Type Safe**: Full TypeScript support for component props |
| 97 | +- **React Standard**: Follows familiar React patterns |
| 98 | +- **Composable**: Can wrap, extend, or completely replace components |
| 99 | +- **Flexible**: Access to all HTML attributes via props spreading |
0 commit comments