DEV Community

Cover image for 88 UI components without writing HTML or CSS: how basic.js works
Buğra Özden
Buğra Özden

Posted on

88 UI components without writing HTML or CSS: how basic.js works

Most UI work on the web is split across three languages: HTML for the structure, CSS for the look, JavaScript for the behavior. Then a framework and a build tool come on top to glue them back together.

For small projects I wanted something simpler. In 2020 I started basic.js, a small library where the whole interface is written in plain JavaScript. Today it carries OzdenUI: 88 components, a catalog with 228 live examples (each with its code) and a few app templates.

This post shows how it works, what it is good for, and where it is the wrong tool.

The idea

basic.js makes DOM objects directly. You call a function, an object appears on the page:

  • no HTML markup (the page body is empty),
  • no CSS authoring (the look is set with properties: color, round, border, fontSize...),
  • no virtual DOM, no build step, no dependencies.

Every page is a single .htm file that loads a few scripts. You open it in a browser and it runs. The minified library is about 48 KB (15 KB gzipped).

A small page

Here is a complete page with a title and two buttons. SmartButton and ScrollArea are two of the components:

<link rel="stylesheet" href="basic/basic.css">
<script src="basic/basic.js"></script>
<script src="basic/scroll-bar.js"></script>
<script src="comp/smart-button.js"></script>
<script src="comp/scroll-area.js"></script>

<script>
const start = function () {

    page.color = "#F4F5F2";

    ScrollArea({ align: "center top", gap: 24, padding: 40 });

        Label({ text: "Profile", fontSize: 24, bold: 1, textColor: Black(0.8) });

        HGroup({ hug: 1, gap: 10 });
            SmartButton({ labelText: "Cancel", onClick: function () { println("Canceled."); } });
            SmartButton({ tone: "cyan", labelText: "Save", onClick: save });
        endGroup();

    endGroup();
};

const save = function (self) {
    self.setWaiting(1);                 // the button shows that a job runs
    self.after(1500, function () {
        self.setWaiting(0);
        self.setTone("green");
        self.setLabelText("Saved");
    });
};
</script>
Enter fullscreen mode Exit fullscreen mode

A few things to notice:

  • start() is called by basic.js when the page is ready. The global page is the root box.
  • Implicit containers. HGroup(...) opens a horizontal group (a flexbox), and every object made after it goes inside, until endGroup(). The indentation of the code is the shape of the UI. Every start call has its end call, and basic.js warns you when one is missing.
  • Props instead of CSS. gap, padding, align, hug: 1 (the group wraps its content), color, round... are plain properties. You can still pass css: { ... } when you need something special.
  • Objects are just objects. SmartButton(...) returns the button. You keep it in a variable and call its setters later: setTone(), setLabelText(), setWaiting().
  • Cleanup through the object. self.after(ms, fn) is a timeout that belongs to the button: when the button is removed, the timer goes with it.

The core objects are few: Box, Label, Button, Input, Icon, and the groups HGroup / VGroup. They take (left, top, width, height) and a props object last, so Box(0, 0, "100%", "100%", { color: "red" }) and Label({ text: "Hi", fontSize: 16 }) both work.

How a component is made

A component is a function with a defaults object. Every default becomes a public property on the component's box, and values change later through setX() functions. Here is a tiny one, a tag:

const TagDefaults = {
    width: 120,
    height: 28,
    labelText: "",
    onClick: function (self) {},
};

const Tag = function (params = {}) {
    mergeIntoIfMissing(params, TagDefaults);   // fills the missing keys
    let box = startObject(params);             // the container: children go inside

    // *** PUBLIC FUNCTIONS
    box.setLabelText = function (value) {
        box.labelText = value;
        box.label.plainText = value;           // a text from outside goes in as plain text
    };

    // *** OBJECT VIEW
    box.color = "#E3F2F4";
    box.round = 14;
    box.label = Label({ width: "100%", top: 5, textAlign: "center", fontSize: 13 });

    // *** OBJECT INIT
    box.clickable = 1;
    box.on("click", function () { box.onClick(box); });
    box.setLabelText(box.labelText);

    return endObject(box);                     // closes the container
};

Tag({ labelText: "New", onClick: function (self) { println(self.labelText); } });
Enter fullscreen mode Exit fullscreen mode

That is the whole pattern: XxxDefaults, startObject(), the public setters, the view, the init code, endObject(). The real components in the repository follow the same skeleton, with a few more rules: style packages (light / dark and others, chosen with styleName), keyboard focus and role / aria props, and cleanup that happens on its own when you call remove().

Because a component is only a function, you can read one from top to bottom in one file. There is no hidden lifecycle, no template compiler, no state library.

What is in the kit

  • Fields (text, email, phone, password, number, currency...), check boxes, radio buttons, sliders, dropdowns.
  • Lists, a table for very many rows, charts, tree views, tabs, breadcrumbs.
  • Dialogs, tooltips and popovers, toasts, a loading screen, side panels.
  • Light and dark looks for each, plus a small "minimal" theme.
  • Templates: an admin panel, mobile app templates (tabs, pages, a side menu), web forms.

Every component has at least one live sample in the catalog, and the code of the sample is shown next to it, so you can copy it into your own page.

What it is good for (and what not)

Good for:

  • Small tools and internal apps: an admin screen, a dashboard, a form, a prototype. You open a file and start.
  • Beginners: one language, one file, the result in the browser at once. There is no toolchain to install before the first button.
  • Long-lived small projects: with no dependencies and no build step, a page written today still opens in five years.

Not good for:

  • Content sites that need SEO. The page is drawn by JavaScript, so a blog or a store page is better served as HTML.
  • A replacement for React (or Vue, Svelte...) in a big app. There is no reactive state system, no server rendering, no large ecosystem. For a big team product, use a framework.
  • Pixel-perfect designs from a design tool. The components have their own look; you can change it through style packages, but it is not a Figma-to-code pipeline.

I would rather say this clearly than have someone find it out after a week.

How it was built

basic.js started in 2020 and grew slowly with my own projects. Much of the recent work (many components, the samples, the tests, the catalog site) was done with Claude Code; the design and the rules of the library are mine. The repository has a test page for the components and their behavior, and tools that compare the screenshots of every page before and after a change.

Try it

I would like to hear what feels strange about this approach, and which components you would miss. Feedback on the API is very welcome.

Top comments (0)