Guide › Templates

On this page

Context

A component can hand values to every component rendered inside it, at any depth, without the page passing props to each one. Tabs, menus and radio groups use it so their parts know the selection.

tscl
{{-- components/tabs.tscl --}}
@input value: string
<script>
  provide('tabs', { value })
</script>
<div class="tabs"><slot /></div>

{{-- components/tab-panel.tscl --}}
@context tabs: { value: string }
@input value: string
<section [hidden]="value !== tabs.value"><slot /></section>

{{-- a page --}}
<tabs value="npm">
  <tab-panel value="npm">npm i clarusjs</tab-panel>
  <tab-panel value="pnpm">pnpm add clarusjs</tab-panel>
</tabs>

The server renders the panels with hidden already right: context is there while the page renders, not only once JavaScript runs.

Providing #

Reading #

In islands #

Context is passed with each render, never stored globally, so it works the same for pages that wait on data and on any server runtime.