百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

snuggsi

> 前端框架
开源

snuggsi - 易于使用的 Web 元素,大小约 1kB

395 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

snuggsi - 易于使用的 Web 元素,大小约 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

Navigation

  • Why ?
  • Easy Installation
  • Browser Support
  • Quick Tour
    • <custom-elements>
      • HTML Declaration
      • Element Definition
      • class Description
    • Template
  • Build Process
  • References
  • Contributors

Why ?

I have a special disdain for beginner JavaScript tutorials that have you run create-react-app as 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

  1. You prefer convention over configuration.
  2. Because You (probably) don't need a JavaScript Framework.
  3. You prefer to be D.R.Y. and build reusable web components on a gradual learning curve.
  4. Web Components are ready for production & Custom Elements v1 has support for every greenfield browser.

Easy Installation

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!)

#UseThePlatform

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:

xml

Et Voila ツ (No really … That's it!)

At this point your custom element can be styled using CSS just like any other element.

xml

  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 ``:

xml

  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:

xml

  Hello {planet}

  

We have finished your Custom Element Declaration using HTML, & CSS! Now on to your Definition.

Element Definition

Every Custom Element MUST be Defined within the CustomElementsRegistry. This is simple with snuggsiツ

Let's define your element using the Element interface:

javascript
//  … 

Element `hello-world`

…

javascript
Element ('hello-world') // classic javascript definition

class Description

Great 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.

javascript
//  … 

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:

javascript
//  … 

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!

javascript
//  … 

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

Hello Kitty! (advanced)

[Play `` Demo](https://jsfiddle.net/yLdatmvz)

…or just copy & pasta into a new HTML file and have at it!

xml

  {greeting}

  
    
      Hello new kitty!
    

    
  

  

…

html

  
  
{nickname}

Rendered Result

xml

That Beast

`` With 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.

xml

  
    
Hello {name}! Your index # is {#}
  

Rendered Result

xml

  
  
  

  
Hello DevPunk! Your number index # is 0

  
Hello Snuggsi! Your number index # is 1

`` With Scalar 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.

xml

  
     Step {#}.
     {self}.

  

Rendered Result

xml

   … 

   Step 1.
   Preheat oven.

   Step 2.
   Place pre-made cake in oven.

   Step 3.
   Don't burn the cake!

   Step 4.
   Nom Nom!

Build Process

snuggsiツ is able to use modern compression algorithms to create bundles as small as ~1500 OCTETS (or one 1500byte Ethernet packet frame)

Read More

CalVer (Calendar Versioning)

A chronological CalVer strategy is used instead of SemVer. Read More

References

  • WebComponents Will outlive Your JavaScript Framework

Contributors

Please read CONTRIBUTING.md before contributing.

Contributing while using Visual Studio Code is simple! Read More

Issues· 45 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScriptcustom-elementsdomecmascriptes6

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架