Reference
CodeBlock
A block of code with a bar above it: a file name, a copy button (.copy), and line numbers (.lines). language: colours it, as on Code; highlight: marks lines; files: makes it a tab for each file.
CodeBlock(_ code: String, highlight: [Number], files: [CodeFile], language: String, …)
Before 5.0
language
title
copy
lines
page Home(path: "/", title: "Home") {
state count = 0
Heading("Welcome").h1
Button("+1").primary { on click { count = count + 1 } }
}
Example
CodeBlock("""
page Home(path: "/", title: "Home") \{
state count = 0
Heading("Welcome").h1
Button("+1").primary \{ on click \{ count = count + 1 \} \}
\}
""", language: "wf", title: "Home.wf", highlight: [2, 4]).copy.lines
A tab a file
CodeBlock(files: [
{ name: "Home.wf", code: #"page Home(path: "/") { Text("Hi") }"#, language: "wf" },
{ name: "styles.css", code: ".hero { padding: 2rem; }", language: "css" },
]).copy
A shell command
CodeBlock("wf init shop -t spa\ncd shop && wf serve", language: "bash").copy
Props
| Prop | Type | Unset | Meaning |
|---|---|---|---|
code | String (positional) | — | The code, shown as written in a block that scrolls sideways |
highlight: | [Number] | none | The lines to mark, counted from 1: |
files: | [CodeFile] | none | A tab for each file, each its own code block: |
language: | String | none: plain | Colours the code as this language: |
title: | String | none: no bar unless | The file name or the language, in the bar above the code; with |
copy: | Bool | false | A button in the bar that copies the code and says |
lines: | Bool | false | Line numbers in a column beside the code, hidden from screen readers. Written as a flag, |
Attribute families: global, aria, data. Plus the 22 universal props every element takes: animate:, exit:, speed:, duration:, delay:, easing:, stagger:, on:, count:, shared:, class:, ref:, drag:, dragGroup:, dropValue:, dropAxis:, dropInside:, roving:, trapFocus:, mount:, update:, cleanup:.
Flags and cases
A flag is written after the parentheses with a dot: a Bool prop, or a case of one of the element's enum props. A case the enum lacks is an error with a hint.
.copy
.lines
Events
Every element also takes the props, the events and the attribute families listed on every element.
Slots
It takes no slots.
Parts
The parts of CodeBlock are written under its name, inside it. Each is described here, with what it takes.
CodeBlock.Bar
The bar above the code.
CodeBlock.Bar { … }
CodeBlock.Lines
The line numbers.
CodeBlock.Lines(_ text: String)
| Prop | Type | Unset | Meaning |
|---|---|---|---|
text | String (positional) | none | The numbers, one a line |
Drawn for it
What CodeBlock draws itself, by the class a stylesheet can select. They are not written.
| Part | Class | Meaning |
|---|---|---|
CodeBlock.Marks | — | The marks over the lines highlight: names. |
CodeBlock.Mark | — | One line's mark. |
Renders with the class wf-code-block, stable across releases, so a stylesheet may target it.