Skip to content
2k
On This Page

Delta Bar

Bar to show negative or positive deviation from a particular threshold.

Revenue is trending above target

Churn improved versus last month

Pipeline is on plan

import { DeltaBar } from "~/components/ui/delta-bar"

export default function DeltaBarDemo() {
  return (
    <div class="grid w-full max-w-sm gap-4">
      <div class="grid gap-2">
        <p class="text-sm font-medium">Revenue is trending above target</p>
        <DeltaBar value={58} />
      </div>
      <div class="grid gap-2">
        <p class="text-sm font-medium">Churn improved versus last month</p>
        <DeltaBar value={-34} />
      </div>
      <div class="grid gap-2">
        <p class="text-sm font-medium">Pipeline is on plan</p>
        <DeltaBar value={0} />
      </div>
    </div>
  )
}

Installation

pnpm dlx solidui-cli@latest add delta-bar

Copy and paste the following code into your project.

components/ui/delta-bar.tsx
import { type ComponentProps } from "@solidjs/web"
import { merge, omit, Show } from "solid-js"

import { cn } from "~/lib/utils"

type DeltaBarProps = ComponentProps<"div"> & {
  value: number
  isIncreasePositive?: boolean
}

const DeltaBar = (rawProps: DeltaBarProps) => {
  const local = merge({ isIncreasePositive: true }, rawProps)
  const others = omit(rawProps, "class", "isIncreasePositive", "value")
  const value = () => Math.max(-100, Math.min(100, local.value))
  const width = () => `${Math.abs(value())}%`
  const tone = () => {
    if (value() === 0) return undefined
    return (value() > 0 && local.isIncreasePositive) ||
      (value() < 0 && !local.isIncreasePositive)
      ? "positive"
      : "negative"
  }
  const indicatorToneClass = () =>
    tone() === "positive"
      ? "bg-green-500 dark:bg-green-400"
      : "bg-red-500 dark:bg-red-400"
  return (
    <div
      class={cn("h-1 rounded-full bg-muted relative flex w-full items-center", local.class)}
      data-slot="delta-bar"
      role="meter"
      {...others}
    >
      <div class="flex h-full w-1/2 justify-end" data-slot="delta-bar-decrease">
        <Show when={value() < 0}>
          <div
            class={cn(
              "rounded-l-full h-full",
              indicatorToneClass()
            )}
            data-side="decrease"
            data-slot="delta-bar-indicator"
            data-tone={tone()}
            style={{ width: width() }}
          />
        </Show>
      </div>
      <div
        class={cn(
          "h-2 w-0.5 shrink-0 rounded-full ring-1 ring-background z-10",
          value() === 0 ? "bg-muted" : indicatorToneClass()
        )}
        data-slot="delta-bar-marker"
        data-tone={tone()}
      />
      <div
        class="flex h-full w-1/2 justify-start"
        data-slot="delta-bar-increase"
      >
        <Show when={value() > 0}>
          <div
            class={cn(
              "rounded-r-full h-full",
              indicatorToneClass()
            )}
            data-side="increase"
            data-slot="delta-bar-indicator"
            data-tone={tone()}
            style={{ width: width() }}
          />
        </Show>
      </div>
    </div>
  )
}

export { DeltaBar, type DeltaBarProps }

Update the import paths to match your project setup.

Usage

import { DeltaBar } from "~/components/ui/delta-bar"
<DeltaBar value={42} />

Examples

Inverted Semantics

Use isIncreasePositive={false} when increases should be shown as unfavorable, like latency or cost.

Response time increased

Error rate decreased

import { DeltaBar } from "~/components/ui/delta-bar"

export default function DeltaBarInverted() {
  return (
    <div class="grid w-full max-w-sm gap-4">
      <div class="grid gap-2">
        <p class="text-sm font-medium">Response time increased</p>
        <DeltaBar value={42} isIncreasePositive={false} />
      </div>
      <div class="grid gap-2">
        <p class="text-sm font-medium">Error rate decreased</p>
        <DeltaBar value={-27} isIncreasePositive={false} />
      </div>
    </div>
  )
}

Custom Colors

Use data-tone="positive|negative" selectors from the root class to override the default indicator and marker colors.

Bookings are outperforming forecast

Refunds are trending above plan

import { DeltaBar } from "~/components/ui/delta-bar"

export default function DeltaBarCustomColors() {
  return (
    <div class="grid w-full max-w-sm gap-4">
      <div class="grid gap-2">
        <p class="text-sm font-medium">Bookings are outperforming forecast</p>
        <DeltaBar
          value={63}
          class="**:data-[tone=negative]:bg-amber-500 **:data-[tone=positive]:bg-sky-500 dark:**:data-[tone=negative]:bg-amber-400 dark:**:data-[tone=positive]:bg-sky-400"
        />
      </div>
      <div class="grid gap-2">
        <p class="text-sm font-medium">Refunds are trending above plan</p>
        <DeltaBar
          value={-28}
          class="**:data-[tone=negative]:bg-amber-500 **:data-[tone=positive]:bg-sky-500 dark:**:data-[tone=negative]:bg-amber-400 dark:**:data-[tone=positive]:bg-sky-400"
        />
      </div>
    </div>
  )
}

API Reference

DeltaBar

PropTypeDefault
valuenumber-
isIncreasePositivebooleantrue
classstring-