# Taskbar

Beveled taskbar with a Start button, a start menu built on dropdown-menu, a task area and a clock slot.

Source: https://www.ui95.dev/ui/taskbar

## Examples

```tsx
import { Taskbar, TaskbarClock, TaskbarTask } from "@/components/retro/taskbar"
import {
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuSub,
  DropdownMenuSubContent,
  DropdownMenuSubTrigger,
} from "@/components/ui/dropdown-menu"

import type { ShowcaseDemo } from "../types"

function renderStartMenu() {
  return (
    <>
      <DropdownMenuSub>
        <DropdownMenuSubTrigger>Programs</DropdownMenuSubTrigger>
        <DropdownMenuSubContent>
          <DropdownMenuItem>Paint</DropdownMenuItem>
          <DropdownMenuItem>Terminal</DropdownMenuItem>
        </DropdownMenuSubContent>
      </DropdownMenuSub>
      <DropdownMenuItem>Documents</DropdownMenuItem>
      <DropdownMenuItem>Settings</DropdownMenuItem>
      <DropdownMenuSeparator />
      <DropdownMenuItem>Shut Down...</DropdownMenuItem>
    </>
  )
}

const PROGRAMS = ["Paint", "Terminal", "Notepad", "Minesweeper", "Calculator", "Solitaire"] as const

function renderTasks(count: number) {
  return PROGRAMS.slice(0, count).map((program, index) => (
    <TaskbarTask key={program} isActive={index === 0}>
      {program}
    </TaskbarTask>
  ))
}

export const demo: ShowcaseDemo = {
  span: "wide",
  variants: [
    {
      label: "start menu, tasks, clock",
      render: () => <Taskbar startMenu={renderStartMenu()}>{renderTasks(2)}</Taskbar>,
    },
    {
      label: "3 visible tasks, rest scroll under the clock",
      render: () => (
        <Taskbar startMenu={renderStartMenu()} maxVisibleTasks={3}>
          {renderTasks(6)}
        </Taskbar>
      ),
    },
    {
      label: "custom start, no clock",
      render: () => (
        <Taskbar startMenu={renderStartMenu()} startLabel="Menu" startMenuTitle="guest" clock={null}>
          {renderTasks(2)}
        </Taskbar>
      ),
    },
    {
      label: "clock only",
      render: () => <Taskbar clock={<TaskbarClock />} />,
    },
  ],
}
```

## Installation

```bash
npx shadcn@latest add https://www.ui95.dev/r/taskbar.json
```

Run it in a Radix shadcn project that already has the UI95 theme.

Dependencies:

```bash
npm install cn
```

Add these items first:

- [button](https://www.ui95.dev/ui/button)
- [dropdown-menu](https://www.ui95.dev/ui/dropdown-menu)

`components/retro/taskbar.tsx`

```tsx
"use client"

import { cn } from "cn"
import * as React from "react"

import { Button } from "@/components/ui/button"
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"

type TaskbarProps = React.ComponentProps<"footer"> & {
  startMenu?: React.ReactNode
  startIcon?: React.ReactNode
  startLabel?: string
  startMenuTitle?: string
  clock?: React.ReactNode
  maxVisibleTasks?: number
}

const DEFAULT_MAX_VISIBLE_TASKS = 6

const CLOCK_REFRESH_MS = 5000

function subscribeClock(onChange: () => void) {
  const interval = window.setInterval(onChange, CLOCK_REFRESH_MS)
  return () => window.clearInterval(interval)
}

function readClock() {
  return new Date().toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" })
}

function readServerClock() {
  return "--:--"
}

function TaskbarClock({ className, ...props }: React.ComponentProps<"span">) {
  const time = React.useSyncExternalStore(subscribeClock, readClock, readServerClock)

  return (
    <span
      data-slot="taskbar-clock"
      className={cn("flex h-7 items-center bevel-sunken offset-shadow-none px-3 text-xl leading-none", className)}
      {...props}
    >
      <time>{time}</time>
    </span>
  )
}

function TaskbarStart({
  icon,
  label,
  title,
  children,
}: {
  icon?: React.ReactNode
  label: string
  title: string
  children: React.ReactNode
}) {
  return (
    <DropdownMenu>
      <DropdownMenuTrigger asChild>
        <Button data-slot="taskbar-start" variant="default" size="sm">
          {icon}
          {label}
        </Button>
      </DropdownMenuTrigger>
      <DropdownMenuContent
        side="top"
        align="start"
        sideOffset={6}
        className="flex min-w-56 p-0"
      >
        <span
          aria-hidden="true"
          className="flex w-8 shrink-0 items-end justify-center bg-titlebar py-2 font-pixel text-[16px] leading-none text-titlebar-foreground uppercase [writing-mode:vertical-rl] rotate-180"
        >
          {title}
        </span>
        <div className="flex min-w-0 flex-1 flex-col p-(--pixel)">{children}</div>
      </DropdownMenuContent>
    </DropdownMenu>
  )
}

type TaskbarTaskProps = React.ComponentProps<"button"> & {
  isActive?: boolean
  icon?: React.ReactNode
}

function TaskbarTask({ isActive = false, icon, className, children, ...props }: TaskbarTaskProps) {
  return (
    <button
      type="button"
      data-slot="taskbar-task"
      data-active={isActive}
      aria-pressed={isActive}
      className={cn(
        "relative flex h-8 min-w-0 shrink-0 basis-[calc((100%-(var(--taskbar-tasks)-1)*var(--pixel)*2)/var(--taskbar-tasks))] items-center gap-2 pixel-border border-border bg-secondary px-2 text-left text-xl leading-none text-secondary-foreground bevel-raised offset-shadow-none select-none data-[active=true]:bevel-sunken [&_svg]:shrink-0",
        className
      )}
      {...props}
    >
      {isActive && (
        <span aria-hidden="true" className="pointer-events-none absolute inset-(--pixel) bg-card dither-50" />
      )}
      {icon && <span className="relative flex">{icon}</span>}
      <span className="relative truncate">{children}</span>
    </button>
  )
}

function scrollTasksSideways(event: React.WheelEvent<HTMLDivElement>) {
  if (Math.abs(event.deltaY) > Math.abs(event.deltaX)) {
    event.currentTarget.scrollLeft += event.deltaY
  }
}

function Taskbar({
  startMenu,
  startIcon,
  startLabel = "Start",
  startMenuTitle = "ui95",
  clock = <TaskbarClock />,
  maxVisibleTasks = DEFAULT_MAX_VISIBLE_TASKS,
  className,
  children,
  ...props
}: TaskbarProps) {
  return (
    <footer
      data-slot="taskbar"
      className={cn(
        "flex h-11 items-center gap-2 border-t-(length:--pixel) border-border bg-secondary px-2 text-secondary-foreground bevel-raised offset-shadow-none",
        className
      )}
      {...props}
    >
      {startMenu !== undefined && (
        <TaskbarStart icon={startIcon} label={startLabel} title={startMenuTitle}>
          {startMenu}
        </TaskbarStart>
      )}
      <div
        data-slot="taskbar-tasks"
        style={{ "--taskbar-tasks": maxVisibleTasks } as React.CSSProperties}
        className="flex min-w-0 flex-1 items-center gap-[calc(var(--pixel)*2)] overflow-x-auto overscroll-x-contain [scrollbar-width:none] [&::-webkit-scrollbar]:hidden"
        onWheel={scrollTasksSideways}
      >
        {children}
      </div>
      {clock}
    </footer>
  )
}

export { Taskbar, TaskbarClock, type TaskbarProps, TaskbarTask, type TaskbarTaskProps }
```

## API reference

### Taskbar

| Prop | Type | Default |
| --- | --- | --- |
| `startMenu` | `React.ReactNode` | - |
| `startIcon` | `React.ReactNode` | - |
| `startLabel` | `string` | `"Start"` |
| `startMenuTitle` | `string` | `"ui95"` |
| `clock` | `React.ReactNode` | `<TaskbarClock />` |
| `maxVisibleTasks` | `number` | `DEFAULT_MAX_VISIBLE_TASKS` |

### TaskbarClock

No props of its own. It forwards the props of the element or primitive it wraps.

### TaskbarTask

| Prop | Type | Default |
| --- | --- | --- |
| `isActive` | `boolean` | `false` |
| `icon` | `React.ReactNode` | - |

## Sounds

- `taskbar.press`
- `taskbar.release`
