{"id":16688905,"url":"https://github.com/danieldietrich/candid","last_synced_at":"2025-03-23T14:31:39.320Z","repository":{"id":44249424,"uuid":"446211373","full_name":"danieldietrich/candid","owner":"danieldietrich","description":"Candid is a surprisingly fresh and frameworkless JavaScript library for building web applications.","archived":false,"fork":false,"pushed_at":"2022-02-19T23:54:13.000Z","size":297,"stargazers_count":6,"open_issues_count":14,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-05-16T01:07:20.337Z","etag":null,"topics":["customelement","declarative","frameworkless","frontend","javascript","library","template","typescript","ui","web","webcomponent"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/danieldietrich.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null},"funding":{"github":["danieldietrich"],"custom":"https://www.paypal.me/danieldietrich13"}},"created_at":"2022-01-09T21:52:38.000Z","updated_at":"2024-04-13T02:40:15.000Z","dependencies_parsed_at":"2022-09-15T22:31:17.022Z","dependency_job_id":null,"html_url":"https://github.com/danieldietrich/candid","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danieldietrich%2Fcandid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danieldietrich%2Fcandid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danieldietrich%2Fcandid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danieldietrich%2Fcandid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danieldietrich","download_url":"https://codeload.github.com/danieldietrich/candid/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244306139,"owners_count":20431747,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["customelement","declarative","frameworkless","frontend","javascript","library","template","typescript","ui","web","webcomponent"],"created_at":"2024-10-12T15:45:37.547Z","updated_at":"2025-03-23T14:31:39.301Z","avatar_url":"https://github.com/danieldietrich.png","language":"TypeScript","readme":"\u003cp align=\"center\"\u003e\u003cimg width=\"103\" src=\"./public/candid-logo.svg\" alt=\"Candid logo\"\u003e\u003c/p\u003e\n\n\u003cp align=\"center\"\u003eCandid is an unopinionated, frameworkless JavaScript library for building web applications.\u003c/p\u003e\n\n\u003cbr\u003e\n\u003cbr\u003e\n\n[![npm version](https://img.shields.io/npm/v/candid?logo=npm\u0026style=flat-square)](https://www.npmjs.com/package/candid/)[![brotlied](https://img.badgesize.io/https://cdn.jsdelivr.net/npm/candid.svg?compression=brotli\u0026label=brotlied\u0026max=1500\u0026softmax=1300\u0026style=flat-square)](https://www.jsdelivr.com/package/npm/candid)[![hits/month](https://data.jsdelivr.com/v1/package/npm/candid/badge)](https://www.jsdelivr.com/package/npm/candid)[![license](https://img.shields.io/github/license/danieldietrich/copy?style=flat-square)](https://opensource.org/licenses/MIT/)[![sponsor](https://img.shields.io/badge/GitHub-💖Sponsors-b5b7b9?logo=github\u0026style=flat-square)](https://github.com/sponsors/danieldietrich)[![follow](https://img.shields.io/twitter/follow/danieldietrich?logo=twitter\u0026style=flat-square)](https://twitter.com/danieldietrich/)\n\n## Features\n\n* **No framework, no dependencies, just markup and pure JavaScript**\n* Builds on top of [web components](https://developer.mozilla.org/en-US/docs/Web/Web_Components), but without boilerplate\n* Out-of-the-box [custom element best practices](https://developers.google.com/web/fundamentals/web-components/best-practices)\n* HTML and JS/TS web component APIs\n* Choose between openness and encapsulation (shadow root)\n* Augment existing HTML elements\n* Web imports of HTML fragments (HTML API only)\n* Lazy loading support for web component contents\n\n## Usage\n\nThe _Vanilla HTML_ users just add the script to their index.html, the module is loaded and available in the global `window.Candid` object.\n\n```html\n\u003cbody\u003e\n  \u003c!-- using a component --\u003e\n  \u003csay-hi\u003e\u003c/say-hi\u003e\n  \u003c!-- defining a component --\u003e\n  \u003cweb-component name=\"say-hi\"\u003e\n    \u003ctemplate\u003e\n      \u003cscript\u003econsole.log('Hi!')\u003c/script\u003e\n    \u003c/template\u003e\n  \u003c/web-component\u003e\n  \u003c!-- initializing Candid --\u003e\n  \u003cscript src=\"https://cdn.jsdelivr.net/npm/candid\"\u003e\u003c/script\u003e\n\u003c/body\u003e\n```\n\nAll web components and web imports (see below) are hidden using `{ display: none }` after the custom element is created.\n\n### Programmatically\n\nCandid has a JavaScript/TypeScript API.\n\nInstallation:\n\n```sh\n$ npm i candid\n```\n\n**Vanilla JS:**\n\n```ts\nimport * as Candid from 'candid';\n\nCandid.init();\n\nconst template = document.createElement('template');\ntemplate.innerHTML = `\u003cscript\u003econsole.log('Hi!')\u003c/script\u003e`;\n\nCandid.defineWebComponent('say-hi', { template });\n\nconst sayHi = document.createElement('say-hi');\ndocument.querySelector('app').appendChild(sayHi);\n```\n\n## Options\n\nA web component can be defined using the following options:\n\n```ts\ntype Options = {\n  extends?: string /* tag name */\n  mode?: ShadowRootMode /* 'open' | 'closed' */ | null\n  props?: Props /* { [key: string]?: string | number | boolean | null } */\n  template?: HTMLTemplateElement | null\n}\n```\n\n## Template\n\nThe template may contain arbitrary HTML.\nStyle has a local scope in the presence of a shadow root.\nInline scripts (without src attribute) will be executed once on web component instantiation.\nAfter that, only callbacks will be called.\n\nA script's `this` is the context of a web component and has the following type:\n\n```ts\ntype Context = {\n  element: HTMLElement               // the custom element\n  root: HTMLElement | ShadowRoot     // element.shadowRoot || element, depending on the mode option\n  onMount?: () =\u003e void               // called when connected to the DOM\n  onUnmount?: () =\u003e void             // called when disconnecting from DOM\n  // called on attribute or property change, if oldValue !== newValue\n  onUpdate?: (name: string, oldValue: string | null, newValue: string | null) =\u003e void\n  onAdopt?: () =\u003e void               // called when custom element changes the document\n  onSlotChange?: (e: Event) =\u003e void  // called when a slot changes\n}\n```\n\nAll callbacks can be set during creation of the web component. After that, the context is read-only.\n\n## Lifecycle\n\nThe user may set callbacks on the context object:\n\n* `onMount`: called when connected to the DOM\n* `onUnmount`: called when disconnecting from DOM\n* `onUpdate`: called on attribute or property change, if oldValue !== newValue\n* `onAdopt`: called when custom element changes the document\n* `onSlotChange`: called when a slot changes\n\nWhen a web component (read: a custom element) is created, the following effects happen:\n\n1. if mode is set then the shadow root is created (default: no shadow root)\n2. the context property is created, containing element and root\n3. the web imports are performed asynchronously\n4. the scripts are removed from the template\n\nAfter 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:\n\n1. the observed properties are linked to the attributes\n2. the template is cloned and inserted into the (shadow) root\n3. the scripts are executed\n4. the context is frozen\n5. the `onMount` callback is called\n6. the `onUpdate` callback is called for all observed properties\n7. the `onSlotChange` callback is called for all slots\n\nWhen the element is disconnected from the DOM, the following effects happen:\n\n* the `onUnmount` callback is called\n\nWhen slot content changes, the following effects happen:\n\n* the `onSlotChange` callback is called\n\nWhen the custom element changes the document, the following effects happen:\n\n* the `onAdopt` callback is called\n\n## Web imports\n\nWeb imports are plain HTML imports that are inserted into the DOM.\n\nOnce 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.\n\n```html\n\u003chead\u003e\n  \u003cbase href=\"/components\"\u003e\n  \u003clink rel=\"preload\" href=\"web-components.html\" as=\"fetch\"\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n  \u003cweb-import src=\"web-components.html\"\u003e\u003c/web-import\u003e\n  \u003cscript type=\"module\" src=\"https://esm.run/candid\"\u003e\u003c/script\u003e\n\u003c/body\u003e\n```\n\nWeb 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.\n\n```html\n\u003cweb-component name=\"lazy-one\" mode=\"open\"\u003e\n  \u003ctemplate\u003e\n    \u003cweb-import src=\"lazy-one.css.html\"\u003e\u003c/web-import\u003e\n    \u003cweb-import src=\"https://my.cdn/lazy-one.js.html\"\u003e\u003c/web-import\u003e\n    \u003ch1\u003eCandid!\u003c/h1\u003e\n  \u003c/template\u003e\n\u003c/web-component\u003e\n```\n\nWeb-imports can be nested and cross-reference different domains. Beware of cycles!\n\n## Extending built-in HTML elements\n\nWeb components work in all major browsers and Candid can be used without any hassles. However, it is possible to extend built-in HTML elements.\n\n```html\n\u003cform\u003e\n  \u003cinput is=\"fancy-input\"\u003e\n\u003c/form\u003e\n\u003cweb-component name=\"fancy-input\" extends=\"input\" props=\"{ type: 'text', placeholder: 'fancy' }\"\u003e\n  \u003ctemplate\u003e\n    \u003ch1\u003eCandid!\u003c/h1\u003e\n  \u003c/template\u003e\n\u003c/web-component\u003e\n```\n\nSafari [does need a polyfill](https://caniuse.com/mdn-api_customelementregistry_builtin) for [customized built-in elements](https://html.spec.whatwg.org/multipage/custom-elements.html#custom-elements-customized-builtin-example).\n\n**Solution:** add the `@ungap/custom-elements` script by [@WebReflection](https://twitter.com/WebReflection) to the HTML `\u003chead\u003e` section.\n\n```html\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/@ungap/custom-elements\"\u003e\u003c/script\u003e\n```\n\n## How to stop FOUC\n\nFOUC 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.\n\n```css\nmy-element:not(:defined) {\n  /* Pre-style, give layout, replicate my-element's eventual styles, etc. */\n  display: inline-block;\n  height: 100vh;\n  opacity: 0;\n  transition: opacity 0.3s ease-in-out;\n}\n```\n\nSee [Google Web Fundamentals](https://developers.google.com/web/fundamentals/web-components/customelements#prestyle) and [StackOverflow](https://stackoverflow.com/questions/62683430/how-to-stop-fouc-from-happening-with-native-web-components).\n\n## Security\n\nCandid internally uses `eval` to evaluate scripts. If there are security concerns, use the [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) (CSP) to prevent the scripts from being evaluated.\n\n## Reflection\n\nGiven an element `el`, the following information is available:\n\n```ts\nconst name = el.hasAttribute('is') : el.getAttribute('is') : el.tagName.toLowerCase();\nconst superTag = el.hasAttribute('is') ? el.tagName.toLowerCase() : undefined;\nconst mode = el.shadowRoot?.mode;\nconst customElement = customElements.get(name);\nconst propNames = customElement.observedAttributes;\nconst props = propNames.reduce((props, name) =\u003e (props[name] = el.getAttribute(name), props), {});\n```\n","funding_links":["https://github.com/sponsors/danieldietrich","https://www.paypal.me/danieldietrich13"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanieldietrich%2Fcandid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanieldietrich%2Fcandid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanieldietrich%2Fcandid/lists"}