Embed diagrams into your website
This guide adds LikeC4 views to a plain HTML page with web components. It works with any site, with or without a framework. To compare it with the other ways, see Choose how to embed diagrams.
Before you start
Section titled “Before you start”You need Node.js and a folder with your .c4 files.
The examples use this layout:
my-site/├── model/ # your .c4 files├── public/ # the files you host│ └── index.html└── package.jsonEmbed a view
Section titled “Embed a view”-
Install LikeC4
Section titled “Install LikeC4”Add
likec4to your dev dependencies:npm i -D likec4pnpm add -D likec4yarn add -D likec4bun add -d likec4The next step also needs
reactandreact-dom. The command offers to install them if they are missing. -
Generate the bundle
Section titled “Generate the bundle”Run the command from the project root:
npx likec4 gen webcomponent ./model -o ./public/likec4-views.jspnpm likec4 gen webcomponent ./model -o ./public/likec4-views.jsyarn likec4 gen webcomponent ./model -o ./public/likec4-views.jsbunx likec4 gen webcomponent ./model -o ./public/likec4-views.jsIt writes one JavaScript file with all your views. React and the styles are bundled inside, so the page needs nothing else.
Option Description [path]Directory with your .c4files (default is the current directory)-o,--outfileOutput file, .jsor.mjs(default islikec4-views.jsin the workspace)-w,--webcomponent-prefixPrefix of the element name (default likec4, gives<likec4-view>)-p,--projectProject to generate, in a multi-project workspace wcandwebcompare aliases ofwebcomponent. Runlikec4 gen webcomponent --helpfor all options. -
Add the view to a page
Section titled “Add the view to a page”Load the script and add a
<likec4-view>element:public/index.html <!doctype html><html><head><title>Architecture</title><script src="./likec4-views.js"></script></head><body><h1>System context</h1><likec4-view view-id="index"></likec4-view></body></html>Add as many elements as you need, one per view. Set the view with these attributes:
Attribute Description view-idId of the view to show (default index)browserOpen the views browser on click. Set "false"to turn it off (defaulttrue)dynamic-variantHow to show a dynamic view: diagramorsequence(defaultdiagram)color-schemeForce lightordark(default follows the system)For example, a dynamic view as a sequence diagram, without the views browser:
<likec4-view view-id="checkout" dynamic-variant="sequence" browser="false"></likec4-view>If you set
-w c4, the element is<c4-view>. -
Open the page
Section titled “Open the page”Serve the
publicfolder with any static server, for example:Terminal window npx serve publicThen open the page in a browser.
Rebuild on changes
Section titled “Rebuild on changes”The bundle does not update by itself. Run the command again each time the model changes.
Add a script to package.json and call it before your site build:
{ "scripts": { "likec4:gen": "likec4 gen webcomponent ./model -o ./public/likec4-views.js", "build": "npm run likec4:gen && <your site build>" }}To build the bundle in CI, run the same command before you deploy. For example, in GitHub Actions:
- run: npm ci- run: npm run likec4:genThen you can add public/likec4-views.js to .gitignore.
Other setups
Section titled “Other setups”Vite or React app
Section titled “Vite or React app”In a React app built with Vite, use the Vite plugin.
It reads your .c4 files and updates the diagrams as you edit them, so there is no generate step.
Import LikeC4View from likec4:react, or define a web component with likec4:webcomponents/<project>.
For React without Vite, generate components with likec4 gen react, see React.
Documentation sites
Section titled “Documentation sites”- MkDocs: use the community MkDocs plugin.
It renders a view from a
likec4-viewcode block in Markdown. - Docusaurus, VitePress and other site generators: use web components as above.
Generate the bundle into the folder for static files (
static/in Docusaurus,public/in VitePress). Load the script on your pages and use<likec4-view>in Markdown or MDX. In VitePress, mark the tags that start withlikec4-as custom elements in the Vue compiler options.
No site yet
Section titled “No site yet”To publish all your views as a website, build the static website.
Its Share dialog gives an <iframe> or web component snippet for each view.