NaNoWriMo Promotes Manuscription

    TypeScript icon, indicating that this package has built-in type declarations

    0.2.6 • Public • Published

    npm versionbrotliedhits/monthlicensesponsorfollow


    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.

    <script src=""></script>

    The Node users add candid to their project:

    npm i -D candid

    Candid can be pure HTML.

      <web-component name="say-hi">
      <script type="module" src="//"></script>

    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.


    $ npm i candid

    Vanilla JS:

    import * as Candid from 'candid';
    const template = document.createElement('template');
    template.innerHTML = `<script>console.log('Hi!')</script>`;
    Candid.defineWebComponent('say-hi', { template });
    const sayHi = document.createElement('say-hi');


    A web component can be defined using the following options:

    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:

    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 DOM
    • onUnmount: called when disconnecting from DOM
    • onUpdate: called on attribute or property change, if oldValue !== newValue
    • onAdopt: called when custom element changes the document
    • onSlotChange: 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 called
    6. the onUpdate callback is called for all observed properties
    7. the onSlotChange 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

    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.

      <base href="/components">
      <link rel="preload" href="web-components.html" as="fetch">
      <web-import src="web-components.html"></web-import>
      <script type="module" src=""></script>

    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.

    <web-component name="lazy-one" mode="open">
        <web-import src="lazy-one.css.html"></web-import>
        <web-import src="https://my.cdn/lazy-one.js.html"></web-import>

    Web-imports can be nested and cross-reference different domains. Beware of cycles!

    Extending built-in HTML elements

    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.

      <input is="fancy-input">
    <web-component name="fancy-input" extends="input" props="{ type: 'text', placeholder: 'fancy' }">

    Safari does need a polyfill for customized built-in elements.

    Solution: add the @ungap/custom-elements script by @WebReflection to the HTML <head> section.

    <script src=""></script>

    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.

    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.


    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.


    Given an element el, the following information is available:

    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), {});


    npm i candid


    DownloadsWeekly Downloads






    Unpacked Size

    23.1 kB

    Total Files


    Last publish


    • danieldietrich