RateLock
- Status
- Implemented
- Family
- 4 of 13
- Registry
- @lopifi/rate-lock
npx shadcn@latest add @lopifi/theme @lopifi/rate-lockThe component, rendering
Real data, the shipped component. Switch the theme to check the claims the spec makes below.
The four-layer spec
A locked rate with a countdown. The countdown is a rule-line progress bar, linear, consistent with the statement grammar rather than a rounded progress pill.
On expiry: the rate refreshes, the new rate crossfades in, and the confirm action is disabled until the user acknowledges the new rate. Silently executing at a rate the user didn't see is the worst failure this family can have.
Warning state at 20% remaining — amber, plus a text countdown, never color alone.
Lifted verbatim from Swaps & FX. The doc and this page are the same paragraphs, so they cannot disagree.
What lands in your repository
One component is never one file. Everything below is copied into your project, where you own it.
- Files
components/ledger/rate-lock.tsx
- Lopifi dependencies · 10
- as-ofbuttoncnfiscal-periodformat-moneymoney.typesrate-linetooltipuse-live-valueuse-shared-now
- npm packages
- @base-ui-components/reactclass-variance-authorityclsxtailwind-merge
- Registry entry
- /r/rate-lock.json
The whole component
No compiled bundle, no black box. This is the file the registry copies, and the file you will edit.
"use client";
import * as React from "react";
import { cn } from "@/lib/ledger/cn";
import { formatRate } from "@/lib/ledger/format-money";
import { Button } from "@/components/ledger/button";
import { RateLine } from "@/components/ledger/rate-line";
export interface RateLockProps {
className?: string;
/** Seconds the lock is valid for. */
durationSeconds?: number;
from: string;
lockedAt: Date;
onRefresh: () => number;
rate: number;
to: string;
}
/**
* On expiry the rate refreshes and the confirm action stays disabled until
* the user acknowledges the new rate. Silently executing at a rate the user
* didn't see is the worst failure this family can have.
*
* The countdown is a rule-line progress bar, linear — consistent with the
* statement grammar rather than a rounded pill.
*/
export function RateLock({
from,
to,
rate,
lockedAt,
durationSeconds = 30,
onRefresh,
className,
}: RateLockProps) {
const [remaining, setRemaining] = React.useState(durationSeconds);
const [pendingRate, setPendingRate] = React.useState<number | null>(null);
const [currentRate, setCurrentRate] = React.useState(rate);
const [lockTime, setLockTime] = React.useState(lockedAt);
/* The latest callback, read at expiry: an inline `onRefresh` would otherwise
restart the countdown on every parent render. */
const refresh = React.useRef(onRefresh);
refresh.current = onRefresh;
const expired = remaining <= 0;
React.useEffect(() => {
if (pendingRate !== null || expired) return;
const id = setInterval(() => setRemaining((r) => Math.max(0, r - 1)), 1000);
return () => clearInterval(id);
}, [pendingRate, expired]);
/* Out of the state updater: an updater must be pure, and StrictMode runs it
twice — which fetched two quotes per expiry. */
React.useEffect(() => {
if (expired && pendingRate === null) setPendingRate(refresh.current());
}, [expired, pendingRate]);
const fraction = remaining / durationSeconds;
const warning = fraction <= 0.2 && pendingRate === null;
const acknowledge = () => {
if (pendingRate === null) return;
setCurrentRate(pendingRate);
setLockTime(new Date());
setPendingRate(null);
setRemaining(durationSeconds);
};
return (
<div className={cn("space-y-2", className)}>
<RateLine asOf={lockTime} from={from} locked={pendingRate === null} rate={currentRate} to={to} />
<div
aria-label="Rate lock remaining"
aria-valuemax={durationSeconds}
aria-valuemin={0}
aria-valuenow={remaining}
className="relative h-[3px] w-full bg-border-subtle"
role="progressbar"
>
<div
className={cn(
// scaleX rather than width: the bar ticks every second, and a width
// change relayouts the row each tick. Linear, because it is a clock.
"absolute inset-0 origin-left transition-transform duration-[1000ms] ease-linear",
warning ? "bg-[var(--warning)]" : "bg-accent",
)}
style={{ transform: `scaleX(${fraction})` }}
/>
</div>
{pendingRate !== null ? (
<div className="ledger-crossfade flex flex-wrap items-center gap-3 rounded-sm bg-warning-subtle p-3">
<p className="flex-1 text-caption text-text-primary">
The rate expired and has been refreshed to{" "}
<span className="numeric font-medium">{formatRate(pendingRate)}</span>. Review it before
continuing.
</p>
<Button onClick={acknowledge} size="sm" variant="primary">
Accept new rate
</Button>
</div>
) : (
<p className={cn("numeric text-caption", warning ? "text-warning" : "text-text-tertiary")}>
{warning ? "Expiring in " : "Rate held for "}
{remaining}s
</p>
)}
</div>
);
}
The accounting it encodes
Concepts this component renders. Each one is defined once, in the lexicon, and used identically in the code and the copy.
Also in Swaps & FX
- SwapWidgetFrom/to with the flip. The signature component.
- RateLineA rate with its timestamp. Never one without the other.
- ConversionBreakdownAmount → fee → rate → recipient gets. Every line.
- CurrencyPairChartRate history with a period selector.
- RateTickerLive multi-pair ticker with value flash.
- SlippageControlTolerance setting with an explicit worst case.
- SwapRouteMulti-hop path visualization.
- FxExposurePosition by currency with unrealized gain/loss.
See also
A component is specified in one place, defined in another, and running in a third. All three, from here.
- Swaps & FX — the family specThe four-layer specification this component was built against, before it was written.Doc
- FX, swaps and rate componentsThe family running, with real figures in it, in both themes and at every density.Live
- CatalogAll 179 components, specified before they are written.Doc
- StatementP&L, balance sheet, or cash flowTerm
Elsewhere in the system
Where RateLock sits in the rest of the system — the live section it renders in, the calculators that compute what it displays, and the ledger payloads it is fed from.
- RateLock, runningThe swaps showcase — live, with theme, density, greyscale and reduced-motion switches.Live
- Swaps & FXThe governing rule for this entire family: A rate is a perishable fact. It never appears without a timestamp, and it is never transacted on without a lock. Specs for 9 React components: SwapWidget, RateLine, RateLock, ConversionBreakdown and 5 more.Doc
Components are copied into your repository and meant to be edited — rounding policy, fiscal calendar and compliance copy differ per company.
Told when Swaps & FX changes
Components are copied into your repo, so a fix here does not reach you on its own. One email when a component in this family changes in a way that needs a migration — and nothing when it does not.