A featherweight, functional frontend JS framework

Overview

flub.js

With inspiration from React and Flutter, flub is a minimal JS framework (core sits little over 1 kB) for quickly prototyping basic JS frontend apps. Born purely out of the hatred of working with vanilla JS, and all the hassle of setting up and deploying frameworks like React, Vue, or Angular; flub is meant to be a helper where other, more heavy handed solutions might not be warranted.

Some advantages of flub.js:

  • Vanilla JS, no external dependancies, components are just POCO functions, no compilation, virtual DOM, translation, ect
  • Extensible, since it's just functions, you can easily wrap components in your own functions to give them default properties and behaviour
  • Simple, no surprises, no new API's to learn, functions simply pass their props as HTML attributes:
    Img({ src: '...', class: 'apple' }) === <img src="..." class="apple" />
  • Stupid simple state, setState and useCreation are provided to your component to update a components own state. The component is completely re-rendered with each state update
  • Easy to integrate and isolate, your components can attach to specific HTML elements, your entire app doesn't have to be in flub
  • Tiny, there's really not much going on here.

UNPKG

Import production core and components libraries from UNPKG

import { App, Frag } from 'https://unpkg.com/flub.js/dist/core.js';
import { Row, Text } from 'https://unpkg.com/flub.js/dist/components.js';

Installation

Point to your index.js as a module in your index.html

<body>
  <!-- ... -->

  <script src="index.js" type="module"></script>
</body>

flub provides a core library, that gives you access to underlying supporting elements such as Div, P, A, ect. They all follow a format of Elm({ children: [], props }).

In your index.js, import flub and scaffold your app

import { App, Div } from 'flub/core.js';

// Our App's entry point
App(document.body, { children: [
  Div({ text: "Hello world!" })
]});

flub also provides a components module which adds support for common positional arguments to reduce bracket clutter, as well as some helpful components

import { Box, FauxLink, Btn, Row, Text } from 'flub/components.js';

function Home({ name = 'default' }, { setState, useCreation }) {
  useCreation(() => console.log("This will only run on first render"));

  console.log("However this will run every render");

  // Box is a Div, takes element or an array of elements
  return Box([
    // FaxuLink is an anchor with an onclick listener
    FauxLink(`Hello ${name}!`, () => alert(`Hello ${name}!`)),
    Btn("Click to change name", () =>
      // To update the component's state, call setState provided in the function params
      setState({ name: "flub" })
    ),
    // Row is a flexbox
    Row([
      // Text is a p tag
      Text('Item 1'),
      // We can change the type of tag to be a header
      Text('Item 2', { type: 'h2' })
    ], { gap: '12px' })
  ]);
}

Usage

At it's core, our most basic object is a Frag. Each Frag has a type, and children. A child can be a DOM Element like so:

App(document.body, { children: [
  Div({ text: "Hello world!" })
]});

Or a function signature, which gives us access to the setState function, which lets us update our component's state by completely re-rendering the component in the DOM

import { App, Div, P } from 'flub/core.js';

App(document.body, { children: [
  Home
]});

function Home({ name = 'default' }, { setState }) {
  return Div({ children: [
    P({ text: `Hello ${name}!` }),
    Button({ text: "Click to change name", onclick: () => {
      setState({ name: "flub" });
    }})
  ]});
}

Examples

Here's a simple Counter app using flub components!

import { App } from 'https://unpkg.com/flub.js/dist/core.js';
import { Row, Btn, Text } from 'https://unpkg.com/flub.js/dist/components.js';

App(document.body, { children: [
  Counter
]});

function Counter({ count = 0 }, { setState }) {
  return Row([
    // Since we want to pass 'count' to a child component,
    //  we have to supply the prop, and it's hooks (so it can access setState, and useCreation!)
    (_, hooks) => CounterLabel({ ..._, count }, hooks),

    Btn("+", () =>
      setState({ count: count + 1 })
    ),

    Btn("-", () =>
      setState({ count: count - 1 })
    )
  ], { gap: '2px', alignItems: 'center' });
}

function CounterLabel({ count = 0 }) {
  return Text(`Count: ${count}`, { type: 'h1' });
}

Check out the SnapShot flub app for a more complete example!

Edit lingering-darkness-640hgr

Footnotes

Just an experiment! Major API changes inbound, not for production use.

You might also like...

A repository demonstrating functional techniques with C# 10 and the similarities between JavaScript, TypeScript, and C#.

A repository demonstrating functional techniques with C# 10 and the similarities between JavaScript, TypeScript, and C#.

Building up from JavaScript to TypeScript to C# 10 and .NET 6 This repository is meant to highlight some of the various functional techniques availabl

Dec 30, 2022

This is a (pretty broken, but mostly functional) organic-shaped jigsaw generator with custom border support

OrganicPuzzleJs This is a (pretty broken, but mostly functional) organic-shaped jigsaw generator with custom border support. It relies on two linbrari

Dec 10, 2022

Functional Programming with NestJS, Prisma. immutable, pure, stateless

Functional-NestJS Functional Programming with NestJS, Prisma. immutable, pure, stateless. 1. Introduction A production ready typescript backend reposi

Dec 6, 2022

🀠 Functional utilities using object property paths with wildcards and regexps 🌡

🀠 Functional utilities using object property paths with wildcards and regexps. 🌡 Available functional methods include: πŸ—ΊοΈ Mapping: map() πŸš‚ Merging

Dec 15, 2022

Typescript library for functional programming.

Sa Lambda Typescript library for functional programming. Document TODO Either Maybe Iterator Pipe & Flow Task (Promise-Like) some math utils Installat

Dec 6, 2022

A RabbitMQ client for TypeScript, with functional programming in mind.

A RabbitMQ client for TypeScript, with functional programming in mind.

RabbitMQ-fp Lets feed our Rabbit' nicely 🐰 This repository contains a wrapper over amqplib written in Typescript with an accent of functionnal progra

Sep 6, 2022

This is a fully functional DAO, a Web3 project made using Solidity, Hardhat, JS, Next.js, Openzeppelin, CSS, HTML, using the Rinkerby network!

My First DAO! This is made for a DAO-Tutorial via learnweb3.io This is a DAO, a decentralised autonomous organisation, essentially a more collective a

Jun 20, 2022

A functional library for watermarking images in the browser

A functional library for watermarking images in the browser. Written with ES6, and made available to current browsers via Babel. Supports urls, file inputs, blobs, and on-page images.

Dec 27, 2022

A utility for creating toggleable items with JavaScript. Inspired by bootstrap's toggle utility. Implemented in vanillaJS in a functional style.

LUX TOGGLE Demo: https://jesschampion.github.io/lux-toggle/ A utility for creating toggleable dom elements with JavaScript. Inspired by bootstrap's to

Oct 3, 2020
Web3 Framework, 0% Backend, 10000% Frontend

β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆ β–ˆβ–ˆ β–ˆβ–ˆ β–ˆβ–ˆ β–ˆβ–ˆ β–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆ β–ˆβ–ˆ β–ˆβ–ˆ β–ˆβ–ˆ β–ˆβ–ˆ STACK β–ˆβ–ˆ β–ˆβ–ˆ β–ˆβ–ˆ β–ˆβ–ˆ β–ˆβ–ˆ β–ˆβ–ˆ Network Without β–ˆβ–ˆ β–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ

Tony Ivanov 28 Dec 9, 2022
Simple components showing differences in each major frontend web framework.

Web Frameworks A collection of simple components in each of the leading frontend web frameworks. So far, covering React, Vue, Angular, Svelte and Soli

Daniel Still 21 Nov 27, 2022
A frontend framework containing of tools for fast development of dashboard, panel, etc.

Khaos-Admin A frontend framework containing of tools for fast development of dashboard, panel, etc. Tools We Provide Fast Development: We handle data

Hamrah Mechanic 7 Nov 27, 2022
Leafjs is a lightweight frontend framework built using web components and browser ES Modules.

Leafjs A lightweight, fast web-components based frontend framework for the future. Introduction Leafjs is a lightweight frontend framework built using

Sam Zhang 29 Dec 23, 2022
Jargon from the functional programming world in simple terms!

Functional Programming Jargon Functional programming (FP) provides many advantages, and its popularity has been increasing as a result. However, each

hemanth.hm 18.1k Jan 4, 2023
a full functional discord bot to send moomoo.io bots

MooMooBot a discord bot which does moomoo.io stuff commands !send <server> <name> ex: !send 8:0:0 Nuro !token (generates a token to test if there's n

Nuro 4 Jun 1, 2022
Functional reactive UI library

reflex Reflex is a functional reactive UI library that is heavily inspired by (pretty much is a port of) elm and it's amazingly simple yet powerful ar

Mozilla 364 Oct 31, 2022
Firebase Angular Skeleton - Quickly create an application with a fully functional authentication, authorization and user management system.

FAngS - Firebase Angular Skeleton FAngS lets you quickly create an application with a fully functional authentication, authorization and user manageme

Ryan Lefebvre 7 Sep 21, 2022
A fun and functional way to write regular expressions (RegExp)

funexp A fun and functional way to write regular expressions (RegExp). FunExp is a useful tool for larger projects that depends on RegExp to do heavy

Matheus Giovani 2 Feb 7, 2022
Fun Ξ» functional programming in JS

fp-js JavaScript Functional Programming Motivation This purposed for learning functional programming (just that). Features Auto-Curry Option Tooling s

RiN 6 Feb 4, 2022