Web components
A web component renders a LikeC4 view as a custom HTML element, like <likec4-view view-id="index">.
It works in any HTML page or framework, no React code needed.
You get one either from the CLI, which generates a standalone script, or from the Vite plugin, if your site is built with Vite.
With the CLI
Section titled “With the CLI”Install
Section titled “Install”Add likec4 to your dev dependencies:
npm i -D likec4pnpm add -D likec4yarn add -D likec4bun add -d likec4The command needs react and react-dom to build the script, and offers to install them if they are missing.
The generated script bundles everything, so your page needs nothing else at runtime.
Generate the script
Section titled “Generate the script”Generate a JavaScript bundle with the web component:
npx likec4 gen webcomponent -o ./src/likec4-webcomponent.jspnpx likec4 gen webcomponent -o ./src/likec4-webcomponent.jsyarn dlx likec4 gen webcomponent -o ./src/likec4-webcomponent.jsbunx likec4 gen webcomponent -o ./src/likec4-webcomponent.jsUse it:
<script src="./src/likec4-webcomponent.js"></script><likec4-view view-id="index"></likec4-view>By default, the CLI generates a likec4-view web component.
You can change the likec4 prefix with -w, --webcomponent-prefix.
For example:
npx likec4 gen webcomponent -w custom-c4 -o ./src/likec4-webcomponent.jspnpx likec4 gen webcomponent -w custom-c4 -o ./src/likec4-webcomponent.jsyarn dlx likec4 gen webcomponent -w custom-c4 -o ./src/likec4-webcomponent.jsbunx likec4 gen webcomponent -w custom-c4 -o ./src/likec4-webcomponent.jsAnd in HTML:
<custom-c4-view view-id="index" browser="true" dynamic-variant="sequence"></custom-c4-view>With --customization <file>, the UI customization is bundled into the generated web components (experimental).
The command also reads the app config (webcomponentPrefix, customization); use --app-config <file> to point to it, or --no-app-config to ignore it.
With the Vite plugin
Section titled “With the Vite plugin”If your site is built with Vite, the LikeC4 Vite plugin provides web components
through the likec4:webcomponents/<project> virtual module, with no generation step and with HMR.
Import defineWebcomponent from it and register the custom element under a name of your choice:
import { defineWebcomponent } from 'likec4:webcomponents/project-a'
defineWebcomponent('likec4-view-project-a')Then use it in HTML:
<likec4-view-project-a view-id="index" browser="true" dynamic-variant="sequence" color-scheme="dark"></likec4-view-project-a>- The module is always project-scoped:
project-amust match a project from your LikeC4 workspace. A workspace without a project config has a single project with the iddefault. - The element name is the one you pass, so the plugin
webcomponentPrefixoption does not apply here. It must follow the custom element naming rules, including at least one hyphen. - The element renders the
LikeC4Viewfromlikec4:react/<project>, so the plugincustomizationoption applies to it. - With TypeScript, add the plugin type references.
Attributes
Section titled “Attributes”Both kinds of web components support the same attributes:
| Attribute | Description |
|---|---|
view-id |
Your view id (default index) |
browser |
Whether to show views browser popup (default true) |
dynamic-variant |
How dynamic view should be rendered Possible values: diagram or sequence (default diagram) |
color-scheme |
Force light or dark color scheme Possible values: light or dark (default: follows system) |