Candid is an unopinionated, frameworkless JavaScript library for building web applications.
npm install candid
Candid is an unopinionated, frameworkless JavaScript library for building web applications.
* No framework, no dependencies, just markup and pure JavaScript
* Builds on top of web components, but without boilerplate
* Out-of-the-box custom element best practices
* HTML and JS/TS web component APIs
* Choose between openness and encapsulation (shadow root)
* Augment existing HTML elements
* Web imports of HTML fragments (HTML API only)
* Lazy loading support for web component contents
The _Vanilla HTML_ users just add the script to their index.html, the module is loaded and available in the global window.Candid object.
``html`
The _Node_ users add candid to their project:
`sh`
npm i -D candid
Candid can be pure HTML.
`html`
All web components and web imports (see below) are hidden using { display: none } after the custom element is created.
Candid has a JavaScript/TypeScript API.
Installation:
`sh`
$ npm i candid
Vanilla JS:
`ts
import * as Candid from 'candid';
Candid.init();
const template = document.createElement('template');
template.innerHTML = ;
Candid.defineWebComponent('say-hi', { template });
const sayHi = document.createElement('say-hi');
document.querySelector('app').appendChild(sayHi);
`
A web component can be defined using the following options:
`ts`
type Options = {
extends?: string / tag name /
mode?: ShadowRootMode / 'open' | 'closed' / | null
props?: Props / { [key: string]?: string | number | boolean | null } /
template?: HTMLTemplateElement | null
}
The template may contain arbitrary HTML.
Style has a local scope in the presence of a shadow root.
Inline scripts (without src attribute) will be executed once on web component instantiation.
After that, only callbacks will be called.
A script's this is the context of a web component and has the following type:
`ts`
type Context = {
element: HTMLElement // the custom element
root: HTMLElement | ShadowRoot // element.shadowRoot || element, depending on the mode option
onMount?: () => void // called when connected to the DOM
onUnmount?: () => void // called when disconnecting from DOM
// called on attribute or property change, if oldValue !== newValue
onUpdate?: (name: string, oldValue: string | null, newValue: string | null) => void
onAdopt?: () => void // called when custom element changes the document
onSlotChange?: (e: Event) => void // called when a slot changes
}
All callbacks can be set during creation of the web component. After that, the context is read-only.
The user may set callbacks on the context object:
* onMount: called when connected to the DOMonUnmount
* : called when disconnecting from DOMonUpdate
* : called on attribute or property change, if oldValue !== newValueonAdopt
* : called when custom element changes the documentonSlotChange
* : called when a slot changes
When a web component (read: a custom element) is created, the following effects happen:
1. if mode is set then the shadow root is created (default: no shadow root)
2. the context property is created, containing element and root
3. the web imports are performed asynchronously
4. the scripts are removed from the template
After creation, the element can be mutated by the user, e.g. setting properties, attributes or registering listeners. When the element is connected to the DOM, the following effects happen:
1. the observed properties are linked to the attributes
2. the template is cloned and inserted into the (shadow) root
3. the scripts are executed
4. the context is frozen
5. the onMount callback is calledonUpdate
6. the callback is called for all observed propertiesonSlotChange
7. the callback is called for all slots
When the element is disconnected from the DOM, the following effects happen:
* the onUnmount callback is called
When slot content changes, the following effects happen:
* the onSlotChange callback is called
When the custom element changes the document, the following effects happen:
* the onAdopt callback is called
Web imports are plain HTML imports that are inserted into the DOM.
Once Candid is imported, web imports are fetched and the DOM elements are replaced by the loaded HTML contents. Especially web components can be imported using the web-import element.
`html`
Web components can have nested web-import elements. These are loaded asynchronously when the first web component is instantiated. Relative urls are resolved against the base URL if the origin is the same, otherwise the URL is resolved using the remote URL.
`html`
Candid!
Web-imports can be nested and cross-reference different domains. Beware of cycles!
Web components work in all major browsers and Candid can be used without any hassles. However, it is possible to extend built-in HTML elements.
`html`
Candid!
Safari does need a polyfill for customized built-in elements.
Solution: add the @ungap/custom-elements script by @WebReflection to the HTML
section.`html
`How to stop FOUC
FOUC stands for Flash of Unstyled Content (FOUC). It happens when web components are already inserted in the DOM but not yet defined. FOUC can be prevented by conditionally styling the elements.
`css
my-element:not(:defined) {
/ Pre-style, give layout, replicate my-element's eventual styles, etc. /
display: inline-block;
height: 100vh;
opacity: 0;
transition: opacity 0.3s ease-in-out;
}
`See Google Web Fundamentals and StackOverflow.
Security
Candid internally uses
eval to evaluate scripts. If there are security concerns, use the Content Security Policy (CSP) to prevent the scripts from being evaluated.Reflection
Given an element
el, the following information is available:`ts
const name = el.hasAttribute('is') : el.getAttribute('is') : el.tagName.toLowerCase();
const superTag = el.hasAttribute('is') ? el.tagName.toLowerCase() : undefined;
const mode = el.shadowRoot?.mode;
const customElement = customElements.get(name);
const propNames = customElement.observedAttributes;
const props = propNames.reduce((props, name) => (props[name] = el.getAttribute(name), props), {});
``