# Looking like Orbit

Plugin pages shown inside the store dashboard should look like the dashboard around them, in light and dark. `@orbitcommerce/plugin-ui` gives you the dashboard's page frame on Constellation by Orbit Commerce, the design system the dashboard is built with. Every starter already uses it.

## Install

```sh
npm install @orbitcommerce/plugin-ui @orbitcommerce/constellation
```

React and React DOM 19 or later are peer dependencies, so the app needs both. The starters already include `@orbitcommerce/plugin-ui` and `@orbitcommerce/constellation`.

## The layout

The dashboard pages share one root layout, `app/(dashboard)/layout.tsx` in the starters:

```tsx
import '@orbitcommerce/constellation/fonts.css';
import '@orbitcommerce/constellation/styles.css';
import '@orbitcommerce/plugin-ui/embed.css';
import { THEME_INIT_SCRIPT } from '@orbitcommerce/plugin-ui/theme';
import type { Metadata } from 'next';
import type { ReactNode } from 'react';
import manifest from '@/plugin.json';

export const metadata: Metadata = { title: manifest.listing.name };

// The script paints light or dark before the first frame, so there is no flash.
// This is the root layout for the dashboard pages only; a storefront widget has its own.
export default function DashboardLayout({ children }: { children: ReactNode }) {
  return (
    <html lang="en" suppressHydrationWarning>
      <head>
        <script dangerouslySetInnerHTML={{ __html: THEME_INIT_SCRIPT }} />
      </head>
      <body className="o-root orbit-embed">{children}</body>
    </html>
  );
}
```

- The three stylesheets bring Constellation by Orbit Commerce's fonts and styles, then the plugin UI's embed frame. If the app has a global stylesheet of its own, import it before these three, in this order.
- `THEME_INIT_SCRIPT` goes in `<head>` and runs before the first frame. It reads `?theme=` and paints light or dark, then follows the dashboard's later messages. Import it from `@orbitcommerce/plugin-ui/theme`, an entry with no client components, so a server layout can import it.
- `suppressHydrationWarning` on `<html>` keeps the class and style the script sets before hydration from looking like a mismatch to React.
- `<body className="o-root orbit-embed">` puts the design system's root class and the embed page class where the page frame expects them.
- `metadata` sets the tab title from the plugin's listing name.

This is the layout for the dashboard pages only. A storefront widget has its own layout, as [Storefront widgets](/guides/storefront-widgets) describes.

## The parts

| Part | What it is for | Main props |
|---|---|---|
| `EmbedShell` | The root of every dashboard page: the page frame, the tooltips, the confirm dialog and the toaster, all mounted once. | `width` is `'page'` (the default, at most 1120 px) or `'wide'` (fills the canvas); `children`; a ref and the usual `div` attributes. |
| `PageHeader` | The page name, a line under it, and the actions on the right, primary last. | `title`; `description?`; `actions?`; `badge?`; `back?` as `{ label, onClick }`; `titleAs?` as `'h1'` (the default) or `'h2'`. |
| `PageTabs` | A plugin's sections as line tabs under the header. | `tabs`, a list of `{ value, label, icon?, count?, disabled? }`; `label`, the tab list's accessible name; `children?`, the `TabsContent` panels; the `Tabs` props, such as `defaultValue`. |
| `EmbedLoading` | While the plugin waits for the dashboard's token or its first data. | `message?`, default "Connecting to the dashboard". |
| `EmbedError` | The plugin cannot work: no token, the dashboard never answered, a fatal load error. | `message`; `title?`, default "This plugin could not connect"; `actions?`. |
| `ConfirmProvider` and `useConfirm` | One confirm dialog for the page, in the dashboard's style. `EmbedShell` mounts the provider, and `useConfirm()` opens the dialog and resolves yes or no. | The provider takes `children`. `useConfirm()` takes `title`, `description?`, `confirmLabel`, `cancelLabel?`, `tone?` (`'critical'` for destructive actions) and `returnFocusTo?`, and resolves `true` or `false`. It needs an `EmbedShell` (or a `ConfirmProvider`) above it and throws without one. |
| `useReturnFocus` | For a dialog you build yourself: the `onCloseAutoFocus` handler that puts focus back on the control that opened it. | `target`, the element or ref to focus; `onCloseAutoFocus?`, your own handler, which runs first. |

`EmbedShell` already holds the toaster, the tooltip provider and the confirm provider, so a page has one shell, not one per section. Use `useConfirm` instead of `window.confirm`: a sandboxed frame cannot show the browser's dialog.

The starters' main page shows the shape:

```tsx
'use client';

import { TabsContent } from '@orbitcommerce/constellation';
import { EmbedError, EmbedLoading, EmbedShell, PageHeader, PageTabs, useEmbedTheme } from '@orbitcommerce/plugin-ui';
import manifest from '@/plugin.json';
import { EventsList } from '@/components/events-list';
import { SettingsForm } from '@/components/settings-form';
import { useOrbit } from '@/components/use-orbit';
import { Overview } from './overview';

const EXAMPLE_TOPIC = manifest.oauth.webhooks.find((webhook) => !webhook.topic.startsWith('plugin.'))?.topic ?? 'plugin.installed';

export default function EmbedPage() {
  useEmbedTheme();
  const state = useOrbit();

  if (state.status === 'connecting') return <EmbedLoading />;
  if (state.status === 'error') return <EmbedError message={state.message} />;

  return (
    <EmbedShell>
      <PageHeader title={manifest.listing.name} description={manifest.listing.shortDescription} />
      <PageTabs
        label="Plugin sections"
        tabs={[
          { value: 'overview', label: 'Overview' },
          { value: 'settings', label: 'Settings' },
          { value: 'events', label: 'Events' },
        ]}
        defaultValue="overview"
      >
        <TabsContent value="overview">
          <Overview orbit={state.orbit} fetchPlugin={state.fetchPlugin} />
        </TabsContent>
        <TabsContent value="settings">
          <SettingsForm orbit={state.orbit} />
        </TabsContent>
        <TabsContent value="events">
          <EventsList fetchPlugin={state.fetchPlugin} exampleTopic={EXAMPLE_TOPIC} />
        </TabsContent>
      </PageTabs>
    </EmbedShell>
  );
}
```

The page calls `useEmbedTheme()` once, which keeps the document's theme in step; a part that draws differently in dark can call the hook itself for the value. While the plugin connects it shows `EmbedLoading`; a failure shows `EmbedError`. Everything else sits in one `EmbedShell`.

## Light and dark

The dashboard loads the page with `?theme=light` or `?theme=dark`, and `THEME_INIT_SCRIPT` paints it before the first frame. `readEmbedThemeFromUrl()` gives your code the URL's value: `'light'`, `'dark'` or `null`. After that the dashboard posts `{ type: 'ORBIT_THEME', theme }` from the parent window whenever its theme changes. `useEmbedTheme()` and `onEmbedTheme()` act only on a message from the parent window. `THEME_INIT_SCRIPT` acts on an `ORBIT_THEME` message from any window. All three ignore an unknown theme.

`useEmbedTheme()` from `@orbitcommerce/plugin-ui` returns `'light'` or `'dark'` for a component. Its first value is `'dark'` when `<html>` already has the `dark` class (the inline script ran), else the URL's `?theme=`, else `'light'`. Each message applies the theme to the document and updates the state, and the listener is removed on unmount. It is safe to call from more than one component.

Without React, `onEmbedTheme(listener)` from `@orbitcommerce/sdk/embed` follows the same messages and returns a function that stops listening, and `readEmbedThemeFromUrl(search?)` gives the first value. To paint the document as well, `applyEmbedTheme(theme)` from `@orbitcommerce/plugin-ui/theme` paints it the same way.

## Blocks that grow and shrink

A dashboard block is a frame whose height the dashboard sets from what you report. `observeEmbedHeight(element)` from `@orbitcommerce/sdk/embed` watches an element with a `ResizeObserver` and reports its height as it changes. It returns a function that stops watching:

```ts
import { observeEmbedHeight } from '@orbitcommerce/sdk/embed';

const stop = observeEmbedHeight(document.getElementById('card')!);
// later, when the block unmounts
stop();
```

Reports are debounced by 50 ms and posted to the parent as `{ type: 'ORBIT_RESIZE', payload: { height } }`. Heights are whole pixels, and a report that is not a finite, positive number, or is above 10,000, is not sent. 10,000 pixels is the ceiling (`EMBED_MAX_HEIGHT`); the dashboard ignores anything taller, so if your content is taller, page or scroll it inside the block. `reportEmbedHeight(height)` sends one height by hand and tells you whether it went. A page opened outside the dashboard has no parent to tell, so it sends nothing.

The product-card starter does this for its card, so the frame grows and shrinks with its content.

## Colour

Take colour only from the `--o-*` tokens of Constellation by Orbit Commerce, through its components or with `var(--o-...)`, for example `var(--o-fg-1)` and `var(--o-fg-2)`. Do not write a hex, `rgb()` or `oklch()` value of your own: it will not follow the dashboard's theme or brand, and dark mode breaks.

## Not in a storefront widget

A storefront widget runs inside the shop's own page, not the dashboard frame. The shop's fonts, spacing and colours decide how it looks, so a widget uses neither `@orbitcommerce/plugin-ui` nor Constellation by Orbit Commerce. Build it with the widget bridge and the shop's own styling; [Storefront widgets](/guides/storefront-widgets) describes it.
