CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-storybook--components

Core Storybook Components (Deprecated - re-exports from storybook/internal/components)

Pending
Overview
Eval results
Files

syntax-highlighting.mddocs/

Syntax Highlighting

Code highlighting components with extensive language support and customization options for displaying formatted code in documentation and interfaces.

Capabilities

SyntaxHighlighter Component

Primary component for rendering syntax-highlighted code blocks with extensive language support.

interface SyntaxHighlighterProps {
  /** Programming language for syntax highlighting */
  language: SupportedLanguage;
  /** Code content to highlight */
  children: string;
  /** Show line numbers */
  showLineNumbers?: boolean;
  /** Color theme for highlighting */
  theme?: string;
  /** Additional styling options */
  [key: string]: any;
}

/**
 * Lazy-loaded syntax highlighter component with extensive language support
 * Optimized for performance with code splitting
 */
const SyntaxHighlighter: React.ComponentType<SyntaxHighlighterProps>;

/**
 * Supported programming languages for syntax highlighting
 * Includes popular languages like JavaScript, TypeScript, Python, etc.
 */
type SupportedLanguage = 
  | "javascript" 
  | "typescript" 
  | "jsx" 
  | "tsx" 
  | "css" 
  | "html" 
  | "json" 
  | "markdown" 
  | "python"
  | "java"
  | "c"
  | "cpp"
  | "csharp"
  | "php"
  | "ruby"
  | "go"
  | "rust"
  | "swift"
  | "kotlin"
  | "scala"
  | "bash"
  | "shell"
  | "sql"
  | "yaml"
  | "xml"
  | "dockerfile"
  | string; // Additional languages supported by the underlying highlighter

Type Definitions

Interfaces for syntax highlighter configuration and rendering.

/**
 * Format types for syntax highlighter configuration
 */
interface SyntaxHighlighterFormatTypes {
  [key: string]: any;
}

/**
 * Props for syntax highlighter renderer components
 */
interface SyntaxHighlighterRendererProps {
  /** Parsed code rows for rendering */
  rows: any[];
  /** Styling information */
  stylesheet: any;
  /** Whether to use inline styles */
  useInlineStyles: boolean;
}

Copy to Clipboard Utility

Utility function for creating copy-to-clipboard functionality.

/**
 * Creates a copy-to-clipboard function for code blocks
 * Returns a function that can copy text to the system clipboard
 */
function createCopyToClipboardFunction(): (text: string) => void;

Usage Examples

Basic Syntax Highlighting:

import { SyntaxHighlighter } from "@storybook/components";

// JavaScript code highlighting
<SyntaxHighlighter language="javascript">
  {`
    function greet(name) {
      return \`Hello, \${name}!\`;
    }
    
    console.log(greet("World"));
  `}
</SyntaxHighlighter>

// TypeScript code highlighting
<SyntaxHighlighter language="typescript">
  {`
    interface User {
      name: string;
      age: number;
    }
    
    const user: User = {
      name: "Alice",
      age: 30
    };
  `}
</SyntaxHighlighter>

Advanced Configuration:

import { SyntaxHighlighter } from "@storybook/components";

// With line numbers and custom theme
<SyntaxHighlighter 
  language="jsx"
  showLineNumbers={true}
  theme="dark"
>
  {`
    import React from 'react';
    
    function Component({ title }) {
      return (
        <div>
          <h1>{title}</h1>
          <p>Hello from React!</p>
        </div>
      );
    }
    
    export default Component;
  `}
</SyntaxHighlighter>

Different Languages:

import { SyntaxHighlighter } from "@storybook/components";

// Python code
<SyntaxHighlighter language="python">
  {`
    def calculate_fibonacci(n):
        if n <= 1:
            return n
        return calculate_fibonacci(n-1) + calculate_fibonacci(n-2)
    
    print(calculate_fibonacci(10))
  `}
</SyntaxHighlighter>

// CSS code
<SyntaxHighlighter language="css">
  {`
    .button {
      background-color: #007bff;
      color: white;
      padding: 8px 16px;
      border: none;
      border-radius: 4px;
      cursor: pointer;
    }
    
    .button:hover {
      background-color: #0056b3;
    }
  `}
</SyntaxHighlighter>

// JSON data
<SyntaxHighlighter language="json">
  {`
    {
      "name": "my-project",
      "version": "1.0.0",
      "dependencies": {
        "react": "^18.0.0",
        "@storybook/components": "^8.6.14"
      }
    }
  `}
</SyntaxHighlighter>

Copy to Clipboard Integration:

import { SyntaxHighlighter, createCopyToClipboardFunction } from "@storybook/components";
import { useState } from "react";

function CodeBlock({ code, language }) {
  const [copied, setCopied] = useState(false);
  const copyToClipboard = createCopyToClipboardFunction();

  const handleCopy = () => {
    copyToClipboard(code);
    setCopied(true);
    setTimeout(() => setCopied(false), 2000);
  };

  return (
    <div style={{ position: 'relative' }}>
      <button 
        onClick={handleCopy}
        style={{ 
          position: 'absolute', 
          top: 8, 
          right: 8,
          zIndex: 1
        }}
      >
        {copied ? 'Copied!' : 'Copy'}
      </button>
      
      <SyntaxHighlighter language={language}>
        {code}
      </SyntaxHighlighter>
    </div>
  );
}

// Usage
<CodeBlock 
  language="typescript"
  code={`
    const example = "Hello World";
    console.log(example);
  `}
/>

Inline Code Highlighting:

import { SyntaxHighlighter } from "@storybook/components";

// For short code snippets
<SyntaxHighlighter 
  language="javascript" 
  style={{ display: 'inline-block', padding: '2px 4px' }}
>
  {`const x = 42;`}
</SyntaxHighlighter>

Supported Languages

The SyntaxHighlighter component supports a wide range of programming languages:

Web Technologies:

  • javascript, js - JavaScript
  • typescript, ts - TypeScript
  • jsx - JavaScript with JSX
  • tsx - TypeScript with JSX
  • css - Cascading Style Sheets
  • html - HTML markup
  • json - JSON data

Backend Languages:

  • python - Python
  • java - Java
  • csharp, cs - C#
  • php - PHP
  • ruby - Ruby
  • go - Go
  • rust - Rust
  • kotlin - Kotlin
  • scala - Scala

System Languages:

  • c - C
  • cpp, c++ - C++
  • bash, shell - Shell scripts

Data and Config:

  • sql - SQL queries
  • yaml, yml - YAML configuration
  • xml - XML markup
  • dockerfile - Docker configuration
  • markdown, md - Markdown

Performance Notes

The SyntaxHighlighter component is lazy-loaded to optimize bundle size. It only loads the highlighting engine when first used, reducing initial page load times.

For applications with many code blocks, consider implementing virtualization or pagination to maintain performance with large amounts of highlighted code.

Theming

The component automatically adapts to Storybook's current theme (light/dark) and provides consistent styling across different syntax highlighting scenarios. Custom themes can be applied using the theme prop.

Install with Tessl CLI

npx tessl i tessl/npm-storybook--components

docs

forms.md

graphics.md

index.md

layout.md

syntax-highlighting.md

tooltips.md

typography.md

ui-components.md

tile.json