Git Product home page Git Product logo

niik's Introduction

N||k

A Haxe/JS library for using css to define event listener bindings.

Live demo of the library in action

But... why?

If you want to add event handling to a DOM element that you don't already have a reference to, the HTML5 API for doing so is document.querySelector("selector").addEventListener("eventtype", callbackFunction). Its a nice API! This library just abstracts over it, allowing you to use CSS to define which selectors map to which events and callbacks, and then to use those definitions to add that functionality to your nodes.

But who is it for?

This is a convenience library to help framework-free Haxe (and Vanilla JS) apps keep HTML docs free of onClick declarations and functions free of verbose querySelector/addEventListener-ing. Not that there's anything wrong with that, the library author just doesn't like it too terribly much. He feels that its too easy to make mistakes, you see.

Oh my, the built .js file is 15kb! What gives?

Admittedly, this lib is being written for use in the author's Haxe projects, so a lot of that 15kb would be there for him anyway. For folks using this from JS, the file is about 3kb when gzipped, which should be fine. The output from the Haxe to JS compiler is pretty self-contained, so bundling it up shouldn't be a problem.

Why CSS, and not JSON or XML?

A few reasons: CSS is the format that gave us these selectors, its designed around defining these types of mappings, its easy to parse, your editor (yes, that one) supports it already, and preprossors like SASS allow you to write nested declarations and then compile them into neat CSS files (without this lib having to implement nested declaration logic).

What about performance?

Undetermined at this point. If its too slow, please submit an issue on this here Github project page. Or get someone to write this project in pure JS. Who knows, it might turn out better ๐Ÿ™‚

Basic Use

Haxe/JS

Make a new Niik object, add callbacks to it using .registerHandler(namestring, callback), set the rules being used with setCSSRules(cssString), bind event handlers to a group of nodes with addHandlersToChildren(parent), to an individual element with addHandlersToElement(element), and remove handlers from an individual element with removeHandlersFromElement(element).

For a simple start, add callbacks with addHandler and then initialize with .startWithString(cssString) (which executes synchroniously and returns the niik object), or .startWithSRC(srcString) (which gets a cssString from a css file located by srcString, and returns a Promise). Note that the two "startWith" methods have undefined behavior if called more then once in the same document.

CSS

Valid selectors are any accepted by querySelector. Valid properties are any event types accepted by addEventListener. Valid values are string keys provided to registerHandler before calling one of the addHandler or startWith methods. Ex: #someid button, .allof-these button {click: doAwesomeCallback}

A note about the "Cascading" bit

Unlike CSS used for styling, properties can have multiple values, and declarations under one selector don't override declarations from another. That is, .a button {click:foo;} #b .a button {click:bar;} will result in invocations of both foo and bar when the button is clicked.

Building

Install Haxe 3.4.7, run haxe build.hxml from within the project folder, and the output will be 'build/niik-standalone.js'.

niik's People

Contributors

zaphodious avatar

Watchers

James Cloos avatar  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.