Why would this make any sense? const myDiv = div( {id: 'container', className: 'my-class'}, h1('Hello World'), p('This is a dynamically generated paragraph.') ); document.body.appendChild(myDiv); That's completely unnecessary these days with template strings. It's gonna be much faster as well to use browser's native parsing. const div = document.createElement('div'); let text = 'This is a dynamically generated paragr…
You can use tagged template functions to escape `${text}`. The result is pretty close to lit-html [1]. [1]: https://lit.dev/docs/v1/lit-html/introduction/
TinyJS – Shorten JavaScript QuerySelect with $ and $$
41–50 of 94 posts
Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$
#42Earlier quoted context omitted.
I'm quite fond of a little helper like so: createElement({ className: 'p-2 flex justify-end', contents: createElement({ tag: 'img', src: '/os-logo-maps.svg' }), }); I got the idea from Lea Verou's blissfuljs. When you're creating a bunch of nested elements it comes in handy.
That’s React without the JSX sugar.
Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$
#43I do believe I've seen something like this under another name. Using "$" to shorten JavaScript? That seems a lot like jQuery. > This README was generated by ChatGPT You don't need AI to explain this one.
Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$
#44Why would this make any sense? const myDiv = div( {id: 'container', className: 'my-class'}, h1('Hello World'), p('This is a dynamically generated paragraph.') ); document.body.appendChild(myDiv); That's completely unnecessary these days with template strings. It's gonna be much faster as well to use browser's native parsing. const div = document.createElement('div'); let text = 'This is a dynamically generated paragr…
Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$
#45 export const $ = document.querySelector.bind(document);
export const $$ = document.querySelectorAll.bind(document);
When using TypeScript the types for querySelectorAll are a bit hairy to map but by defining the consts like above the types "just work". for (const tag of $$("p")) ...
I don't use Array.from in the result of $$ because sometimes creating an Array is not necessary. The NodeList returned can be iterated directly or converted to an array later if really needed: [...$$("p")].map(p => ...)
Since I use TypeScript I lean on TSX for building HTML. I use preact's render-to-string package to convert it to a string [1].---
Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$
#46If ya don't want to do includes, ` window.$ = document.querySelector.bind(document) window.$$ = document.querySelectorAll.bind(document) `
edit: how about this so you can use array functions too when you need?
window.$ = document.querySelector.bind(document);
window.$$ = document.querySelectorAll.bind(document);
window.$$$ = (selector) => Array.from(document.querySelectorAll(selector));
Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$
#47If ya don't want to do includes, ` window.$ = document.querySelector.bind(document) window.$$ = document.querySelectorAll.bind(document) `
Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$
#48Why would this make any sense? const myDiv = div( {id: 'container', className: 'my-class'}, h1('Hello World'), p('This is a dynamically generated paragraph.') ); document.body.appendChild(myDiv); That's completely unnecessary these days with template strings. It's gonna be much faster as well to use browser's native parsing. const div = document.createElement('div'); let text = 'This is a dynamically generated paragr…
Here's a way to do that with a tagged template function (named it `htmlFragment` to make it super clear what it's for):
// a couple of functions to help
const sanitizeHTML = (unsafeStr) => {
const div = document.createElement('div');
div.textContent = unsafeStr;
return div.innerHTML;
};
const htmlFragment = (fragments, ...variables) => {
const result = variables.map((variable, i) => fragments[i] + sanitizeHTML(variable));
result.push(fragments[fragments.length-1]);
return result.join('');
};
// updated your code here
const div = document.createElement('div');
let text = 'This is a dynamically generated paragraph';
div.innerHTML = htmlFragment`
Hello world
${text}
`;
document.body.append(...div.children);
Unfortunately, to my knowledge there isn't yet a close-to-the-metal solution for templating and data binding in HTML/JS, although several proposals are currently being discussed.Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$
#49Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$
#50 const $id = new Proxy({}, {
// get element from cache, or from DOM
get: (tgt, k, r) => (tgt[k] || ((r = document.getElementById(k)) && (tgt[k] = r))),
// prevent programming errors
set: () => $throw(`Attempt to overwrite id cache key!`)
});
It's nice to be able to refer to elements by property name, so:
Is reachable with: $id.thing
And, since the underlying structure is just an object, you can still enumerate it with Object.keys, which can sometimes be a useful debugging aid and general catalog of accessed elements.Anyways.. Proxy is a wildly underappreciated and used class in JavaScript.