snuggsi ツ - Easy Web Elements in ~1kB
snuggsi ツ - Easy Web Elements in ~1kiloByte
All you need is a browser and basic understanding of HTML, CSS, & JavaScript Classes to be productive!
Performance is the art of avoiding work - #FreeJewelry
I have a special disdain for beginner JavaScript tutorials that have you run
create-react-appas the first step, and this exercise has only strengthened my conviction that every beginner programmer should get to grips with HTML, CSS and vanilla JS before delving into frameworks. ⸺ "Using the platform" - Elise Hein
Made with Vanilla JS™ No need to learn Node.js, React, Webpack, Babel, or Gulp. (You can if ya want to use snuggsiツ with those tools. But you don't need to!)
snuggsiツ works in a plain ol' *.html file! Simply place the following `
…html
…
…
…
…
…
…
Rule of thumb: _Close all **non-standard** unknown `HTML` Element tags!_
As you learned earlier there are a few conventions to adhere to be considered a _"valid **Custom** Element"_ the `HTML` tag name will need a lowercase alpha-numeric character followed by a hyphen _(at minimum)_:
```html
Rule of thumb: Use kebab case (with hyphens) for tag names.
We now know enough to be dangerous and make your own HTML Custom Element tag:
Et Voila ツ (No really … That's it!)
At this point your custom element can be styled using CSS just like any other element.
Hello
…
html
This is a token {baz} and {bat} is another! Rule of thumb: If the {token} name is not in a thesaurus then I probably shouldn't use it.
A "live token" is a declarative way to bind data values to your Custom Element. A nice convention to a real historical P.I.T.A. of keeping values updated. Live {token}s are also "✨ automagically" updated each time the element re-renders.
Let's add a {token} to ``:
Hello {planet}
Rule of thumb: A {token} is not "live" until there is a class description for its functionality.
Lastly, we can visually enhance your `` Custom Element by making it "block level" with CSS display: block. This way your element plays nicely in your layout:
Hello {planet}
We have finished your Custom Element Declaration using HTML, & CSS! Now on to your Definition.
Element DefinitionEvery Custom Element MUST be Defined within the CustomElementsRegistry. This is simple with snuggsiツ
Let's define your element using the Element interface:
// …
Element `hello-world`
…
javascript
Element ('hello-world') // classic javascript definitionclass DescriptionGreat so far! Although your Element behaves like any other HTMLElement, we should add some functionality custom to your needs.
Next, we need to pass a class description to the function returned by your Element definition.
// …
Element `hello-world`
( class HelloWorld extends HTMLElement { … } ) Rule of thumb: MUST define a class which extends HTMLElement
Let's shorten your description up a bit by using an anonymous class expression to describe the class. This convention is preferred as using an explicit class declaration name can potentially pollute the global namespace:
// …
Element `hello-world`
( class extends HTMLElement { … } )
…
javascript
// … {planet} …
Element `hello-world`
(class extends HTMLElement {
get planet () // used for {planet} token
// "✨ automagic" token binding
{ return 'world ' }
}) Rule of thumb: class properties are functions begining with the keywords get & set.
Rule of thumb: {tokens} will use the class property value of the same name by default.
⚠️ The Live {token} value is updated after each re-render but it beyond the scope of this simple example.
Since your hello-world Custom Element is an HTMLElement at its core, we can access your property directly from the DOM!
// …
document.querySelector
('hello-world').planet // world
…
javascript
//
// `onclick` is "automagically" attached
//
// ACHOO!
//
Element `hello-world`
(class extends HTMLElement {
// native event handler names
// are "✨automagically" attached to `hello-world`
onclick (event) {
// prevent event from bubbling
// Custom Element will re-render
// after event unless prevented
event.preventDefault ()
event.target // element which triggered event
this // is ALWAYS bound to the Custom Element container
}
onsneeze (event) {
/* must be declared in HTML
*/
}
})
…
html
Hello {planet}
Greet
[Play `` Demo](https://jsfiddle.net/yLdatmvz)
…or just copy & pasta into a new HTML file and have at it!
{greeting}
Hello new kitty!
…
html
{nickname}
That Beast
Array of Objects Context Each `` will be mapped over each
context item within the array. When the array items
are objects each property will map to a respective
{token} of the same name.
Note: The # symbol is used to retrieve the collection's current index of iteration.
Hello {name}! Your index # is {#}
Hello DevPunk! Your number index # is 0
Hello Snuggsi! Your number index # is 1
Array Context Each `` will be mapped over each
context item within the array. When the array items
are scalar (i.e. strings, numbers, booleans)
each item will map to a {self} helper token.
Note: The # symbol is used to retrieve the collection's current index of iteration.
Step {#}.
{self}.
…
Step 1.
Preheat oven.
Step 2.
Place pre-made cake in oven.
Step 3.
Don't burn the cake!
Step 4.
Nom Nom!
snuggsiツ is able to use modern compression algorithms to create bundles as small as ~1500 OCTETS (or one 1500byte Ethernet packet frame)
A chronological CalVer strategy is used instead of SemVer. Read More
Please read CONTRIBUTING.md before contributing.
Contributing while using Visual Studio Code is simple! Read More
No open issues yet, or sync has not completed.