Skip to content
Cojeev UI

Code block

Tools

Displays a code snippet with an optional caption, language label, wrapping, and copy feedback.

React · copy into your project · light and dark themes

Try it

Loading preview…

Take it with you

Copy the example, API and usage notes for your editor or AI assistant.

Add to your project

Terminal
npx shadcn@latest add https://luv-jeri.github.io/cojeev-ui/r/code-block.json

The command includes the component’s shared dependencies. Your project keeps editable source files.

Include the complete example

This example also composes button, typography. Install its parts together:

Terminal
npx shadcn@latest add https://luv-jeri.github.io/cojeev-ui/r/button.json https://luv-jeri.github.io/cojeev-ui/r/code-block.json https://luv-jeri.github.io/cojeev-ui/r/typography.json

Using code block

  • Pass code as a string and use title or language to explain the snippet; language labels do not add syntax highlighting.

  • Enable wrap for long lines or use the default scrolling view, and use CopyButton when only the copy action is needed.

  • The copy control tries the Clipboard API and then a selection-based fallback when that API is unavailable, including on HTTP pages.

API

Compose the exported parts below. The prop types are read from the React source during the registry build.

Native element or underlying primitive props are inherited where declared, including accessible labels, event handlers and refs.

CopyButtonProps

PropTypeRequired
codestringYes

CodeBlockProps

PropTypeRequired
codestringYes
languagestringNo
titlestringNo
wrapbooleanNo
copyLabelstringNo

Accessibility

  • Keep the copy status available so users hear whether copying succeeded or manual copying is needed.

  • Provide a descriptive caption and preserve keyboard access to overflowing code.

States and appearance

restcopyingcopiederror

For conditional content, keep a MotionPresence boundary around keyed MotionSurface children. The shared entrance and exit states work with native primitives and respect your motion preference.

Try pointer and keyboard interaction, and switch the appearance control in the navigation to compare light and dark mode. Motion respects reduced motion preferences.