Framework Integration
Embed with any React version or no React
Use the standalone build of the plugin on React 16 or 17, on a page with no React, or from a script tag.
@databrainhq/plugin/standalone carries its own React. Your app can run React 16, React 17, another framework, or no framework.
It is the same package and the same version as the default build. It registers the same <dbn-dashboard> and <dbn-metric> elements, with the same attributes, the v1 and v2 charts, and the same styles. Each element renders with its own React inside its own shadow root. Your app only sees two custom elements.
When to use it
| Your app | Import |
|---|---|
| React 18 or 19 | The default build, @databrainhq/. See Embed in React. |
| React 16 or 17 | @databrainhq/ |
| A page with no React: plain HTML, server-rendered pages | @databrainhq/ |
| A script tag from a CDN, with no bundler | @databrainhq/ |
| Vue, Angular or Svelte | Either. The default build needs react and react-dom in your dependencies. The standalone build does not. |
| Not sure | @databrainhq/. It does not depend on the React version of your app. |
The cost is size, because React comes with the plugin:
| Build | What the page loads (gzip) |
|---|---|
Default ES build, @databrainhq/ | About 2.56 MB first, the rest when a chart or editor is first used |
Standalone ES build, standalone.es.js | About 2.73 MB first, the rest when first used |
Standalone script tag, standalone.umd.js | About 4.79 MB, one file, before the first chart |
Standalone for webpack 4, standalone.legacy.es.js | About 4.87 MB, one file |
If your app runs React 18 or 19, use the default build.
Before you start
- Your backend returns a guest token at
/api/databrain-token. Create a guest token - You have a dashboard id. Find the dashboard id
- Your page’s domain is in the data app’s whitelisted domains.
Install with a bundler
Use this for webpack 4 or 5, Vite, esbuild and the Angular CLI.
-
Install the plugin.
npm install @databrainhq/pluginThis works with no extra flags on React 16, 17, 18 and 19, and on apps with no React. The package does not add React to your app.
-
Import the standalone build once in your entry file, in browser code only.
import '@databrainhq/plugin/standalone'; -
Add
<dbn-dashboard>or<dbn-metric>to your page. The framework guides show how: Vue, Angular, Svelte, JavaScript. Use the/standaloneimport in place of/web. For React 16 or 17, see React 16 and 17.
You see the dashboard in your app.
webpack 5, Vite, esbuild and the Angular CLI get the split ES build. webpack 4 does not read the exports field of package.json, so it gets standalone.legacy.es.js, the same build as one file. You do not change the import.
Load it from a CDN
A script tag needs no bundler and no React on the page.
<script type="module" src="https://unpkg.com/@databrainhq/plugin@<version>/dist/standalone.es.js"></script>
<dbn-dashboard
token="GUEST_TOKEN_FROM_YOUR_SERVER"
dashboard-id="your-dashboard-id"
style="display:block;height:800px"
></dbn-dashboard>Replace <version> with an exact plugin version. type="module" is the lighter path: the browser loads the entry first, and the other files from dist/standalone-chunks/ when they are needed.
If your page cannot load modules, use a classic script tag. It loads the whole plugin as one file before the first chart.
<script src="https://unpkg.com/@databrainhq/plugin@<version>/dist/standalone.umd.js"></script>Serve the whole dist/ folder, as unpkg and jsDelivr do. The module build loads its chunks from it. The classic script loads its font files from it.
To set the token from JavaScript, see Embed with JavaScript.
React 16 and 17
React 16 and 17 give a custom element strings only. They do not set JS properties, and they do not attach onX props as event listeners. So:
- Write attributes in kebab case:
dashboard-id,metric-id,ui-version,enable-download-csv. - Give objects and arrays as JSON strings:
options={JSON.stringify({ disableMetricCreation: true })}. An object given directly arrives as"[object Object]"and is not read. - Give booleans as
"true"or"false". - Give a function, such as
handle-server-eventorchart-click-function, as the name of a function onwindow. - Listen for element events, such as
dbn-error, withaddEventListeneron a ref. AnonDbnErrorprop does nothing.
import React, { useEffect, useRef, useState } from 'react';
import '@databrainhq/plugin/standalone';
window.onDbnServerEvent = (event) => console.log(event);
export function Dashboard({ dashboardId }) {
const [token, setToken] = useState();
const ref = useRef(null);
useEffect(() => {
fetch('/api/databrain-token')
.then((res) => res.json())
.then((data) => setToken(data.token));
}, []);
useEffect(() => {
const el = ref.current;
if (!el) return undefined;
const onError = (event) => console.log(event.detail);
el.addEventListener('dbn-error', onError);
return () => el.removeEventListener('dbn-error', onError);
}, [token, dashboardId]);
if (!token) return null;
return (
<dbn-dashboard
key={dashboardId}
ref={ref}
token={token}
dashboard-id={dashboardId}
enable-download-csv="true"
options={JSON.stringify({ disableMetricCreation: true, hideDashboardName: true })}
handle-server-event="onDbnServerEvent"
/>
);
}key={dashboardId} remounts the element when the dashboard changes.
Size a metric
React 16 cannot pass the style prop of <dbn-metric>. It refuses a string style (React error #62). Put the element in a wrapper and size the wrapper, or use the width and height attributes.
<div style={{ width: 480, height: 320 }}>
<dbn-metric token={token} metric-id={metricId} />
</div>Custom charts and CSP
Custom charts draw only with the v2 chart engine (ui-version="v2"). If your page sends a Content-Security-Policy and you show custom charts, allow frame-src blob: and script-src 'unsafe-inline'. Without them, the custom chart card says “Chart could not load”. Other charts are not affected. The rules are the same for the default and the standalone build. See CSP for your page.
If something goes wrong
npm installstops withERESOLVEon React 16 or 17. Your plugin version is older than the standalone build. Install the latest version. If you must keep the old version, add--legacy-peer-deps.- On React 16 or 17, the bundler cannot resolve
react-dom/client, or the console showscreateRoot is not a function. The page loads the default build. Import@databrainhq/plugin/standalonein place of@databrainhq/plugin/web. - An option or theme has no effect on React 16 or 17. Give it as a JSON string, with a kebab-case name.
- Both builds load on one page. The first one to run registers the elements. The second one does nothing. Load only one.