Order Book
solanaui/order-bookFreeComponent
A real-time order book display with bid/ask levels, depth visualization, mid-price, and spread indicator.
Live preview
live · rendered by the registry
Rendered by solanaui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://www.solanaui.dev/r/order-book.jsonSource
1import { cn } from "@/lib/utils";23interface OrderBookProps {4 bids: {5 price: number;6 size: number;7 }[];8 asks: {9 price: number;10 size: number;11 }[];12 className?: string;13}1415const formatSize = (value: number) => {16 if (value >= 1_000_000) return `${(value / 1_000_000).toFixed(2)}M`;17 if (value >= 1_000) return `${(value / 1_000).toFixed(0)}K`;18 return value.toFixed(0);19};2021const OrderBook = ({ bids, asks, className }: OrderBookProps) => {22 if (bids.length === 0 && asks.length === 0) return null;2324 const highestBid = bids[0]?.price ?? 0;25 const lowestAsk = asks[0]?.price ?? 0;26 const midPrice =27 bids.length > 0 && asks.length > 028 ? (highestBid + lowestAsk) / 229 : highestBid || lowestAsk;30 const spread = lowestAsk - highestBid;31 const spreadPercent = highestBid > 0 ? (spread / highestBid) * 100 : 0;3233 let cumulativeBidSize = 0;34 const bidTotals = bids.map((bid) => {35 cumulativeBidSize += bid.size;36 return cumulativeBidSize;37 });3839 let cumulativeAskSize = 0;40 const askTotals = asks.map((ask) => {41 cumulativeAskSize += ask.size;42 return cumulativeAskSize;43 });4445 const maxTotal = Math.max(46 bidTotals[bidTotals.length - 1] || 0,47 askTotals[askTotals.length - 1] || 0,48 );4950 const reversedAsks = [...asks].reverse();51 const reversedAskTotals = [...askTotals].reverse();5253 return (54 <div55 className={cn(56 "w-full flex flex-col overflow-hidden rounded-lg border bg-card text-card-foreground",57 className,58 )}59 >60 {/* Column headers */}61 <div className="grid grid-cols-3 px-3 py-2 text-xs text-muted-foreground border-b">62 <span>Price</span>63 <span className="text-right">Size</span>64 <span className="text-right">Total</span>65 </div>6667 {/* Asks (reversed so lowest ask is at bottom, near spread) */}68 <div className="flex flex-col overflow-auto">69 {reversedAsks.map((ask, i) => {70 const total = reversedAskTotals[i];71 const depth = maxTotal > 0 ? (total / maxTotal) * 100 : 0;72 return (73 <div key={`ask-${ask.price}`} className="relative">74 <div75 className="absolute right-0 top-0 bottom-0 bg-red-400/10"76 style={{ width: `${depth}%` }}77 />78 <div className="relative grid grid-cols-3 px-3 py-1 text-xs">79 <span className="text-red-400 font-medium tabular-nums">80 {ask.price.toFixed(2)}81// … truncated
Files it writes
More from solanaui
All 31 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Action Boxaction-box | solanaui | Components | Free | no deps | |
| Activity Feedactivity-feed | solanaui | Components | Free | 1 dep | |
| Address Displayaddress-display | solanaui | Components | Free | 1 dep | |
| Auth Cardauth-card | solanaui | Components | Free | no deps | |
| Health Barhealth-bar | solanaui | Components | Free | no deps | |
| Leverage Sliderleverage-slider | solanaui | Components | Free | no deps | |
| NFT Cardnft-card | solanaui | Components | Free | no deps | |
| Order Formorder-form | solanaui | Components | Free | 1 dep |
