Code Block
rare-ui/code-blockFreeComponent
A clean code block that builds its entire theme from a single accent color. Pass code and a hex, it does the rest.
Install it
npx shadcn@latest add https://rareui.com/r/code-block.jsonSource
1'use client'23import { Copy } from 'lucide-react'4import { AnimatePresence, motion, useReducedMotion } from 'motion/react'5import { Highlight, type PrismTheme } from 'prism-react-renderer'6import React, { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from 'react'78import { cn } from '@/lib/utils'910const TAP_SPRING = { type: 'spring', stiffness: 500, damping: 30 } as const11const SWAP_SPRING = { type: 'spring', duration: 0.3, bounce: 0 } as const12const CHECK_SPRING = { type: 'spring', duration: 0.4, bounce: 0.35 } as const13const COPY_RESET_MS = 18001415export type CodeBlockProps = Omit<React.ComponentProps<'div'>, 'children'> & {16 /** The source code to render. */17 code: string18 /** Prism language id, e.g. "tsx", "css", "json", "bash". */19 language?: string20 /** Any hex color. The whole theme is built from shades of it. */21 accent?: string22 /** "auto" follows the page theme; pass "dark" or "light" to pin it. */23 mode?: 'auto' | 'dark' | 'light'24 /** Filename or path shown in the header. Falls back to the language id when omitted. */25 filename?: string26 /** Show the outer frame — background, border, rounded corners, and header. Turn off to render just the code. */27 showFrame?: boolean28 /** Show the header bar. Ignored when the frame is off. */29 showHeader?: boolean30 /** Show the line-number gutter. */31 showLineNumbers?: boolean32 /** Show the copy-to-clipboard button. */33 showCopyButton?: boolean34 /** Optional 1-based line numbers to highlight with an accent wash. Off when omitted. */35 highlightLines?: number[]36}37function resolvePageMode(): 'dark' | 'light' {38 const root = document.documentElement39 if (root.classList.contains('dark')) return 'dark'40 if (root.classList.contains('light')) return 'light'41 const attr = root.getAttribute('data-theme')42 if (attr === 'dark') return 'dark'43 if (attr === 'light') return 'light'44 // jsdom has no matchMedia45 if (typeof window.matchMedia !== 'function') return 'dark'46 return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'47}4849function subscribeToPageMode(onChange: () => void) {50 const observer = new MutationObserver(onChange)51 observer.observe(document.documentElement, {52 attributes: true,53 attributeFilter: ['class', 'data-theme'],54 })55 const media =56 typeof window.matchMedia === 'function'57// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from rare-ui
All 13 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Bounce Sidebarbounce-sidebar | rare-ui | Components | Free | 1 dep | |
| Duration Pickerduration-picker | rare-ui | Components | Free | 5 deps | |
| Emoji Reactionemoji-reaction | rare-ui | Components | Free | 4 deps | |
| Family Drawerfamily drawer | rare-ui | Components | Free | 2 deps | |
| Fluid Orbfluid-orb | rare-ui | Components | Free | no deps | |
| Folder Componentfolder-component | rare-ui | Components | Free | 1 dep | |
| GitHub Activitygithub-activity | rare-ui | Components | Free | 1 dep | |
| Gravity Lettersgravity-letters | rare-ui | Components | Free | no deps |
