Skip to content
MarkasUI
Type to search

    Guide

    MarkasUI

    Form

    Feedback

    Overlay

    Navigation

    Data

    Sections

    Animations

    Backgrounds

    Buttons

    Templates

    Pages

    Code Block

    Monospace code block with an optional file name, line numbers, and a copy button. Plain text, no syntax highlighting.

    format-price.ts
    export function formatPrice(amount: number) {  return new Intl.NumberFormat("en-US", {    style: "currency",    currency: "USD",  }).format(amount);}
    format-price.ts
    export function formatPrice(amount: number) {  return new Intl.NumberFormat("en-US", {    style: "currency",    currency: "USD",  }).format(amount);}
    format-price.ts
    export function formatPrice(amount: number) {  return new Intl.NumberFormat("en-US", {    style: "currency",    currency: "USD",  }).format(amount);}
    format-price.ts
    export function formatPrice(amount: number) {  return new Intl.NumberFormat("en-US", {    style: "currency",    currency: "USD",  }).format(amount);}
    format-price.ts
    export function formatPrice(amount: number) {  return new Intl.NumberFormat("en-US", {    style: "currency",    currency: "USD",  }).format(amount);}
    @php
        $code = <<<'CODE'
    export function formatPrice(amount: number) {
      return new Intl.NumberFormat("en-US", {
        style: "currency",
        currency: "USD",
      }).format(amount);
    }
    CODE;
    @endphp
    
    <x-markas.code-block :code="$code" title="format-price.ts" />
    import { CodeBlock } from "@/components/markas/CodeBlock";
    
    const code = [
      "export function formatPrice(amount: number) {",
      '  return new Intl.NumberFormat("en-US", {',
      '    style: "currency",',
      '    currency: "USD",',
      "  }).format(amount);",
      "}",
    ].join("\n");
    
    export default function CodeBlockExample() {
      return <CodeBlock code={code} title="format-price.ts" />;
    }
    <script setup lang="ts">
    import CodeBlock from "@/components/markas/CodeBlock.vue";
    
    const code = [
      "export function formatPrice(amount: number) {",
      '  return new Intl.NumberFormat("en-US", {',
      '    style: "currency",',
      '    currency: "USD",',
      "  }).format(amount);",
      "}",
    ].join("\n");
    </script>
    
    <template>
      <CodeBlock :code="code" title="format-price.ts" />
    </template>
    <script lang="ts">
      import CodeBlock from "$lib/components/markas/CodeBlock.svelte";
    
      const code = [
        "export function formatPrice(amount: number) {",
        '  return new Intl.NumberFormat("en-US", {',
        '    style: "currency",',
        '    currency: "USD",',
        "  }).format(amount);",
        "}",
      ].join("\n");
    </script>
    
    <CodeBlock {code} title="format-price.ts" />
    ---
    import CodeBlock from "@/components/markas/CodeBlock.astro";
    
    const code = [
      "export function formatPrice(amount: number) {",
      '  return new Intl.NumberFormat("en-US", {',
      '    style: "currency",',
      '    currency: "USD",',
      "  }).format(amount);",
      "}",
    ].join("\n");
    ---
    
    <CodeBlock code={code} title="format-price.ts" />
    import { Component } from "@angular/core";
    import { CodeBlock } from "./markas/code-block.component";
    
    @Component({
      selector: "markas-example",
      imports: [CodeBlock],
      template: `<div
        markas-code-block
        [code]="code"
        title="format-price.ts"
      ></div>`,
    })
    export default class CodeBlockExample {
      protected readonly code = [
        "export function formatPrice(amount: number) {",
        '  return new Intl.NumberFormat("en-US", {',
        '    style: "currency",',
        '    currency: "USD",',
        "  }).format(amount);",
        "}",
      ].join("\n");
    }

    Examples

    Line numbers

    Counter.tsx
    import { useState } from "react";export function Counter() {  const [count, setCount] = useState(0);  const add = () => setCount(count + 1);  return (    <button onClick={add}>      Clicked {count} times    </button>  );}
    Counter.tsx
    import { useState } from "react";export function Counter() {  const [count, setCount] = useState(0);  const add = () => setCount(count + 1);  return (    <button onClick={add}>      Clicked {count} times    </button>  );}
    Counter.tsx
    import { useState } from "react";export function Counter() {  const [count, setCount] = useState(0);  const add = () => setCount(count + 1);  return (    <button onClick={add}>      Clicked {count} times    </button>  );}
    Counter.tsx
    import { useState } from "react";export function Counter() {  const [count, setCount] = useState(0);  const add = () => setCount(count + 1);  return (    <button onClick={add}>      Clicked {count} times    </button>  );}
    Counter.tsx
    import { useState } from "react";export function Counter() {  const [count, setCount] = useState(0);  const add = () => setCount(count + 1);  return (    <button onClick={add}>      Clicked {count} times    </button>  );}

    Terminal commands

    Terminal
    pnpm add markasuipnpm markasui initpnpm markasui add carousel
    Terminal
    pnpm add markasuipnpm markasui initpnpm markasui add carousel
    Terminal
    pnpm add markasuipnpm markasui initpnpm markasui add carousel
    Terminal
    pnpm add markasuipnpm markasui initpnpm markasui add carousel
    Terminal
    pnpm add markasuipnpm markasui initpnpm markasui add carousel

    Without title

    {  "name": "acme-dashboard",  "private": true,  "scripts": {    "dev": "vite",    "build": "vite build"  }}
    {  "name": "acme-dashboard",  "private": true,  "scripts": {    "dev": "vite",    "build": "vite build"  }}
    {  "name": "acme-dashboard",  "private": true,  "scripts": {    "dev": "vite",    "build": "vite build"  }}
    {  "name": "acme-dashboard",  "private": true,  "scripts": {    "dev": "vite",    "build": "vite build"  }}
    {  "name": "acme-dashboard",  "private": true,  "scripts": {    "dev": "vite",    "build": "vite build"  }}

    Long lines

    create-invoice.sh
    curl -X POST https://api.example.com/v1/invoices -H "Authorization: Bearer $API_TOKEN" -d '{"customer":"cus_1024","amount":25000}'
    create-invoice.sh
    curl -X POST https://api.example.com/v1/invoices -H "Authorization: Bearer $API_TOKEN" -d '{"customer":"cus_1024","amount":25000}'
    create-invoice.sh
    curl -X POST https://api.example.com/v1/invoices -H "Authorization: Bearer $API_TOKEN" -d '{"customer":"cus_1024","amount":25000}'
    create-invoice.sh
    curl -X POST https://api.example.com/v1/invoices -H "Authorization: Bearer $API_TOKEN" -d '{"customer":"cus_1024","amount":25000}'
    create-invoice.sh
    curl -X POST https://api.example.com/v1/invoices -H "Authorization: Bearer $API_TOKEN" -d '{"customer":"cus_1024","amount":25000}'

    Installation

    npx markasui@latest add code-block

    Usage

    @php
        $code = <<<'CODE'
    pnpm add markasui
    pnpm markasui add carousel
    CODE;
    @endphp
    
    <x-markas.code-block :code="$code" title="Terminal" />

    Needs Alpine.js for the copy button.

    import { CodeBlock } from "@/components/markas/CodeBlock";
    
    const code = `pnpm add markasui
    pnpm markasui add carousel`;
    
    <CodeBlock code={code} title="Terminal" />;
    <script setup lang="ts">
    const code = `pnpm add markasui
    pnpm markasui add carousel`;
    </script>
    
    <template>
      <CodeBlock :code="code" title="Terminal" />
    </template>
    <script lang="ts">
      import CodeBlock from "$lib/components/markas/CodeBlock.svelte";
    
      const code = `pnpm add markasui\npnpm markasui init`;
    </script>
    
    <CodeBlock {code} title="Terminal" showLineNumbers />
    ---
    import CodeBlock from "@/components/markas/CodeBlock.astro";
    
    const code = `pnpm add markasui\npnpm markasui init`;
    ---
    
    <CodeBlock code={code} title="Terminal" showLineNumbers />

    A small bundled script handles the copy button of every code block on the page.

    import { Component } from "@angular/core";
    import { CodeBlock } from "./markas/code-block.component";
    
    @Component({
      selector: "app-example",
      imports: [CodeBlock],
      template: `
        <div markas-code-block [code]="code" title="Terminal" showLineNumbers></div>
      `,
    })
    export class Example {
      code = "pnpm add markasui\npnpm markasui init";
    }
    • Code is plain text without syntax highlighting, so it stays small and identical in every framework.
    • Add showLineNumbers (show-line-numbers in Blade and Vue) for longer snippets.
    • The copy button uses navigator.clipboard, which needs HTTPS or localhost.

    Props

    NameTypeDefaultDescription
    codestring-The code to show and copy. Passed as a string so all three frameworks render the same lines.
    titlestring-Optional file name or caption shown in the header.
    showLineNumbersbooleanfalseShow line numbers. They are hidden from screen readers and not selectable.
    copyLabelstringCopy codeText of the copy button.
    copiedLabelstringCopiedText shown for two seconds after the code is copied.

    Accessibility

    • The pre element has tabindex=0, so keyboard users can focus it and scroll long lines horizontally.
    • The copy button is a native button with visible text, and the copied message is announced through aria-live.
    • Line numbers are aria-hidden so screen readers read only the code.

    Related components

    More data components with the same classes in every framework.