Foundations

Code Block

Highlighted code with a file name, line numbers and a copy button.

npx shadcn-svelte@latest add http://localhost:5173/r/code-block.json

With a file name

A header bar names the file and holds the copy button. Colors come from the theme's syntax tokens, so they follow light and dark mode.

light
counter.svelte
<script lang="ts">	import { Button } from '$lib/components/ui/button';	// Counts clicks and says so.	let count = $state(0);	const label = $derived(count === 1 ? 'time' : 'times');</script><Button onclick={() => count++}>	Clicked {count} {label}</Button>
dark
counter.svelte
<script lang="ts">	import { Button } from '$lib/components/ui/button';	// Counts clicks and says so.	let count = $state(0);	const label = $derived(count === 1 ? 'time' : 'times');</script><Button onclick={() => count++}>	Clicked {count} {label}</Button>

Line numbers and highlighted lines

Numbers are a CSS counter, so they never end up in a copied selection. Highlighted lines run the full width, even when the code scrolls sideways.

light
debounce.ts
export function debounce<T extends unknown[]>(fn: (...args: T) => void, wait = 200) {	let timer: ReturnType<typeof setTimeout> | undefined;	return (...args: T) => {		clearTimeout(timer);		timer = setTimeout(() => fn(...args), wait);	};}
dark
debounce.ts
export function debounce<T extends unknown[]>(fn: (...args: T) => void, wait = 200) {	let timer: ReturnType<typeof setTimeout> | undefined;	return (...args: T) => {		clearTimeout(timer);		timer = setTimeout(() => fn(...args), wait);	};}

Without a header

The copy button gets its own column in the corner, so code never scrolls underneath it.

light
npx shadcn-svelte@latest add http://localhost:5173/r/code-block.json
.card {	border-radius: 24px;	box-shadow: 0 1px 2px rgb(0 0 0 / 0.04);	transition: scale 160ms cubic-bezier(0.23, 1, 0.32, 1);}.card:active {	scale: 0.97;}
dark
npx shadcn-svelte@latest add http://localhost:5173/r/code-block.json
.card {	border-radius: 24px;	box-shadow: 0 1px 2px rgb(0 0 0 / 0.04);	transition: scale 160ms cubic-bezier(0.23, 1, 0.32, 1);}.card:active {	scale: 0.97;}

Scrolling

Give it a max height and it scrolls on its own; long lines scroll sideways. The code region is focusable, so it scrolls from the keyboard too.

light
{	"name": "code-block",	"type": "registry:ui",	"dependencies": ["shiki"],	"registryDependencies": ["local:utils", "local:button", "local:icon-swap"]}export function debounce<T extends unknown[]>(fn: (...args: T) => void, wait = 200) {	let timer: ReturnType<typeof setTimeout> | undefined;	return (...args: T) => {		clearTimeout(timer);		timer = setTimeout(() => fn(...args), wait);	};}.card {	border-radius: 24px;	box-shadow: 0 1px 2px rgb(0 0 0 / 0.04);	transition: scale 160ms cubic-bezier(0.23, 1, 0.32, 1);}.card:active {	scale: 0.97;}
dark
{	"name": "code-block",	"type": "registry:ui",	"dependencies": ["shiki"],	"registryDependencies": ["local:utils", "local:button", "local:icon-swap"]}export function debounce<T extends unknown[]>(fn: (...args: T) => void, wait = 200) {	let timer: ReturnType<typeof setTimeout> | undefined;	return (...args: T) => {		clearTimeout(timer);		timer = setTimeout(() => fn(...args), wait);	};}.card {	border-radius: 24px;	box-shadow: 0 1px 2px rgb(0 0 0 / 0.04);	transition: scale 160ms cubic-bezier(0.23, 1, 0.32, 1);}.card:active {	scale: 0.97;}