Embed in a Next.js app

Put a query result into a page of an existing Next.js app, themed only in its own region, talking to your data server from the browser. The component is ordinary React with "use client" at the top; the page loads it in the browser only.

The panel:

SurfacesPanel.tsx:

tsx
"use client";

import { connectDataSource } from "@surfaces-ui/client-connect";
import { DataSourceProvider, QueryResult, ThemeProvider } from "@surfaces-ui/components";
import type { DataSource } from "@surfaces-ui/data";
import { useMemo } from "react";

export interface SurfacesPanelProps {
  /** Your data server, e.g. "https://data.example.com". Read in the browser, so it must not hold a secret. */
  baseUrl?: string;
  sourceId: string;
  sql: string;
  /** Any other DataSource, such as your own adapter or one for tests. Used instead of `baseUrl`. */
  dataSource?: DataSource;
}

/**
 * A query result in its own themed region. Everything here runs in the
 * browser: the theme reads the system's light or dark setting and the grid
 * measures itself, so the page loads it with `ssr: false`.
 */
export function SurfacesPanel({ baseUrl = "", sourceId, sql, dataSource }: SurfacesPanelProps) {
  const data = useMemo(() => dataSource ?? connectDataSource({ baseUrl }), [dataSource, baseUrl]);
  return (
    // "scoped" themes only this region, so the rest of your app keeps its own styles.
    <ThemeProvider scope="scoped" storage={null}>
      <DataSourceProvider dataSource={data}>
        <QueryResult sourceId={sourceId} sql={sql} height={420} exportName="orders" />
      </DataSourceProvider>
    </ThemeProvider>
  );
}

Notes on it:

  • "use client" is needed. The library's components use state, effects and the browser, and its packages do not mark themselves as client code, so import them only from files that start with it.
  • scope="scoped" puts the theme's --sf-* variables on the panel's own element instead of on :root, so the rest of your app keeps its look. storage={null} keeps the theme in memory; leave it out to remember a person's choice in localStorage.
  • connectDataSource talks to a server that implements the Surfaces services, such as surfaces-dev. To use your own API instead, pass any DataSource as dataSource (see "Bring your own backend").
  • baseUrl is read in the browser. Never put a credential in it; the data server resolves credentials by name on its side.
  • To draw what an agent sends instead of a fixed result, render SurfaceView from @surfaces-ui/spec/react with lazyAnalyticsRenderers, which keeps the charts and editors out of the first load until something is drawn.

The rest lives in your Next.js app. These files have not been run here; they are written for the App Router.

Load the stylesheet once, in the root layout:

tsx
// app/layout.tsx
import "@surfaces-ui/components/styles.css";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  );
}

Load the panel in the browser only. In the App Router, next/dynamic with ssr: false must be called from a client component, so give it a small file of its own:

tsx
// app/orders/PanelLoader.tsx
"use client";

import dynamic from "next/dynamic";

export const OrdersPanel = dynamic(() => import("../../components/SurfacesPanel").then((m) => m.SurfacesPanel), {
  ssr: false,
  loading: () => <p>Loading orders…</p>,
});
tsx
// app/orders/page.tsx
import { OrdersPanel } from "./PanelLoader";

export default function OrdersPage() {
  return (
    <main>
      <h1>Orders</h1>
      <OrdersPanel baseUrl={process.env.NEXT_PUBLIC_SURFACES_URL ?? ""} sourceId="retail" sql="SELECT * FROM orders" />
    </main>
  );
}

The packages are published as compiled ES modules, so transpilePackages should not be needed. If your build reports untranspiled syntax from them, or you point Next.js at the packages' source in a workspace, list them:

ts
// next.config.ts
import type { NextConfig } from "next";

const config: NextConfig = {
  transpilePackages: ["@surfaces-ui/components", "@surfaces-ui/client-connect", "@surfaces-ui/data", "@surfaces-ui/tokens"],
};

export default config;

If the data server is on another origin, it must allow your app's origin for CORS, or put it behind a route in your app so the browser calls the same origin.

The test renders the panel in jsdom with memoryDataSource passed as dataSource, and checks that it draws the rows and that the theme is scoped to the panel. It does not run inside Next.js, and Next.js itself has not been run for this recipe: the layout, loader, page and config above are unverified.

panel.test.tsx:

tsx
// @vitest-environment jsdom
import { memoryDataSource } from "@surfaces-ui/data";
import { cleanup, render, screen, waitFor } from "@testing-library/react";
import { afterEach, describe, expect, it } from "vitest";
import { SurfacesPanel } from "./SurfacesPanel.js";

afterEach(cleanup);

const orders = [
  { order_id: 1, region: "Europe", amount: 40 },
  { order_id: 2, region: "Americas", amount: 15 },
];

describe("SurfacesPanel", () => {
  it("draws the rows of a query in its own themed region", async () => {
    const { container } = render(<SurfacesPanel sourceId="shop" sql="SELECT * FROM orders" dataSource={memoryDataSource({ sourceId: "shop", tables: { orders } })} />);
    await waitFor(() => expect(document.querySelectorAll(".sf-grid-row")).toHaveLength(2));
    const rows = [...document.querySelectorAll(".sf-grid-row")].map((r) => r.textContent);
    expect(rows[0]).toContain("Europe");
    expect(rows[1]).toContain("Americas");
    expect(screen.getByRole("tab", { name: "Table" })).toBeTruthy();
    // Scoped: the theme's variables apply to the panel's own region, not to :root.
    const region = container.querySelector<HTMLElement>("[data-sf-scope]")!;
    expect(region.contains(document.querySelector(".sf-grid-row"))).toBe(true);
    const css = container.querySelector("style")!.textContent!;
    expect(css).toContain(`[data-sf-scope="${region.dataset.sfScope}"]`);
    expect(css).not.toContain(":root");
  });
});

Source and test: apps/recipes/src/nextjs-embed