Skip to content
ui8kit

component / components

Switcher

Switcher component

Install

npx ui8kit add switcher

Pulls in: box, compose, kit-box, presets, utils

Preview

Code

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework svelte --dir src --registry-url https://registry.buildy.tw/svelte --strict-cdn
npx ui8kit add switcher
components/switcher.svelte
<!-- Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT. -->
<!--
  A segmented switcher: a tab list whose items (SwitcherItem) share size and layout. Canary handed these down
  through context; here each item takes the same two props. Arrow keys: attach `moveFocus` to the keydown
  (horizontal, activate).
-->
<script lang="ts" module>
  import type { Snippet } from "svelte";
  import { variants } from "../utils";

  /** Switcher: the classes of each value (shadcn/cva style). */
  export const switcherVariants = variants({
    base: "inline-flex max-w-full items-center gap-1 overflow-x-auto bg-muted inset-ring inset-ring-border rounded",
    variants: {
      size: {
        sm: "h-8 p-1",
        default: "h-10 p-1",
        lg: "h-12 p-1",
      },
      layout: {
        text: "",
        icon: "gap-0",
      },
    },
    defaultVariants: { size: "default", layout: "text" },
  });

  export type SwitcherProps = {
    size?: "sm" | "default" | "lg";
    layout?: "text" | "icon";
    label?: string;
    class?: string;
    children?: Snippet;
  };
</script>

<script lang="ts">
  import Box from "../compose/Box.svelte";
  import { cn } from "../utils";

  let { size = "default", layout = "text", label, class: className, children }: SwitcherProps = $props();
</script>

<Box
  class={cn(switcherVariants({ size: size, layout: layout }), className)}
  role="tablist"
  aria-label={label}
  data-slot="switcher"
>
  {@render children?.()}
</Box>

Svelte registry item (JSON)

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework react --dir src --registry-url https://registry.buildy.tw/react --strict-cdn
npx ui8kit add switcher
components/switcher.tsx
// Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
/**
 * A segmented switcher: a tab list whose items (SwitcherItem) share size and layout. Canary handed these down
 * through context; here each item takes the same two props. Arrow keys: attach `moveFocus` to the keydown
 * (horizontal, activate).
 */
import { type ReactNode } from "react";
import { Box } from "../compose";
import { cn, variants } from "../utils";

/** Switcher: the classes of each value (shadcn/cva style). */
export const switcherVariants = variants({
  base: "inline-flex max-w-full items-center gap-1 overflow-x-auto bg-muted inset-ring inset-ring-border rounded",
  variants: {
    size: {
      sm: "h-8 p-1",
      default: "h-10 p-1",
      lg: "h-12 p-1",
    },
    layout: {
      text: "",
      icon: "gap-0",
    },
  },
  defaultVariants: { size: "default", layout: "text" },
});

export type SwitcherProps = {
  size?: "sm" | "default" | "lg";
  layout?: "text" | "icon";
  label?: string;
  className?: string;
  children?: ReactNode;
};

export function Switcher({ size = "default", layout = "text", label, className, children }: SwitcherProps) {
  return (
    <Box
      className={cn(switcherVariants({ size: size, layout: layout }), className)}
      role="tablist"
      aria-label={label}
      data-slot="switcher"
    >
      {children}
    </Box>
  );
}

React registry item (JSON)

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework vue --dir src --registry-url https://registry.buildy.tw/vue --strict-cdn
npx ui8kit add switcher
components/switcher.vue
<!-- Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT. -->
<!--
  A segmented switcher: a tab list whose items (SwitcherItem) share size and layout. Canary handed these down
  through context; here each item takes the same two props. Arrow keys: attach `moveFocus` to the keydown
  (horizontal, activate).
-->
<script lang="ts">
import { variants } from "../utils";

/** Switcher: the classes of each value (shadcn/cva style). */
export const switcherVariants = variants({
  base: "inline-flex max-w-full items-center gap-1 overflow-x-auto bg-muted inset-ring inset-ring-border rounded",
  variants: {
    size: {
      sm: "h-8 p-1",
      default: "h-10 p-1",
      lg: "h-12 p-1",
    },
    layout: {
      text: "",
      icon: "gap-0",
    },
  },
  defaultVariants: { size: "default", layout: "text" },
});

export type SwitcherProps = {
  size?: "sm" | "default" | "lg";
  layout?: "text" | "icon";
  label?: string;
  class?: string;
};
</script>

<script setup lang="ts">
import Box from "../compose/Box.vue";
import { cn } from "../utils";

const { size = "default", layout = "text", label, class: className } = defineProps<SwitcherProps>();
defineSlots<{ default?(): unknown }>();
</script>

<template>
  <Box
    :class="cn(switcherVariants({ size: size, layout: layout }), className)"
    role="tablist"
    :aria-label="label"
    data-slot="switcher"
  >
    <slot />
  </Box>
</template>

Vue registry item (JSON)

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework solid --dir src --registry-url https://registry.buildy.tw/solid --strict-cdn
npx ui8kit add switcher
components/switcher.tsx
// Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
/**
 * A segmented switcher: a tab list whose items (SwitcherItem) share size and layout. Canary handed these down
 * through context; here each item takes the same two props. Arrow keys: attach `moveFocus` to the keydown
 * (horizontal, activate).
 */
import { type JSX } from "solid-js";
import { Box } from "../compose";
import { cn, variants } from "../utils";

/** Switcher: the classes of each value (shadcn/cva style). */
export const switcherVariants = variants({
  base: "inline-flex max-w-full items-center gap-1 overflow-x-auto bg-muted inset-ring inset-ring-border rounded",
  variants: {
    size: {
      sm: "h-8 p-1",
      default: "h-10 p-1",
      lg: "h-12 p-1",
    },
    layout: {
      text: "",
      icon: "gap-0",
    },
  },
  defaultVariants: { size: "default", layout: "text" },
});

export type SwitcherProps = {
  size?: "sm" | "default" | "lg";
  layout?: "text" | "icon";
  label?: string;
  class?: string;
  children?: JSX.Element;
};

export function Switcher({
  size = "default",
  layout = "text",
  label,
  class: className,
  children,
}: SwitcherProps) {
  return (
    <Box
      class={cn(switcherVariants({ size: size, layout: layout }), className)}
      role="tablist"
      aria-label={label}
      data-slot="switcher"
    >
      {children}
    </Box>
  );
}

Solid registry item (JSON)

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework templ --dir src --registry-url https://registry.buildy.tw/templ --strict-cdn
npx ui8kit add switcher
blocks/switcher.templ
// Code generated by packages/kit-compose/src/server-organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
// Source: packages/compose-gen/specs/patterns/switcher.json

package blocks

import (
	"github.com/ui8kit/codegen/generated/templ/ui/box"
	"github.com/ui8kit/codegen/generated/templ/compose"
	uiutils "github.com/ui8kit/codegen/generated/templ/orgutils"
)

// SwitcherProps are the props of Switcher; the values are the JSON shapes of the TypeScript props.
type SwitcherProps struct {
	Size any `json:"size"`
	Layout any `json:"layout"`
	Label any `json:"label"`
	Class string `json:"className"`
}

// A segmented switcher: a tab list whose items (SwitcherItem) share size and layout. Canary handed these down through context; here each item takes the same two props. Arrow keys: attach `moveFocus` to the keydown (horizontal, activate).
templ Switcher(p SwitcherProps) {
	{{ vSize := uiutils.Def(p.Size, "default") }}
	{{ vLayout := uiutils.Def(p.Layout, "text") }}
	{{ vLabel := p.Label }}
	@compose.Box(compose.BoxProps{BoxProps: box.BoxProps{Class: uiutils.S(uiutils.Cnv(recipeSwitcher.Of(map[string]any{"size": vSize, "layout": vLayout}), p.Class)), Attrs: uiutils.Attrs("role", "tablist", "aria-label", vLabel, "data-slot", "switcher")}}) {
		{ children... }
	}
}

templ registry item (JSON)

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework twig --dir src --registry-url https://registry.buildy.tw/twig --strict-cdn
npx ui8kit add switcher
blocks/switcher.html.twig
{#- Code generated by packages/kit-compose/src/server-organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
    Source: packages/compose-gen/specs/patterns/switcher.json
    A segmented switcher: a tab list whose items (SwitcherItem) share size and layout. Canary handed these down through context; here each item takes the same two props. Arrow keys: attach `moveFocus` to the keydown (horizontal, activate).
    Props (p): size, layout, label, className; children: markup. -#}
{%- set v_size = (p.size ?? 'default') -%}
{%- set v_layout = (p.layout ?? 'text') -%}
{%- set v_label = (p.label ?? null) -%}
{%- set c_1 -%}
  {{ og_slot((children ?? null))|raw }}
{%- endset -%}
{{ include('compose/Box.html.twig', {'class': og_s(og_cnv([og_recipe('blocks/recipes/switcher.variants.json', {'size': v_size, 'layout': v_layout}), (p.className ?? '')])), 'attrs': og_attrs(['role', 'tablist', 'aria-label', v_label, 'data-slot', 'switcher']), 'children': c_1}, with_context = false) }}

Twig registry item (JSON)

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework latte --dir src --registry-url https://registry.buildy.tw/latte --strict-cdn
npx ui8kit add switcher
blocks/switcher.latte
{* Code generated by packages/kit-compose/src/server-organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
    Source: packages/compose-gen/specs/patterns/switcher.json
    A segmented switcher: a tab list whose items (SwitcherItem) share size and layout. Canary handed these down through context; here each item takes the same two props. Arrow keys: attach `moveFocus` to the keydown (horizontal, activate).
    Props (p): size, layout, label, className; children: markup. *}
{var $v_size = ($p['size'] ?? 'default')}
{var $v_layout = ($p['layout'] ?? 'text')}
{var $v_label = ($p['label'] ?? null)}
{capture $c_1}
  {=og_slot(($children ?? null))|noescape}
{/capture}
{=og_include('compose/Box.latte', ['class' => og_s(og_cnv([og_recipe('blocks/recipes/switcher.variants.json', ['size' => $v_size, 'layout' => $v_layout]), ($p['className'] ?? '')])), 'attrs' => og_attrs(['role', 'tablist', 'aria-label', $v_label, 'data-slot', 'switcher']), 'children' => (string) $c_1])|noescape}

Latte registry item (JSON)

Point the CLI at this runtime once, then add the item:

npx ui8kit init --yes --framework jinja2 --dir src --registry-url https://registry.buildy.tw/jinja2 --strict-cdn
npx ui8kit add switcher
blocks/switcher.html.j2
{#- Code generated by packages/kit-compose/src/server-organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
    Source: packages/compose-gen/specs/patterns/switcher.json
    A segmented switcher: a tab list whose items (SwitcherItem) share size and layout. Canary handed these down through context; here each item takes the same two props. Arrow keys: attach `moveFocus` to the keydown (horizontal, activate).
    Props (p): size, layout, label, className; children: markup. -#}
{%- set v_size = og_def(p['size'], 'default') -%}
{%- set v_layout = og_def(p['layout'], 'text') -%}
{%- set v_label = og_def(p['label'], none) -%}
{%- set c_1 -%}
  {{ og_slot(og_def(children, none))|safe }}
{%- endset -%}
{{ og_include('compose/box.html.j2', {'p': {'Class': og_s(og_cnv([og_recipe('blocks/recipes/switcher.variants.json', {'size': v_size, 'layout': v_layout}), og_def(p['className'], '')])), 'Attrs': og_attrs(['role', 'tablist', 'aria-label', v_label, 'data-slot', 'switcher'])}, 'children': c_1}) }}

Jinja2 registry item (JSON)

Props

Names and types come from the xgen specs of the source. Standard HTML attributes (id, role, tabIndex, aria-*) pass through to the root element; the class prop is className in React.

A segmented switcher: a tab list whose items (SwitcherItem) share size and layout. Canary handed these down through context; here each item takes the same two props. Arrow keys: attach `moveFocus` to the keydown (horizontal, activate).

Props of Switcher
PropTypeDefault Description
size "sm" | "default" | "lg""default"height
layout "text" | "icon""text"text or icon items
label string-accessible name

Used in