Skip to content
ui8kit

component / components

Nav Item

Nav Item component

Install

npx ui8kit add nav-item

Pulls in: box, button-plain, compose, kit-box, kit-button, kit-link, link-plain, nav-item-row, presets, utils

Preview

No static preview for this item: it needs content or a parent that this page cannot supply. The code is below.

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 nav-item
components/nav-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. -->
<!--
  A sidebar list item: icon, label and an optional right slot. With `href` it is a link, with `interactive` a
  button, otherwise a static box.
-->
<script lang="ts" module>
  import type { Snippet } from "svelte";
  import { variants } from "../utils";

  /** NavItem: the classes of each value (shadcn/cva style). */
  export const navItemVariants = variants({
    base: "flex w-full items-center p-2 rounded",
    variants: {
      level: {
        "1": "",
        "2": "",
        "3": "pr-2 pl-12",
        "4": "pr-2 pl-20",
      },
      state: {
        default: "",
        selected: "",
      },
    },
    defaultVariants: { level: "1", state: "default" },
  });
  /** NavItem: the classes of each value (shadcn/cva style). */
  export const navItemContentVariants = variants({
    base:
      "flex h-8 min-w-0 flex-1 items-center justify-between px-4 py-2 text-xs font-bold text-muted-foreground " +
      "inset-ring inset-ring-transparent rounded",
    variants: {
      level: {
        "1": "",
        "2": "",
        "3": "px-2",
        "4": "px-2",
      },
      state: {
        default: "",
        selected: "bg-background text-foreground inset-ring-border",
      },
    },
    defaultVariants: { level: "1", state: "default" },
  });

  export type NavItemProps = {
    selected?: boolean;
    level?: "1" | "2" | "3" | "4";
    href?: string;
    interactive?: boolean;
    reserveRight?: boolean;
    contentClass?: string;
    class?: string;
    children?: Snippet;
    icon?: Snippet;
    right?: Snippet;
    onclick?: () => void;
  };
</script>

<script lang="ts">
  import Box from "../compose/Box.svelte";
  import ButtonPlain from "../compose/ButtonPlain.svelte";
  import LinkPlain from "../compose/LinkPlain.svelte";
  import NavItemRow from "../components/nav-item-row.svelte";
  import { cn, urlOf } from "../utils";

  let {
    selected = false,
    level,
    href,
    interactive = false,
    reserveRight = false,
    contentClass,
    class: className,
    children,
    icon,
    right,
    onclick,
  }: NavItemProps = $props();
  const state = $derived(selected ? "selected" : "default");
  const outer = $derived(
    cn(
      navItemVariants({ level: level, state: state }),
      "group in-data-[nav=collapsed]:justify-center",
      className,
    )
  );
  const inner = $derived(
    cn(
      navItemContentVariants({ level: level, state: state }),
      (!selected && "group-hover:bg-muted"),
      "in-data-[nav=collapsed]:w-10 in-data-[nav=collapsed]:flex-none in-data-[nav=collapsed]:justify-center in-data-[nav=collapsed]:px-0",
      contentClass,
    )
  );
</script>

{#if href}
  <LinkPlain
    href={urlOf(href)}
    class={outer}
    aria-current={(selected ? "page" : undefined)}
    data-slot="nav-item"
    data-state={state}
    onclick={() => onclick?.()}
  >
    <NavItemRow inner={inner} reserveRight={reserveRight} icon={icon} right={right} children={children} />
  </LinkPlain>
{:else if interactive}
  <ButtonPlain
    type="button"
    class={cn(outer, "text-left")}
    aria-current={(selected ? "page" : undefined)}
    data-slot="nav-item"
    data-state={state}
    onclick={() => onclick?.()}
  >
    <NavItemRow inner={inner} reserveRight={reserveRight} icon={icon} right={right} children={children} />
  </ButtonPlain>
{:else}
  <Box class={outer} aria-current={(selected ? "page" : undefined)} data-slot="nav-item" data-state={state}>
    <NavItemRow inner={inner} reserveRight={reserveRight} icon={icon} right={right} children={children} />
  </Box>
{/if}

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 nav-item
components/nav-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.
/**
 * A sidebar list item: icon, label and an optional right slot. With `href` it is a link, with `interactive` a
 * button, otherwise a static box.
 */
import { type ReactNode } from "react";
import { Box, ButtonPlain, LinkPlain } from "../compose";
import { NavItemRow } from "../components/nav-item-row";
import { cn, urlOf, variants } from "../utils";

/** NavItem: the classes of each value (shadcn/cva style). */
export const navItemVariants = variants({
  base: "flex w-full items-center p-2 rounded",
  variants: {
    level: {
      "1": "",
      "2": "",
      "3": "pr-2 pl-12",
      "4": "pr-2 pl-20",
    },
    state: {
      default: "",
      selected: "",
    },
  },
  defaultVariants: { level: "1", state: "default" },
});

/** NavItem: the classes of each value (shadcn/cva style). */
export const navItemContentVariants = variants({
  base:
    "flex h-8 min-w-0 flex-1 items-center justify-between px-4 py-2 text-xs font-bold text-muted-foreground " +
    "inset-ring inset-ring-transparent rounded",
  variants: {
    level: {
      "1": "",
      "2": "",
      "3": "px-2",
      "4": "px-2",
    },
    state: {
      default: "",
      selected: "bg-background text-foreground inset-ring-border",
    },
  },
  defaultVariants: { level: "1", state: "default" },
});

export type NavItemProps = {
  selected?: boolean;
  level?: "1" | "2" | "3" | "4";
  href?: string;
  interactive?: boolean;
  reserveRight?: boolean;
  contentClass?: string;
  className?: string;
  children?: ReactNode;
  icon?: ReactNode;
  right?: ReactNode;
  onClick?: () => void;
};

export function NavItem({
  selected = false,
  level,
  href,
  interactive = false,
  reserveRight = false,
  contentClass,
  className,
  children,
  icon,
  right,
  onClick,
}: NavItemProps) {
  const state = selected ? "selected" : "default";
  const outer =
    cn(
      navItemVariants({ level: level, state: state }),
      "group in-data-[nav=collapsed]:justify-center",
      className,
    );
  const inner =
    cn(
      navItemContentVariants({ level: level, state: state }),
      (!selected && "group-hover:bg-muted"),
      "in-data-[nav=collapsed]:w-10 in-data-[nav=collapsed]:flex-none in-data-[nav=collapsed]:justify-center in-data-[nav=collapsed]:px-0",
      contentClass,
    );
  return (
    <>
      {href ? (
        <LinkPlain
          href={urlOf(href)}
          className={outer}
          aria-current={(selected ? "page" : undefined)}
          data-slot="nav-item"
          data-state={state}
          onClick={() => onClick?.()}
        >
          <NavItemRow inner={inner} reserveRight={reserveRight} icon={icon} right={right} children={children} />
        </LinkPlain>
      ) : interactive ? (
        <ButtonPlain
          type="button"
          className={cn(outer, "text-left")}
          aria-current={(selected ? "page" : undefined)}
          data-slot="nav-item"
          data-state={state}
          onClick={() => onClick?.()}
        >
          <NavItemRow inner={inner} reserveRight={reserveRight} icon={icon} right={right} children={children} />
        </ButtonPlain>
      ) : (
        <Box className={outer} aria-current={(selected ? "page" : undefined)} data-slot="nav-item" data-state={state}>
          <NavItemRow inner={inner} reserveRight={reserveRight} icon={icon} right={right} children={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 nav-item
components/nav-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. -->
<!--
  A sidebar list item: icon, label and an optional right slot. With `href` it is a link, with `interactive` a
  button, otherwise a static box.
-->
<script lang="ts">
import { variants } from "../utils";

/** NavItem: the classes of each value (shadcn/cva style). */
export const navItemVariants = variants({
  base: "flex w-full items-center p-2 rounded",
  variants: {
    level: {
      "1": "",
      "2": "",
      "3": "pr-2 pl-12",
      "4": "pr-2 pl-20",
    },
    state: {
      default: "",
      selected: "",
    },
  },
  defaultVariants: { level: "1", state: "default" },
});
/** NavItem: the classes of each value (shadcn/cva style). */
export const navItemContentVariants = variants({
  base:
    "flex h-8 min-w-0 flex-1 items-center justify-between px-4 py-2 text-xs font-bold text-muted-foreground " +
    "inset-ring inset-ring-transparent rounded",
  variants: {
    level: {
      "1": "",
      "2": "",
      "3": "px-2",
      "4": "px-2",
    },
    state: {
      default: "",
      selected: "bg-background text-foreground inset-ring-border",
    },
  },
  defaultVariants: { level: "1", state: "default" },
});

export type NavItemProps = {
  selected?: boolean;
  level?: "1" | "2" | "3" | "4";
  href?: string;
  interactive?: boolean;
  reserveRight?: boolean;
  contentClass?: string;
  class?: string;
};
</script>

<script setup lang="ts">
import { computed } from "vue";
import Box from "../compose/Box.vue";
import ButtonPlain from "../compose/ButtonPlain.vue";
import LinkPlain from "../compose/LinkPlain.vue";
import NavItemRow from "../components/nav-item-row.vue";
import { cn, urlOf } from "../utils";

const {
  selected = false,
  level,
  href,
  interactive = false,
  reserveRight = false,
  contentClass,
  class: className,
} = defineProps<NavItemProps>();
const emit = defineEmits<{ click: [] }>();
defineSlots<{ default?(): unknown, icon?(): unknown, right?(): unknown }>();
const state = computed(() => selected ? "selected" : "default");
const outer = computed(() =>
  cn(
    navItemVariants({ level: level, state: state.value }),
    "group in-data-[nav=collapsed]:justify-center",
    className,
  )
);
const inner = computed(() =>
  cn(
    navItemContentVariants({ level: level, state: state.value }),
    (!selected && "group-hover:bg-muted"),
    "in-data-[nav=collapsed]:w-10 in-data-[nav=collapsed]:flex-none in-data-[nav=collapsed]:justify-center in-data-[nav=collapsed]:px-0",
    contentClass,
  )
);
</script>

<template>
  <LinkPlain v-if="href"
    :href="urlOf(href)"
    :class="outer"
    :aria-current="(selected ? 'page' : undefined)"
    data-slot="nav-item"
    :data-state="state"
    @click="() => emit('click')"
  >
    <NavItemRow :inner="inner" :reserveRight="reserveRight">
      <template v-if="$slots.icon" #icon><slot name="icon" /></template>
      <template v-if="$slots.right" #right><slot name="right" /></template>
      <template v-if="$slots.default" #default><slot /></template>
    </NavItemRow>
  </LinkPlain>
  <ButtonPlain v-else-if="interactive"
    type="button"
    :class="cn(outer, 'text-left')"
    :aria-current="(selected ? 'page' : undefined)"
    data-slot="nav-item"
    :data-state="state"
    @click="() => emit('click')"
  >
    <NavItemRow :inner="inner" :reserveRight="reserveRight">
      <template v-if="$slots.icon" #icon><slot name="icon" /></template>
      <template v-if="$slots.right" #right><slot name="right" /></template>
      <template v-if="$slots.default" #default><slot /></template>
    </NavItemRow>
  </ButtonPlain>
  <Box
    v-else
    :class="outer"
    :aria-current="(selected ? 'page' : undefined)"
    data-slot="nav-item"
    :data-state="state"
  >
    <NavItemRow :inner="inner" :reserveRight="reserveRight">
      <template v-if="$slots.icon" #icon><slot name="icon" /></template>
      <template v-if="$slots.right" #right><slot name="right" /></template>
      <template v-if="$slots.default" #default><slot /></template>
    </NavItemRow>
  </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 nav-item
components/nav-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.
/**
 * A sidebar list item: icon, label and an optional right slot. With `href` it is a link, with `interactive` a
 * button, otherwise a static box.
 */
import { type JSX } from "solid-js";
import { Box, ButtonPlain, LinkPlain } from "../compose";
import { NavItemRow } from "../components/nav-item-row";
import { cn, urlOf, variants } from "../utils";

/** NavItem: the classes of each value (shadcn/cva style). */
export const navItemVariants = variants({
  base: "flex w-full items-center p-2 rounded",
  variants: {
    level: {
      "1": "",
      "2": "",
      "3": "pr-2 pl-12",
      "4": "pr-2 pl-20",
    },
    state: {
      default: "",
      selected: "",
    },
  },
  defaultVariants: { level: "1", state: "default" },
});

/** NavItem: the classes of each value (shadcn/cva style). */
export const navItemContentVariants = variants({
  base:
    "flex h-8 min-w-0 flex-1 items-center justify-between px-4 py-2 text-xs font-bold text-muted-foreground " +
    "inset-ring inset-ring-transparent rounded",
  variants: {
    level: {
      "1": "",
      "2": "",
      "3": "px-2",
      "4": "px-2",
    },
    state: {
      default: "",
      selected: "bg-background text-foreground inset-ring-border",
    },
  },
  defaultVariants: { level: "1", state: "default" },
});

export type NavItemProps = {
  selected?: boolean;
  level?: "1" | "2" | "3" | "4";
  href?: string;
  interactive?: boolean;
  reserveRight?: boolean;
  contentClass?: string;
  class?: string;
  children?: JSX.Element;
  icon?: JSX.Element;
  right?: JSX.Element;
  onClick?: () => void;
};

export function NavItem({
  selected = false,
  level,
  href,
  interactive = false,
  reserveRight = false,
  contentClass,
  class: className,
  children,
  icon,
  right,
  onClick,
}: NavItemProps) {
  const state = selected ? "selected" : "default";
  const outer =
    cn(
      navItemVariants({ level: level, state: state }),
      "group in-data-[nav=collapsed]:justify-center",
      className,
    );
  const inner =
    cn(
      navItemContentVariants({ level: level, state: state }),
      (!selected && "group-hover:bg-muted"),
      "in-data-[nav=collapsed]:w-10 in-data-[nav=collapsed]:flex-none in-data-[nav=collapsed]:justify-center in-data-[nav=collapsed]:px-0",
      contentClass,
    );
  return (
    <>
      {href ? (
        <LinkPlain
          href={urlOf(href)}
          class={outer}
          aria-current={(selected ? "page" : undefined)}
          data-slot="nav-item"
          data-state={state}
          onClick={() => onClick?.()}
        >
          <NavItemRow inner={inner} reserveRight={reserveRight} icon={icon} right={right} children={children} />
        </LinkPlain>
      ) : interactive ? (
        <ButtonPlain
          type="button"
          class={cn(outer, "text-left")}
          aria-current={(selected ? "page" : undefined)}
          data-slot="nav-item"
          data-state={state}
          onClick={() => onClick?.()}
        >
          <NavItemRow inner={inner} reserveRight={reserveRight} icon={icon} right={right} children={children} />
        </ButtonPlain>
      ) : (
        <Box class={outer} aria-current={(selected ? "page" : undefined)} data-slot="nav-item" data-state={state}>
          <NavItemRow inner={inner} reserveRight={reserveRight} icon={icon} right={right} children={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 nav-item
blocks/nav-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/nav-item.json

package blocks

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

// NavItemProps are the props of NavItem; the values are the JSON shapes of the TypeScript props.
type NavItemProps struct {
	Selected any `json:"selected"`
	Level any `json:"level"`
	Href any `json:"href"`
	Interactive any `json:"interactive"`
	ReserveRight any `json:"reserveRight"`
	ContentClass any `json:"contentClass"`
	Class string `json:"className"`
	Icon templ.Component `json:"-"`
	Right templ.Component `json:"-"`
}

// A sidebar list item: icon, label and an optional right slot. With `href` it is a link, with `interactive` a button, otherwise a static box.
templ NavItem(p NavItemProps) {
	{{ vSelected := uiutils.Def(p.Selected, false) }}
	{{ vLevel := p.Level }}
	{{ vHref := p.Href }}
	{{ vInteractive := uiutils.Def(p.Interactive, false) }}
	{{ vReserveRight := uiutils.Def(p.ReserveRight, false) }}
	{{ vContentClass := p.ContentClass }}
	{{ vState := any(uiutils.Cond(uiutils.T(vSelected), "selected", "default")) }}
	{{ vOuter := any(uiutils.Cnv(recipeNavItem.Of(map[string]any{"level": vLevel, "state": vState}), "group in-data-[nav=collapsed]:justify-center", p.Class)) }}
	{{ vInner := any(uiutils.Cnv(recipeNavItemContent.Of(map[string]any{"level": vLevel, "state": vState}), uiutils.And(!uiutils.T(vSelected), "group-hover:bg-muted"), "in-data-[nav=collapsed]:w-10 in-data-[nav=collapsed]:flex-none in-data-[nav=collapsed]:justify-center in-data-[nav=collapsed]:px-0", vContentClass)) }}
	if uiutils.T(vHref) {
		@compose.LinkPlain(compose.LinkPlainProps{LinkProps: link.LinkProps{Href: uiutils.URL(vHref, false), Class: uiutils.S(vOuter), AriaCurrent: uiutils.S(uiutils.Cond(uiutils.T(vSelected), "page", nil)), Attrs: uiutils.Attrs("data-slot", "nav-item", "data-state", vState)}}) {
			@NavItemRow(NavItemRowProps{Inner: vInner, ReserveRight: vReserveRight, Icon: p.Icon, Right: p.Right}) {
				{ children... }
			}
		}
	} else if uiutils.T(vInteractive) {
		@compose.ButtonPlain(compose.ButtonPlainProps{ButtonProps: button.ButtonProps{Type: "button", Class: uiutils.S(uiutils.Cnv(vOuter, "text-left")), Attrs: uiutils.Attrs("aria-current", uiutils.Cond(uiutils.T(vSelected), "page", nil), "data-slot", "nav-item", "data-state", vState)}}) {
			@NavItemRow(NavItemRowProps{Inner: vInner, ReserveRight: vReserveRight, Icon: p.Icon, Right: p.Right}) {
				{ children... }
			}
		}
	} else {
		@compose.Box(compose.BoxProps{BoxProps: box.BoxProps{Class: uiutils.S(vOuter), Attrs: uiutils.Attrs("aria-current", uiutils.Cond(uiutils.T(vSelected), "page", nil), "data-slot", "nav-item", "data-state", vState)}}) {
			@NavItemRow(NavItemRowProps{Inner: vInner, ReserveRight: vReserveRight, Icon: p.Icon, Right: p.Right}) {
				{ 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 nav-item
blocks/nav-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/nav-item.json
    A sidebar list item: icon, label and an optional right slot. With `href` it is a link, with `interactive` a button, otherwise a static box.
    Props (p): selected, level, href, interactive, reserveRight, contentClass, className, icon (slot), right (slot); children: markup. -#}
{%- set v_selected = (p.selected ?? false) -%}
{%- set v_level = (p.level ?? null) -%}
{%- set v_href = (p.href ?? null) -%}
{%- set v_interactive = (p.interactive ?? false) -%}
{%- set v_reserveRight = (p.reserveRight ?? false) -%}
{%- set v_contentClass = (p.contentClass ?? null) -%}
{%- set v_state = (og_t(v_selected) ? 'selected' : 'default') -%}
{%- set v_outer = og_cnv([og_recipe('blocks/recipes/nav-item.variants.json', {'level': v_level, 'state': v_state}), 'group in-data-[nav=collapsed]:justify-center', (p.className ?? '')]) -%}
{%- set v_inner = og_cnv([og_recipe('blocks/recipes/nav-item-content.variants.json', {'level': v_level, 'state': v_state}), og_and([not (og_t(v_selected)), 'group-hover:bg-muted']), 'in-data-[nav=collapsed]:w-10 in-data-[nav=collapsed]:flex-none in-data-[nav=collapsed]:justify-center in-data-[nav=collapsed]:px-0', v_contentClass]) -%}
{%- if og_t(v_href) %}
  {%- set c_2 -%}
    {%- set c_1 -%}
      {{ og_slot((children ?? null))|raw }}
    {%- endset -%}
    {{ include('blocks/nav-item-row.html.twig', {'p': {'inner': v_inner, 'reserveRight': v_reserveRight, 'icon': (p.icon ?? null), 'right': (p.right ?? null)}, 'children': og_markup(c_1)}, with_context = false) }}
  {%- endset -%}
  {{ include('compose/LinkPlain.html.twig', {'href': og_url(og_s(v_href)), 'class': og_s(v_outer), 'ariaCurrent': og_s((og_t(v_selected) ? 'page' : null)), 'attrs': og_attrs(['data-slot', 'nav-item', 'data-state', v_state]), 'children': c_2}, with_context = false) }}
{%- elseif og_t(v_interactive) %}
  {%- set c_4 -%}
    {%- set c_3 -%}
      {{ og_slot((children ?? null))|raw }}
    {%- endset -%}
    {{ include('blocks/nav-item-row.html.twig', {'p': {'inner': v_inner, 'reserveRight': v_reserveRight, 'icon': (p.icon ?? null), 'right': (p.right ?? null)}, 'children': og_markup(c_3)}, with_context = false) }}
  {%- endset -%}
  {{ include('compose/ButtonPlain.html.twig', {'type': 'button', 'class': og_s(og_cnv([v_outer, 'text-left'])), 'attrs': og_attrs(['aria-current', (og_t(v_selected) ? 'page' : null), 'data-slot', 'nav-item', 'data-state', v_state]), 'children': c_4}, with_context = false) }}
{%- else %}
  {%- set c_6 -%}
    {%- set c_5 -%}
      {{ og_slot((children ?? null))|raw }}
    {%- endset -%}
    {{ include('blocks/nav-item-row.html.twig', {'p': {'inner': v_inner, 'reserveRight': v_reserveRight, 'icon': (p.icon ?? null), 'right': (p.right ?? null)}, 'children': og_markup(c_5)}, with_context = false) }}
  {%- endset -%}
  {{ include('compose/Box.html.twig', {'class': og_s(v_outer), 'attrs': og_attrs(['aria-current', (og_t(v_selected) ? 'page' : null), 'data-slot', 'nav-item', 'data-state', v_state]), 'children': c_6}, with_context = false) }}
{%- endif %}

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 nav-item
blocks/nav-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/nav-item.json
    A sidebar list item: icon, label and an optional right slot. With `href` it is a link, with `interactive` a button, otherwise a static box.
    Props (p): selected, level, href, interactive, reserveRight, contentClass, className, icon (slot), right (slot); children: markup. *}
{var $v_selected = ($p['selected'] ?? false)}
{var $v_level = ($p['level'] ?? null)}
{var $v_href = ($p['href'] ?? null)}
{var $v_interactive = ($p['interactive'] ?? false)}
{var $v_reserveRight = ($p['reserveRight'] ?? false)}
{var $v_contentClass = ($p['contentClass'] ?? null)}
{var $v_state = (og_t($v_selected) ? 'selected' : 'default')}
{var $v_outer = og_cnv([og_recipe('blocks/recipes/nav-item.variants.json', ['level' => $v_level, 'state' => $v_state]), 'group in-data-[nav=collapsed]:justify-center', ($p['className'] ?? '')])}
{var $v_inner = og_cnv([og_recipe('blocks/recipes/nav-item-content.variants.json', ['level' => $v_level, 'state' => $v_state]), og_and([!(og_t($v_selected)), 'group-hover:bg-muted']), 'in-data-[nav=collapsed]:w-10 in-data-[nav=collapsed]:flex-none in-data-[nav=collapsed]:justify-center in-data-[nav=collapsed]:px-0', $v_contentClass])}
{if og_t($v_href)}
  {capture $c_2}
    {capture $c_1}
      {=og_slot(($children ?? null))|noescape}
    {/capture}
    {=og_include('blocks/nav-item-row.latte', ['p' => ['inner' => $v_inner, 'reserveRight' => $v_reserveRight, 'icon' => ($p['icon'] ?? null), 'right' => ($p['right'] ?? null)], 'children' => og_markup($c_1)])|noescape}
  {/capture}
  {=og_include('compose/LinkPlain.latte', ['href' => og_url(og_s($v_href)), 'class' => og_s($v_outer), 'ariaCurrent' => og_s((og_t($v_selected) ? 'page' : null)), 'attrs' => og_attrs(['data-slot', 'nav-item', 'data-state', $v_state]), 'children' => (string) $c_2])|noescape}
{elseif og_t($v_interactive)}
  {capture $c_4}
    {capture $c_3}
      {=og_slot(($children ?? null))|noescape}
    {/capture}
    {=og_include('blocks/nav-item-row.latte', ['p' => ['inner' => $v_inner, 'reserveRight' => $v_reserveRight, 'icon' => ($p['icon'] ?? null), 'right' => ($p['right'] ?? null)], 'children' => og_markup($c_3)])|noescape}
  {/capture}
  {=og_include('compose/ButtonPlain.latte', ['type' => 'button', 'class' => og_s(og_cnv([$v_outer, 'text-left'])), 'attrs' => og_attrs(['aria-current', (og_t($v_selected) ? 'page' : null), 'data-slot', 'nav-item', 'data-state', $v_state]), 'children' => (string) $c_4])|noescape}
{else}
  {capture $c_6}
    {capture $c_5}
      {=og_slot(($children ?? null))|noescape}
    {/capture}
    {=og_include('blocks/nav-item-row.latte', ['p' => ['inner' => $v_inner, 'reserveRight' => $v_reserveRight, 'icon' => ($p['icon'] ?? null), 'right' => ($p['right'] ?? null)], 'children' => og_markup($c_5)])|noescape}
  {/capture}
  {=og_include('compose/Box.latte', ['class' => og_s($v_outer), 'attrs' => og_attrs(['aria-current', (og_t($v_selected) ? 'page' : null), 'data-slot', 'nav-item', 'data-state', $v_state]), 'children' => (string) $c_6])|noescape}
{/if}

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 nav-item
blocks/nav-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/nav-item.json
    A sidebar list item: icon, label and an optional right slot. With `href` it is a link, with `interactive` a button, otherwise a static box.
    Props (p): selected, level, href, interactive, reserveRight, contentClass, className, icon (slot), right (slot); children: markup. -#}
{%- set v_selected = og_def(p['selected'], false) -%}
{%- set v_level = og_def(p['level'], none) -%}
{%- set v_href = og_def(p['href'], none) -%}
{%- set v_interactive = og_def(p['interactive'], false) -%}
{%- set v_reserveRight = og_def(p['reserveRight'], false) -%}
{%- set v_contentClass = og_def(p['contentClass'], none) -%}
{%- set v_state = ('selected' if og_t(v_selected) else 'default') -%}
{%- set v_outer = og_cnv([og_recipe('blocks/recipes/nav-item.variants.json', {'level': v_level, 'state': v_state}), 'group in-data-[nav=collapsed]:justify-center', og_def(p['className'], '')]) -%}
{%- set v_inner = og_cnv([og_recipe('blocks/recipes/nav-item-content.variants.json', {'level': v_level, 'state': v_state}), og_and([not (og_t(v_selected)), 'group-hover:bg-muted']), 'in-data-[nav=collapsed]:w-10 in-data-[nav=collapsed]:flex-none in-data-[nav=collapsed]:justify-center in-data-[nav=collapsed]:px-0', v_contentClass]) -%}
{%- if og_t(v_href) %}
  {%- set c_2 -%}
    {%- set c_1 -%}
      {{ og_slot(og_def(children, none))|safe }}
    {%- endset -%}
    {{ og_include('blocks/nav-item-row.html.j2', {'p': {'inner': v_inner, 'reserveRight': v_reserveRight, 'icon': og_def(p['icon'], none), 'right': og_def(p['right'], none)}, 'children': og_markup(c_1)}) }}
  {%- endset -%}
  {{ og_include('compose/link-plain.html.j2', {'p': {'Href': og_url(og_s(v_href)), 'Class': og_s(v_outer), 'AriaCurrent': og_s(('page' if og_t(v_selected) else none)), 'Attrs': og_attrs(['data-slot', 'nav-item', 'data-state', v_state])}, 'children': c_2}) }}
{%- elif og_t(v_interactive) %}
  {%- set c_4 -%}
    {%- set c_3 -%}
      {{ og_slot(og_def(children, none))|safe }}
    {%- endset -%}
    {{ og_include('blocks/nav-item-row.html.j2', {'p': {'inner': v_inner, 'reserveRight': v_reserveRight, 'icon': og_def(p['icon'], none), 'right': og_def(p['right'], none)}, 'children': og_markup(c_3)}) }}
  {%- endset -%}
  {{ og_include('compose/button-plain.html.j2', {'p': {'Type': 'button', 'Class': og_s(og_cnv([v_outer, 'text-left'])), 'Attrs': og_attrs(['aria-current', ('page' if og_t(v_selected) else none), 'data-slot', 'nav-item', 'data-state', v_state])}, 'children': c_4}) }}
{%- else %}
  {%- set c_6 -%}
    {%- set c_5 -%}
      {{ og_slot(og_def(children, none))|safe }}
    {%- endset -%}
    {{ og_include('blocks/nav-item-row.html.j2', {'p': {'inner': v_inner, 'reserveRight': v_reserveRight, 'icon': og_def(p['icon'], none), 'right': og_def(p['right'], none)}, 'children': og_markup(c_5)}) }}
  {%- endset -%}
  {{ og_include('compose/box.html.j2', {'p': {'Class': og_s(v_outer), 'Attrs': og_attrs(['aria-current', ('page' if og_t(v_selected) else none), 'data-slot', 'nav-item', 'data-state', v_state])}, 'children': c_6}) }}
{%- endif %}

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 sidebar list item: icon, label and an optional right slot. With `href` it is a link, with `interactive` a button, otherwise a static box.

Props of NavItem
PropTypeDefault Description
selected booleanfalsethe current item
level "1" | "2" | "3" | "4"-indent level
href string-
interactive booleanfalserender a button (the click event is wired)
reserveRight booleanfalsekeep the empty 20 by 20 frame on the right when there is no right content
contentClass string-classes of the inner row