Skip to content

Web API

@grain.sh/web turns Grain documents into semantic HTML and Web Components. It is the browser adapter used by the docs playground and the quickest path to a rendered interface.


WebAdapter

typescript
import { WebAdapter } from '@grain.sh/web';

const adapter = new WebAdapter(config);

What This Package Owns

  • DOM rendering for the documented Grain primitives
  • custom element registration for browser use
  • theme-token driven styling
  • browser event hooks for interactive surfaces

Constructor Options

typescript
interface WebAdapterConfig {
  theme?: Record<string, string>;
  classPrefix?: string;
}

Methods

render()

typescript
adapter.render(grain: string, options?: RenderOptions): HTMLElement | null
typescript
interface RenderOptions {
  container?: HTMLElement | string;
  position?: 'replace' | 'append' | 'prepend' | 'before' | 'after';
  animate?: boolean;
}

on()

typescript
adapter.on(event: string, callback: (payload: any) => void): () => void

Events

EventPayloadDescription
action{ action: string, element: HTMLElement }Action button clicked
copy{ content: string }Content copied
think:toggle{ visible: boolean }Think visibility toggled

getThemeCSS()

typescript
const css = adapter.getThemeCSS();
console.log(css);
// --grain-primary: #2155ff;
// --grain-secondary: #5c6c87;

Theme Tokens

@grain.sh/web ships a default visual system and applies it through CSS custom properties on the render host.

TokenPurpose
--grain-primaryPrimary accent color for actions and emphasis
--grain-secondarySecondary accent for subdued surfaces
--grain-backgroundHost background token
--grain-surfaceBase card surface
--grain-surface-strongElevated surface for nested panels
--grain-borderBorder color across primitives
--grain-textPrimary text color
--grain-mutedSecondary text and labels
--grain-errorError and destructive state color
--grain-successSuccess state color
--grain-warningWarning state color
--grain-shadowShared component shadow
--grain-radiusShared component radius
--grain-font-familyBase UI font
--grain-font-monoMonospace font for structured output

Example:

typescript
const adapter = new WebAdapter({
  theme: {
    '--grain-primary': 'var(--brand-accent)',
    '--grain-surface': 'var(--panel-surface)',
    '--grain-text': 'var(--text-strong)'
  }
});

Production Notes

  • Prefer pinned package versions instead of @latest once the interface is in use.
  • Validate model output with @grain.sh/core if malformed Grain should fail before it reaches the DOM.
  • Keep the Grain document itself as the source of truth and treat theme settings as presentation concerns.

Released under the MIT License.