• Stars
    star
    481
  • Rank 91,384 (Top 2 %)
  • Language
    TypeScript
  • License
    Other
  • Created almost 2 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple framework for webapps

Template

Template is a simple JS framework for creating interactive applications.

It focuses on using web-native patterns.

Calling it a framework is a bit of an exaggeration, it's a single class that manages HTML <template>s.

The entire "framework" is here: ./template.ts

Usage

Your Hello World example:

<!DOCTYPE >
<html>
  <head></head>
  <body>
    <div id="app"></div>
    <script type="module">
      import Template from "./template.js";
      // Create an HTMLTemplateElement from the text representation of HTML
      const html = Template.createElement(
        '<div><h1 class="message">Hello Template</h2></div>'
      );
      // Create an HTMLStyleElement from the text representation of a style node
      const css = Template.createStyle("<style></style>");
      class HelloWorld extends Template {
        constructor() {
          super(html, css);
        }
        setMessage(msg) {
          // We can use getElement to query for child nodes, in this case: class="message"
          // Anything you want to update during runtime should be stored on "this"
          const message = this.getElement(".message");
          // Update the content of <h1 class="message">
          message.innerText = msg;
        }
      }
      // Get the div we want to mount into
      const app = document.getElementById("app");
      // Create an instance of our HelloWorld component
      const helloworld = new HelloWorld();
      // Mount our component into the dom
      helloworld.mount(app);
      // Set our message
      helloworld.setMessage("Hello Template!");
    </script>
  </body>
</html>

How it works

Template uses HTML <template> to create reusable components.

We then "mount" a <template> into an element on the page by creating a shadow DOM.

The shadow DOM encapsulates the reusable components making sure that the CSS and JS from one component can not interfere with another.

Using .addChild(selector: string, child: Template) allows us to nest these components. The parent keeps track of all mounted children. When we unmount the parent, it will recursively unmount all children.

We follow React's philosophy of state flowing down. Our components manage their own children, configuring the child's state. Children manage updating their own DOM to reflect changes in state.

We also follow React's philosophy of state bubbling up. When a user interacts with a child node, the child node emits an event. The parent receives the event and decides what to do.

State management, event propogation, etc. are still early and need more thought.

Build process

You'll want to use a bundler like vite

For example:

index.html

<div class="WelcomeComponent">
  <div class="new button">
    <div class="icon"></div>
    <div class="title">Create New<br />Account</div>
  </div>
  <div class="pair button">
    <div class="icon"></div>
    <div class="title">Pair Existing<br />Account</div>
  </div>
</div>

index.css

.WelcomeComponent {
  background-color: hsla(0, 0%, 100%, 1);
  border-radius: 5px;
  display: flex;
  flex-direction: row;
}
.button {
  padding: 1em;
  margin: 0.5em;
  cursor: pointer;
  background-color: inherit;
  transition: background-color linear 0.1s;
  border-radius: inherit;
}
.button:hover {
  background-color: hsla(0, 0%, 90%, 1);
  transition: background-color linear 0.1s;
}

index.ts

import Template from "template";
import Feather from "feather-icons";
import html from "./index.html?raw";
import css from "./index.css?raw";

const template = Template.createElement(html);
const style = Template.createStyle(css);

class Welcome extends Template {
  constructor() {
    super(template, style);
  }
  mount(host: HTMLElement) {
    super.mount(host);
    this.getElement(".new > .icon").innerHTML =
      Feather.icons["user-plus"].toSvg();
    this.getElement(".new").addEventListener("click", () => {
      this.emit("new");
    });
    this.getElement(".pair > .icon").innerHTML =
      Feather.icons["smartphone"].toSvg();
    this.getElement(".pair").addEventListener("click", () => {
      this.emit("pair");
    });
    return this;
  }
}

export default Welcome;

More Repositories

1

peerweb.js

A client side library for fetching and rendering a static website via torrent over WebRTC
JavaScript
198
star
2

awesome-p2p

An awesome list for those interested in building decentralized applications
147
star
3

peerweb

Welcome to the Decentralized Web
JavaScript
118
star
4

getos

A Node.js module that returns the OS/Distribution name of the environment you are working on
JavaScript
78
star
5

dante

Build tests against Docker images by harnessing the power of layers
Go
44
star
6

dockeri.co

Badges for Docker
TypeScript
39
star
7

Taboo

CSS
36
star
8

presentation

JavaScript
27
star
9

wikiscroll

HTML
24
star
10

libfuse-template

A jumping off point for implementing a fuse file system
C
10
star
11

docker-webhook

JavaScript
8
star
12

isDev

A simple tool for determining if your node server is running in dev mode or not
JavaScript
7
star
13

docker-TrinityCore

Trinity Core files in a docker container
Shell
6
star
14

irc-sh

pure bash irc client
Shell
5
star
15

aws-mock-lambda-api-gateway

JavaScript
5
star
16

blob-to-stream

Convert a blob to a stream!
JavaScript
4
star
17

dagGen

A simple library for generating random directed acyclic graphs represented by adjacency matrices
Java
4
star
18

this-vc-does-not-exist

HTML
3
star
19

takeoff

JavaScript
3
star
20

awesome-prompt-engineering

3
star
21

knowledge

Documenting everything! Inspired by https://github.com/yoshuawuyts/knowledge/
3
star
22

kickass_nord

Generate OpenVPN configurations for Nord with fallbacks and bypasses
DIGITAL Command Language
3
star
23

build-container-sync

JavaScript
3
star
24

login-microservices

A collection of micro-services that solves login. Can be used for rapid application development.
JavaScript
3
star
25

theArtOfComputerProgramming

"The Art of Computer Programming" by Donald Knuth
Java
3
star
26

concurrent-request

JavaScript
3
star
27

nodedocs

Generate documentation from your cwd served up on localhost:8080
JavaScript
2
star
28

frea-primary-mirror

2
star
29

home_automation

Shell
2
star
30

marvin

An AI for handling async callbacks in JavaScript
JavaScript
2
star
31

rpi_setup

Download the latest raspbian lite image and get it ready for SSH
Shell
2
star
32

blockly-drones

JavaScript
2
star
33

buffered-reader

Buffer responses from asynchronous functions to increase the performance of Node.js applications
JavaScript
2
star
34

turn-it-up

Rust
2
star
35

peerweb-testing

Just testing the travis-ci configuration
JavaScript
2
star
36

ttlmap

{⏰} Map with TTL for values
JavaScript
1
star
37

todd

Sleeps on my couch and is helpful sometimes
JavaScript
1
star
38

awesome-projects

Projects I think are awesome
1
star
39

mpi-pi

A distributed monte-carlo algorithm for approximating pi using mpi.
C
1
star
40

npm-datamining

Scary datasets harvested from npm
JavaScript
1
star
41

peerweb-old

JavaScript
1
star
42

mded

Markdown Editor
JavaScript
1
star
43

siuPersonalWebsite

My Personal Homepage for SIUC
1
star
44

laptop

Lua
1
star
45

docker-iojs-test-server

1
star
46

frea-cli

JavaScript
1
star
47

stlaursWebsite

The Repository for STLAURS
PHP
1
star
48

retrohacker.github.io

HTML
1
star
49

goLex

A simple lexical analyzer written in Go
Go
1
star
50

varstring

A simple node library for replacing variables with strings in a string. Great for generating configuration files.
JavaScript
1
star
51

npm-change-resolve

JavaScript
1
star
52

docker-node-scratch

Node.js Docker Images....... From scratch!
Shell
1
star
53

json-types

TypeScript
1
star
54

objectify

JavaScript
1
star
55

owningTheStack

gimmicks for owningTheStack presentation
Shell
1
star
56

anki-terraform

Issue repository for my Terraform Anki deck
1
star
57

nodebuild

JavaScript
1
star
58

fuzzee-benchmarks

Run benchmarking tests to test the performance of fuzzee against jfuzzylogic
JavaScript
1
star
59

docker-integration

Jenkins test server for docker images
Shell
1
star
60

dotfiles_old

Contains a setup script for my dev environment and dotfiles for vim, xresources, and zsh configurations.
Python
1
star
61

node-template

An opinionated template to get a Node.js project up and running
JavaScript
1
star
62

documentation

storj documentation and tutorial
1
star
63

distributed_web

JavaScript
1
star
64

Minix

The incredibly lightweight RESTful routing package
JavaScript
1
star
65

new_website

JavaScript
1
star
66

CloudCompute2

C++
1
star
67

add-listener-first

A node module that ensures your EventEmitter listener will be run first
JavaScript
1
star
68

arduino-tests

Experimenting with writing tests for arduino code
C++
1
star
69

orgdeps

List the npm dependencies of a GitHub organization's public projects in a Unix friendly way
JavaScript
1
star
70

docker-npg

Docker script for building a node container with postgresql baked inside.
Shell
1
star
71

website-2015

Legacy website for blankenship.io
HTML
1
star
72

ofcourse

The backend REST server for ofCourse
JavaScript
1
star
73

old-dotfiles

C
1
star
74

npm-graphql

A graphql API for the npm registry
JavaScript
1
star
75

wedding

Wedding Website!
HTML
1
star
76

parse-docker-image-name

Aptly named module: given the name of a docker image, parse it into its components
JavaScript
1
star
77

kak-waka

KakouneScript
1
star
78

mpi-histogram

Generates a histogram of numbers using MPI.
C
1
star