Skip to content
ui8kit

component / components

Switcher Item

Switcher Item component

Install

npx ui8kit add switcher-item

Pulls in: button-plain, compose, kit-button, 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-item
components/switcher-item.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. -->
<!-- One item of a Switcher (default or active). Pass the same size and layout as the Switcher. -->
<script lang="ts" module>
  import type { Snippet } from "svelte";
  import { variants } from "../utils";

  /** SwitcherItem: the classes of each value (shadcn/cva style). */
  export const switcherItemVariants = variants({
    base:
      "inline-flex shrink-0 items-center justify-center whitespace-nowrap font-bold text-muted-foreground inset-ring " +
      "inset-ring-transparent rounded",
    variants: {
      size: {
        sm: "h-6 gap-1 px-3 text-xs",
        default: "h-8 gap-2 px-4 text-sm",
        lg: "h-10 gap-2 px-5 text-sm",
      },
      state: {
        default: "",
        active: "bg-background text-foreground inset-ring-border",
      },
      layout: {
        text: "",
        icon: "[&>svg]:size-4",
      },
    },
    defaultVariants: { size: "default", state: "default", layout: "text" },
    compoundVariants: [
      { layout: "icon", size: "sm", class: "w-6 px-0" },
      { layout: "icon", size: "default", class: "w-8 px-0" },
      { layout: "icon", size: "lg", class: "w-10 px-0" },
    ],
  });

  export type SwitcherItemProps = {
    active?: boolean;
    size?: "sm" | "default" | "lg";
    layout?: "text" | "icon";
    class?: string;
    children?: Snippet;
    onclick?: () => void;
  };
</script>

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

  let {
    active = false,
    size = "default",
    layout = "text",
    class: className,
    children,
    onclick,
  }: SwitcherItemProps = $props();
</script>

<ButtonPlain
  type="button"
  role="tab"
  class={cn(
    switcherItemVariants({ size: size, layout: layout, state: (active ? "active" : "default") }),
    className
  )}
  aria-selected={active}
  tabindex={(active ? 0 : -1)}
  data-slot="switcher-item"
  data-state={(active ? "active" : "default")}
  onclick={() => onclick?.()}
>
  {@render children?.()}
</ButtonPlain>

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-item
components/switcher-item.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.
/** One item of a Switcher (default or active). Pass the same size and layout as the Switcher. */
import { type ReactNode } from "react";
import { ButtonPlain } from "../compose";
import { cn, variants } from "../utils";

/** SwitcherItem: the classes of each value (shadcn/cva style). */
export const switcherItemVariants = variants({
  base:
    "inline-flex shrink-0 items-center justify-center whitespace-nowrap font-bold text-muted-foreground inset-ring " +
    "inset-ring-transparent rounded",
  variants: {
    size: {
      sm: "h-6 gap-1 px-3 text-xs",
      default: "h-8 gap-2 px-4 text-sm",
      lg: "h-10 gap-2 px-5 text-sm",
    },
    state: {
      default: "",
      active: "bg-background text-foreground inset-ring-border",
    },
    layout: {
      text: "",
      icon: "[&>svg]:size-4",
    },
  },
  defaultVariants: { size: "default", state: "default", layout: "text" },
  compoundVariants: [
    { layout: "icon", size: "sm", class: "w-6 px-0" },
    { layout: "icon", size: "default", class: "w-8 px-0" },
    { layout: "icon", size: "lg", class: "w-10 px-0" },
  ],
});

export type SwitcherItemProps = {
  active?: boolean;
  size?: "sm" | "default" | "lg";
  layout?: "text" | "icon";
  className?: string;
  children?: ReactNode;
  onClick?: () => void;
};

export function SwitcherItem({
  active = false,
  size = "default",
  layout = "text",
  className,
  children,
  onClick,
}: SwitcherItemProps) {
  return (
    <ButtonPlain
      type="button"
      role="tab"
      className={cn(
        switcherItemVariants({ size: size, layout: layout, state: (active ? "active" : "default") }),
        className
      )}
      aria-selected={active}
      tabIndex={(active ? 0 : -1)}
      data-slot="switcher-item"
      data-state={(active ? "active" : "default")}
      onClick={() => onClick?.()}
    >
      {children}
    </ButtonPlain>
  );
}

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-item
components/switcher-item.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. -->
<!-- One item of a Switcher (default or active). Pass the same size and layout as the Switcher. -->
<script lang="ts">
import { variants } from "../utils";

/** SwitcherItem: the classes of each value (shadcn/cva style). */
export const switcherItemVariants = variants({
  base:
    "inline-flex shrink-0 items-center justify-center whitespace-nowrap font-bold text-muted-foreground inset-ring " +
    "inset-ring-transparent rounded",
  variants: {
    size: {
      sm: "h-6 gap-1 px-3 text-xs",
      default: "h-8 gap-2 px-4 text-sm",
      lg: "h-10 gap-2 px-5 text-sm",
    },
    state: {
      default: "",
      active: "bg-background text-foreground inset-ring-border",
    },
    layout: {
      text: "",
      icon: "[&>svg]:size-4",
    },
  },
  defaultVariants: { size: "default", state: "default", layout: "text" },
  compoundVariants: [
    { layout: "icon", size: "sm", class: "w-6 px-0" },
    { layout: "icon", size: "default", class: "w-8 px-0" },
    { layout: "icon", size: "lg", class: "w-10 px-0" },
  ],
});

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

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

const {
  active = false,
  size = "default",
  layout = "text",
  class: className,
} = defineProps<SwitcherItemProps>();
const emit = defineEmits<{ click: [] }>();
defineSlots<{ default?(): unknown }>();
</script>

<template>
  <ButtonPlain
    type="button"
    role="tab"
    :class="cn(
      switcherItemVariants({ size: size, layout: layout, state: (active ? 'active' : 'default') }),
      className
    )"
    :aria-selected="active"
    :tabindex="(active ? 0 : -1)"
    data-slot="switcher-item"
    :data-state="(active ? 'active' : 'default')"
    @click="() => emit('click')"
  >
    <slot />
  </ButtonPlain>
</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-item
components/switcher-item.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.
/** One item of a Switcher (default or active). Pass the same size and layout as the Switcher. */
import { type JSX } from "solid-js";
import { ButtonPlain } from "../compose";
import { cn, variants } from "../utils";

/** SwitcherItem: the classes of each value (shadcn/cva style). */
export const switcherItemVariants = variants({
  base:
    "inline-flex shrink-0 items-center justify-center whitespace-nowrap font-bold text-muted-foreground inset-ring " +
    "inset-ring-transparent rounded",
  variants: {
    size: {
      sm: "h-6 gap-1 px-3 text-xs",
      default: "h-8 gap-2 px-4 text-sm",
      lg: "h-10 gap-2 px-5 text-sm",
    },
    state: {
      default: "",
      active: "bg-background text-foreground inset-ring-border",
    },
    layout: {
      text: "",
      icon: "[&>svg]:size-4",
    },
  },
  defaultVariants: { size: "default", state: "default", layout: "text" },
  compoundVariants: [
    { layout: "icon", size: "sm", class: "w-6 px-0" },
    { layout: "icon", size: "default", class: "w-8 px-0" },
    { layout: "icon", size: "lg", class: "w-10 px-0" },
  ],
});

export type SwitcherItemProps = {
  active?: boolean;
  size?: "sm" | "default" | "lg";
  layout?: "text" | "icon";
  class?: string;
  children?: JSX.Element;
  onClick?: () => void;
};

export function SwitcherItem({
  active = false,
  size = "default",
  layout = "text",
  class: className,
  children,
  onClick,
}: SwitcherItemProps) {
  return (
    <ButtonPlain
      type="button"
      role="tab"
      class={cn(
        switcherItemVariants({ size: size, layout: layout, state: (active ? "active" : "default") }),
        className
      )}
      aria-selected={active}
      tabindex={(active ? 0 : -1)}
      data-slot="switcher-item"
      data-state={(active ? "active" : "default")}
      onClick={() => onClick?.()}
    >
      {children}
    </ButtonPlain>
  );
}

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-item
blocks/switcher-item.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-item.json

package blocks

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

// SwitcherItemProps are the props of SwitcherItem; the values are the JSON shapes of the TypeScript props.
type SwitcherItemProps struct {
	Active any `json:"active"`
	Size any `json:"size"`
	Layout any `json:"layout"`
	Class string `json:"className"`
}

// One item of a Switcher (default or active). Pass the same size and layout as the Switcher.
templ SwitcherItem(p SwitcherItemProps) {
	{{ vActive := uiutils.Def(p.Active, false) }}
	{{ vSize := uiutils.Def(p.Size, "default") }}
	{{ vLayout := uiutils.Def(p.Layout, "text") }}
	@compose.ButtonPlain(compose.ButtonPlainProps{ButtonProps: button.ButtonProps{Type: "button", Class: uiutils.S(uiutils.Cnv(recipeSwitcherItem.Of(map[string]any{"size": vSize, "layout": vLayout, "state": uiutils.Cond(uiutils.T(vActive), "active", "default")}), p.Class)), Attrs: uiutils.Attrs("role", "tab", "aria-selected", vActive, "tabindex", uiutils.Cond(uiutils.T(vActive), float64(0), float64(-1)), "data-slot", "switcher-item", "data-state", uiutils.Cond(uiutils.T(vActive), "active", "default"))}}) {
		{ 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-item
blocks/switcher-item.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-item.json
    One item of a Switcher (default or active). Pass the same size and layout as the Switcher.
    Props (p): active, size, layout, className; children: markup. -#}
{%- set v_active = (p.active ?? false) -%}
{%- set v_size = (p.size ?? 'default') -%}
{%- set v_layout = (p.layout ?? 'text') -%}
{%- set c_1 -%}
  {{ og_slot((children ?? null))|raw }}
{%- endset -%}
{{ include('compose/ButtonPlain.html.twig', {'type': 'button', 'class': og_s(og_cnv([og_recipe('blocks/recipes/switcher-item.variants.json', {'size': v_size, 'layout': v_layout, 'state': (og_t(v_active) ? 'active' : 'default')}), (p.className ?? '')])), 'attrs': og_attrs(['role', 'tab', 'aria-selected', v_active, 'tabindex', (og_t(v_active) ? 0 : -1), 'data-slot', 'switcher-item', 'data-state', (og_t(v_active) ? 'active' : 'default')]), '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-item
blocks/switcher-item.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-item.json
    One item of a Switcher (default or active). Pass the same size and layout as the Switcher.
    Props (p): active, size, layout, className; children: markup. *}
{var $v_active = ($p['active'] ?? false)}
{var $v_size = ($p['size'] ?? 'default')}
{var $v_layout = ($p['layout'] ?? 'text')}
{capture $c_1}
  {=og_slot(($children ?? null))|noescape}
{/capture}
{=og_include('compose/ButtonPlain.latte', ['type' => 'button', 'class' => og_s(og_cnv([og_recipe('blocks/recipes/switcher-item.variants.json', ['size' => $v_size, 'layout' => $v_layout, 'state' => (og_t($v_active) ? 'active' : 'default')]), ($p['className'] ?? '')])), 'attrs' => og_attrs(['role', 'tab', 'aria-selected', $v_active, 'tabindex', (og_t($v_active) ? 0 : -1), 'data-slot', 'switcher-item', 'data-state', (og_t($v_active) ? 'active' : 'default')]), '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-item
blocks/switcher-item.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-item.json
    One item of a Switcher (default or active). Pass the same size and layout as the Switcher.
    Props (p): active, size, layout, className; children: markup. -#}
{%- set v_active = og_def(p['active'], false) -%}
{%- set v_size = og_def(p['size'], 'default') -%}
{%- set v_layout = og_def(p['layout'], 'text') -%}
{%- set c_1 -%}
  {{ og_slot(og_def(children, none))|safe }}
{%- endset -%}
{{ og_include('compose/button-plain.html.j2', {'p': {'Type': 'button', 'Class': og_s(og_cnv([og_recipe('blocks/recipes/switcher-item.variants.json', {'size': v_size, 'layout': v_layout, 'state': ('active' if og_t(v_active) else 'default')}), og_def(p['className'], '')])), 'Attrs': og_attrs(['role', 'tab', 'aria-selected', v_active, 'tabindex', (0 if og_t(v_active) else -1), 'data-slot', 'switcher-item', 'data-state', ('active' if og_t(v_active) else 'default')])}, '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.

One item of a Switcher (default or active). Pass the same size and layout as the Switcher.

Props of SwitcherItem
PropTypeDefault Description
active booleanfalsethe selected item
size "sm" | "default" | "lg""default"height
layout "text" | "icon""text"text or icon

Used in