nicbarker /clay

High performance UI layout library in C.

★ 18,418⑂ 748👁 102⑃ 324● 337▤ 5
CZlibCreated 2024-07-21nicbarker.com/clay
Download: 0.14 - Aspect Ratio Elements, Better Clipping0.14 ↓0.142025-06-06
+1
Daily
18,418
stars
748
forks
102
watchers
324
prs
337
issues
5
releases

README

Clay, A UI Layout Library
_Clay_ (short for C Layout) is a high performance 2D UI layout library.

 Major Features
- Microsecond layout performance
- Flex-box like layout model for complex, responsive layouts including text wrapping, scrolling containers and aspect ratio scaling
- Transition API for easy layout animations
- Single 4.8k LOC clay.h file with zero dependencies (including no standard library linking)
- Wasm support: compile with clang to a 15kb uncompressed .wasm file for use in the browser
- Static arena based memory use with no malloc / free, and low total memory overhead (e.g. ~3.5mb for 8192 layout elements).
- React-like nested declarative syntax
- Renderer agnostic: outputs a sorted list of rendering primitives that can be easily composited in any 3D engine, and even compiled to HTML (examples provided)

Take a look at the clay website for an example of clay compiled to wasm and running in the browser, or others in the examples directory.

You can also watch the introduction video for an overview of the motivation behind Clay's development and a short demo of its usage.



_An example GUI application built with clay_

 Quick Start

Download or clone clay.h and include it after defining CLAY_IMPLEMENTATION in one file.

C
// Must be defined in one file, _before_ include "clay.h"
define CLAY_IMPLEMENTATION
include "../../clay.h"

const Clay_Color COLOR_LIGHT = (Clay_Color) {224, 215, 210, 255};
const Clay_Color COLOR_RED = (Clay_Color) {168, 66, 28, 255};
const Clay_Color COLOR_ORANGE = (Clay_Color) {225, 138, 50, 255};

void HandleClayErrors(Clay_ErrorData errorData) {
    // See the Clay_ErrorData struct for more information
    printf("%s", errorData.errorText.chars);
    switch(errorData.errorType) {
        // etc
    }
}

// Example measure text function
static inline Clay_Dimensions MeasureText(Clay_StringSlice text, Clay_TextElementConfig config, uintptr_t userData) {
    // Clay_TextElementConfig contains members such as fontId, fontSize, letterSpacing etc
    // Note: Clay_String-chars is not guaranteed to be null terminated
    return (Clay_Dimensions) {
            .width = text.length  config-fontSize, // fontSize
    };
}

// Layout config is just a struct that can be declared statically, or inline
Clay_ElementDeclaration sidebarItemConfig = (Clay_ElementDeclaration) {
    .layout = {
        .sizing = { .width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_FIXED(50) }
    },
    .backgroundColor = COLOR_ORANGE
};

// Re-useable components are just normal functions
void SidebarItemComponent() {
    CLAY(id, sidebarItemConfig) {
        // children go here...
    }
}

int main() {
    // Note: malloc is only used here as an example, any allocator that provides
    // a pointer to addressable memory of at least totalMemorySize will work
    uint64_t totalMemorySize = Clay_MinMemorySize();
    Clay_Arena arena = Clay_CreateArenaWithCapacityAndMemory(totalMemorySize, malloc(totalMemorySize));

    // Note: screenWidth and

Top issues

Latest release

0.14 - Aspect Ratio Elements, Better Clipping v0.14 2025-06-06
New Features

 Non-image aspect ratio scaling elements, image stretching

Previously, only image elements supported aspect ratio scaling. The new .aspectRatio element configuration option allows you to specify a fixed width / height aspect ratio for any element. This also allows you to create image elements with no specified aspect ratio, allowing for stretched / distorted images with a different aspect ratio to their source dimensions. Different images rendering modes similar to the CSS concepts of contain and cover, as well as 9-slice support are scheduled for 0.15.

C
// Old
CLAY({

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON