Code Block
A block of code with a copy button, light syntax highlighting and an optional header. The highlighting is built in, so there's nothing extra to install. Its palette stays almost black and white: keywords are heavier, strings and numbers take the one accent color, and comments step back.
Install
npx shadcn@latest add @nelumbo/code-blockUsage
import { CodeBlock } from "@/components/ui/code-block"export function Protocol({ script }: { script: string }) { return <CodeBlock code={script} language="python" title="trim_reads.py" lineNumbers />}Basic
Pass the code as a string and name its language. The copy button sits in the corner.
from Bio import SeqIO# Keep reads whose mean Phred quality clears the cutoffMIN_Q = 28def passes(record, min_q=MIN_Q): quals = record.letter_annotations["phred_quality"] return sum(quals) / len(quals) >= min_qreads = SeqIO.parse("sample_R1.fastq", "fastq")kept = (r for r in reads if passes(r))n = SeqIO.write(kept, "sample_R1.trimmed.fastq", "fastq")print(f"kept {n} reads")Header
A title adds a header row with a file icon. showLanguage names the language on the right, next to the copy button.
from Bio import SeqIO# Keep reads whose mean Phred quality clears the cutoffMIN_Q = 28def passes(record, min_q=MIN_Q): quals = record.letter_annotations["phred_quality"] return sum(quals) / len(quals) >= min_qreads = SeqIO.parse("sample_R1.fastq", "fastq")kept = (r for r in reads if passes(r))n = SeqIO.write(kept, "sample_R1.trimmed.fastq", "fastq")print(f"kept {n} reads"){ "plate": "QC-2025-114", "assay": "Qubit dsDNA HS", "units": "ng/uL", "wells": [ { "well": "A1", "sample": "S01", "conc": 42.8, "pass": true }, { "well": "A2", "sample": "S02", "conc": 3.1, "pass": false }, { "well": "A3", "sample": null, "conc": 0, "pass": false } ]}Inverse
variant inverse is dark in light mode, for commands and snippets that should stand apart from the page. In dark mode it is a raised surface. The docs' own snippets use it.
# Align paired reads to GRCh38, then sort and indexbwa mem -t 8 -R "@RG\tID:S01\tSM:S01" ref/GRCh38.fa \ reads/S01_R1.fastq.gz reads/S01_R2.fastq.gz \ | samtools sort -@ 4 -o aln/S01.bam -samtools index aln/S01.bamecho "done: $SAMPLE"# Align paired reads to GRCh38, then sort and indexbwa mem -t 8 -R "@RG\tID:S01\tSM:S01" ref/GRCh38.fa \ reads/S01_R1.fastq.gz reads/S01_R2.fastq.gz \ | samtools sort -@ 4 -o aln/S01.bam -samtools index aln/S01.bamecho "done: $SAMPLE"Line numbers and highlighted lines
lineNumbers adds a gutter. highlight tints lines, as an array or a string like "8-9,12".
process QUANT { tag "$sample" cpus 8 memory '16 GB' input: tuple val(sample), path(reads) path index output: path "${sample}/quant.sf", emit: quant script: """ salmon quant -i $index -l A -1 ${reads[0]} -2 ${reads[1]} -p $task.cpus -o $sample """}Diff
With diff, lines starting with + or - are marked added or removed. Color is used here because it carries meaning, and screen readers hear “Added” or “Removed” too. The rest of each line keeps its highlighting.
params: genome: GRCh38Removed: aligner: starAdded: aligner: star_salmonRemoved: min_mapped_reads: 5Added: min_mapped_reads: 10 skip_qc: falseAdded: save_trimmed: trueTabs
CodeBlockTabs holds several versions of the same thing: package managers, languages or files. The copy button copies the open tab.
npx shadcn@latest add @nelumbo/code-blockSELECT s.sample_id, s.tissue, COUNT(v.id) AS variantsFROM samples sLEFT JOIN variants v ON v.sample_id = s.sample_id AND v.qual >= 30WHERE s.cohort = 'BRCA-2025'GROUP BY s.sample_id, s.tissueORDER BY variants DESCLIMIT 20;Long code
maxLines cuts long code off with a soft fade and a button that shows all of it. Opening and closing animate the height.
library(DESeq2)library(tidyverse)counts <- read.csv("counts/gene_counts.csv", row.names = 1)samples <- read.csv("metadata/samples.csv", row.names = 1)# Treatment against vehicle, controlling for batchdds <- DESeqDataSetFromMatrix( countData = round(counts), colData = samples, design = ~ batch + treatment)dds <- dds[rowSums(counts(dds) >= 10) >= 3, ]dds <- DESeq(dds)res <- results(dds, contrast = c("treatment", "drug", "vehicle"), alpha = 0.05)res <- lfcShrink(dds, coef = "treatment_drug_vs_vehicle", type = "apeglm")hits <- as.data.frame(res) %>% rownames_to_column("gene") %>% filter(padj < 0.05, abs(log2FoldChange) > 1) %>% arrange(padj)write.csv(hits, "results/de_genes.csv", row.names = FALSE)message(nrow(hits), " genes pass FDR 5% and |log2FC| > 1")Wrapping
Long lines scroll sideways by default, and the code area takes keyboard focus so it can be scrolled with the arrow keys. wrap breaks them onto new lines instead, which suits sequences.
>NM_007294.4 Homo sapiens BRCA1 DNA repair associated, transcript variant 1, mRNA (partial)ATGGATTTATCTGCTCTTCGCGTTGAAGAAGTACAAAATGTCATTAATGCTATGCAGAAAATCTTAGAGTGTCCCATCTGTCTGGAGTTGATCAAGGAACCTGTCTCCACAAAGTGTGACCACATATTTTGCAAATTTTGCATGCTGAAACTTCTCAACCAGAAGAAAGGGCCTTCACAGTGTCCTTTATGTAAGAATGA>NM_007294.4 Homo sapiens BRCA1 DNA repair associated, transcript variant 1, mRNA (partial)ATGGATTTATCTGCTCTTCGCGTTGAAGAAGTACAAAATGTCATTAATGCTATGCAGAAAATCTTAGAGTGTCCCATCTGTCTGGAGTTGATCAAGGAACCTGTCTCCACAAAGTGTGACCACATATTTTGCAAATTTTGCATGCTGAAACTTCTCAACCAGAAGAAAGGGCCTTCACAGTGTCCTTTATGTAAGAATGAProps
| Prop | Type | Default | Notes |
|---|---|---|---|
code | string | — | The code to show and copy. |
language | string | "text" | ts, tsx, js, jsx, json, python, r, bash, sql, yaml, css, html, nextflow, fasta, diff or text. |
title | ReactNode | — | Adds a header row, usually a file name. |
icon | IconName | by language | Icon before the title. |
showLanguage | boolean | false | Names the language in the header. |
variant | "tonal" | "inverse" | "tonal" | A soft grey surface, or dark in light mode. |
lineNumbers | boolean | false | Adds a line-number gutter. |
highlight | number[] | string | — | Lines to tint, e.g. [3, 4] or "3-4,8". |
diff | boolean | false | Marks lines starting with + or - as added or removed. |
maxLines | number | — | Cuts longer code off behind a Show all button. |
wrap | boolean | false | Wraps long lines instead of scrolling. |
copy | boolean | true | Shows the copy button. |
CodeBlockTabs items | { value, label, code, language, … }[] | — | One tab per item; each takes the same options as CodeBlock. |