BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/rare-ui/code-block

Code Block

rare-ui/code-block
FreeComponent

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.json

Source

components/ui/code-block.tsxrareui.com/r/code-block.json · first 6 KB
1'use client'
2
3import { 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'
7
8import { cn } from '@/lib/utils'
9
10const TAP_SPRING = { type: 'spring', stiffness: 500, damping: 30 } as const
11const SWAP_SPRING = { type: 'spring', duration: 0.3, bounce: 0 } as const
12const CHECK_SPRING = { type: 'spring', duration: 0.4, bounce: 0.35 } as const
13const COPY_RESET_MS = 1800
14
15export type CodeBlockProps = Omit<React.ComponentProps<'div'>, 'children'> & {
16 /** The source code to render. */
17 code: string
18 /** Prism language id, e.g. "tsx", "css", "json", "bash". */
19 language?: string
20 /** Any hex color. The whole theme is built from shades of it. */
21 accent?: string
22 /** "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?: string
26 /** Show the outer frame — background, border, rounded corners, and header. Turn off to render just the code. */
27 showFrame?: boolean
28 /** Show the header bar. Ignored when the frame is off. */
29 showHeader?: boolean
30 /** Show the line-number gutter. */
31 showLineNumbers?: boolean
32 /** Show the copy-to-clipboard button. */
33 showCopyButton?: boolean
34 /** 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.documentElement
39 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 matchMedia
45 if (typeof window.matchMedia !== 'function') return 'dark'
46 return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
47}
48
49function 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

  • motion
  • prism-react-renderer
  • lucide-react

Other registry items

  • utils

Files it writes

  • components/ui/code-block.tsx

Facts

Registry
rare-ui
Type
registry:ui
Access
Free
Files written
1
Licence
the repository states none
First indexed
2026-08-07
Last confirmed
4 days ago

Go to the source

rareui.com swamimalode07/rare-ui on GitHub Raw registry item This item as JSON

More from rare-ui

All 13 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
Bounce Sidebarbounce-sidebarrare-uiComponentsFree1 dep
Duration Pickerduration-pickerrare-uiComponentsFree5 deps
Emoji Reactionemoji-reactionrare-uiComponentsFree4 deps
Family Drawerfamily drawerrare-uiComponentsFree2 deps
Fluid Orbfluid-orbrare-uiComponentsFreeno deps
Folder Componentfolder-componentrare-uiComponentsFree1 dep
GitHub Activitygithub-activityrare-uiComponentsFree1 dep
Gravity Lettersgravity-lettersrare-uiComponentsFreeno deps
BlockDex

Built by

Kynth Studios

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studios

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studios

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex