Skip to content
ui8kit

component / components

Cart Count

Cart Count component

Install

npx ui8kit add cart-count

Pulls in: compose, inline, kit-text, lucide-icon, utils

Preview

In the cart: 2

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 cart-count
components/cart-count.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. -->
<!--
  The cart count as a live status (role status, aria-live polite): a bag icon and the words with the number. The
  page updates the number; screen readers announce it.
-->
<script lang="ts" module>
  import type { Snippet } from "svelte";

  export type CartCountProps = {
    count?: number;
    label: string;
  };
</script>

<script lang="ts">
  import Inline from "../compose/Inline.svelte";
  import LucideIcon from "../icons/LucideIcon.svelte";
  import { tpl } from "../utils";

  let { count = 0, label }: CartCountProps = $props();
</script>

<Inline
  class="inline-flex h-8 items-center gap-2 rounded bg-muted px-4 text-sm font-medium"
  role="status"
  aria-live="polite"
  data-slot="cart-count"
>
  <LucideIcon name="shopping-bag" size={16} />
  <Inline>
    {tpl(label, (count == null ? undefined : String(count)))}
  </Inline>
</Inline>

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 cart-count
components/cart-count.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.
/**
 * The cart count as a live status (role status, aria-live polite): a bag icon and the words with the number. The
 * page updates the number; screen readers announce it.
 */
import { type ReactNode } from "react";
import { Inline } from "../compose";
import { LucideIcon } from "../icons/LucideIcon";
import { tpl } from "../utils";

export type CartCountProps = {
  count?: number;
  label: string;
};

export function CartCount({ count = 0, label }: CartCountProps) {
  return (
    <Inline
      className="inline-flex h-8 items-center gap-2 rounded bg-muted px-4 text-sm font-medium"
      role="status"
      aria-live="polite"
      data-slot="cart-count"
    >
      <LucideIcon name="shopping-bag" size={16} />
      <Inline>
        {tpl(label, (count == null ? undefined : String(count)))}
      </Inline>
    </Inline>
  );
}

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 cart-count
components/cart-count.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. -->
<!--
  The cart count as a live status (role status, aria-live polite): a bag icon and the words with the number. The
  page updates the number; screen readers announce it.
-->
<script lang="ts">
export type CartCountProps = {
  count?: number;
  label: string;
};
</script>

<script setup lang="ts">
import Inline from "../compose/Inline.vue";
import LucideIcon from "../icons/LucideIcon.vue";
import { tpl } from "../utils";

const { count = 0, label } = defineProps<CartCountProps>();
</script>

<template>
  <Inline
    class="inline-flex h-8 items-center gap-2 rounded bg-muted px-4 text-sm font-medium"
    role="status"
    aria-live="polite"
    data-slot="cart-count"
  >
    <LucideIcon name="shopping-bag" :size="16" />
    <Inline>
      {{ tpl(label, (count == null ? undefined : String(count))) }}
    </Inline>
  </Inline>
</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 cart-count
components/cart-count.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.
/**
 * The cart count as a live status (role status, aria-live polite): a bag icon and the words with the number. The
 * page updates the number; screen readers announce it.
 */
import { type JSX } from "solid-js";
import { Inline } from "../compose";
import { LucideIcon } from "../icons/LucideIcon";
import { tpl } from "../utils";

export type CartCountProps = {
  count?: number;
  label: string;
};

export function CartCount({ count = 0, label }: CartCountProps) {
  return (
    <Inline
      class="inline-flex h-8 items-center gap-2 rounded bg-muted px-4 text-sm font-medium"
      role="status"
      aria-live="polite"
      data-slot="cart-count"
    >
      <LucideIcon name="shopping-bag" size={16} />
      <Inline>
        {tpl(label, (count == null ? undefined : String(count)))}
      </Inline>
    </Inline>
  );
}

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 cart-count
blocks/cart-count.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/organisms/cart-count.json

package blocks

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

// CartCountProps are the props of CartCount; the values are the JSON shapes of the TypeScript props.
type CartCountProps struct {
	Count any `json:"count"`
	Label any `json:"label"`
}

// The cart count as a live status (role status, aria-live polite): a bag icon and the words with the number. The page updates the number; screen readers announce it.
templ CartCount(p CartCountProps) {
	{{ vCount := uiutils.Def(p.Count, float64(0)) }}
	{{ vLabel := p.Label }}
	@compose.Inline(compose.InlineProps{TextProps: text.TextProps{Class: "inline-flex h-8 items-center gap-2 rounded bg-muted px-4 text-sm font-medium", Attrs: uiutils.Attrs("role", "status", "aria-live", "polite", "data-slot", "cart-count")}}) {
		@icons.LucideIcon(icons.LucideIconProps{Name: "shopping-bag", Size: 16})
		@compose.Inline(compose.InlineProps{TextProps: text.TextProps{}}) {
			{ uiutils.Text(uiutils.Tpl(vLabel, uiutils.Str(vCount))) }
		}
	}
}

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 cart-count
blocks/cart-count.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/organisms/cart-count.json
    The cart count as a live status (role status, aria-live polite): a bag icon and the words with the number. The page updates the number; screen readers announce it.
    Props (p): count, label. -#}
{%- set v_count = (p.count ?? 0) -%}
{%- set v_label = (p.label ?? null) -%}
{%- set c_2 -%}
  {{ include('icons/LucideIcon.html.twig', {'name': 'shopping-bag', 'size': 16}, with_context = false) }}
  {%- set c_1 -%}
    {{ og_text(og_tpl(v_label, [og_str(v_count)])) }}
  {%- endset -%}
  {{ include('compose/Inline.html.twig', {'children': c_1}, with_context = false) }}
{%- endset -%}
{{ include('compose/Inline.html.twig', {'class': 'inline-flex h-8 items-center gap-2 rounded bg-muted px-4 text-sm font-medium', 'attrs': og_attrs(['role', 'status', 'aria-live', 'polite', 'data-slot', 'cart-count']), 'children': c_2}, 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 cart-count
blocks/cart-count.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/organisms/cart-count.json
    The cart count as a live status (role status, aria-live polite): a bag icon and the words with the number. The page updates the number; screen readers announce it.
    Props (p): count, label. *}
{var $v_count = ($p['count'] ?? 0)}
{var $v_label = ($p['label'] ?? null)}
{capture $c_2}
  {=og_include('icons/LucideIcon.latte', ['name' => 'shopping-bag', 'size' => 16])|noescape}
  {capture $c_1}
    {=og_text(og_tpl($v_label, [og_str($v_count)]))}
  {/capture}
  {=og_include('compose/Inline.latte', ['children' => (string) $c_1])|noescape}
{/capture}
{=og_include('compose/Inline.latte', ['class' => 'inline-flex h-8 items-center gap-2 rounded bg-muted px-4 text-sm font-medium', 'attrs' => og_attrs(['role', 'status', 'aria-live', 'polite', 'data-slot', 'cart-count']), 'children' => (string) $c_2])|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 cart-count
blocks/cart-count.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/organisms/cart-count.json
    The cart count as a live status (role status, aria-live polite): a bag icon and the words with the number. The page updates the number; screen readers announce it.
    Props (p): count, label. -#}
{%- set v_count = og_def(p['count'], 0) -%}
{%- set v_label = og_def(p['label'], none) -%}
{%- set c_2 -%}
  {{ og_include('icons/lucide-icon.html.j2', {'p': {'Name': 'shopping-bag', 'Size': 16}}) }}
  {%- set c_1 -%}
    {{ og_text(og_tpl(v_label, [og_str(v_count)])) }}
  {%- endset -%}
  {{ og_include('compose/inline.html.j2', {'p': {}, 'children': c_1}) }}
{%- endset -%}
{{ og_include('compose/inline.html.j2', {'p': {'Class': 'inline-flex h-8 items-center gap-2 rounded bg-muted px-4 text-sm font-medium', 'Attrs': og_attrs(['role', 'status', 'aria-live', 'polite', 'data-slot', 'cart-count'])}, 'children': c_2}) }}

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.

The cart count as a live status (role status, aria-live polite): a bag icon and the words with the number. The page updates the number; screen readers announce it.

Props of CartCount
PropTypeDefault Description
count number0
label (required)string-the words with {0} for the number

Used in