Snippet

A customizable code snippet component for React with copy functionality, syntax highlighting, and advanced features.

About

The Snippet component provides an easy way to display and copy code snippets in your React application. It supports syntax highlighting, custom styling, collapsible snippets, multi-snippet support, and various configuration options.

Installation

Install the component using your preferred package manager:

npx install @polyblock/snippet

Or manually add the component to your project.

Usage

Import and use the Snippet and SnippetColor components in your React application:

import { Snippet, SnippetColor } from '@/components/polyblock/snippet'

export default function MyComponent() {
return (
<Snippet language="javascript">
This is a PolyBlock Snippet component with <SnippetColor className="text-rose-500">colored text</SnippetColor>!
</Snippet>
)
}

Examples

Basic Usage

This is a Basic Usage Snippet!

Colored Text

This Snippet has COLOR

Custom Variants

This is a Custom Variant Snippet!

With Symbol

$This Snippet has a symbol

To hide the symbol, simply omit the symbol prop or set it to null or an empty string.

Custom Display Content

Custom Display Content

This is custom content displayed instead of the actual code.

A PolyBlock Component

With Multiple Snippets

// Initiate neural interface to download cyberpunk quotes

Collapsible Snippet

This is a collapsible snippet
This component is engineered based on Shadcn's collapsible snippet.

API Reference

The Snippet component accepts the following props:

PropTypeDescriptionDefault
languagestringThe programming language for syntax highlighting"plaintext"
SnippetColorComponentA component to add colored text within a Snippet-
variant"default" | "outline" | "custom"The visual style variant"default"
size"default" | "sm" | "lg" | nullThe size of the component"default"
symbolstring | ReactNodeThe symbol to display before each line-
timeoutnumberDuration for which the "Copied" message is displayed (ms)2000
codeStringstringThe code to be copied (if different from displayed content)-
displayContentReactNodeCustom JSX to display instead of the code-
snippetsArrayArray of objects with label and code for multiple snippets-
disableTooltipbooleanDisable the copy button tooltipfalse
disableCopybooleanDisable the copy functionalityfalse
hideCopyButtonbooleanHide the copy buttonfalse
disableAnimationbooleanDisable animations on the copy buttonfalse
onCopyTextfunctionCallback function called when text is copied-
collapsiblebooleanMake the snippet collapsiblefalse
defaultOpenbooleanDefault open state for collapsible snippetsfalse

Advanced Usage

Internal Context Usage

The Snippet component uses SnippetContext internally to manage multiline snippets, consistent symbol display (when provided), and styling across nested components. This context is crucial for maintaining functionality in complex snippets but is handled automatically by the component.

Multiline Snippets

The Snippet component efficiently handles multiline code snippets, ensuring consistent formatting and symbol display across all lines. This is managed internally through the SnippetContext.

Colored Text with SnippetColor

The SnippetColor component allows you to add colored text within your Snippet. This is useful for highlighting specific parts of your code without affecting the copied text. Use it by wrapping the text you want to color with SnippetColor and providing a className for the desired color.

Syntax Highlighting Integration

While the component supports different languages, you may need to integrate with a syntax highlighting library for more advanced highlighting. Refer to the documentation of your chosen syntax highlighting library for integration details.

Accessibility

The Snippet component is designed with accessibility in mind. The copy button supports keyboard navigation and includes appropriate ARIA attributes.

Performance Considerations

For very large code snippets, consider using the collapsible feature to improve initial load performance. This allows users to expand the snippet only when needed.

Nested Components

The SnippetCopy component is designed to work within the Snippet component. The internal context ensures that styling and functionality are consistent even in complex, nested structures.