SearchCtrlK

  • ↑↓navigate
  • ↵open
  • escclose

Can’t find it? Contact us

codeTabsSync

Why JS.

tsx
import { codeTabsSync } from "@sdxc/ui/docs";

Usage

Keeps every set of code tabs sharing syncKey on one choice. Attach it to an element inside the set: on insertion it selects the remembered choice (a cached page was rendered for whoever asked first), and each switch is stored and mirrored into the other sets on the page. With local storage, other browser tabs follow too.

Signature

typescript
codeTabsSync(syncKey: string, persist: CodeTabsPersistence): MixinDescriptor<HTMLElement>

Returns a mixin descriptor for an element inside the set's [data-code-tabs] root.

Parameters

ParameterTypeRequiredDescription
syncKeystringYesThe key the tab sets share.
persist"cookie" | "local" | "none"YesWhere the choice is remembered between pages.

Examples

tsx
<span hidden mix={[codeTabsSync("package-manager", "cookie")]} />

The events, constants and types its module publishes, each imported from @sdxc/ui/docs too.

Constant

CODE_TABS_ATTRIBUTE

Attribute every set of code tabs carries, holding its sync key (empty when unsynced).

typescript
const CODE_TABS_ATTRIBUTE = "data-code-tabs"
Function

codeTabsStorageKey

The cookie and localStorage key a sync key's choice is stored under. A server that renders the remembered choice reads the cookie by this name, or uses readCodeTabsCookie.

typescript
codeTabsStorageKey(syncKey: string): string
ParameterTypeRequiredDescription
syncKeystringYesThe key the tab sets share, such as package-manager.
Function

readCodeTabsCookie

Reads the remembered choice for syncKey from a request's Cookie header, so the server renders the reader's tab already selected and the page never flashes.

typescript
readCodeTabsCookie(cookieHeader: string | null | undefined, syncKey: string): string | undefined
ParameterTypeRequiredDescription
cookieHeaderstring | null | undefinedYesThe request's Cookie header, or nothing when none was sent.
syncKeystringYesThe key the tab sets share.
tsx
<CodeTabs syncKey="package-manager" persist="cookie" selected={readCodeTabsCookie(request.headers.get("Cookie"), "package-manager")} … />
Type

CodeTabsPersistence

Where a set of code tabs remembers the reader's choice between pages.

typescript
type CodeTabsPersistence = "cookie" | "local" | "none"
@sdxc/ui

Can’t find it? Contact us