Giter VIP home page Giter VIP logo

flashdrive's Introduction

Flashdrive

A lazy web framework

Use

  • Import flashdrive.min.js
<script src="flashdrive.min.js"></script>
  • Add a root anchor to the page
<div id="root"></div>
  • Set up main() in another JS file as the entry point
function main(fd){
    ...
}
  • Optionally, import the stylesheet
<style src="style.css"></style>

Reference

flash is all about creating small "cards" that the user navigates between. There isn't any layout, each card is rendered top to bottom and can be styled by css

All rendering hapens inside of JavaScript functions. Each function is structured like this:

function card(fd){
    return () => {
        
    }
}

Where the function returned is called every time the component renders, from the wrapper function context.

When the card first appears on the screen, everything inside the containing function is called once. If you need to initalize variables, do it there.

The entire state of each card is held in fd.state. All form inputs are bound to this variable, and you can also store any additional application state as well. If any state variable that you stored changes, the function returned from the card function is called, and the entire card rerenders. Unless you clear it out manually, state will also persist between all cards.

If you need to, you can also trigger a rerender with fd.refresh()

title

Usage:

fd.title("My awesome title")

Sets the title of the current card

heading

Usage:

fd.heading("Heading",headingLevel)

Displays a heading on the card at the given headingLevel

text

Usage:

fd.text("Goodbye, moon!")

Displays a line of text in the card

link

Usage:

fd.link("Link title", nextCardFunction, toPass)

Displays a link that will redirect the user to the next card in the stack. toPass should be set to anything you want to pass to the next card.

extLink

Usage:

fd.extLink("Link title", "https://www.example.com")

Displays a link that will redirect the user to an external website

goto

Usage:

fd.goto(nextCardFunction, toPass)

When called, redirects the user to the next card. toPass behaves the same as it does in link.

field

Usage:

fd.field("title", "type", "stateKey", validationFunc)

Displays an HTML input field with a title. The type directly maps to all HTML input field types. When the field value changes, the key in the state represented by stateKey will also change.

To handle validation, you can pass a function into validationFunc that will be called when the user exits the field. It should be structured like this:

let validationFunc = (newValue) => {
    let isValid = false;
    return [isValid, "message"];
}

If isValid is false, the validation message is shown. Otherwise, we don't show any validation message.

pick

Usage:

fd.pick("title", ["option 1", "option 2"], "stateKey")

Displays an HTML select element with a title. Options in the select are defined by the array in the second parameter, and by default, the key in the state is set to the first option. Like field, the key in the state represented by stateKey will change when the selected value changes

button

fd.button("Label", functionToCall)

Displays a button that will call a function

Examples

A counter

function main(fd){
	function increase(){
		fd.state.sum++;
	}

    function decrease(){
		fd.state.sum--;
	}

	fd.state.sum = 0

	return () => {
		fd.title("counters");
		fd.text(`Sum is ${fd.state.sum}`);
		fd.button("add 1", increase);
        fd.button("subtract 1", decrease)
	};
}

flashdrive's People

Contributors

quin2 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.