Framework Integration
Embed in Solid
Show a Databrain dashboard or metric in a Solid app.
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
Steps
- Install the plugin.
npm install @databrainhq/plugin- Import it once in your entry file, for example
src/index.tsx, beforerender.
import '@databrainhq/plugin/web';- Add JSX types for the elements, for example in
src/databrain.d.ts. The plugin ships no JSX types for Solid.
import 'solid-js';
declare module 'solid-js' {
namespace JSX {
interface IntrinsicElements {
'dbn-dashboard': JSX.HTMLAttributes<HTMLElement> & {
[attr: string]: unknown;
};
'dbn-metric': JSX.HTMLAttributes<HTMLElement> & {
[attr: string]: unknown;
};
}
}
}- Add a component that gets the token and renders
<dbn-dashboard>.
import { createResource, Show } from 'solid-js';
type DashboardElement = HTMLElement & {
handleServerEvent?: (...args: unknown[]) => unknown;
};
async function fetchToken(): Promise<string> {
const res = await fetch('/api/databrain-token');
return (await res.json()).token;
}
export function Dashboard(props: { dashboardId: string }) {
const [token] = createResource(fetchToken);
const setup = (el: DashboardElement) => {
el.handleServerEvent = (event) => console.log(event);
};
return (
<Show when={token()} keyed>
{(guestToken) => (
<Show when={props.dashboardId} keyed>
{(id) => (
<dbn-dashboard
ref={setup}
token={guestToken}
dashboard-id={id}
enable-download-csv="true"
options={{ disableMetricCreation: true, hideDashboardName: true }}
/>
)}
</Show>
)}
</Show>
);
}You see the dashboard in your app.
Show one metric
<dbn-metric token={guestToken} metric-id={metricId} />How the props work
- Solid sets props on a custom element as JS properties.
dashboard-idbecomesdashboardId. - Objects pass as they are. Write booleans as
"true"or{true}. - Functions go on the element through
ref. Assign an inline arrow function. Do not pass functions in JSX: Solid treatsonprops as event listeners. - The keyed
<Show>remounts the element when the dashboard changes.
If something goes wrong
- TypeScript says
dbn-dashboarddoes not exist onJSX.IntrinsicElements. Add the declaration from step 3 and include it intsconfig.json. - The embed shows the first dashboard of the data app, not yours. Write the attribute as
dashboard-id, in kebab case. Use the id, not the dashboard name. handleServerEventnever runs. Assign an inline arrow function. The element drops a named function.