Skip to content
Star

Set limit

A monthly agent budget with a draggable cap and a forecast. Free and MIT licensed, as HTML, a typed React component or an Astro component, in three styles with light and dark mode.

$npx designme add set-limit
$npx shadcn@latest add https://designme.dev/r/shadcn/set-limit.json

Need this designed into your product? Book a call

Monthly agent budgetResets Nov 1
Spent $128Forecast $184
01

Install

Add it to your project with the CLI.

HTML, CSS and JS

$npx designme add set-limit

React

$npx designme add set-limit --react

Astro

$npx designme add set-limit --astro

shadcn, React

$npx shadcn@latest add https://designme.dev/r/shadcn/set-limit.json

shadcn, Astro

$npx shadcn@latest add https://designme.dev/r/shadcn/astro/set-limit.json

The CLI copies the source into your project, so you own it and can change anything. Where each version lands, what it needs and the shadcn theme bridge: Installation.

02

About

What it is for and what it does.

Set limit is a budget card for capping what an agent can spend in a month. A bar shows what has been spent so far and where spending is forecast to land, and the cap sits in the head as a labelled field ("Cap $170"): the person types a new figure there or drags the knob along the bar. A line under the bar warns when the forecast will pass the cap, and a switch in the foot, Pause runs at the cap, decides whether runs pause there or keep going past it. It is on by default.

  • When agent runs cost money and a team needs a monthly ceiling
  • When people should see whether their current pace will go over before it happens
  • When the team must choose what the agent does at the cap: pause, or keep going past it
  • When a cap needs to be adjusted quickly by typing an exact amount or dragging the bar
03

Usage

Import it in React and drive it from your own state, or place it in an Astro page.

React

import { SetLimit } from '@/components/designme/set-limit/SetLimit';

React example

The demo above as a React component, 23 lines you can copy.

'use client';
// The animated preview from designme.dev, rebuilt from the React component: an example of driving it from your own state.
import { useRef, useState } from 'react';
import { SetLimit } from './SetLimit';
import { useStory } from '../lib/useScript';

/* ---------- Set limit: the cap eases down from $200 to $170, past the forecast, and the warning comes in, unless someone
   drags the cap or types a budget first ---------- */
export default function SetLimitDemo() {
  const [cap, setCap] = useState(200);
  const ref = useRef<HTMLDivElement>(null);
  useStory(ref, async ({ wait }) => {
    await wait(1500);
    const t0 = performance.now();
    for (let p = 0; p < 1;) {
      await wait(16);
      p = Math.min(1, (performance.now() - t0) / 900);
      setCap(Math.round((200 - 30 * (1 - Math.pow(1 - p, 3))) / 10) * 10);
    }
  });
  return <div ref={ref} style={{ width: '100%' }}><SetLimit value={cap} onValueChange={setCap} /></div>;
}

Astro

In a page in src/pages. It plays the demo above; change its set-limit.js to show your own data. Attributes like class go on its root element.

---
import SetLimit from '../components/designme/set-limit/SetLimit.astro';
---
<SetLimit />
<SetLimit data-style="soft" class="my-class" />
04

Props

Everything SetLimit takes.

PropTypeWhat it does
valuenumberThe cap. Leave it out to let the slider set it
defaultValuenumber
onValueChange(cap: number) => void
minnumberThe lowest cap the slider allows
maxnumberThe highest cap the slider allows
stepnumberHow far one arrow key press or slider notch moves the cap
spentnumberSpent so far this period, the solid part of the bar
forecastnumberWhere spending is headed by the end of the period, the striped part of the bar
pauseAtCapbooleanWhether runs pause at the cap (the switch in the foot); off, they keep going past it. Leave it out to let the switch set it
defaultPauseAtCapbooleanOn by default
onPauseAtCapChange(pause: boolean) => void
pauseLabelstringThe switch's words, "Pause runs at the cap" by default
titlestring
subtitlestringThe quiet line under the title, like "Resets Nov 1"
passesOnstringWhen the forecast passes the cap, like "Oct 27"
format(n: number) => stringWrites an amount, like "$200"
capLabelstringThe word in front of the cap, "Cap" by default
editablebooleanShows the cap as a small field the person can type in (the default). Enter or leaving the field keeps it, held to min and max; Escape goes back
message(state: SetLimitMessage) => ReactNodeReplaces the line under the bar, which by default warns when the forecast passes the cap
classNamestring
05

Code

The full source, as the CLI installs it.

SetLimit.tsxReact · 155 lines
'use client';
import { useEffect, useRef, useState, type KeyboardEvent, type PointerEvent, type ReactNode } from 'react';
import { cx } from '../lib/cx';
import { useControllable, useTween } from '../lib/hooks';
import { Icon } from '../lib/icons';
import { useGrow } from '../lib/motion';
import '../designme.css';
import './set-limit.css';

export interface SetLimitMessage {
  /** True when the forecast goes past the cap */
  over: boolean;
  cap: number;
  /** The cap minus the forecast, what is left at the end of the period */
  left: number;
  /** True when runs pause at the cap, false when they keep going past it */
  pauseAtCap: boolean;
}

export interface SetLimitProps {
  /** The cap. Leave it out to let the slider set it */
  value?: number;
  defaultValue?: number;
  onValueChange?: (cap: number) => void;
  /** The lowest cap the slider allows */
  min?: number;
  /** The highest cap the slider allows */
  max?: number;
  /** How far one arrow key press or slider notch moves the cap */
  step?: number;
  /** Spent so far this period, the solid part of the bar */
  spent?: number;
  /** Where spending is headed by the end of the period, the striped part of the bar */
  forecast?: number;
  /** Whether runs pause at the cap (the switch in the foot); off, they keep going past it. Leave it out to let the switch set it */
  pauseAtCap?: boolean;
  /** On by default */
  defaultPauseAtCap?: boolean;
  onPauseAtCapChange?: (pause: boolean) => void;
  /** The switch's words, "Pause runs at the cap" by default */
  pauseLabel?: string;
  title?: string;
  /** The quiet line under the title, like "Resets Nov 1" */
  subtitle?: string;
  /** When the forecast passes the cap, like "Oct 27" */
  passesOn?: string;
  /** Writes an amount, like "$200" */
  format?: (n: number) => string;
  /** The word in front of the cap, "Cap" by default */
  capLabel?: string;
  /** Shows the cap as a small field the person can type in (the default). Enter or leaving the field keeps it, held to min and max; Escape goes back */
  editable?: boolean;
  /** Replaces the line under the bar, which by default warns when the forecast passes the cap */
  message?: (state: SetLimitMessage) => ReactNode;
  className?: string;
}

/** A spending cap on a bar that shows what is spent and what is forecast, with a switch for whether runs pause at the cap. */
export function SetLimit({ value, defaultValue = 200, onValueChange, min = 50, max = 500, step = 10, spent = 128, forecast = 184, pauseAtCap, defaultPauseAtCap = true, onPauseAtCapChange, pauseLabel = 'Pause runs at the cap',
  title = 'Monthly agent budget', subtitle = 'Resets Nov 1', passesOn = 'Oct 27', format = n => '$' + n, capLabel = 'Cap', editable = true, message, className }: SetLimitProps) {
  const [capRaw, setCap] = useControllable(value, defaultValue, onValueChange);
  const [pause, setPause] = useControllable(pauseAtCap, defaultPauseAtCap, onPauseAtCapChange);
  const cap = Math.max(min, Math.min(max, capRaw));
  const [drag, setDrag] = useState(false);
  // the bars fill in a moment after the card appears
  const [ready, setReady] = useState(false);
  useEffect(() => { const t = setTimeout(() => setReady(true), 200); return () => clearTimeout(t); }, []);
  const bar = useRef<HTMLDivElement>(null);
  const [outer, inner] = useGrow();
  // the knob and bars glide to a new cap, except while it is being dragged
  const shown = useTween(cap, drag ? 1 : 450);
  // the figure being typed, while the field has focus
  const [draft, setDraft] = useState<string | null>(null);
  const before = useRef(cap), cancel = useRef(false);
  const input = useRef<HTMLInputElement>(null);
  const clamp = (n: number) => Math.max(min, Math.min(max, Math.round(n)));
  const commit = () => {
    if (draft === null) return;
    const n = cancel.current || draft === '' ? before.current : clamp(parseInt(draft, 10));
    setCap(n); cancel.current = false; setDraft(null);
  };
  // the symbols around the number in `format`, like the "$" in "$200", stay put while the number is typed
  const text = format(cap), first = text.search(/\d/), last = text.length - [...text].reverse().join('').search(/\d/);
  const pre = first < 0 ? '' : text.slice(0, first), post = first < 0 ? '' : text.slice(last);

  const pct = (x: number) => (x - min) / (max - min) * 100;
  const snap = (x: number) => Math.max(min, Math.min(max, Math.round(x / step) * step));
  const at = (e: PointerEvent) => { const b = bar.current!.getBoundingClientRect(); setCap(snap(min + (max - min) * Math.max(0, Math.min(1, (e.clientX - b.left) / b.width)))); };
  const key = (e: KeyboardEvent) => {
    const dir = ({ ArrowRight: 1, ArrowUp: 1, ArrowLeft: -1, ArrowDown: -1 } as Record<string, number>)[e.key]; if (!dir) return;
    e.preventDefault(); setCap(Math.max(min, Math.min(max, cap + dir * step)));
  };

  const over = forecast > cap, left = cap - forecast;
  const used = ready ? Math.min(100, spent / shown * pct(shown)) : 0;
  const fc = ready ? Math.min(pct(shown), forecast / shown * pct(shown)) : 0;
  const figure = draft ?? String(Math.round(shown));
  const line = message ? message({ over, cap, left, pauseAtCap: pause })
    : over ? <span>Forecast passes your cap around {passesOn}. {pause ? 'Runs will pause.' : 'Runs will keep going past it.'}</span>
      : <span>On track. About {format(left)} left at month end.</span>;

  return (
    // the panel grows and shrinks with the line under the bar, staying centred where it is centred
    <div data-dm="" ref={outer} className={cx('dm-slx dm-panel dm-growing', className)}><div ref={inner} className="dm-grow">
      <div className="dm-slx-h">
        <div><b>{title}</b>{subtitle && <span className="dm-muted">{subtitle}</span>}</div>
        {/* the cap is labelled and, when it can change, is a small field with the currency in front (a click selects it all,
            so typing replaces it) */}
        <span className="dm-slx-cap"><span className="dm-muted" aria-hidden="true">{capLabel}</span>
        {editable ? (
          <label className="dm-num dm-slx-v" onPointerDown={e => { if (document.activeElement !== input.current) { e.preventDefault(); input.current?.focus(); } }}>
            {pre && <span className="dm-cur">{pre}</span>}<input ref={input} className="dm-slx-in" inputMode="numeric" autoComplete="off" aria-label={`${title}, ${capLabel.toLowerCase()}`} value={figure} style={{ width: Math.max(1, figure.length) + 'ch' }}
              onFocus={e => { before.current = cap; setDraft(String(cap)); e.currentTarget.select(); }}
              onChange={e => { const v = e.target.value.replace(/\D/g, '').slice(0, 6); setDraft(v); const n = +v; if (v && n >= min && n <= max) setCap(n); }}
              onKeyDown={e => {
                if (e.key === 'Enter') { e.preventDefault(); e.currentTarget.blur(); }
                else if (e.key === 'Escape') { cancel.current = true; e.currentTarget.blur(); }
                else if (e.key === 'ArrowUp' || e.key === 'ArrowDown') { e.preventDefault(); const n = clamp((parseInt(draft ?? '', 10) || cap) + (e.key === 'ArrowUp' ? step : -step)); setDraft(String(n)); setCap(n); }
              }}
              onBlur={commit} />{post}
          </label>
        ) : <b className="dm-num dm-slx-v">{format(cap)}</b>}
        </span>
      </div>
      <div ref={bar} className="dm-slx-bar"
        onPointerDown={e => { e.currentTarget.setPointerCapture(e.pointerId); setDrag(true); at(e); }}
        onPointerMove={e => { if (drag) at(e); }}
        onPointerUp={() => setDrag(false)} onPointerCancel={() => setDrag(false)}>
        <div className="dm-slx-used" style={{ width: used + '%' }} />
        <div className="dm-slx-fc" style={{ width: fc + '%' }} />
        {/* the cap it sets is the figure in the head, so the knob carries no amount of its own */}
        <button type="button" className={cx('dm-slx-k', drag && 'dm-drag')} role="slider" aria-label="Budget cap"
          aria-valuemin={min} aria-valuemax={max} aria-valuenow={cap} aria-valuetext={format(cap)} style={{ left: pct(shown) + '%' }} onKeyDown={key} />
      </div>
      <div className="dm-slx-lg dm-muted">
        <span><i style={{ background: 'var(--dm-accent-mark,var(--dm-accent))' }} />Spent <b className="dm-num">{format(spent)}</b></span>
        <span><i className="dm-fc" />Forecast <b className="dm-num">{format(forecast)}</b></span>
      </div>
      <div className="dm-slx-warn" aria-live="polite">
        {/* the line comes in again when it changes between on track and over, or the switch changes */}
        {/* on track: a neutral line with the green tick; over: a warning notice, a faint warning tint, the filled warning sign and
            the words in the text colour */}
        {ready && <span key={String(over) + pause} className={cx(over ? 'dm-w' : 'dm-o', 'dm-enter')}>
          {over ? <svg className="dm-slx-wi" viewBox="0 0 24 24" aria-hidden="true"><path className="dm-a" d="M12 3.6 2.6 19.9h18.8z" /><path className="dm-b" d="M12 9.6v4.4M12 17v.01" /></svg> : <Icon name="ok" size="xs" />}{line}
        </span>}
      </div>
      {/* the foot: one switch row, its words on the left and the switch on the right edge */}
      <label className="dm-slx-ft">
        <span>{pauseLabel}</span>
        <button type="button" className="dm-toggle dm-slx-tg" role="switch" aria-checked={pause} aria-label={pauseLabel} onClick={() => setPause(!pause)} />
      </label>
    </div></div>
  );
}
set-limit.cssReact · 53 lines
/* Generated from designme.dev by scripts/sync.mjs. Edit the site, then sync. */
/* ---------- Set limit ---------- */
.dm-slx{padding:var(--dm-s-4);display:flex;flex-direction:column;gap:14px;container:slx/inline-size}
/* the budget figure shares the title's line: the two sit on one baseline */
.dm-slx-h{display:flex;align-items:first baseline;justify-content:space-between}
.dm-slx-h>div{display:flex;flex-direction:column;font-size:14px}
.dm-slx-h b{font-weight:500}
.dm-slx-h .dm-muted{font-size:12px}
/* the cap is labelled and reads as a field at rest (owner, 28 Nov): "Cap" in the muted text, then a small field with its $ in
   front, white with the dimmed field edge, which steps up one grey while you type. A cap you can't change is the figure alone */
.dm-slx-cap{display:inline-flex;align-items:baseline;gap:var(--dm-s-2)}
.dm-slx-v{display:inline-flex;align-items:baseline;font-size:14px;font-weight:500!important;letter-spacing:-.02em;padding:3px var(--dm-s-2);border-radius:var(--dm-r-field);cursor:text;transition:box-shadow .2s var(--dm-ease)}
.dm-slx-v:has(input){background:var(--dm-surface);box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-field-line) 50%,transparent)}
.dm-slx-v:has(input):focus-within{box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-field-line-strong) 50%,transparent)}
.dm-slx-v .dm-cur{color:var(--dm-muted);font-weight:400;margin-right:2px}
@supports (corner-shape:squircle){.dm-slx-v{corner-shape:squircle;border-radius:var(--dm-r-field-sq)}}
.dm-slx-in{width:3ch;min-width:1ch;padding:0;margin:0;border:0;background:none;font:inherit;font-variant-numeric:tabular-nums;letter-spacing:inherit;color:inherit;text-align:right;outline:none;caret-color:var(--dm-accent-fg,var(--dm-accent))}
/* the bar, with the same room above and below its knob. The cap it sets is the figure in the head, so the knob carries no
   amount of its own over it */
.dm-slx-bar{position:relative;height:12px;border-radius:calc(6px * var(--dm-rk-s,var(--dm-rk)));background:var(--dm-chip);margin:var(--dm-s-2) 0;touch-action:none;cursor:pointer}
.dm-slx-used{position:absolute;left:0;top:0;bottom:0;border-radius:calc(6px * var(--dm-rk-s,var(--dm-rk)));background:var(--dm-accent-mark,var(--dm-accent));transition:width .8s var(--dm-ease);z-index:1}
/* the forecast: stripes of the accent, and the legend's swatch is the same stripes */
.dm-slx{--dm-slx-hatch:repeating-linear-gradient(-45deg,color-mix(in srgb,var(--dm-accent-mark,var(--dm-accent)) 45%,transparent) 0 3px,color-mix(in srgb,var(--dm-accent-mark,var(--dm-accent)) 18%,transparent) 3px 6px)}
.dm-slx-fc{position:absolute;left:0;top:0;bottom:0;border-radius:calc(6px * var(--dm-rk-s,var(--dm-rk)));background:var(--dm-slx-hatch);transition:width .8s var(--dm-ease)}
/* the knob: ink, with a quiet grey ring while it is pointed at or dragged (a highlight is never a coloured glow) */
.dm-slx-k{position:absolute;top:50%;width:6px;height:26px;margin:-13px 0 0 -3px;border-radius:var(--dm-r-pill);background:var(--dm-fg);z-index:2;cursor:grab;transition:box-shadow var(--dm-t-fast) var(--dm-ease)}
.dm-slx-k.dm-drag{box-shadow:0 0 0 4px color-mix(in srgb,var(--dm-fg) 12%,transparent)}
@media (hover:hover) and (pointer:fine){.dm-slx-k:hover{box-shadow:0 0 0 4px color-mix(in srgb,var(--dm-fg) 12%,transparent)}}
.dm-slx-k.dm-drag{cursor:grabbing}
.dm-slx-lg{display:flex;gap:var(--dm-s-4);font-size:12px}
.dm-slx-lg span{display:flex;align-items:center;gap:6px}
.dm-slx-lg b{color:var(--dm-fg);font-weight:500}
.dm-slx-lg i{width:8px;height:8px;border-radius:var(--dm-r-tiny,3px)}
.dm-slx-lg i.dm-fc{background:var(--dm-slx-hatch)}
/* the line under the bar. On track: a neutral line with the green tick. Over: a warning notice (owner, 18 Nov), a faint
   warning tint as wide as the content, the filled warning sign in the warning mark, and the words in the text colour */
.dm-slx-warn{font-size:12.5px;line-height:18px;min-height:18px}
.dm-slx-warn .dm-o{display:flex;align-items:center;gap:var(--dm-s-2);color:var(--dm-fg2)}
.dm-slx-warn .dm-o svg{flex:none;color:var(--dm-ok-mark)}
.dm-slx-warn .dm-w{display:grid;grid-template-columns:16px minmax(0,1fr);align-items:start;gap:var(--dm-s-2);padding:var(--dm-s-2) 10px;text-wrap:pretty;border-radius:var(--dm-r-in);background:color-mix(in srgb,var(--dm-warn-bg) 45%,transparent);color:var(--dm-fg)}
.dm-slx-wi{width:16px;height:16px;margin-top:1px;overflow:visible}
.dm-slx-wi .dm-a{fill:var(--dm-warn-mark);stroke:var(--dm-warn-mark);stroke-width:2;stroke-linejoin:round}
.dm-slx-wi .dm-b{fill:none;stroke:var(--dm-surface);stroke-width:2.2;stroke-linecap:round}
/* what happens at the cap is one switch row in the foot (owner, 28 Nov): its words on the content's left edge and the library's
   switch ending on its right edge, on by default. The foot sits under a hairline (Soft, Blueprint); Studio gives it a foot strip */
.dm-slx-ft{display:flex;align-items:center;justify-content:space-between;gap:var(--dm-s-3);font-size:13px;line-height:20px;color:var(--dm-fg);cursor:pointer}
.dm-slx-ft{border-top:1px solid color-mix(in srgb,var(--dm-line) 50%,transparent);padding-top:var(--dm-s-4)}
:root[data-style="studio"] .dm-slx-ft,:root [data-style="studio"] .dm-slx-ft,:root [data-style="studio"].dm-slx-ft{margin:0 calc(-1*var(--dm-s-4)) calc(-1*var(--dm-s-4));padding:var(--dm-strip-y) var(--dm-s-4);background:var(--dm-strip);border-top:1px solid var(--dm-line);border-radius:0 0 var(--dm-r-card) var(--dm-r-card)}
:root:not([data-style="soft"],[data-style="studio"]) .dm-slx-warn .dm-w:not(:where([data-style="soft"],[data-style="soft"] *,[data-style="studio"],[data-style="studio"] *)){border-radius:0}
/* Studio (owner, 17 and 20 Nov): its content 16px from each edge; the bar has Studio's soft inner shadow, as Slider's rail does */
:root[data-style="studio"] .dm-slx.dm-panel,:root [data-style="studio"] .dm-slx.dm-panel,:root [data-style="studio"].dm-slx.dm-panel{padding:16px}
:root[data-style="studio"] .dm-slx-bar,:root [data-style="studio"] .dm-slx-bar,:root [data-style="studio"].dm-slx-bar{box-shadow:var(--dm-track-ring)}
SetLimit.astroAstro · 30 lines
---
// Generated from designme.dev by scripts/sync.mjs. Edit the site, then sync.
// Set limit: A monthly agent budget with a draggable cap and a forecast.
// The markup, CSS and script are designme.dev's own, run by the shared core in ../core, which mounts the component when it scrolls
// into view and plays its demo. To show your own data, change set-limit.js. The markup is kept exactly as it is (is:raw).
import type { HTMLAttributes } from 'astro/types';
import '../core/designme.css';
import './set-limit.css';

export interface Props extends HTMLAttributes<'div'> {
  /** "soft" or "studio" draws it in that style. Leave it out for Blueprint, or for the style set on <html> (data-style="soft" or "studio") */
  'data-style'?: 'soft' | 'studio';
}
const { class: className, ...rest } = Astro.props;
---
<div class:list={['card', className]} data-c="set-limit" {...rest}>
  <div class="stage-in"><Fragment is:raw>
<div class="slx panel">
  <div class="slx-h"><div><b>Monthly agent budget</b><span class="muted">Resets Nov 1</span></div><span class="slx-cap"><span class="muted" aria-hidden="true">Cap</span><label class="num slx-v"><span class="cur">$</span><input class="slx-in" inputmode="numeric" autocomplete="off" aria-label="Monthly cap, in dollars" value="200"></label></span></div>
  <div class="slx-bar"><div class="slx-used"></div><div class="slx-fc"></div><button class="slx-k" role="slider" aria-label="Budget cap" aria-valuemin="50" aria-valuemax="500"></button></div>
  <div class="slx-lg muted"><span><i style="background:var(--dm-accent-mark,var(--dm-accent))"></i>Spent <b class="num slx-sp">$128</b></span><span><i class="fc"></i>Forecast <b class="num slx-fv">$184</b></span></div>
  <div class="slx-warn"></div>
  <label class="slx-ft"><span>Pause runs at the cap</span><button class="toggle slx-tg" role="switch" aria-checked="true" aria-label="Pause runs at the cap"></button></label>
</div>
  </Fragment></div>
</div>
<script>
  import './set-limit.js';
</script>
set-limit.htmlHTML · 12 lines
<div class="card" data-c="set-limit">
  <div class="stage-in">
    <div class="slx panel">
      <div class="slx-h"><div><b>Monthly agent budget</b><span class="muted">Resets Nov 1</span></div><span class="slx-cap"><span class="muted" aria-hidden="true">Cap</span><label class="num slx-v"><span class="cur">$</span><input class="slx-in" inputmode="numeric" autocomplete="off" aria-label="Monthly cap, in dollars" value="200"></label></span></div>
      <div class="slx-bar"><div class="slx-used"></div><div class="slx-fc"></div><button class="slx-k" role="slider" aria-label="Budget cap" aria-valuemin="50" aria-valuemax="500"></button></div>
      <div class="slx-lg muted"><span><i style="background:var(--dm-accent-mark,var(--dm-accent))"></i>Spent <b class="num slx-sp">$128</b></span><span><i class="fc"></i>Forecast <b class="num slx-fv">$184</b></span></div>
      <div class="slx-warn"></div>
      <label class="slx-ft"><span>Pause runs at the cap</span><button class="toggle slx-tg" role="switch" aria-checked="true" aria-label="Pause runs at the cap"></button></label>
    </div>
  </div>
</div>
set-limit.cssHTML · 52 lines
/* ---------- Set limit ---------- */
.slx{padding:var(--dm-s-4);display:flex;flex-direction:column;gap:14px;container:slx/inline-size}
/* the budget figure shares the title's line: the two sit on one baseline */
.slx-h{display:flex;align-items:first baseline;justify-content:space-between}
.slx-h>div{display:flex;flex-direction:column;font-size:14px}
.slx-h b{font-weight:500}
.slx-h .muted{font-size:12px}
/* the cap is labelled and reads as a field at rest (owner, 28 Nov): "Cap" in the muted text, then a small field with its $ in
   front, white with the dimmed field edge, which steps up one grey while you type. A cap you can't change is the figure alone */
.slx-cap{display:inline-flex;align-items:baseline;gap:var(--dm-s-2)}
.slx-v{display:inline-flex;align-items:baseline;font-size:14px;font-weight:500!important;letter-spacing:-.02em;padding:3px var(--dm-s-2);border-radius:var(--dm-r-field);cursor:text;transition:box-shadow .2s var(--dm-ease)}
.slx-v:has(input){background:var(--dm-surface);box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-field-line) 50%,transparent)}
.slx-v:has(input):focus-within{box-shadow:inset 0 0 0 1px color-mix(in srgb,var(--dm-field-line-strong) 50%,transparent)}
.slx-v .cur{color:var(--dm-muted);font-weight:400;margin-right:2px}
@supports (corner-shape:squircle){.slx-v{corner-shape:squircle;border-radius:var(--dm-r-field-sq)}}
.slx-in{width:3ch;min-width:1ch;padding:0;margin:0;border:0;background:none;font:inherit;font-variant-numeric:tabular-nums;letter-spacing:inherit;color:inherit;text-align:right;outline:none;caret-color:var(--dm-accent-fg,var(--dm-accent))}
/* the bar, with the same room above and below its knob. The cap it sets is the figure in the head, so the knob carries no
   amount of its own over it */
.slx-bar{position:relative;height:12px;border-radius:calc(6px * var(--dm-rk-s,var(--dm-rk)));background:var(--dm-chip);margin:var(--dm-s-2) 0;touch-action:none;cursor:pointer}
.slx-used{position:absolute;left:0;top:0;bottom:0;border-radius:calc(6px * var(--dm-rk-s,var(--dm-rk)));background:var(--dm-accent-mark,var(--dm-accent));transition:width .8s var(--dm-ease);z-index:1}
/* the forecast: stripes of the accent, and the legend's swatch is the same stripes */
.slx{--dm-slx-hatch:repeating-linear-gradient(-45deg,color-mix(in srgb,var(--dm-accent-mark,var(--dm-accent)) 45%,transparent) 0 3px,color-mix(in srgb,var(--dm-accent-mark,var(--dm-accent)) 18%,transparent) 3px 6px)}
.slx-fc{position:absolute;left:0;top:0;bottom:0;border-radius:calc(6px * var(--dm-rk-s,var(--dm-rk)));background:var(--dm-slx-hatch);transition:width .8s var(--dm-ease)}
/* the knob: ink, with a quiet grey ring while it is pointed at or dragged (a highlight is never a coloured glow) */
.slx-k{position:absolute;top:50%;width:6px;height:26px;margin:-13px 0 0 -3px;border-radius:var(--dm-r-pill);background:var(--dm-fg);z-index:2;cursor:grab;transition:box-shadow var(--dm-t-fast) var(--dm-ease)}
.slx-k.drag{box-shadow:0 0 0 4px color-mix(in srgb,var(--dm-fg) 12%,transparent)}
@media (hover:hover) and (pointer:fine){.slx-k:hover{box-shadow:0 0 0 4px color-mix(in srgb,var(--dm-fg) 12%,transparent)}}
.slx-k.drag{cursor:grabbing}
.slx-lg{display:flex;gap:var(--dm-s-4);font-size:12px}
.slx-lg span{display:flex;align-items:center;gap:6px}
.slx-lg b{color:var(--dm-fg);font-weight:500}
.slx-lg i{width:8px;height:8px;border-radius:var(--dm-r-tiny,3px)}
.slx-lg i.fc{background:var(--dm-slx-hatch)}
/* the line under the bar. On track: a neutral line with the green tick. Over: a warning notice (owner, 18 Nov), a faint
   warning tint as wide as the content, the filled warning sign in the warning mark, and the words in the text colour */
.slx-warn{font-size:12.5px;line-height:18px;min-height:18px}
.slx-warn .o{display:flex;align-items:center;gap:var(--dm-s-2);color:var(--dm-fg2)}
.slx-warn .o svg{flex:none;color:var(--dm-ok-mark)}
.slx-warn .w{display:grid;grid-template-columns:16px minmax(0,1fr);align-items:start;gap:var(--dm-s-2);padding:var(--dm-s-2) 10px;text-wrap:pretty;border-radius:var(--dm-r-in);background:color-mix(in srgb,var(--dm-warn-bg) 45%,transparent);color:var(--dm-fg)}
.slx-wi{width:16px;height:16px;margin-top:1px;overflow:visible}
.slx-wi .a{fill:var(--dm-warn-mark);stroke:var(--dm-warn-mark);stroke-width:2;stroke-linejoin:round}
.slx-wi .b{fill:none;stroke:var(--dm-surface);stroke-width:2.2;stroke-linecap:round}
/* what happens at the cap is one switch row in the foot (owner, 28 Nov): its words on the content's left edge and the library's
   switch ending on its right edge, on by default. The foot sits under a hairline (Soft, Blueprint); Studio gives it a foot strip */
.slx-ft{display:flex;align-items:center;justify-content:space-between;gap:var(--dm-s-3);font-size:13px;line-height:20px;color:var(--dm-fg);cursor:pointer}
.slx-ft{border-top:1px solid color-mix(in srgb,var(--dm-line) 50%,transparent);padding-top:var(--dm-s-4)}
:root[data-style="studio"] .slx-ft,:root [data-style="studio"] .slx-ft,:root [data-style="studio"].slx-ft{margin:0 calc(-1*var(--dm-s-4)) calc(-1*var(--dm-s-4));padding:var(--dm-strip-y) var(--dm-s-4);background:var(--dm-strip);border-top:1px solid var(--dm-line);border-radius:0 0 var(--dm-r-card) var(--dm-r-card)}
:root:not([data-style="soft"],[data-style="studio"]) .slx-warn .w:not(:where([data-style="soft"],[data-style="soft"] *,[data-style="studio"],[data-style="studio"] *)){border-radius:0}
/* Studio (owner, 17 and 20 Nov): its content 16px from each edge; the bar has Studio's soft inner shadow, as Slider's rail does */
:root[data-style="studio"] .slx.panel,:root [data-style="studio"] .slx.panel,:root [data-style="studio"].slx.panel{padding:16px}
:root[data-style="studio"] .slx-bar,:root [data-style="studio"] .slx-bar,:root [data-style="studio"].slx-bar{box-shadow:var(--dm-track-ring)}
set-limit.jsHTML · 45 lines
DM.def('set-limit', (r, c) => {
  const bar = $('.slx-bar', r), k = $('.slx-k', r), v = $('.slx-v', r), used = $('.slx-used', r), fc = $('.slx-fc', r), warn = $('.slx-warn', r);
  const MIN = 50, MAX = 500, SPENT = 128, FC = 184; let cap = 200, pause = true;
  const pct = x => (x - MIN) / (MAX - MIN) * 100;
  const vin = $('.slx-in', r), fit = () => { vin.style.width = Math.max(1, vin.value.length) + 'ch'; };
  function upd(anim){
    k.style.left = pct(cap) + '%'; k.setAttribute('aria-valuenow', cap); k.setAttribute('aria-valuetext', '$' + cap);
    // while someone types, the field keeps what they typed
    if (document.activeElement !== vin){ vin.value = cap; fit(); }
    used.style.width = Math.min(100, SPENT / cap * pct(cap)) + '%'; fc.style.width = Math.min(pct(cap), FC / cap * pct(cap)) + '%';
    const over = FC > cap;
    const h = over ? `<span class="w"><svg class="slx-wi" viewBox="0 0 24 24" aria-hidden="true"><path class="a" d="M12 3.6 2.6 19.9h18.8z"/><path class="b" d="M12 9.6v4.4M12 17v.01"/></svg><span>Forecast passes your cap around Oct 27. ${pause ? 'Runs will pause.' : 'Runs will keep going past it.'}</span></span>` : `<span class="o">${I('ok','xs')}<span>On track. About <span class="slx-left">$${cap - FC}</span> left at month end.</span></span>`;
    // a new message comes in with the entrance; while it stays the same, only the amount left follows the cap
    if (warn.dataset.h !== String(over) + pause){ warn.dataset.h = String(over) + pause; warn.innerHTML = h; enter(warn.firstChild); }
    else if (!over) $('.slx-left', warn).textContent = '$' + (cap - FC);
  }
  const at = e => { glide++; const b = bar.getBoundingClientRect(); cap = Math.round((MIN + (MAX - MIN) * Math.max(0, Math.min(1, (e.clientX - b.left) / b.width))) / 10) * 10; upd(); };
  bar.onpointerdown = e => { bar.setPointerCapture(e.pointerId); k.classList.add('drag'); at(e); bar.onpointermove = at; };
  bar.onpointerup = bar.onpointercancel = () => { k.classList.remove('drag'); bar.onpointermove = null; };
  k.onkeydown = e => { if (e.key === 'ArrowRight' || e.key === 'ArrowUp'){ cap = Math.min(MAX, cap + 10); upd(); e.preventDefault(); } if (e.key === 'ArrowLeft' || e.key === 'ArrowDown'){ cap = Math.max(MIN, cap - 10); upd(); e.preventDefault(); } };
  // the switch in the foot: on, runs pause at the cap; off, they keep going past it, and the warning says which
  const tg = $('.slx-tg', r); tg.onclick = () => { pause = !pause; tg.setAttribute('aria-checked', pause); upd(); };
  // typing a budget: the cap glides to it while it's in range; Enter or leaving the field keeps it (held to the range), Escape goes back
  let glide = 0, before = cap;
  const goTo = n => { const id = ++glide, from = cap; tween(from, n, 450, x => { if (id === glide){ cap = Math.round(x); upd(); } }); };
  const commit = () => { const n = vin.value === '' ? before : Math.max(MIN, Math.min(MAX, parseInt(vin.value, 10))); vin.value = n; fit(); if (n !== cap) goTo(n); };
  // a click selects the whole figure at once, so typing replaces it
  v.addEventListener('pointerdown', e => { if (document.activeElement !== vin){ e.preventDefault(); vin.focus({ preventScroll:true }); } });
  vin.onfocus = () => { before = cap; vin.select(); };
  vin.oninput = () => { vin.value = vin.value.replace(/\D/g, '').slice(0, 4); fit(); const n = +vin.value; if (vin.value && n >= MIN && n <= MAX) goTo(n); };
  vin.onkeydown = e => {
    if (e.key === 'Enter'){ e.preventDefault(); vin.blur(); }
    else if (e.key === 'Escape'){ vin.value = before; goTo(before); vin.blur(); }
    else if (e.key === 'ArrowUp' || e.key === 'ArrowDown'){ e.preventDefault(); const n = Math.max(MIN, Math.min(MAX, (parseInt(vin.value, 10) || cap) + (e.key === 'ArrowUp' ? 10 : -10))); vin.value = n; fit(); goTo(n); }
  };
  vin.onblur = commit;
  used.style.width = 0; fc.style.width = 0; k.style.left = pct(cap) + '%'; fit();
  // the line under the bar is one line on track and a notice when the forecast passes the cap: the panel grows and shrinks
  // with it, staying centred in its stage (owner, 17 Nov)
  const root = $('.slx', r); c.offs.push(grow(root));
  c.later(200, () => upd());
  // the preview lowers the cap once on its own, unless you take over first
  c.auto(1500, () => tween(200, 170, 900, x => { if (!c.mine){ cap = Math.round(x / 10) * 10; upd(); } }));
});
06

Questions

What people ask about it.

Does this component stop the agent when the cap is reached?
No. It shows and sets the cap and whether runs pause at it. Read them from onValueChange and onPauseAtCapChange and enforce them where your runs are billed.
How do I show amounts in another currency?
Pass format, like n => n + ' €'. The text around the number stays fixed while someone types a new cap.
Can I write my own warning text?
Yes. message receives whether the forecast is over, the cap, what is left and whether runs pause at the cap (pauseAtCap), and returns what to show under the bar.