ObservedTS is a TypeScript-based web framework designed around events. It provides a set of utilities and classes to create and manage a web application.
To get started use the createApp to create the application and bind it to a root element.
function createApp(root: HTMLElement, config: AppConfig): AppAfter the app is created you can start it by calling the start function on the created app.
function start(): AppTo add pages to your app there's the page function. This function takes in a path and a Page provider (Which can be lazily loaded).
function page(path: string, loader: RouteLoader, navGuard?: NavGuard): AppThere is also the ability to add navguards to the routing which can allow, prevent or redirect the current routing.
A simple single page.
import { createApp, create, page } from 'observedts'
const root = document.querySelector("body");
const pageProvider = () => page({}, create("h1", {}, "Hello World!")) //Creates a page with a Hello World! header
const app = createApp(root, {}) //Create the app and attach it to the body
.page("/", pageProvider) //Adds page to the base route
.start(); //Starts the appTo create a basic element you use the create function.
function create<K extends keyof HTMLElementTagNameMap>(
type: K,
attributes: Record<string, AttrValue> = {},
...nodes: DomChild[]
): HTMLElementTagNameMap[K];To create elements for an svg there is the createSVG function.
function createSVG<K extends keyof SVGElementTagNameMap>(
type: K,
attributes: Record<string, AttrValue> = {},
...nodes: DomChild[]
): SVGElementTagNameMap[K];create("h1", {class: "m-3"}, "Hello World!");You can create an observer by using the observed function or class.
const count = observed(0);const count = new Observed<number>(0);You can subscribe to changes using the subscribe method.
count.subscribe(newValue => {
console.log(`Count changed to: ${newValue}`);
});You can also retrieve the current value using the get method.
console.log(`Count currently is: ${count.get()}`);You can update the observed value by calling the set method.
count.set(1); // Updates the value to 1 and triggers the subscription callbackIf you're working with nested values you can use the notify method to inform abount nested changes without having to set the value again.
const nestedData = observed({a: 10, b:20});
nestedData.get().b = 30;
nestedData.notify();You can create an container by using the container function or class.
const container = container();const container = new Container();You can add nodes to a container using the add method.
container.add(create("h1", {class: "m-3"}, "Hello World!"));You can remove nodes in a container using the removeNodes method.
const header = create("h1", {class: "m-3"}, "Hello World!");
container.add(header);
...
container.removeNodes(header);You can clear the entire container using the clear method.
container.clear();You can swap the contents between 2 containers using the swap method
container.swap(container2)You can use the foreach function to instantiate a template for each element in the array.
export function foreach<T, K>(
arr: Observed<T[]>,
key: (item: T, index: number) => K,
template: (api: {
index: () => number;
get: () => T;
set: (val: T | undefined) => void;
notify: () => void;
subscribe: (fn: (value: T) => void | boolean, cleanup?: () => void) => void;
}) => Container
): Container;The template instance has access to the same functions as a normal observed variable, but it also has access to its index and an optional cleanup method.
foreach(nav, (_, index) => index, (api) => container(
create('li', {class: "relative mt-6"},
create("h2", {class: "text-xs font-semibold text-zinc-900 dark:text-white"}, api.get().name),
create("ul", {role: "list", class: "relative mt-3 ml-2 space-y-3 lg:space-y-2 border-l"},
...
)
)
))There is a conditional function that helps you create a container which is only visible when the condition is met.
function conditional(condition: Observed<boolean>, ...nodes: DomChild[]): Container;There is a createTransition funtion that helps create a conditional container with a smooth animation.
function createTransition<K extends keyof HTMLElementTagNameMap>(
options: TransitionOptions,
type: K,
attributes: Record<string, AttrValue> = {},
...nodes: DomChild[]
)There is a store function to store data globally.
function store(key: string, val?: any)There is a debounce function to debounce input.
function debounce(fn: (...args: any[]) => void, timeout = 300)There is a lazy function to lazy load data.
function lazy<T>(cb: (() => T)): Lazy<T>