### Bootstrapping React Application - index.js Source: https://github.com/jsebrech/plainvanilla/blob/main/public/blog/articles/2024-09-28-unreasonable-effectiveness-of-vanilla-js/index.html This snippet shows the standard entry point for a React application. It imports the stylesheet, initializes the React root, and renders the main `App` component into the DOM element with the ID 'root'. This is the typical setup for a React project. ```JavaScript import './styles.css'; import { createRoot } from 'react-dom/client'; import App from './App.js'; const root = createRoot(document.getElementById('root')); root.render(); ``` -------------------------------- ### Defining a Custom Element Constructor (JavaScript) Source: https://github.com/jsebrech/plainvanilla/blob/main/public/pages/components.html This snippet shows the start of a custom element's constructor. The constructor is invoked exactly once when the element is created, making it the ideal place to perform initial setup like attaching a Shadow DOM. It typically begins with a call to super() to invoke the parent HTMLElement constructor. ```JavaScript constructor() { ``` -------------------------------- ### Styling Custom Elements with CSS Source: https://github.com/jsebrech/plainvanilla/blob/main/public/blog/articles/2024-09-16-life-and-times-of-a-custom-element/observer/example.html Defines basic CSS styles for the document body and the `x-wall` custom element, ensuring proper display and spacing for the custom components. ```css body { font-family: system-ui, sans-serif; margin: 1em; } x-wall { display: block; margin-bottom: 1em; } ``` -------------------------------- ### Vanilla JS Port of Preact Signals Example - JavaScript Source: https://github.com/jsebrech/plainvanilla/blob/main/public/blog/articles/2024-08-30-poor-mans-signals/index.html This example demonstrates the combined use of `signal` and `computed` to replicate a common pattern from Preact's signals. It shows how a `count` signal can drive a `doubled` computed signal, with effects logging their values as they change, illustrating reactive updates. ```JavaScript const count = signal(0); const doubled = computed(() => count.value * 2, [count]); count.effect(() => console.log('Count:', count.value)); doubled.effect(() => console.log('Doubled:', doubled.value)); count.value++; count.value++; count.value++; ``` -------------------------------- ### Running Static Website with Node.js HTTP Server Source: https://github.com/jsebrech/plainvanilla/blob/main/README.md This command uses `npx` to execute `http-server`, a simple static file server, to serve the `public/` directory. The `-c-1` option disables caching. It's a quick way to get a local development server running without complex configurations. ```Shell npx http-server public -c-1 ``` -------------------------------- ### Installing ESLint Globally for Vanilla JS Projects Source: https://github.com/jsebrech/plainvanilla/blob/main/public/blog/articles/2024-10-20-editing-plain-vanilla/index.html This command globally installs ESLint along with its core JavaScript rules and the `globals` package. This allows ESLint to be reused across multiple vanilla JavaScript projects without per-project installation, serving as a prerequisite for VS Code's ESLint extension. ```Shell npm install -g eslint @eslint/js globals ``` -------------------------------- ### Running Static Website with Python HTTP Server Source: https://github.com/jsebrech/plainvanilla/blob/main/README.md This command starts a simple HTTP server using Python's `http.server` module. It serves files from the `public/` directory on port `8000`. The `--directory` option ensures the server serves content from the specified path, making it ideal for quick static site hosting. ```Shell python3 -m http.server 8000 --directory public ``` -------------------------------- ### Nested Context Providers and Subscriber - HTML Source: https://github.com/jsebrech/plainvanilla/blob/main/public/blog/articles/2024-10-07-needs-more-context/index.html This HTML example illustrates the use of multiple context providers, demonstrating how a subscriber receives context from the nearest provider. A global `context-provider` on the `body` sets a default 'dark' theme, while a nested `context-provider` within a panel overrides it with a 'light' theme for its children. This showcases context overriding and scope. ```HTML Nested Context Example

Global Theme (Dark)

Local Theme (Light)

``` -------------------------------- ### Using Context Protocol Elements in HTML - HTML Source: https://github.com/jsebrech/plainvanilla/blob/main/public/blog/articles/2024-10-07-needs-more-context/index.html This HTML example demonstrates how to use the previously defined custom elements (`theme-toggle` and `my-subscriber`) on a web page. It includes the necessary JavaScript files for the custom element definitions. The `my-subscriber` element will automatically request the 'theme-toggle' context provided by `theme-toggle`, illustrating the end-to-end context communication. ```HTML Context Protocol Example ``` -------------------------------- ### HTML Page Integrating imports.js and Custom Element (HTML/JavaScript) Source: https://github.com/jsebrech/plainvanilla/blob/main/public/pages/sites.html This HTML example illustrates how to integrate the `imports.js` file into a web page. It loads UMD libraries via standard script tags, imports `imports.js` as a module, and defines a custom `` web component that consumes the `dayjs` and `web-vitals` exports to display dynamic content and log performance metrics. ```HTML Metrics Page ``` -------------------------------- ### Adding x-bottle to x-wall - JavaScript Source: https://github.com/jsebrech/plainvanilla/blob/main/public/blog/articles/2024-09-16-life-and-times-of-a-custom-element/observer/example.html Defines a global JavaScript function `add` that creates a new `x-bottle` element and appends it to the `x-wall` element. This action automatically triggers the `MutationObserver` on `x-wall`, updating the displayed bottle count. ```javascript function add() { document.querySelector('x-wall').append( document.createElement('x-bottle')); } ``` -------------------------------- ### Structuring CSS with @import (CSS) Source: https://github.com/jsebrech/plainvanilla/blob/main/public/pages/styling.html This example demonstrates how to use the `@import` rule in a main CSS file (e.g., `index.css`) to organize and load other stylesheets. This approach helps manage CSS complexity by separating concerns into distinct files like resets, variables, global styles, and component-specific styles. ```CSS @import "./styles/reset.css"; @import "./styles/variables.css"; @import "./styles/global.css"; @import "./components/x-header/x-header.css"; @import "./components/x-avatar/x-avatar.css"; @import "./components/x-footer/x-footer.css"; ``` -------------------------------- ### Initial State of Custom Elements in HTML Source: https://github.com/jsebrech/plainvanilla/blob/main/public/blog/articles/2024-09-16-life-and-times-of-a-custom-element/shadowed/example.html This HTML snippet shows the initial rendering of the `x-shadowed` and `x-shadowed-later` custom elements before their JavaScript definitions are applied or their shadow DOMs are attached, indicating they are currently undefined and not shadowed. ```html : undefined, not shadowed : undefined, not shadowed ``` -------------------------------- ### Styling Custom Elements with CSS Source: https://github.com/jsebrech/plainvanilla/blob/main/public/blog/articles/2024-09-16-life-and-times-of-a-custom-element/shadowed/example.html This CSS snippet provides basic styling for the document body and sets a background color for the `x-shadowed` and `x-shadowed-later` custom elements when they are not yet shadowed or have their default styling. ```css body { font-family: system-ui, sans-serif; margin: 1em; } x-shadowed, x-shadowed-later { background-color: lightgray; } ``` -------------------------------- ### Defining x-wall Custom Element with MutationObserver - JavaScript Source: https://github.com/jsebrech/plainvanilla/blob/main/public/blog/articles/2024-09-16-life-and-times-of-a-custom-element/observer/example.html Registers the `x-wall` custom element. Its `connectedCallback` initializes a paragraph element for text display and sets up a `MutationObserver` to watch for `x-bottle` child additions or removals, triggering the `update` method to refresh the bottle count. ```javascript customElements.define('x-wall', class extends HTMLElement { connectedCallback() { if (this.line) return; // prevent double initialization this.line = document.createElement('p'); this.insertBefore(this.line, this.firstChild); new MutationObserver(() => this.update()).observe(this, { childList: true }); this.update(); } update() { const count = this.querySelectorAll('x-bottle').length; this.line.textContent = `${count} ${count === 1 ? 'bottle' : 'bottles'} of beer on the wall`; } }); ``` -------------------------------- ### Defining x-bottle Custom Element - JavaScript Source: https://github.com/jsebrech/plainvanilla/blob/main/public/blog/articles/2024-09-16-life-and-times-of-a-custom-element/observer/example.html Registers the `x-bottle` custom element. Its `connectedCallback` simply sets its text content to an emoji, visually representing a single bottle. ```javascript customElements.define('x-bottle', class extends HTMLElement { connectedCallback() { this.textContent = '🍺'; } }); ``` -------------------------------- ### Defining Custom Elements with Shadow DOM in JavaScript Source: https://github.com/jsebrech/plainvanilla/blob/main/public/blog/articles/2024-09-16-life-and-times-of-a-custom-element/shadowed/example.html This JavaScript function defines two custom elements, `x-shadowed` and `x-shadowed-later`. `x-shadowed` attaches an open shadow DOM immediately in its constructor. `x-shadowed-later` attaches its shadow DOM dynamically upon a user click, demonstrating deferred shadow DOM attachment. ```javascript function define() { customElements.define('x-shadowed', class extends HTMLElement { constructor() { super(); this.attachShadow({mode: 'open'}); this.shadowRoot.innerHTML = ` shadowed `; } }); customElements.define('x-shadowed-later', class extends HTMLElement { connectedCallback() { this.innerHTML = 'constructed, click to shadow'; this.querySelector('a').onclick = (e) => { e.preventDefault(); this.addShadow() }; } addShadow() { this.attachShadow({mode: 'open'}); this.shadowRoot.innerHTML = ` shadowed `; } }); document.querySelector('button#define').setAttribute('disabled', true); } ``` -------------------------------- ### Vue.js Event Binding Syntax Example (HTML) Source: https://github.com/jsebrech/plainvanilla/blob/main/public/blog/articles/2024-09-03-unix-philosophy/index.html This HTML snippet illustrates Vue.js's declarative syntax for binding a DOM event (`click`) to a JavaScript method (`doThis`). It provides inspiration for the design of a generic `bind()` function that would handle event binding back to data in a 'plain vanilla' web development context. ```HTML ``` -------------------------------- ### Vue.js Attribute Binding Syntax Example (HTML) Source: https://github.com/jsebrech/plainvanilla/blob/main/public/blog/articles/2024-09-03-unix-philosophy/index.html This HTML snippet demonstrates Vue.js's declarative syntax for binding an HTML attribute (`src`) to a JavaScript property (`imageSrc`). It serves as an inspiration for the design of a generic `bind()` function that would connect data to DOM elements in a 'plain vanilla' web development approach. ```HTML ``` -------------------------------- ### Configuring NODE_PATH for Global ESLint with NVM Source: https://github.com/jsebrech/plainvanilla/blob/main/public/blog/articles/2024-10-20-editing-plain-vanilla/index.html This shell command sets the `NODE_PATH` environment variable to the global npm root directory. This is necessary when using Node Version Manager (nvm) to ensure that globally installed packages like ESLint are discoverable by tools such as VS Code. ```Shell export NODE_PATH=$(npm root -g) ``` -------------------------------- ### Styling Shadow DOM Content with :host (CSS) Source: https://github.com/jsebrech/plainvanilla/blob/main/public/pages/components.html This CSS snippet demonstrates how to style elements within a Shadow DOM. The :host pseudo-selector targets the custom element itself (the host of the Shadow DOM), while other selectors like h1 apply styles to elements inside the Shadow DOM, benefiting from its encapsulation. It also shows importing a reset.css as Shadow DOM starts unstyled. ```CSS @import url("reset.css"); /* Assuming reset.css is imported */ :host { display: block; /* Example host style */ padding: 10px; } h1 { color: blue; /* Example isolated style */ font-size: 2em; } ``` -------------------------------- ### Handling Boolean Attributes in a Custom Element Source: https://github.com/jsebrech/plainvanilla/blob/main/public/blog/articles/2025-04-21-attribute-property-duality/index.html This JavaScript class extends the previous example to correctly handle boolean attributes. It demonstrates the standard HTML behavior where the mere presence of a boolean attribute (regardless of its value) makes the corresponding property true, and its absence makes it false. ```JavaScript class MyHello extends HTMLElement { static get observedAttributes() { return ['value', 'active']; } constructor() { super(); this._value = ''; this._active = false; } get value() { return this._value; } set value(v) { this._value = v; this.setAttribute('value', v); } get active() { return this._active; } set active(v) { this._active = !!v; if (this._active) { this.setAttribute('active', ''); } else { this.removeAttribute('active'); } } attributeChangedCallback(name, oldValue, newValue) { if (name === 'value') { this._value = newValue; } else if (name === 'active') { this._active = newValue !== null; } } } ``` -------------------------------- ### Scoping Styles within Shadow DOM (CSS) Source: https://github.com/jsebrech/plainvanilla/blob/main/public/pages/styling.html This example illustrates how styles applied within a component's Shadow DOM, such as for an `h1` element inside `x-header`, are isolated from the main document. It also highlights that CSS variables defined in the light DOM can be inherited and used within the Shadow DOM, maintaining theme consistency. ```CSS /* Inside x-header's shadow DOM stylesheet */ h1 { font-size: 2em; color: var(--color-text); /* Uses a variable passed from light DOM */ } ``` -------------------------------- ### SPA Bootstrapping with index.html (HTML) Source: https://github.com/jsebrech/plainvanilla/blob/main/public/pages/applications.html This HTML snippet shows the `index.html` file for a Plain Vanilla single-page application. It imports various JavaScript modules, including `App.js` and a `router.js`, and then renders the main `` web component, serving as the application's entry point. ```HTML Plain Vanilla App ``` -------------------------------- ### Bootstrapping Vanilla JS Application - index.js Source: https://github.com/jsebrech/plainvanilla/blob/main/public/blog/articles/2024-09-28-unreasonable-effectiveness-of-vanilla-js/index.html This snippet serves as the entry point for the vanilla JavaScript application. It imports all custom web components and then directly injects the root `` component into the document body, demonstrating a simpler bootstrapping process without a framework's overhead. ```JavaScript import './styles.css'; import './App.js'; import './TasksContext.js'; import './AddTask.js'; import './TaskList.js'; document.body.innerHTML = ''; ``` -------------------------------- ### Running Static Website with PHP Built-in Server Source: https://github.com/jsebrech/plainvanilla/blob/main/README.md This command utilizes PHP's built-in development server to serve the `public/` directory. It listens on `localhost` at port `8000`. The `-t` option specifies the document root, making it suitable for serving static files or simple PHP applications. ```Shell php -S localhost:8000 -t public ``` -------------------------------- ### Getting a Button Element by ID (JavaScript) Source: https://github.com/jsebrech/plainvanilla/blob/main/public/blog/articles/2024-09-16-life-and-times-of-a-custom-element/defined2/example.html This helper function retrieves a button element from the DOM using its ID. It's used by other functions, such as `setButtonDisabled`, to access and manipulate control buttons. ```JavaScript function getButton(id) { return document.querySelector('button#' + id); } ``` -------------------------------- ### Single-Page Application with x-router (HTML) Source: https://github.com/jsebrech/plainvanilla/blob/main/public/pages/applications.html This HTML snippet demonstrates how to use the `x-router` web component to build a single-page application. It defines multiple route-specific components (`x-home`, `x-about`, `x-contact`) within the router, which are then shown or hidden based on the URL hash. ```HTML ``` -------------------------------- ### Initializing Storage and UI (JavaScript) Source: https://github.com/jsebrech/plainvanilla/blob/main/public/blog/articles/2024-09-16-life-and-times-of-a-custom-element/defined2/example.html This snippet initializes a global `storage` object to hold references to the custom element instance and immediately calls the `update()` function to set the initial state of the UI controls and status display. ```JavaScript let storage = { }; update(); ``` -------------------------------- ### Bundling Third-Party Dependencies in imports.js (JavaScript) Source: https://github.com/jsebrech/plainvanilla/blob/main/public/pages/sites.html This JavaScript snippet from `imports.js` demonstrates how to bundle third-party libraries. It uses `import` for ESM modules like `web-vitals` and accesses UMD modules like `dayjs` from the global `window` object, extending `dayjs` with a plugin before exporting all dependencies for application-wide use. ```JavaScript import { getCLS, getFID, getLCP } from './web-vitals.js'; const dayjs = window.dayjs; const relativeTime = window.dayjs_plugin_relativeTime; dayjs.extend(relativeTime); export { dayjs, getCLS, getFID, getLCP }; ``` -------------------------------- ### Defining Custom Element x-example (JavaScript) Source: https://github.com/jsebrech/plainvanilla/blob/main/public/blog/articles/2024-09-16-life-and-times-of-a-custom-element/defined/example.html This JavaScript function `define()` registers the `x-example` custom element with the browser. It extends `HTMLElement` and sets a 'status' attribute to 'ready' when the element is connected to the DOM, indicating its initialization. ```JavaScript function define() { customElements.define('x-example', class extends HTMLElement { constructor() { super(); } connectedCallback() { this.setAttribute('status', 'ready'); } }); } ``` -------------------------------- ### Structuring Page with HTML Landmarks - HTML Source: https://github.com/jsebrech/plainvanilla/blob/main/public/pages/sites.html Recommends organizing page markup using semantic HTML landmark elements like
,
, and