Display

Code block

Shows code with its syntax colored, line numbers, lines you want noticed, and a copy button. It unrolls the first time it scrolls into view.

Installation

npx sinaui add code-block

Examples

A file, with lines marked

highlight takes lines and ranges: “4-6”, or “3, 8-9”.

src/lib/astrolabe.ts
// The angle of a star above the horizon, from its hour angle and declination.
export function altitude(latitude: number, declination: number, hourAngle: number) {
	const rad = Math.PI / 180;
	const sin =
		Math.sin(latitude * rad) * Math.sin(declination * rad) +
		Math.cos(latitude * rad) * Math.cos(declination * rad) * Math.cos(hourAngle * rad);
	return Math.asin(sin) / rad;
}

// Maragheh observatory, Vega at its meridian.
altitude(37.39, 38.78, 0);
<script lang="ts">
	import CodeBlock from '#lib/ui/CodeBlock.svelte';

	const code = `// The angle of a star above the horizon, from its hour angle and declination.
export function altitude(latitude: number, declination: number, hourAngle: number) {
	const rad = Math.PI / 180;
	const sin =
		Math.sin(latitude * rad) * Math.sin(declination * rad) +
		Math.cos(latitude * rad) * Math.cos(declination * rad) * Math.cos(hourAngle * rad);
	return Math.asin(sin) / rad;
}

// Maragheh observatory, Vega at its meridian.
altitude(37.39, 38.78, 0);`;
</script>

<CodeBlock {code} lang="ts" filename="src/lib/astrolabe.ts" highlight="4-6" class="demo" />

<style>
	:global(.demo) {
		inline-size: min(40rem, 100%);
	}
</style>

A diff

Added lines in green, removed in red.

catalog.ts
  export const catalog = {
-   shelves: 12,
+   shelves: 14,
+   reading_rooms: ['Main hall', 'Manuscripts'],
    open: '08:00'
  };
<script lang="ts">
	import CodeBlock from '#lib/ui/CodeBlock.svelte';

	const code = `  export const catalog = {
-   shelves: 12,
+   shelves: 14,
+   reading_rooms: ['Main hall', 'Manuscripts'],
    open: '08:00'
  };`;
</script>

<CodeBlock {code} lang="diff" filename="catalog.ts" class="demo" />

<style>
	:global(.demo) {
		inline-size: min(40rem, 100%);
	}
</style>

A command

Without line numbers, for one-liners.

shell
npx sinaui add code-block
<script lang="ts">
	import CodeBlock from '#lib/ui/CodeBlock.svelte';
</script>

<CodeBlock code="npx sinaui add code-block" lang="shell" lineNumbers={false} class="demo" />

<style>
	:global(.demo) {
		inline-size: min(40rem, 100%);
	}
</style>

Props

PropTypeDefault
codestringrequired
langcss, diff, go, html, js, json, python, shell, sql, svelte, ts, yaml'ts'
filenamestring
highlightstring: “3”, “3-5”, “3-5, 9”
lineNumbersbooleantrue

Accessibility

  • A region named after its file; it takes focus, so long lines can be scrolled from the keyboard.
  • Line numbers aren't read or copied, only the code.
  • Copying is announced; if the browser won't allow it, it says how to copy by hand.
  • With reduced motion it appears without unrolling.
Introduction
Theming
Changelog
Button
Action button
Confirm button
Copy button
Hold to confirm
Split button
Calendar
Checkbox
Color picker
Combobox
Date picker
Date range picker
File drop
Form
Input
Mention input
Multi select
Number field
OTP input
Password input
Phone input
Radio cards
Radio group
Rich text editor
Search field
Segmented control
Select
Signature pad
Slider
Switch
Tag input
Upload
Wheel picker
Textarea
Time field
Toggle group
Toolbar
Alert dialog
Command palette
Context menu
Dialog
Drawer
Dropdown
Hover card
Popover
Tooltip
Announcement bar
Alert
Empty state
Meter
Progress
Rolling number
Skeleton
Spinner
Toast
Avatar
Avatar group
Badge
Card
Carousel
Chart
Chat
Code block
Data table
Folder
Kbd
Mind map
Resizable panels
Scroll area
Separator
Sortable
Table
Tag group
Virtual list
Audio player
Image
Image compare
Video
Visualizer
Breadcrumb
File tree
Menubar
Navigation menu
Pagination
Sidebar layout
Stepper
Tabs
Alarm clock
Clock
Dynamic island
Fluid orb
Hourglass
Stopwatch
Timer
AI chat
AI media
Auth
Board
Booking
Dashboard
File explorer
Messaging
Notifications
Settings
Team
Overview
Pricing
Agent chat
Calendar
Voice agent
Mail inbox
CMS
Blog
Marketing site
E-commerce
AI agents
SaaS landing page
SaaS app
Accordion
Dark theme
Light theme