Code

The Code component provides a reusable pattern for code in the interface.

Code block example

The code component can be used to display a block of code with syntax highlighting. In this example the language parameter is defined as php.

class Automobile
    {
        private $vehicleMake;
        private $vehicleModel;

        public function __construct($make, $model)
        {
            $this->vehicleMake = $make;
            $this->vehicleModel = $model;
        }

        public function getMakeAndModel()
        {
            return $this->vehicleMake . ' ' . $this->vehicleModel;
        }
    }

    class AutomobileFactory
    {
        public static function create($make, $model)
        {
            return new Automobile($make, $model);
        }
    }

Code block with description

The content parameter adds a descriptive paragraph above the code block, giving context about what the code does.

A helper function that formats a date string into a human-readable format.

function formatDate(string $date, string $format = 'Y-m-d'): string
    {
        return date($format, strtotime($date));
    }

HTML code block

Setting escape to true HTML-encodes the slot content before output, allowing HTML markup to be displayed as literal code rather than rendered by the browser.

<div class="c-card">
        <div class="c-card__header">
            <h2 class="c-card__title">Card title</h2>
        </div>
        <div class="c-card__body">
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
        </div>
    </div>

CSS code block

The language parameter accepts any Prism.js language identifier, such as css, to apply the correct syntax highlighting.

.c-button {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        padding: 0.5rem 1rem;
        border-radius: 0.25rem;
        font-weight: 600;
        cursor: pointer;
    }

    .c-button--primary {
        background-color: var(--color-primary);
        color: var(--color-white);
    }

API

Parameter Default Description
Undocumented code... -
echo 'Whoops, theres no code here. Where is it?' -
php -
false -
div -
pre -

CSS API

Variable Default Type Values Description
var(--border-radius) range - Border Radius
var(--corner-shape) select square, round, squircle, circular, bevel, scoop Corner Shape
var(--font-family-code) font - Code Font
var(--space) range - Base spacing unit. Used for padding and margin inside components.
var(--color--surface-border) color - Manual companion token for borders on surface elements.
var(--color--surface-alt) color - Manual companion token for subtle surface backgrounds.
var(--color--surface-contrast-muted) color - Derived muted contrast for text and icons on surface backgrounds.

Similar components