Unkey
Components

Code

A versatile code component for displaying inline and block code snippets with customizable styling and integrated buttons.

Code

The Code component provides a consistent way to display code snippets within your application. It supports both inline and block code display with customizable styling options and integrated button functionality.

Features

  • Multiple variants (default, ghost, legacy)
  • Integrated copy and visibility buttons
  • Consistent monospace font
  • Customizable styling
  • Accessible by default
  • Responsive design
  • Focus states for better interaction

Usage

import { Code, CopyButton, VisibleButton } from "@unkey/ui";
 
function MyComponent() {
  return (
    <Code
      copyButton={<CopyButton value="const hello = 'world';" />}
      visibleButton={<VisibleButton isVisible={true} setIsVisible={() => {}} />}
    >
      const hello = "world";
    </Code>
  );
}

Examples

Default Variant with Buttons

The default variant provides a subtle background with a border and integrated buttons.

curl -XPOST 'https://api.unkey.dev/v1/ratelimits.limit' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer ********' \
  -d '{
    "namespace": "demo_namespace",
    "identifier": "user_123",
    "limit": 10,
    "duration": 10000
  }'

Variant Styles

The Code component supports different visual styles.

Default Variant
Ghost Variant
Legacy Variant

Props

PropTypeDefaultDescription
variant"default" | "ghost" | "legacy""default"The visual style of the code component
classNamestringundefinedAdditional CSS classes for the wrapper div
buttonsClassNamestringundefinedAdditional CSS classes for the buttons container
preClassNamestringundefinedAdditional CSS classes for the pre element
copyButtonReact.ReactNodeundefinedCopy button component to display
visibleButtonReact.ReactNodeundefinedVisibility toggle button component

The component also accepts all standard HTML pre element props.

Variants

Default

  • Subtle background with border
  • White background in light mode, black in dark mode
  • Gray border for visual separation

Ghost

  • Transparent background
  • No border
  • Minimal visual impact

Legacy

  • Primary text color
  • Subtle background
  • Hover effects with primary border
  • Rounded corners

Styling

The Code component includes:

  • Monospace font for code readability
  • Consistent padding and border radius
  • Focus states for keyboard navigation
  • Hover effects for better interaction
  • Dark mode support
  • Responsive design
  • Flexible button positioning

Best Practices

  • Use the default variant for code snippets with buttons
  • Use the ghost variant when you need minimal visual impact
  • Use the legacy variant for backward compatibility
  • Add appropriate padding around the code component
  • Consider using syntax highlighting for complex code blocks
  • Ensure sufficient contrast between code and background
  • Use semantic HTML structure for better accessibility
  • Position buttons appropriately using the buttonsClassName prop

Accessibility

The Code component is built with accessibility in mind:

  • Proper ARIA attributes
  • Keyboard focus management
  • High contrast text
  • Semantic HTML structure
  • Screen reader support
  • Focus states for interactive elements

On this page