Giter VIP home page Giter VIP logo

hacky's Introduction

⚙️ Hacky Code Size NPM Version

<2kb tagged template alternative for Crank.js

Hacky is something that I've always wanted. I've used React previously, but I find hooks too magical and JSX a finicky process that requires a build step.

When I discovered Crank.js, I fell in love because of how intuitive it was to understand. Imagine Hacky as Crank.js with tagged templates, but with a lightweight core and simplistic API.

If you're looking for something a bit more comprehensive, check out Million — Virtual DOM into the future! 💥🦁✨

-Aiden (@aidenybai)

random.cat API Example

Below is an implementation of a random.cat API fetcher example using Hacky (Live Demo).

import { html, render } from 'https://cdn.skypack.dev/hacky';

const fetchCat = async (url = 'https://aws.random.cat/meow') => {
  const res = await fetch(url);
  const { file } = await res.json();
  return file;
};

function* Cats({ width, height }) {
  const [cats, setCats] = this.createState([]);
  const [message, setMessage] = this.createState('Fetch cat image');
  const [disabled, setDisabled] = this.createState(false);

  const addCat = async () => {
    setMessage('Fetching...');
    setDisabled(true);

    try {
      const newCat = await fetchCat();
      setCats([...cats(), newCat]);
      setMessage('Fetch cat image');
      setDisabled(false);
    } catch (err) {
      console.error(err);
      setMessage('Failed to fetch. Retrying...');
      setTimeout(() => addCat(), 1000);
    }
  };

  while (true) {
    const catImages = cats().map(
      (cat) => html`<img key=${cat} src=${cat} width=${width} height=${height} />`,
    );
    yield html`
      <button disabled=${disabled()} onClick=${addCat} style="width: 100%">${message()}</button>
      <div>${catImages}</div>
    `;
  }
}

render(html`<${Cats} width=${100} height=${100} />`, document.body);

render() function has a standard interface that is used in many Virtual DOM libraries. First argument is a Virtual DOM to render, and the second one is a DOM node that will be used as the live DOM reference.

html tagged templates can produce Virtual DOM nodes, which define your DOM view.

this.createState() function will instantiate a new state reference, in which you can mutate by destructuring the getter (index 0) and setter (index 1).

Acknowledgments

Hacky takes heavy inspiration from Crank.js, and depends on Million. Feel free to check them out if you interested in an alternative library to use.

License

Million is MIT-licensed open-source software by Aiden Bai.

hacky's People

Contributors

aidenybai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

bring-shrubbery

hacky's Issues

feat: clean up intervals, subscriptions, etc. when unmounting

I'd like to do something like this:

function* Component() {
  const interval = setInterval(() => {
    // Do something
  }, 1000);

  try {
    while (true) {
      yield html`Hi`;
    }
  } finally {
    // Runs when unmounting
    clearTimeout(interval);
  }
}

Can it be supported? Or is there already some way to do it?

EDIT: @aidenybai From what I understand, Crank.js does that by calling return() on the generator when unmounting, so JS will automatically run the finally blocks. Seems relatively simple. 🤔

feat: async generator functions / async components

Hey, thanks for your work on this lib.

Crank.js allows components to be defined as async generator functions. For example:

async function IPAddress () {
  const res = await fetch("https://api.ipify.org");
  const address = await res.text();
  return <div>Your IP Address: {address}</div>;
}

await renderer.render(<IPAddress />, document.body);
console.log(document.body.innerHTML); // <div>Your IP Address: 127.0.0.1</div>

Can Hacky support it too? It seems it would be mostly a matter of adding await in some parts of the code.

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.