# heresy **Repository Path**: mirrors_WebReflection/heresy ## Basic Information - **Project Name**: heresy - **Description**: React-like Custom Elements via V1 API builtin extends. - **Primary Language**: Unknown - **License**: ISC - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-19 - **Last Updated**: 2025-12-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ![heresy logo](heresy.png) heresy Don't simulate the DOM. Be the DOM. - - - **Social Media Photo by [Alexey Zhavoronkov](https://twitter.com/alexusrnd) from [a2.agencylash](https://a2.agency/)** ![WebReflection status](https://offline.report/status/webreflection.svg) [![License: ISC](https://img.shields.io/badge/License-ISC-yellow.svg)](https://opensource.org/licenses/ISC) [![Build Status](https://travis-ci.com/WebReflection/heresy.svg?branch=master)](https://travis-ci.com/WebReflection/heresy) [![Greenkeeper badge](https://badges.greenkeeper.io/WebReflection/heresy.svg)](https://greenkeeper.io/) React-like Custom Elements via the V1 API built-in extends. **[Also available for SSR](https://github.com/WebReflection/heresy-ssr#readme).** - - - ### 📣 Community Announcement Please ask questions in the [dedicated discussions repository](https://github.com/WebReflection/discussions) to help the community around this project grow ♥ --- ### V1 Breaking Changes Please be sure you understand the [breaking changes landed in lighterhtml](https://github.com/WebReflection/lighterhtml#v4-breaking-changes). - - - ## What is this _heresy_ ? This project is some sort of answer to these major trends: * believing you cannot have tiny APIs which are able to compete with most famous frameworks * believing custom elements are not cool enough to compete with such frameworks * believing the built-in extends of custom elements are unnecessary or not useful at all Borrowing concepts and patterns from various libraries, _heresy_ enables custom elements as you've never seen before: * declarative UI (i.e. `
`) without needing JSX transformations or tooling at all * locally scoped custom elements to avoid name clashing and make components reusable in any context, similarly to what you can do with React components * automatic component name definition passed through the optional `Component.style(...selectors)` to inject related styles only once per definition * automatic [handleEvent](https://medium.com/@WebReflection/dom-handleevent-a-cross-platform-standard-since-year-2000-5bf17287fd38) pattern so that you can forget the unnecessary overhead of `this.method = this.method.bind(this)` * out of the box lifecycle events, such as `oninit(event)`, `onconnected(event)`, `ondisconnected(event)` or `onattributechanged(event)`, so that you can skip the ugly `attributeChangedCallback` and other unintuitive callbacks right away (but still use them if you like) * out of the box `observedAttributes` and `booleanAttributes` behavior, borrowed directly from [HyperHTMLElement](https://github.com/WebReflection/hyperHTML-Element#readme) Class * automatic, smart component initializer via `Component.new()` that avoids all the quirks related to the initialization of custom elements and built-ins * an ever available `comp.is` string (you won't believe it's not always an attribute if created procedurally via a registered class) * automatic, lazy `this.html` and `this.svg` template literal tags, to populate a component's content within its optionally, locally scoped defined elements * provides a simplified way to target rendered nodes through the React-like `ref()` utility * **hooks** implemented via `render({useState, ...})` definition. If a render has an argument, it will contain all hooks exported from [augmentor](https://github.com/WebReflection/augmentor#readme). Import `createContext` from _heresy_, to be able to use `render({useContext})`. Import `defineHook` to create custom hooks. ### Custom hooks It is possible to define your own hooks through the `defineHook(name, fn)` utility. ```js import {defineHook} from 'heresy'; defineHook('useCounter', ({useRef}) => () => { const counter = useRef(0); return counter.current++; }); // using the useCounter in a render const Comp = { extends: 'span', render({useCounter}) { const count = useCounter(); this.textContent = count; } }; ``` Please note that name must be unique, so if you'd like to be sure there won't ever be conflicts, use a `Symbol` instead of a string. ```js import {defineHook} from 'heresy'; const uso = Symbol(); defineHook(uso, ({useState}) => () => { const [current, update] = useState({}); return [current, state => { update({...current, ...state}); }]; }); const Comp = { extends: 'p', render({[uso]: useStateObject}) { const [state, update] = useStateObject(); // do something with the state } }; ``` ### Usage in a nutshell A component can be defined through both classes or raw object literals. ```js // // as object literal const literal = { name: 'Item', extends: 'li', // will extends li constructor render() { this.html`my name is ${this.props.name}`; } }; // as class class Item extends HTMLLiElement { static name = 'Item'; // necessary if code gets transpiled static tagName = 'li'; // necessary to indicate the kind render() { this.html`my name is ${this.props.name}`; } } ``` While both the name and tag it represents, can be defined within the class or object, it's rather suggested to pre-define at least the tag it's going to represent, but not the name. ```js const literal = { extends: 'li', render() { this.html`my name is ${this.props.name}`; } }; // in this way it's possible to define the name only via define('Item', literal); ``` Alternatively, it is possible to not include name and tag, defining these via the `Comp:tag` or `Comp` convention. ```js class Item extends HTMLLiElement { render() { this.html`my name is ${this.props.name}`; } } define('MyItem
  • ', Item); // OR define('MyItem:li', Item); ``` #### Which tag ? The beauty and power of the built-in extends of custom elements is that you can literally represent any tag you want/need. However, if you'd like to simply extend a non-standard tag, you can always fall back to the `element` tag kind, which will extend `HTMLElement`, and represent the component through its retrieved `` name. ```js // either as object const Component = { extends: 'element', onconnected() { console.log(this.outerHTML); } }; // or as class class Component extends HTMLElement { static get tagName() { return 'element'; } onconnected() { console.log(this.outerHTML); } }; const MyElement = heresy.define('MyElement', Component); document.body.appendChild(MyElement.new()); // in console: ``` ### Local components in a nutshell While `define(...)` will use the global registry to define the specific declarative name, making it a good practice to namespace it (i.e. `FWDatePicker`, `StencilForm` etc.), it is possible to define local components through the usage of `includes`, also aliased as `contains`. Such a list will still pass through the registry, so that local components are fully valid custom elements that never name-clash with anything else, so that it's easier to split complex components into various sub-modules and only define their main container globally. The following example has been rewritten with extra details and is [live on codepen](https://codepen.io/WebReflection/pen/argjeV?editors=0010). ```js import {define, ref, render, html} from 'heresy'; import {User, Pass} from './form/ui.js'; import {validate, switchPage} from './form/utils.js'; const Form = { extends: 'form', includes: {User, Pass}, oninit() { // refs can be declared upfront or inline (see render) this.user = ref(); this.addEventListener('submit', this); }, onsubmit(event) { event.preventDefault(); if (validate(this.user.current, this.pass.current)) fetch('/log-in').then(switchPage).catch(console.error); }, // render is invoked automatically on connected // if no connected, or callback is explicitly defined render() { this.html` `; } }; define('SiteLogin', Form); render(document.body, html``); ``` The `includes` or `contains` property, if present, must be a map of `"Name": Component` pairs, where the name could also define the tag type, like it does with `define(...)`. In the previous example both `User` and `Pass` are components extending `input`, so that the tag name is not necessary, but `{"User ``` ## Compatibility The [test page](https://webreflection.github.io/heresy/test/) uses and describes a few techniques to address all browsers, from IE9 to latest evergreen. The following list describes the _heresy_'s compatibility break down: * IE9 and IE10 *might* need an `Object.freeze` patch, to avoid breaking on frozen template literals when passed to polyfilled WeakMaps. The patch checks for the existence of `WeakMap`, hence it's completely safe for any modern browser, including IE11. * old Edge and all IE might need a Custom Elements polyfill upfront. In this case the famous [document-register-element](https://github.com/WebReflection/document-register-element) would be the suggested choice, since it patches built-ins right away, too. * Safari and WebKit have an understandable but pretty stubborn position regarding built-in elements, so that a 1K [polyfill](https://github.com/ungap/custom-elements-builtin) is needed in case you target Safari and WebKit. * you don't need a polyfill for Safari if you only extend `element`, but you'll miss out 90% of the fun with programming through built-in extends ### Broader wider compatibility in a nutshell ```html ``` Alternatively, you can use this minified version to never download the Safari-only polyfill. ```html ``` ## Concept Custom Elements built-ins are likely the best thing we have to build components the way we want to. Instead of using a non standard indirection as JSX is, we can use the power of [domtagger](https://github.com/WebReflection/domtagger#domtagger), the [hyperHTML](https://github.com/WebReflection/hyperHTML) and [lighterhtml](https://github.com/WebReflection/lighterhtml) tag engine, to replace once any `` with or without nested nodes. The Custom Elements V1 API provides enough primitives to intercept any sort of attribute (i.e. the `props` in the example), but also react to events such `connectedCallback` or `disconnectedCallback` and `attributeChangedCallback`. Mixed up with built-in extends in a way that any component is a real thing on the DOM instead of a facade of itself, _heresy_ makes the creation of apps, from simple to complex, a no-brainer: define the content through `this.html` or `this.svg` and that's it. When any class is defined, it's not just necessarily a useless `HTMLElement`, it can be pretty much any kind of element. The following example is [live in Code Pen](https://codepen.io/WebReflection/pen/eoxobK?editors=0010). ```js import {define, ref, html, render} from 'heresy'; // a div define(class Div extends HTMLDivElement { static get name() { return 'Div'; } static get tagName() { return 'div'; } }); // a paragraph define('P

    ', class extends HTMLParagraphElement {}); // a h1 define('H1

    ', class extends HTMLHeadingElement {}); // render them all + ref example const refs = {}; // refs can be created right away refs.div = ref(); // or within the render render(document.body, html`

    Hello there

    This is how custom elements look via heresy.

    Isn't this awesome?

    `); console.log(refs.h1.current); // the H1 instance/node ``` ### Local components live example You can see the [following example live](https://webreflection.github.io/heresy/test/local.html). ```js // p.js - could be an object too export default class extends HTMLParagraphElement { static get tagName() { return 'p'; } oninit() { console.log(this.outerHTML); } }; // first.js - it has a local P import P from './p.js'; export default { extends: 'div', includes: {P}, // with its own definition render() { this.html`

    first

    `; } }; // second.js - it uses P again as local import P from './p.js'; export default { extends: 'div', includes: {P}, // with its own definition render() { this.html`

    second

    `; } }; // index.js const {define, render, html} = heresy; import First from './first.js'; import Second from './second.js'; const Div = define('Div', { extends: 'div', includes: {First, Second}, render() { this.html``; } }); // either render(document.body, html`
    `); // or even document.body.appendChild(Div.new()); ``` ## CSS - The components style precedence Components are defined once per kind, and the styles of local components are appended live before the outer component, giving it the ability to force extra styles when needed, or improve the specificity for a specific component/style when used within some other. ```js const Div = define('Div', { extends: 'div', includes: {First, Second}, // will receive the selectors for self and included components style(Div, First, Second) { // since outer component style is injected after // it is possible to eventually overwrite nested // components through higher priority / specificity return ` ${Div} { font-size: 16px; } ${Div} > ${First} { padding: 0; } ${Div} ${Second} { font-weight: smaller; } `; console.log([Div, First, Second].join(', ')); }, render() { this.html``; } }); ``` You can see what the `style(...)` receives reading the console in this [live demo](https://webreflection.github.io/heresy/test/local.html). ## CSS - How to query or style all globally defined components Every global built-in extend will have a `-heresy` suffix to ensure both that the Custom Element can be registered, but also grant a common pattern to reach components. ```css *[is$='-heresy']:hover { opacity: .8; } /* ⚠ too specific: it does not work with local components */ tag[is='specific-heresy'] { display: block; } ``` ## CSS - How to query or style local components When components are defined locally, there will be an incremental number between the component name and the `-heresy` suffix. Instead of addressing a specific suffix, it is instead suggested to address the known prefix. ```css /* ℹ usable for both globally registered and nested components */ tag[is^='my-button-'] { display: block; } ``` - - - ## Project Showcases * the classic [TodoMVC](https://github.com/WebReflection/heresy-todo) ## Project Achievements * declared elements are the instance you'd expect (no virtual, no facade) * declared elements can be of any kind (table, tr, select, option, ...), including element * declare any component within other components, breaking the limits of a single, name-clashing based registry * any attribute change, or node lifecycle, can be tracked via the Custom Elements V1 API (no componentDidMount and friends) * `oninit`, `onconnected`, `ondisconnected`, and `onattributechanged` events out of the box * `handleEvent` paradigm out of the box * `observedAttributes` and `booleanAttributes` do what everyone expects these to do * no redundant dom nodes, no ghost fragments, an "as clean as possible" output * the performance of lighterhtml, fine tuned for this specific use case * it's [SSR (Server Side Rendering) friendly](https://github.com/WebReflection/heresy-ssr#readme), and custom elements hydrate automatically * usage of `ref` to simplify reaching nodes after render * automatic `render` when the method is present and no `onconnected` or `connectedCallback` has been explicitly defined * CSS specificity is granted per each component via `style: selector => '...'` so that **there is no need to use Shadow DOM** and the heavy polyfills related to it