Skip to content

Code Block

Syntax-highlighted code rendered server-side with Chroma. It follows the active theme, includes an accessible copy button, and falls back to plain text when a lexer is unknown.

Default Rendered example
main.go
package main

import "fmt"

func main() {
    fmt.Println("hello, world")
}
Usage Example
@codeblock.CodeBlock(codeblock.Config{
    Language: "go",
    Label:    "Hello",
    Code:     `package main

func main() {
    fmt.Println("hello, world")
}`,
})

HTML

Use Language: "html" for templates, fragments, and Alpine/HTMX snippets.

HTML Rendered example
html
<button x-data="{ open: false }" @click="open = !open" class="btn">Toggle</button>
HTML
@codeblock.CodeBlock(codeblock.Config{
    Language: "html",
    Code:     `<button x-data="{open:false}" @click="open=!open" class="btn">Toggle</button>`,
})

CSS

CSS snippets use the same highlighted shell, so theme token examples stay readable in light and dark mode.

CSS Rendered example
css
.btn {
    color: var(--color-primary);
    padding: 0.5rem 1rem;
    border-radius: 0.375rem;
}
CSS
@codeblock.CodeBlock(codeblock.Config{
    Language: "css",
    Code:     `.btn { color: var(--color-primary); padding: 0.5rem 1rem; }`,
})

Bash

Command snippets can set a short Label when the language name is not specific enough.

Bash Rendered example
Install
go get github.com/a-h/templ
npm install -D tailwindcss@latest
Bash
@codeblock.CodeBlock(codeblock.Config{
    Language: "bash",
    Label:    "Install",
    Code:     "go get github.com/a-h/templ",
})

Scrollable

Set MaxHeight for long snippets that should scroll inside the code panel instead of pushing the page apart.

Scrollable Rendered example
long.go
package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintln(w, "hello")
    })
    http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(http.StatusOK)
        fmt.Fprintln(w, "ok")
    })
    http.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintln(w, "v1.0.0")
    })
    if err := http.ListenAndServe(":8080", nil); err != nil {
        panic(err)
    }
}
Scrollable
@codeblock.CodeBlock(codeblock.Config{
    Language:  "go",
    MaxHeight: "180px",
    Code:      longGoSnippet,
})

Go API

v0.2.7

The examples above cover behavior and composition. pkg.go.dev is the canonical reference for exported types, functions, methods, and Go documentation.

github.com/araihu/goshtoso/components/codeblock
Open v0.2.7 API