Code

The Code component is used to display blocks of pre-formatted text, typically code snippets. It provides a monospaced font, an optional header with a title, and copy-to-clipboard functionality.

Installation

$ cbui-cli install @crossbuildui/code

Import

index.tsx
1import { Code } from '@/crossbuildui/code'; 2// Required for copy functionality: 3// import * as Clipboard from 'expo-clipboard'; 4// For default copy icon (or custom icons): 5// import Icon from 'react-native-vector-icons/MaterialIcons';

Basic Usage

Pass a string as children to display it as a code block.

index.tsx
1<Code> 2 const greeting = "Hello, World!"; 3 console.log(greeting); 4</Code>

Sizes

Adjust the text size, padding, and icon size using the size prop: 'sm', 'md' (default), or 'lg'.

index.tsx
1<Code size="sm" showHeader title="Small Code"> 2 {'const small = true;'} 3</Code> 4 5<Code size="md" showHeader title="Medium Code (Default)"> 6 {'const medium = "default";'} 7</Code> 8 9<Code size="lg" showHeader title="Large Code"> 10 {'const large = 123;'} 11</Code>

Radius

Control the border radius of the code block container with the radius prop.

index.tsx
1<Code radius="none" showHeader title="No Radius"> 2 {'border-radius: 0;'} 3</Code> 4 5<Code radius="sm" showHeader title="Small Radius"> 6 {'border-radius: 4px;'} 7</Code> 8 9<Code radius="lg" showHeader title="Large Radius"> 10 {'border-radius: 12px;'} 11</Code>

Copy Functionality

When showHeader is true, a copy button is displayed. You can provide custom text to be copied via copyText (otherwise, string children are copied). A custom copyIcon can be supplied, and an onCopy callback is fired upon successful copy.

index.tsx
1// Using a custom icon (example with MaterialCommunityIcons) 2// const CustomCopyIcon = <Icon name="content-duplicate" size={16} color="#d9d9da" />; 3 4<Code 5 showHeader 6 title="Custom Copy Icon" 7 // copyIcon={CustomCopyIcon} 8 copyText={"console.warn('Custom text copied!');"} 9 onCopy={(text) => alert("Copied: " + text)} 10> 11 {'// This text will be displayed, but copyText will be copied. 12console.log("Original text");'} 13</Code>

Theming and Colors

The Code component aims for a consistent, terminal-like appearance. Its primary background, foreground, border, and header colors are fixed based on the current theme mode (light/dark) and are not affected by the color prop.

  • In light mode, the code background is white, and text is black.
  • In dark mode, the code background is black, and text is white.
  • The main border, header background, and header foreground (title, copy icon) use fixed shades derived from the application's dark theme configuration to ensure a consistent look across modes.

Props Overview

PROPTYPEDEFAULTDESCRIPTION
childrenReactNode-The code string or ReactNode to display. String children are used for copying by default.
size'sm' | 'md' | 'lg''md'Size of text, padding, and icons.
colorCodeColor'default'Largely overridden by fixed theming.
radiusCodeRadius'md'Border radius of the container.
showHeaderbooleanfalseShow header with title and copy button.
titlestring"Code"Title in header if showHeader is true.
copyIconReactElement-Custom copy icon element.
onCopy(text: string) => void-Callback after successful copy.
stylesCodeSlotsStyles-Custom styles for component slots (base, header, title, copyButton, content for text).
styleStyleProp<ViewStyle>-Style for the main wrapper View. Overrides styles.base.
textStyleStyleProp<TextStyle>-Style for the code text. Merged after styles.content.
copyTextstring-Explicit text to copy. Overrides string children for copy action.

Styling

Customize the appearance using:

  • style: Applied to the outermost wrapper View of the component.
  • styles (slot styles): An object to style specific internal parts:
    • base: Styles the main wrapper, merged with default wrapper styles.
    • header: Styles the header container.
    • title: Styles the title text in the header.
    • copyButton: Styles the Pressable area for the copy icon.
    • content: Styles applied to the code Text component itself.
  • textStyle: Additional styles applied directly to the code Text, merged after styles.content.
index.tsx
1<Code 2 style={{ marginVertical: 10, opacity: 0.9 }} // Styles the outermost wrapper 3 styles={{ 4 base: { borderColor: 'green', borderWidth: 2 }, // Overrides default border, applied to wrapper 5 header: { backgroundColor: '#333' }, 6 title: { color: 'lightgreen', fontStyle: 'italic' }, 7 copyButton: { padding: 8, borderRadius: 5, backgroundColor: 'rgba(255,255,255,0.1)' }, 8 // 'content' slot styles the text itself 9 content: { color: 'cyan', lineHeight: 20 } 10 }} 11 textStyle={{ fontWeight: 'bold' }} // Additional direct style for the text, merged after styles.content 12 showHeader 13 title="Styled Code Block" 14> 15 const styledCode = true; 16 if (styledCode) { 17 // Apply custom styles 18 } 19</Code>

Accessibility

The Code component aims to be accessible:

  • The code content, if provided as a string, is rendered within a Text component and is readable by screen readers.
  • The copy button in the header is a Pressable. While it uses icons, ensure that if you provide a custom copyIcon that is not inherently descriptive, you might need to consider adding accessibility labels if the context requires it (though typically the visual context of a copy icon next to code is clear). The default icons are standard UI elements.
  • The "copied" state provides visual feedback with a checkmark icon.