{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "leaderboard-05",
  "description": "A ranked list that can be reversed and expanded to show every entry.",
  "registryDependencies": [
    "card",
    "button",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/country-flag.json",
    "https://www.dashboardblocks.com/r/radix/format.json",
    "https://www.dashboardblocks.com/r/radix/leaderboard.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/leaderboard/leaderboard-05.tsx",
      "content": "'use client'\n\nimport {\n  BlockReady,\n  BlockStateProvider,\n  BlockStateRows,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport { CountryFlag } from '@/registry/components/dashboardblocks/country-flag'\nimport {\n  Leaderboard,\n  LeaderboardBar,\n  LeaderboardItem,\n  LeaderboardLabel,\n  LeaderboardRank,\n  LeaderboardValue,\n} from '@/registry/components/dashboardblocks/leaderboard'\nimport { IconPlaceholder } from '@/registry/icons/icon-placeholder'\nimport { formatNumber, formatPercent } from '@/registry/lib/dashboardblocks/format'\nimport { useState } from 'react'\n\nimport { Button } from '@/components/ui/button'\nimport { Card, CardContent } from '@/components/ui/card'\n\nimport { cn } from '@/lib/utils'\n\ninterface CountryStat {\n  /** ISO 3166-1 alpha-2 code, such as `GB`. */\n  code: string\n  name: string\n  visitors: number\n}\n\ninterface Leaderboard5Props extends BlockStateProps {\n  /** Leave out, or pass none, to show the empty state. */\n  countries?: CountryStat[]\n  /** Defaults to a sentence about the leading country and the top three. */\n  description?: string\n  initialCount?: number\n  title: string\n}\n\nconst exampleProps: Leaderboard5Props = {\n  countries: [\n    { code: 'US', name: 'United States', visitors: 24_318 },\n    { code: 'DE', name: 'Germany', visitors: 11_204 },\n    { code: 'GB', name: 'United Kingdom', visitors: 9_876 },\n    { code: 'IN', name: 'India', visitors: 7_452 },\n    { code: 'FR', name: 'France', visitors: 5_931 },\n    { code: 'BR', name: 'Brazil', visitors: 4_207 },\n    { code: 'JP', name: 'Japan', visitors: 3_618 },\n    { code: 'CA', name: 'Canada', visitors: 2_944 },\n  ],\n  initialCount: 5,\n  title: 'Top countries',\n}\n\nfunction summarize(ranked: CountryStat[]) {\n  const total = ranked.reduce((sum, country) => sum + country.visitors, 0)\n  const [first] = ranked\n  if (!first || total === 0) return 'No visitors yet.'\n  const share = (visitors: number) =>\n    formatPercent(visitors / total, { maximumFractionDigits: 0 })\n  const lead = `${first.name} sent ${share(first.visitors)} of ${formatNumber(total)} visitors`\n  if (ranked.length <= 3) return `${lead}.`\n  const top = ranked.slice(0, 3).reduce((sum, country) => sum + country.visitors, 0)\n  return `${lead}, and the top 3 countries ${share(top)}.`\n}\n\nconst pressScale =\n  'transition-[color,background-color,border-color,box-shadow,scale,translate] duration-150 ease-out active:scale-[0.96]'\n\nconst swapTransition =\n  'transition-[opacity,filter,scale] duration-300 ease-[cubic-bezier(0.2,0,0,1)] motion-reduce:transition-none'\n\nfunction IconSwap({\n  active,\n  activeIcon,\n  className,\n  inactiveIcon,\n  ...props\n}: {\n  active: boolean\n  activeIcon: React.ReactNode\n  className?: string\n  'data-icon'?: 'inline-end' | 'inline-start'\n  inactiveIcon: React.ReactNode\n}) {\n  return (\n    <span aria-hidden='true' className={cn('relative inline-flex', className)} {...props}>\n      <span\n        className={cn(\n          'absolute inset-0 flex items-center justify-center',\n          swapTransition,\n          active\n            ? 'scale-100 opacity-100 blur-[0px]'\n            : 'scale-[0.25] opacity-0 blur-[4px]',\n        )}\n      >\n        {activeIcon}\n      </span>\n      <span\n        className={cn(\n          'flex',\n          swapTransition,\n          active\n            ? 'scale-[0.25] opacity-0 blur-[4px]'\n            : 'scale-100 opacity-100 blur-[0px]',\n        )}\n      >\n        {inactiveIcon}\n      </span>\n    </span>\n  )\n}\n\nconst Leaderboard5 = (props: Leaderboard5Props) => {\n  const { countries = [], initialCount = 5, errorMessage, status, title } = props\n  const state = getBlockState(status, countries)\n  const [ascending, setAscending] = useState(false)\n  const [expanded, setExpanded] = useState(false)\n  const [skipBarAnimation, setSkipBarAnimation] = useState(false)\n\n  const ranked = [...countries].sort((a, b) => b.visitors - a.visitors)\n  const total = ranked.reduce((sum, country) => sum + country.visitors, 0) || 1\n  const max = ranked[0]?.visitors || 1\n  const ordered = ascending ? [...ranked].reverse() : ranked\n  const visible = expanded ? ordered : ordered.slice(0, initialCount)\n  const canExpand = !state && countries.length > initialCount\n  const { description = summarize(ranked) } = props\n\n  return (\n    <Card>\n      <CardContent>\n        <BlockStateProvider errorMessage={errorMessage} state={state}>\n          <section className='@container flex flex-col gap-4'>\n            <header className='flex items-start justify-between gap-3'>\n              <div className='flex min-w-0 flex-col gap-0.5'>\n                <h2 className='font-semibold tracking-tight'>{title}</h2>\n                <BlockReady className='text-sm'>\n                  <p className='text-muted-foreground text-sm'>{description}</p>\n                </BlockReady>\n              </div>\n              <Button\n                variant='ghost'\n                size='icon-sm'\n                aria-label={\n                  ascending ? 'Sort by most visitors' : 'Sort by fewest visitors'\n                }\n                disabled={state !== undefined}\n                onClick={() => {\n                  setSkipBarAnimation(true)\n                  setAscending((value) => !value)\n                }}\n                className={pressScale}\n              >\n                <IconSwap\n                  active={ascending}\n                  activeIcon={\n                    <IconPlaceholder\n                      lucide='ArrowUpNarrowWideIcon'\n                      tabler='IconSortAscending'\n                      hugeicons='SortByUp01Icon'\n                      phosphor='SortAscendingIcon'\n                      remixicon='RiSortAsc'\n                    />\n                  }\n                  inactiveIcon={\n                    <IconPlaceholder\n                      lucide='ArrowDownWideNarrowIcon'\n                      tabler='IconSortDescending'\n                      hugeicons='SortByDown01Icon'\n                      phosphor='SortDescendingIcon'\n                      remixicon='RiSortDesc'\n                    />\n                  }\n                />\n              </Button>\n            </header>\n            <div className='flex flex-col gap-2 border-t pt-4'>\n              <BlockStateRows rows={initialCount} />\n              {!state && (\n                <Leaderboard>\n                  {visible.map((country, index) => {\n                    const share = country.visitors / total\n                    return (\n                      <LeaderboardItem key={country.code}>\n                        <LeaderboardBar\n                          animated={!skipBarAnimation}\n                          className={\n                            country === ranked[0]\n                              ? 'bg-[color-mix(in_oklab,var(--color-chart-2)_32%,transparent)]'\n                              : undefined\n                          }\n                          delay={index * 100}\n                          value={(country.visitors / max) * 100}\n                        />\n                        <LeaderboardRank rank={ranked.indexOf(country) + 1} />\n                        <CountryFlag code={country.code} />\n                        <LeaderboardLabel className='truncate'>\n                          {country.name}\n                        </LeaderboardLabel>\n                        <span className='shrink-0 text-xs text-muted-foreground tabular-nums'>\n                          {formatPercent(share, {\n                            maximumFractionDigits: 1,\n                            minimumFractionDigits: 1,\n                          })}\n                        </span>\n                        <LeaderboardValue className='w-14'>\n                          {formatNumber(country.visitors)}\n                        </LeaderboardValue>\n                      </LeaderboardItem>\n                    )\n                  })}\n                </Leaderboard>\n              )}\n              {canExpand && (\n                <Button\n                  variant='ghost'\n                  size='sm'\n                  aria-expanded={expanded}\n                  onClick={() => {\n                    setSkipBarAnimation(true)\n                    setExpanded((value) => !value)\n                  }}\n                  className={cn('self-start', pressScale)}\n                >\n                  {expanded ? 'Show less' : `Show all ${countries.length}`}\n                  <IconSwap\n                    active={expanded}\n                    activeIcon={\n                      <IconPlaceholder\n                        lucide='ChevronUpIcon'\n                        tabler='IconChevronUp'\n                        hugeicons='ArrowUp01Icon'\n                        phosphor='CaretUpIcon'\n                        remixicon='RiArrowUpSLine'\n                      />\n                    }\n                    data-icon='inline-end'\n                    inactiveIcon={\n                      <IconPlaceholder\n                        lucide='ChevronDownIcon'\n                        tabler='IconChevronDown'\n                        hugeicons='ArrowDownIcon'\n                        phosphor='CaretDownIcon'\n                        remixicon='RiArrowDownSLine'\n                      />\n                    }\n                  />\n                </Button>\n              )}\n            </div>\n          </section>\n        </BlockStateProvider>\n      </CardContent>\n    </Card>\n  )\n}\n\nexport {\n  Leaderboard5,\n  exampleProps as leaderboard5ExampleProps,\n  type CountryStat,\n  type Leaderboard5Props,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/leaderboard/leaderboard-05.tsx"
    }
  ],
  "meta": {
    "shows": "A ranked list that can be reversed and expanded to show every entry.",
    "size": "half",
    "usedWith": [
      "leaderboard-01",
      "data-table-01",
      "breakdown-01"
    ],
    "component": "Leaderboard5",
    "props": "{ countries?: { code: string; name: string; visitors: number }[]; description?: string; initialCount?: number; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
