Code-block
cubby-ui/code-blockFreeComponent
A code-block component.
Live preview
live · rendered by the registry
Rendered by cubby-ui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://cubby-ui.dev/r/code-block.jsonSource
1"use client";23import {4 useLayoutEffect,5 useState,6 useMemo,7 createContext,8 useContext,9} from "react";10import * as React from "react";11import { mergeProps } from "@base-ui/react/merge-props";12import { useRender } from "@base-ui/react/use-render";13import { highlight } from "@/registry/default/code-block/lib/shiki-shared";14import { stripDiffMarker } from "@/registry/default/code-block/lib/transformers/utils";15import { cn } from "@/lib/utils";16import { solidSurface } from "@/registry/default/lib/elevated";17import { CopyButton } from "@/registry/default/copy-button/copy-button";18import {19 SiTypescript,20 SiJavascript,21 SiPython,22} from "@icons-pack/react-simple-icons";23import type { BundledLanguage } from "shiki/langs";24import { Tabs, TabsList, TabsTrigger } from "@/registry/default/tabs/tabs";25import {26 ScrollArea,27 type FadeEdges,28} from "@/registry/default/scroll-area/scroll-area";2930import { HugeiconsIcon } from "@hugeicons/react";31import { ComputerTerminal01Icon } from "@hugeicons/core-free-icons";3233interface CodeBlockContextValue {34 code: string;35 language: string;36 nodes: React.ReactElement | undefined;37 lines: string[];38 hasFocus: boolean;39 showDiff: boolean;40 floatingCopy: boolean;41}4243const CodeBlockContext = createContext<CodeBlockContextValue | null>(null);4445function useCodeBlock() {46 const context = useContext(CodeBlockContext);47 if (!context) {48 throw new Error(49 "CodeBlock subcomponents must be used within a CodeBlock component",50 );51 }52 return context;53}5455type LanguageIconRenderer = React.ComponentType<{56 size: number;57 className: string;58}>;5960function renderHugeicon(61 icon: typeof ComputerTerminal01Icon,62): LanguageIconRenderer {63 function HugeLanguageIcon({64 size,65 className,66 }: {67 size: number;68 className: string;69 }) {70 return (71 <HugeiconsIcon72 icon={icon}73 size={size}74 className={className}75 strokeWidth={2}76 />77 );78 }79 return HugeLanguageIcon;80}8182const LANGUAGE_ICONS: Record<string, LanguageIconRenderer> = {83 typescript: SiTypescript,84 ts: SiTypescript,85 tsx: SiTypescript,86 javascript: SiJavascript,87 js: SiJavascript,88 jsx: SiJavascript,89 bash: renderHugeicon(ComputerTerminal01Icon),90 sh: renderHugeicon(ComputerTerminal01Icon),91 shell: renderHugeicon(ComputerTerminal01Icon),92 python: SiPython,93 py: SiPython,94};9596// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from cubby-ui
All 79 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | cubby-ui | Components | Free | 2 deps | |
| Alertalert | cubby-ui | Components | Free | 1 dep | |
| Alert-dialogalert-dialog | cubby-ui | Components | Free | 2 deps | |
| Aspect-ratioaspect-ratio | cubby-ui | Components | Free | no deps | |
| Autocompleteautocomplete | cubby-ui | Components | Free | 2 deps | |
| Avataravatar | cubby-ui | Components | Free | 1 dep | |
| Badgebadge | cubby-ui | Components | Free | 1 dep | |
| Base Drawerbase-drawer | cubby-ui | Components | Free | 2 deps |
