slim.js

slim.js

高性能声明式Web组件库 简化前端开发流程

slim.js是一款高性能的Web组件开发库,利用浏览器原生API操作DOM,无需编译器和外部依赖。它提供反应式组件、数据绑定、指令系统和插件机制,支持创建高效的自定义元素。slim.js体积小巧,兼容性强,可与其他框架集成,适合构建现代Web应用。

Slim.jsWeb Components前端开发响应式组件数据绑定Github开源项目

hello, slim.js

Build Status

Chat on gitter

Hello slim.js - your declarative web components library

import { Slim } from 'slim-js'; import { tag, template } from 'slim-js/decorators'; @tag('my-awesome-element') @template(` <button @click="this.inc()"> + </button> <span>{{this.count}}</span> <button @click="this.dec()"> - </button> `) class extends Slim { count = 0; inc() { this.count++ } dec() { this.count-- } }

Read the documentation

Slim is an ultra fast, native and elegant library for Web Components development

It's fast. Very fast.

Super fast. It leverages the native browser's API to manipulate DOM using templates, with custom directives. Imagine vue or angular's strctured templates combined with react's ease of use - combined, faster and lighter.

What's the difference? Why slim.js?

It's native. It has no black magic, no compilers and no external dependencies. It aims for fast performance, low CPU usage, using the native HTMLElement class inheritence and CustomElementsRegistry.

It works anywhere. You can combine slim.js in any other framework, combine any framework into slim.js app or any other combination.

You can write fully functional classes with complex code, or create pure "render-only" components without writing a single function.

Opt-in/Opt-out anything. The core is super small (2927 bytes gzipped), every directive is a self-contained module. Opt-in if you like. Pick your preferences for using (or not) shadow-dom. Work directly with properties on the element or create a view-model layer.

It's super easy to learn. Anything in your template that is wrapped with bars (example: <div>{{ this.user.name }}</div> ) is the actual code running. And it runs only when the used properties change. Changes affects only the specific DOM nodes that are bound. It means you can manipulate DOM manually without bothering the core, and vice-versa. No virtual dom engine or anything like that.

What about bundling, typescript, or other tools?

It just works. The library is written in javascript, and it has index.d.ts to support strongly-typed projects. It also provides access to it's internals, if you want to hack things out.

What are "directives" anyway?

Directives are middlewares that executes code on your template whenever you have attributes in your markup. For example, if you opt-in for the property-directive, any attribute that starts with a period (i.e. <some-element .user="{{this.user}}"></some-element>) will trigger the property-directive into action: this.user will project as a user property into some-element. Another example is the foreach-directive: You can map arrays or iterables to repeating elements. For example

<ul> <li *foreach="{{this.users.slice(0, 50)}}"> <img src="{{item.picture}}" /> <span class="user-name">{{item.name}}</span> </li> </ul>

slim.js provides the following out-of-the-box directives:

  • custom-code (default)<form disabled="{{this.isFormReady(this.data)}}">...</form> or <div style="{{this.getStyleFor(this.user)}}">Welcome, {{this.user.name}}!</div>
  • property <img .src="{{this.imageSource}}">
  • events <input @change="this.handleInputChange(event)">
  • if <div *if="{{!this.isLoaded}}" class="spinner">Loading...</div>
  • foreach <li *foreach="{{this.someArray}}">{{item.value}}</li>
  • reference <form #ref="myForm">...</form> will create a property targeting the DOM element
  • repeat (optional, faster, with lazy memory release) <li *repeat>

All the directives (excluding custom-code) are optional - each is a standalone module. Choose the directives you use, or write your own!

What about plugins?

You can hook into any slim.js component's lifecycle using plugins. You plugin will be notified for step of the lifecycle, so you can add you own custom code or change things on-the-go.

Isn't a runtime library slower than compiled libraries?

Well, slim.js is an exception. Your custom code is memoized, and every piece of in-template code is created only once, can be even shared accross components. The core is very small and efficient, and releasing bound nodes is done progressively as background-tasks, keeping the user interface responsive as first priority. Modern browsers supports requestIdleCallback, so if supported, the removed elements will be released slowly and progressively.

Is this another framework?

Yes, and No. It's a very thin, fast, and extensible core library for writing custom-elements, powered by optional plugins and directives. You have the power to decide. It adds the firepower of a framework to your web-components, using the browser's native capabilities — as defined by W3C standards.

Here's what you get:

  • HTML markup with custom inlined code.
  • Reactive components: If a property is in the template, it becomes reactive. The core wraps it with a getter and setter function (and keeping your original one intact, if exists). No need to declare, it's derived from the tempalte.
  • Data binding: data changes triggers changes in the rendered HTML.
  • Directive system: Register your own directives.
  • Plugins: Add global lifecycle hooks for every slim.js component.
  • It feels like a framework (in a good way), but without the limits of a classic framework. It works everywhere, you can pick your own framework.
  • Low-footprint: it's less than 3KB gzipped. Directives are optional.
  • Single file for core functionality, and you're good to go.
  • No dependencies, everything is based on native browsers' API. Choose your own tools.
  • (Optional) Decorators for ES7/Next syntax, via Babel included.
  • Works with mixins from other component libraries, such as Polymer, out of the box.

Standards-compliant

  • ES6
  • Web Components V1
  • No transpiling or compilation required

No Setup required. It just works.

Use native (or bundlers) import statements, load from the CDN, or use the non-module (commonJS) file.

Documentation

The official website is built with Slim.js (with source maps). Check out the source code, see it for yourself.

Migration from older versions

Version 3 introduced the plugins.

Version 4 introduced the es modules.

Version 5 has new engine, directive system and plugin system.

es modules

import { Slim } from 'slim-js';

IIFE (no-modules)

<script src="slim-js/dist/index.legacy.js"></script>

Access a global Slim object.

HTML + Modules

<script type="module" src="slim-js/dist/index.js"></script>

Contibutors are welcome.

USE THE PLATFORM

Would you like to spend some good money on a good library and support the project? Contact eavichay@gmail.com.

编辑推荐精选

扣子-AI办公

扣子-AI办公

职场AI,就用扣子

AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!

堆友

堆友

多风格AI绘画神器

堆友平台由阿里巴巴设计团队创建,作为一款AI驱动的设计工具,专为设计师提供一站式增长服务。功能覆盖海量3D素材、AI绘画、实时渲染以及专业抠图,显著提升设计品质和效率。平台不仅提供工具,还是一个促进创意交流和个人发展的空间,界面友好,适合所有级别的设计师和创意工作者。

图像生成AI工具AI反应堆AI工具箱AI绘画GOAI艺术字堆友相机AI图像热门
码上飞

码上飞

零代码AI应用开发平台

零代码AI应用开发平台,用户只需一句话简单描述需求,AI能自动生成小程序、APP或H5网页应用,无需编写代码。

Vora

Vora

免费创建高清无水印Sora视频

Vora是一个免费创建高清无水印Sora视频的AI工具

Refly.AI

Refly.AI

最适合小白的AI自动化工作流平台

无需编码,轻松生成可复用、可变现的AI自动化工作流

酷表ChatExcel

酷表ChatExcel

大模型驱动的Excel数据处理工具

基于大模型交互的表格处理系统,允许用户通过对话方式完成数据整理和可视化分析。系统采用机器学习算法解析用户指令,自动执行排序、公式计算和数据透视等操作,支持多种文件格式导入导出。数据处理响应速度保持在0.8秒以内,支持超过100万行数据的即时分析。

AI工具酷表ChatExcelAI智能客服AI营销产品使用教程
TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
AIWritePaper论文写作

AIWritePaper论文写作

AI论文写作指导平台

AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。

AI辅助写作AI工具AI论文工具论文写作智能生成大纲数据安全AI助手热门
博思AIPPT

博思AIPPT

AI一键生成PPT,就用博思AIPPT!

博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供多种版式,一键自适应切换,完美适配各种办公场景。

AI办公办公工具AI工具博思AIPPTAI生成PPT智能排版海量精品模板AI创作热门
潮际好麦

潮际好麦

AI赋能电商视觉革命,一站式智能商拍平台

潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。

下拉加载更多