npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hyperapp-r

v2.0.4

Published

The tiny framework for building web interfaces. (forked from hyperapp)

Downloads

3

Readme

Hyperapp-R

Hyperapp-R is fork from Hyperapp V2.

(Version note: Hyperapp-R 2.0.4 = Hyperapp 2.0.3)

Features

  • 99% backword compatibility -- Libraries and middleware for Hyperapp V2 can be used in Hyperapp-R as well
  • Return of functions from Hyperapp V1
    • Ressurection of DOM event handlers -- oncreate, onupdate, onremove, ondestroy
    • Ressurection of style attribute by string -- <div style="margin-top: 10em;"></div>
  • TypeScript full support
  • Small change
    • Distribution package uses Common JS format instead of ES Module (This will avoid errors when using tools like jest)

Install

# for NPM
$ npm install --save-dev hyperapp-r

# for Yarn
$ yarn add -D hyperapp-r

The related library can be installed as follows.

# for NPM
$ npm install --save-dev @hyperapp-r/events

# for Yarn
$ yarn add -D @hyperapp-r/events

Start Guide

All you have to do after installation is replace hyperapp with hyperapp-r.

// Before:
import { app, h } from "hyperapp-r"
import { preventDefault } from "@hyperapp/events"

// After:
import { app, h } from "hyperapp-r"
import { preventDefault } from "@hyperapp-r/events"

hyperapp-r and @hyperapp-r/xxx has the same interface as hyperapp V2. So if you replace the module name, it will work exactly the same.

If you are using a library for hyperapp V2, you can leave the name as it is. The library for hyperapp V2 works the same way in Hyperapp-R.

import { app, h } from "hyperapp-r"
import { component } from "hyperapp-component" // As it is

Additional functions

Lifecycle Events (Return from Hyperapp V1)

As with V1, You can be notified when elements managed by the virtual DOM are created, updated or removed via lifecycle events. Use them for animation, data fetching, wrapping third-party libraries, cleaning up resources, etc.

Note that lifecycle events are attached to virtual DOM nodes, not to components. Consider adding a key to ensure that the event is attached to a specific DOM element, rather than a recycled one.

In addition, as an original function of Hyperapp-R, it is possible to set an event with another name.

  • oncreate -> created
  • onupdate -> updated
  • onremove -> removing
  • ondestroy -> destroyed

This is because in Hyperapp-R, you can use Action in event handlers such as onclick or onchange, but you cannot use Action in lifecycle events such as oncreate or onupdate, and you can use only functions that receive DOM elements. In other words, it is not possible to update State or execute Effect in lifecycle events.

oncreate (alias: created)

This event is fired after the element is created and attached to the DOM. Use it to manipulate the DOM node directly, make a network request, create a slide/fade in animation, etc.

import { h } from "hyperapp-r"

export const Textbox = ({ placeholder }) => (
  <input
    type="text"
    placeholder={placeholder}
    oncreate={element => element.focus()}
  />
)

onupdate (alias: updated)

This event is fired every time we update the element attributes. Use oldAttributes inside the event handler to check if any attributes changed or not.

import { h } from "hyperapp-r"

export const Textbox = ({ placeholder }) => (
  <input
    type="text"
    placeholder={placeholder}
    onupdate={(element, oldAttributes) => {
      if (oldAttributes.placeholder !== placeholder) {
        // Handle changes here!
      }
    }}
  />
)

onremove (alias: removing)

This event is fired before the element is removed from the DOM. Use it to create slide/fade out animations. Call done inside the function to remove the element. This event is not called in its child elements.

import { h } from "hyperapp-r"

export const MessageWithFadeout = ({ title }) => (
  <div onremove={(element, done) => fadeout(element).then(done)}>
    <h1>{title}</h1>
  </div>
)

ondestroy (alias: destroyed)

This event is fired after the element has been removed from the DOM, either directly or as a result of a parent being removed. Use it for invalidating timers, canceling a network request, removing global events listeners, etc.

import { h } from "hyperapp-r"

export const Camera = ({ onerror }) => (
  <video
    poster="loading.png"
    oncreate={element => {
      navigator.mediaDevices
        .getUserMedia({ video: true })
        .then(stream => (element.srcObject = stream))
        .catch(onerror)
    }}
    ondestroy={element => element.srcObject.getTracks()[0].stop()}
  />
)

Style attribute by string (Return from Hyperapp V1)

As with V1, the style attribute value can be specified as a string.

// Standard
<div style={{color: "red", textAlign: "center"}} />

// It is an error in Hyperapp V2, but it is accepted in Hyperapp-R
<div style="color: red; text-align: center;" />      

TypeScript Support

Hyperapp-R has full support for TypeScript.

import { Action, EffectRunner, Dispatchable, Effect, app, h } from "hyperapp-r";

type State = number;

// Action without payload - Action<State>
const Decrement1: Action<State> = (state) => (state - 1);

// Action with payload - Action<State, Payload>
const Increment: Action<State, number> = (state, value) => (state + value);

// Return other Action
const Increment1: Action<State> = (state) => ([Increment, 1]);

// Effect Runner - EffectRunner<State, NextPayload, Props>
const TimeoutRunner: EffectRunner<State, void, {delay: number, action: Dispatchable<State>}> = (dispatch, props) => {
  setTimeout(() => dispatch(props.action));
}

// Effect
const timeout = (action: Dispatchable<State>, delay: number): Effect<State> => [TimeoutRunner, {delay: delay, action: action}]

// Action with Effect
const DelayedIncrement1: Action<State> = (state) => ([state, timeout(Increment1, 1000)]);

// app
app<State>({
    init: 0
  , view: (state) => (
      <div>
        <h2>{state}</h2>
        <button onclick={Decrement1}>-1</button>
        <button onclick={Increment1}>+1</button>
        <button onclick={DelayedIncrement1}>Delayed +1</button>
      </div>
    )
  , node: document.getElementById("app")!
});