Code Examples
These are examples of codeblock components you can build using Code Hike.
You can find the explanation and implementation in each example's page. Feel free to copy, paste, modify and combine them to create the perfect codeblocks for your project.
Callout
Add callouts inside your code blocks.
```jsconst lorem = ipsum(dolor, sit)// !callout[/amet/] This is a calloutconst [amet, consectetur] = [0, 0]lorem.adipiscing((sed, elit) => {if (sed) {amet += elit}})```
const lorem = ipsum(dolor, sit)const [amet, consectetur] = [0, 0]This is a calloutlorem.adipiscing((sed, elit) => {if (sed) {amet += elit}})
ClassName
Add a class name to a piece of code.
```jsfunction lorem(ipsum, dolor = 1) {// !className line-throughconst sit = ipsum == null ? 0 : ipsum.sitdolor = sit - amet(dolor)// !className[/sit/] bg-red-700 rounded-lg px-1return sit ? consectetur(ipsum) : []}```
function lorem(ipsum, dolor = 1) {const sit = ipsum == null ? 0 : ipsum.sitdolor = sit - amet(dolor)return sit ? consectetur(ipsum) : []}
Code Mentions
Add a language picker when you need to switch between languages in a code block.
<HoverContainer>The [base case](hover:one) returns 1.```cppint factorial(int n) {if (n == 0) {// !hover onereturn 1;} else {// !hover tworeturn n * factorial(n - 1);}}```The [recursive case](hover:two) multiplies something.</HoverContainer>
The base case returns 1.
int factorial(int n) {if (n == 0) {return 1;} else {return n * factorial(n - 1);}}
The recursive case multiplies something.
Hover the mentions
Collapse
Fold and unfold blocks of code.
```js// !collapse(1:4)function lorem(ipsum, dolor = 1) {const sit = ipsum == null ? 0 : 1dolor = sit - amet(dolor)return sit ? consectetur(ipsum) : []}// !collapse(1:4) collapsedfunction ipsum(ipsum, dolor = 1) {const sit = ipsum == null ? 0 : 1dolor = sit - amet(dolor)return sit ? consectetur(ipsum) : []}```
const sit = ipsum == null ? 0 : 1dolor = sit - amet(dolor)return sit ? consectetur(ipsum) : []}}
Click on the function signature to collapse/expand the content of the function
Copy Button
Add a copy button to a code block.
```jsfunction lorem(ipsum, dolor = 1) {const sit = ipsum == null ? 0 : ipsum.sitdolor = sit - amet(dolor)return sit ? consectetur(ipsum) : []}```
function lorem(ipsum, dolor = 1) {const sit = ipsum == null ? 0 : ipsum.sitdolor = sit - amet(dolor)return sit ? consectetur(ipsum) : []}
Diff
Show inserted and deleted lines.
```jsfunction lorem(ipsum, dolor = 1) {const sit = ipsum == null ? 0 : ipsum.sit// !diff -dolor = ipsum - sit// !diff +dolor = sit - amet(dolor)return sit ? consectetur(ipsum) : []}```
function lorem(ipsum, dolor = 1) {const sit = ipsum == null ? 0 : ipsum.sit-dolor = ipsum - sit+dolor = sit - amet(dolor)return sit ? consectetur(ipsum) : []}
See Diff implementation.
File name
Show a title or file name with the code block.
```js index.jsfunction lorem(ipsum, dolor = 1) {const sit = ipsum == null ? 0 : 1dolor = sit - amet(dolor)return sit ? consectetur(ipsum) : []}```
function lorem(ipsum, dolor = 1) {const sit = ipsum == null ? 0 : 1dolor = sit - amet(dolor)return sit ? consectetur(ipsum) : []}
Focus
Focus blocks of code. Dim the unfocused code. Ensure the focused code is visible when there's overflow.
Useful when you want to change a codeblock focus depending on the user's interaction.
```jsfunction lorem(ipsum, dolor = 1) {const sit = ipsum == null ? 0 : ipsum.sitdolor = sit - amet(dolor)return sit ? consectetur(ipsum) : []}function ipsum(ipsum, dolor = 1) {return dolor}// !focus(1:5)function dolor(ipsum, dolor = 1) {const sit = ipsum == null ? 0 : ipsum.sitdolor = sit - amet(dolor)return sit ? consectetur(ipsum) : []}```
function lorem(ipsum, dolor = 1) {const sit = ipsum == null ? 0 : ipsum.sitdolor = sit - amet(dolor)return sit ? consectetur(ipsum) : []}function ipsum(ipsum, dolor = 1) {return dolor}function dolor(ipsum, dolor = 1) {const sit = ipsum == null ? 0 : ipsum.sitdolor = sit - amet(dolor)return sit ? consectetur(ipsum) : []}
See Focus implementation.
Fold
Fold inline content.
```jsx// !fold[/className="(.*?)"/gm]function Foo() {return (<div className="bg-red-200 opacity-50"><span className="block">hey</span></div>)}```
function Foo() {return (<div className=""><span className="">hey</span></div>)}
See Fold implementation.
Footnotes
Append footnotes to your code.
```rb# !ref Library importrequire 'sinatra'# !ref URL mappingget '/hi' do"Hello World!"end```
require 'sinatra'get '/hi' do"Hello World!"end
- Library import
- URL mapping
Language Switcher
Add a language picker when you need to switch between languages in a code block.
<CodeSwitcher>```java !!public class Main {public static int factorial(int n) {if (n == 0) {return 1;} else {return n * factorial(n - 1);}}}``````cpp !!int factorial(int n) {if (n == 0) {return 1;} else {return n * factorial(n - 1);}}``````scala !!object Main {def factorial(n: Int): Int = {if (n == 0) {return 1} else {return n * factorial(n - 1)}}}``````py !!def factorial(n):if n == 0:return 1else:return n * factorial(n - 1)``````matlab !!function result = factorial(n)if n == 0result = 1;elseresult = n * factorial(n - 1);endend``````js !!function factorial(n) {if (n === 0) {return 1} else {return n * factorial(n - 1)}}``````kotlin !!fun factorial(n: Int): Int {return if (n == 0) {1} else {n * factorial(n - 1)}}``````go !!func factorial(n int) int {if n == 0 {return 1} else {return n * factorial(n - 1)}}``````swift !!func factorial(n: Int) -> Int {if n == 0 {return 1} else {return n * factorial(n: n - 1)}}``````rust !!fn factorial(n: i32) -> i32 {if n == 0 {return 1;} else {return n * factorial(n - 1);}}``````fsharp !!let rec factorial n =if n = 0 then1elsen * factorial (n - 1)```</CodeSwitcher>
public class Main {public static int factorial(int n) {if (n == 0) {return 1;} else {return n * factorial(n - 1);}}}
Pick a different language from the dropdown to see the code block change.
Line Numbers
Display line numbers.
```jsfunction lorem(ipsum, dolor = 1) {const sit = ipsum == null ? 0 : 1dolor = sit - amet(dolor)return sit ? consectetur(ipsum) : []}```
1function lorem(ipsum, dolor = 1) {2const sit = ipsum == null ? 0 : 13dolor = sit - amet(dolor)4return sit ? consectetur(ipsum) : []5}
Link
Add links to code snippets.
```jsfunction lorem(ipsum, dolor = 1) {const sit = ipsum == null ? 0 : ipsum.sitdolor = sit - amet(dolor)// !link[/ipsum/] https://example.comreturn sit ? consectetur(ipsum) : []}```
function lorem(ipsum, dolor = 1) {const sit = ipsum == null ? 0 : ipsum.sitdolor = sit - amet(dolor)}
See Link implementation.
Mark
Highlight lines.
```jsfunction lorem(ipsum, dolor = 1) {// !markreturn dolor}function ipsum(lorem, dolor = 1) {// !mark(1:2) goldconst sit = lorem == null ? 0 : lorem.sitdolor = sit - amet(dolor)// !mark[/sit/] pinkreturn sit ? consectetur(lorem) : []}```
function lorem(ipsum, dolor = 1) {return dolor}function ipsum(lorem, dolor = 1) {const sit = lorem == null ? 0 : lorem.sitdolor = sit - amet(dolor)return sit ? consectetur(lorem) : []}
See Mark implementation.
Tabs
Show multiple code blocks in tabs.
<CodeWithTabs>```js !!tabs main.jsfunction lorem(ipsum, dolor = 1) {const sit = ipsum == null ? 0 : ipsum.sitdolor = sit - amet(dolor)return sit ? consectetur(ipsum) : []}``````css !!tabs styles.cssbody {margin: 0;padding: 0;}```</CodeWithTabs>
function lorem(ipsum, dolor = 1) {const sit = ipsum == null ? 0 : ipsum.sitdolor = sit - amet(dolor)return sit ? consectetur(ipsum) : []}
See Tabs implementation.
Token Transitions
Animate the transition between code blocks at a token level.
```scalaobject Main {def factorial(n: Int): Int = {if (n == 0) {return 1} else {return n * factorial(n - 1)}}}``````pythondef factorial(n):if n == 0:return 1else:return n * factorial(n - 1)```
object Main {def factorial(n: Int): Int = {if (n == 0) {return 1} else {return n * factorial(n - 1)}}}
Tooltip
A tooltip annotation where the tooltip content can be any MDX content.
<CodeWithTooltips>```js !code// !tooltip[/lorem/] descriptionfunction lorem(ipsum, dolor = 1) {const sit = ipsum == null ? 0 : ipsum.sitdolor = sit - amet(dolor)// !tooltip[/consectetur/] inspectreturn sit ? consectetur(ipsum) : []}```## !!tooltips description### Hello worldLorem ipsum **dolor** sit amet `consectetur`.Adipiscing elit _sed_ do eiusmod.## !!tooltips inspect```jsfunction consectetur(ipsum) {const { a, b } = ipsumreturn a + b}```</CodeWithTooltips>
function (ipsum, dolor = 1) {const sit = ipsum == null ? 0 : ipsum.sitdolor = sit - amet(dolor)return sit ? (ipsum) : []}
Hover (or focus) the lorem and consectetur tokens to see the respective tooltips.
Transpile
Sometimes you have a code block that you want to show together with its versions in different languages. It may be typescript and javascript, or sass and css, or maybe you want to transform a cURL command into calls to SDKs in different languages.
If you are using React Server Components and you have a function to transpile the code, you can call that function inside the component.
Here's an example showing how to transpile a typescript code block to javascript, and then showing both versions in tabs:
```tsinterface Greeter {greet(): string}function sayHello(greeter: Greeter) {console.log(greeter.greet())}```
interface Greeter {greet(): string}function sayHello(greeter: Greeter) {console.log(greeter.greet())}
Typescript
Include Typescript compiler's information in your code blocks using Typescript Twoslash.
```tsconst hi = "Hello"const msg = `${hi}, world`// ^?// @errors: 2588msg = 123```
const = "Hello"const = `${}, world`const msg: "Hello, world"= 123Cannot assign to 'msg' because it is a constant.
Word Wrap
Wrap lines to avoid horizontal overflow.
```jsfunction lorem(ipsum, dolor, sit) {ipsum.amet({ consectetur: [0, 1] },{adipiscing: elit.sed,eiusmod: "lorem ipsum dolor sit amet",sit,},)}```