Skip to Content

Framework Integration

Embed in Solid

Show a Databrain dashboard or metric in a Solid app.

Before you start

Steps

  1. Install the plugin.
npm install @databrainhq/plugin
  1. Import it once in your entry file, for example src/index.tsx, before render.
import '@databrainhq/plugin/web';
  1. 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;
      };
    }
  }
}
  1. 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} />

Find the metric id

How the props work

  • Solid sets props on a custom element as JS properties. dashboard-id becomes dashboardId.
  • 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 treats on props as event listeners.
  • The keyed <Show> remounts the element when the dashboard changes.

If something goes wrong

  • TypeScript says dbn-dashboard does not exist on JSX.IntrinsicElements. Add the declaration from step 3 and include it in tsconfig.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.
  • handleServerEvent never runs. Assign an inline arrow function. The element drops a named function.