A minimal (less than 1K minified) DOM manipulation utility.
The goal is to make the most common DOM tasks a little less verbose.
npm install rossini
or
<script src="../dist/rossini.umd.js"></script>
or
<script src="https://unpkg.com/rossini@latest/dist/rossini.umd.js"></script>
// module
import { que, on, el, E } from 'rossini';
// commonjs
const { que, on, el, E } = require('rossini');
// browser
const {el, on, que, E} = window.rossini;
See examples/tell.html for more usage examples
3 functions and an enum.
Pronounced ke as in “what?” in Spanish. It’s an alias for document.querySelector
const b = que('#mybutton');
This is a convenience when creating DOM elements (hence el) with the goal to save some verbosity like createElement(), setAttribute() and append(). It returns the newly created element, ready to be added to the DOM.
const br = el('hr');
const h1 = el('h1', {id: 'myhead'}, 'Hello from my Rossini!');
The children spread allow any number of elements (created with el() or not) or a string. If it’s a string a text node is created and appended.
document.body.append(
el('div', null, el('p', {id: "para"}, 'Paragraph text'))
);
document.body.append(
el('div', null,
el('p', {id: "para"}, 'Paragraph text'),
el('span', {}, 'More text'),
),
);
// yes, I like trailing commas
The API follows pretty closely React’s createElement() which is what creates elements behind the JSX sugar.
This is a wrapper for addEventListener plus some extras:
removeEventListener so you can cleanup and tap memory leaksevent.target is not the element but a childon('#myButton', 'click', console.log);
on(document, 'DOMContentLoaded', (event, element) => console.log(event, element));
This is an enumeration of the 3 most common event names. Goals:
laod event isn’t firingclick is a muscle memory but using pointerdown is betterDOMContentLoaded abbreviationon('#myButton', E.click, console.log);
on(document, E.dcl, console.log);
on(window, E.load, console.log);
Although the code is pure 100% unadulturated vanilla JavaScript, if you like TS you should be good, thanks to the magic of tsc and declaration generation. There is a rossini.d.ts under /dist
IDE autocomplete should be working too because of the JSDoc blocks present in the source.
I was half asleep when I thought of this micro-library and, while still in bed, I started designing the API in my head.
Gioachino Rossini, the prolific Italian opera composer, was known for his habit of finishing an aria in the morning while still in bed.