Snippet
The Snippet component is designed to display inline or multiline code snippets or commands, providing an easy way to copy them to the clipboard.
Installation
$ cbui-cli install @crossbuildui/snippet
cbui-cli
globally and authenticated your account. This will ensure you can access all the necessary features.Import
1import { Snippet } from '@/crossbuildui/snippet';
2// Required for copy functionality:
3// import * as Clipboard from 'expo-clipboard';
4// For default copy/check icons (or custom icons):
5// import Icon from 'react-native-vector-icons/MaterialIcons';
expo-clipboard
for its copy-to-clipboard functionality and react-native-vector-icons/MaterialIcons
for the default copy/check icons. Ensure these are installed and configured in your project.Basic Usage
Provide string children to display a simple snippet. Use the symbol
prop to change the leading symbol. If children
is complex ReactNode, use codeString
for accurate copying.
1<Snippet>npm install @crossbuildui/snippet</Snippet>
2
3<Snippet symbol="#">yarn add @crossbuildui/snippet</Snippet>
4
5<Snippet codeString="Just this part is copied">
6 <Text>Display text can be <Text style={{fontWeight: 'bold'}}>ReactNode</Text></Text>
7</Snippet>
Multiline Snippets
Set multiline=
to enable multiline display. You can optionally provide maxLines
to limit the visible height and enable scrolling.
1<Snippet multiline maxLines={3}>
2 {`function greet(name) {
3 // This is a multiline snippet
4 // It will scroll if content exceeds maxLines
5 console.log("Hello, " + name + "!");
6 }
7 greet("World");`}
8</Snippet>
Sizes
Adjust padding, font size, and icon size using the size
prop: 'sm'
, 'md'
(default), or 'lg'
.
1<Snippet size="sm">Small snippet content</Snippet>
2<Snippet size="md" style={{marginTop: 8}}>Medium snippet (default)</Snippet>
3<Snippet size="lg" style={{marginTop: 8}}>Large snippet content</Snippet>
Radius
Control the border radius of the snippet container with the radius
prop.
1<Snippet radius="none">No radius</Snippet>
2<Snippet radius="sm" style={{marginTop: 8}}>Small radius</Snippet>
3<Snippet radius="md" style={{marginTop: 8}}>Medium radius (default)</Snippet>
4<Snippet radius="lg" style={{marginTop: 8}}>Large radius</Snippet>
Symbols
Hide the default symbol with hideSymbol
, or provide a custom string or ReactNode
to the symbol
prop.
1<Snippet hideSymbol>No symbol here</Snippet>
2
3{/* Example using a hypothetical Icon component */}
4{/* <Snippet symbol={<Icon name="code-tags" size={16} />} style={{marginTop: 8}}>
5 Custom icon symbol
6</Snippet> */}
7
8<Snippet symbol="❯" style={{marginTop: 8}}>
9 Custom string symbol
10</Snippet>
Copy Functionality
Disable copying with disableCopy
. Customize the copy and check (copied state) icons using copyIcon
and checkIcon
. Provide specific text for copying via codeString
. Use onCopy
for a callback after successful copy.
1<Snippet disableCopy style={{marginBottom: 8}}>
2 Copying is disabled for this snippet.
3</Snippet>
4
5{/* Example using hypothetical Icon components */}
6{/* <Snippet
7 copyIcon={<Icon name="content-paste" size={16} />}
8 checkIcon={<Icon name="check-circle" size={16} color="green" />}
9 style={{marginBottom: 8}}
10>
11 Snippet with custom copy and check icons.
12</Snippet> */}
13
14<Snippet
15 codeString="This specific text will be copied."
16 onCopy={() => alert('Copied custom text!')}
17>
18 The displayed text is different from the copied text.
19</Snippet>
Animations
The copy icon animates to a check icon upon successful copy. Disable this animation with disableAnimation
.
1<Snippet disableAnimation>
2 No animation on copy icon change for this snippet.
3</Snippet>
Props Overview
PROP | TYPE | DEFAULT | DESCRIPTION |
---|---|---|---|
children | ReactNode | ReactNode[] | - | Content of the snippet. String children are copied by default. |
size | 'sm' | 'md' | 'lg' | 'md' | Visual size (padding, font). |
radius | 'none' | 'sm' | 'md' | 'lg' | 'md' | Border radius. |
symbol | string | ReactNode | '$' | Leading symbol. |
timeout | number | 2000 | Duration (ms) for copied state. |
codeString | string | - | Explicit string to copy, overrides children for copy action. |
copyIcon | ReactNode | - | Custom copy icon. |
checkIcon | ReactNode | - | Custom check (copied) icon. |
disableCopy | boolean | false | Disable copy button. |
hideSymbol | boolean | false | Hide the symbol. |
disableAnimation | boolean | false | Disable copy state animations. |
styles | Partial<SnippetStyleSlots> | - | Custom styles for component slots. |
style | StyleProp<ViewStyle> | - | Style for the outermost container. |
onCopy | () => void | - | Callback on successful copy. |
multiline | boolean | false | Enable multiline display. |
maxLines | number | - | Max lines for multiline before scrolling. |
Styling
Customize the appearance using:
style
: Applied to the outermost wrapperView
of the component.styles
(slot styles): An object to style specific internal parts:base
: The main wrapper. Default background and border are theme-dependent (colors.default['100']
,colors.default['300']
).symbolContainer
: Container for the symbol.symbolText
: Style for the symbol if it's a string. Default color iscolors.foreground
.contentWrapper
: Wrapper for the children content.text
: Style for the defaultText
wrapper around string children. Default color iscolors.foreground
. Uses monospaced font.copyButton
: The copy buttonPressable
area.scrollableView
: TheScrollView
style for multiline snippets.scrollContent
: ThecontentContainerStyle
for theScrollView
.
The component uses a monospaced font (Menlo for iOS, monospace for Android) for string children and symbols by default. If complex ReactNode
is passed as children, you are responsible for its styling, including font family.
1<Snippet
2 style={{ marginVertical: 10, borderColor: 'purple', borderWidth: 2 }} // Styles the outermost wrapper
3 styles={{
4 base: { backgroundColor: 'lightyellow' }, // Overrides default background/border, applied to wrapper
5 symbolContainer: { paddingRight: 12 },
6 symbolText: { color: 'orange', fontWeight: 'bold' },
7 contentWrapper: { flexShrink: 1 }, // Allow content to shrink if needed
8 text: { color: 'darkblue', fontStyle: 'italic' }, // Styles the Text wrapper for string children
9 copyButton: { padding: 6, backgroundColor: 'rgba(0,0,0,0.1)' },
10 scrollableView: { maxHeight: 80 } // For multiline with maxLines
11 }}
12 symbol=">"
13 multiline
14>
15 This is a styled snippet.
16 It demonstrates custom styles for various slots.
17 The text color and symbol are customized.
18</Snippet>
Accessibility
The Snippet
component includes accessibility features:
- The copy button has
accessibilityRole="button"
. - The copy button has an
accessibilityLabel
of "Copy code" or "Copied" depending on its state. - Content provided as strings is rendered within
Text
components, making it accessible to screen readers.