Skip to content

Generate React

NPM VersionNPM Downloads

Generate React components with views from your architecture model.

Ensure you have likec4 in your dependencies:

npm i likec4

The following command generates a JavaScript bundle with React Component (and .d.ts):

npx likec4 codegen react --outfile ./src/likec4.generated.js

# Aliases
npx likec4 generate react -o ./src/likec4.generated.js
npx likec4 gen react -o ./src/likec4.generated.js

To use the component:

import { LikeC4View } from './likec4.generated'
const App = () => {
return (
<div>
<LikeC4View viewId="index" />
</div>
)
}
PropertyDescription
viewIdTyped enumeration of your views
whereOptional, see filter
injectFontCssInjects CSS with IBM Plex Sans font from CDN.
Default is true

where is same view predicate, but applies dynamically and enables to show/hide elements based on the context. For example:

import { LikeC4View } from './likec4.generated'
// Keeps elements and relationships where:
// - tag is not 'legacy'
// - and
// - tag is 'v1' or 'v2'
const App = () => {
return (
<div>
<LikeC4View
viewId="index"
where={{
and: [
{ tag: { neq: 'legacy' } },
{
or: [
{ tag: { eq: 'v1' } },
{ tag: { eq: 'v2' } }
]
}
]
}}/>
</div>
)
}

Layout stays the same, i.e. elements are not rearranged.
Be aware, where applies both to elements and relationships.

LikeC4View renders views from your model, and allows exploring in the popup browser.
Component works in most usecases, but if you need more - use ReactLikeC4:

import { ReactLikeC4, type LikeC4ViewId } from './likec4.generated'
const App = () => {
const [viewId, setViewId] = useState<LikeC4ViewId>('index')
return (
<ReactLikeC4
viewId={viewId}
pannable
zoomable={false}
keepAspectRatio
showNavigationButtons
enableDynamicViewWalkthrough={false}
enableElementDetails
enableRelationshipDetails
showDiagramTitle={false}
onNavigateTo={setViewId}
onNodeClick={...}
/>
)
}

ReactLikeC4 is a low-level component, giving you more control and allowing react to the events. Check source code for available options.

Feel free to share your ideas or ask questions in GitHub discussions.