### Install and Run Local Server (Bash) Source: https://github.com/kennyfrc/cami.js/blob/master/examples/README.md This snippet uses the Bun package manager to install the 'serve' package globally and then executes it to start a local web server serving the current directory. This is necessary to view the project examples in a browser. ```bash bun install --global serve bunx serve ``` -------------------------------- ### Complete Cami.js Counter Component Example (Live Demo) Source: https://github.com/kennyfrc/cami.js/blob/master/docs/features/reactive_web_components.md A slightly modified version of the complete counter component example, including additional CSS classes on buttons, presented as a live demo within an HTML article tag. ```html ``` -------------------------------- ### HTML Structure for Cami.js Popover Example Source: https://github.com/kennyfrc/cami.js/blob/master/docs/learn_by_example/popover.md Provides the basic HTML layout for the page, including a heading, paragraph, the custom `cami-popover` element, and script tags to include the Cami.js library. ```html

Popover

When you click on either the top / bottom / left / right buttons, a popover will appear in the corresponding position against the reference element.

``` -------------------------------- ### Complete Cami.js Counter Component Example (Basic) Source: https://github.com/kennyfrc/cami.js/blob/master/docs/features/reactive_web_components.md A full example demonstrating the definition of a reactive counter component using Cami.js, including class definition, template, and registration, embedded within an HTML script tag for immediate use. ```html ``` -------------------------------- ### HTML Structure for Cami.js Registration Form Source: https://github.com/kennyfrc/cami.js/blob/master/docs/features/streams.md Defines the basic HTML structure for the registration form example, including a heading, the custom element, and instructions for testing. It also includes script tags to load the Cami.js library. ```html

Registration Form

Try entering an email that is already taken, such as trevinowanda@example.net (mock email)

``` -------------------------------- ### CSS Styling for Popover Backdrop Source: https://github.com/kennyfrc/cami.js/blob/master/docs/learn_by_example/popover.md Defines CSS rules for a popover backdrop element. Although commented as unused in this specific example, it shows how a fixed-position overlay could be styled. ```css ``` -------------------------------- ### Creating ObservableStore Instances with Cami.js Source: https://github.com/kennyfrc/cami.js/blob/master/docs/api/observable_store.md Provides examples of using the `store` function to create new ObservableStore instances. It shows how to create a store with the default localStorage persistence enabled and how to disable persistence using the options object. ```javascript // Create a store with default localStorage support const CartStore = store({ cartItems: [] }); // Create a store without localStorage support const NonPersistentStore = store({ items: [] }, { localStorage: false }); ``` -------------------------------- ### HTML Structure for Circle Drawer Source: https://github.com/kennyfrc/cami.js/blob/master/docs/learn_by_example/circle_drawer.md Defines the basic HTML layout for the circle drawer application, including the main container, the custom `` element, and script tags to include the Cami.js library. ```html

Circle Drawer

``` -------------------------------- ### Defining Custom Element and Dependencies HTML Source: https://github.com/kennyfrc/cami.js/blob/master/docs/learn_by_example/contact_manager.md This HTML snippet sets up the basic page structure, includes the custom `` element where the application logic will be rendered, and links the Cami.js library script necessary for the custom element and reactivity. ```html
``` -------------------------------- ### Embedding and Registering a Cami.js Component (counter-component) Source: https://github.com/kennyfrc/cami.js/blob/master/README.md Provides another example of a Cami.js counter component, demonstrating how to embed it within standard HTML (`
`) and register it with a different tag name (``). The component logic for state and rendering is identical to the basic example. ```HTML

Counter

``` -------------------------------- ### Importing Cami.js Library Components Source: https://github.com/kennyfrc/cami.js/blob/master/docs/learn_by_example/popover.md Imports necessary components (`html`, `ReactiveElement`) from the Cami.js library using a module script, which are required for defining custom reactive elements. ```javascript ``` -------------------------------- ### Cami Counter Demo with Styling (HTML/JS) Source: https://github.com/kennyfrc/cami.js/blob/master/docs/index.md Provides a demo example of the Cami counter component with added CSS classes for styling and a paragraph tag for the count display. Includes the CDN script and the JavaScript component definition and registration. ```html

Counter

``` -------------------------------- ### HTML Structure for Cami.js Nested Update Example Source: https://github.com/kennyfrc/cami.js/blob/master/docs/learn_by_example/nested_updates.md Defines the basic HTML structure including the custom element tag `` and includes the Cami.js library script. ```html
User Info with Deeply Nested Data
``` -------------------------------- ### HTML Structure for Temperature Converter Source: https://github.com/kennyfrc/cami.js/blob/master/docs/learn_by_example/temperature_converter.md Provides the basic HTML layout for the temperature converter, including the custom element tag and the necessary script imports for the Cami library and the custom element definition. ```HTML

Temperature Converter

``` -------------------------------- ### Converting HTTP Response to JSON with cami.js Source: https://github.com/kennyfrc/cami.js/blob/master/docs/api/http.md This example shows how to make an HTTP GET request using the `http` function and then use the `.toJson()` method on the resulting `HTTPStream` to parse the response body as JSON. It demonstrates handling the successful JSON data and potential errors using `.then()` and `.catch()`. ```JavaScript http('https://api.example.com/data') .toJson() .then(data => console.log(data)) .catch(error => console.error(error)); ``` -------------------------------- ### DemoPopoverElement onConnect Method Source: https://github.com/kennyfrc/cami.js/blob/master/docs/learn_by_example/popover.md The `onConnect` lifecycle method for `DemoPopoverElement`. It performs an early render, finds the reference element in the DOM, and sets the initial placement to 'top'. ```javascript onConnect() { this.render(); // early render because we need the referenceElement to be set this.referenceElement = document.querySelector(".referenceElement"); this.placement = 'top'; } ``` -------------------------------- ### CSS Styles for Circle Drawer Layout Source: https://github.com/kennyfrc/cami.js/blob/master/docs/learn_by_example/circle_drawer.md Provides the CSS rules to style the layout of the circle drawer application, including flexbox for arrangement, margins, borders, and positioning for the canvas area and circles. ```css ``` -------------------------------- ### HTML Structure and Styles for Anchored Popover Source: https://github.com/kennyfrc/cami.js/blob/master/docs/learn_by_example/anchored_popover.md Defines the basic HTML layout including a custom `cami-anchored-popover` element and provides CSS styles for centering text and styling the popover backdrop. ```HTML

Anchored Popover

An anchored popover relative to any coordinate. In this example, we're usign the mouse position as the reference element.

``` -------------------------------- ### Making Basic HTTP Request with cami.js Source: https://github.com/kennyfrc/cami.js/blob/master/docs/api/http.md This example illustrates how to initiate an HTTP request using the main `http` function with a URL string. It shows how to use the `.tap()` operator to inspect the stream data (the response) and `.catchError()` to handle any errors that occur during the request. ```JavaScript http('https://api.example.com/data') .tap(data => console.log(data)) .catchError(error => console.error(error)); ``` -------------------------------- ### Cami.js Counter Component Definition Source: https://github.com/kennyfrc/cami.js/blob/master/docs/learn_by_example/counter_interval.md Defines a `ReactiveElement` subclass `CounterElement` with a `count` property, a `doubleCount` getter (formula), `onConnect` lifecycle method to set up an interval and effects, and a `template` method for rendering. ```javascript const { html, ReactiveElement } = cami; class CounterElement extends ReactiveElement { count = 0; get doubleCount() { return this.count * 2; } onConnect() { setInterval(() => this.count++, 1000); this.effect(() => console.log(`Count: ${this.count}`)); this.effect(() => console.log(`Double Count: ${this.doubleCount}`)); } template() { return html`
Double Count: ${this.doubleCount}
`; } } customElements.define('counter-component', CounterElement); ``` -------------------------------- ### Creating ObservableStream from another Stream (JS) Source: https://github.com/kennyfrc/cami.js/blob/master/docs/api/observable_stream.md This example demonstrates how to use the static ObservableStream.from method to create a new ObservableStream instance from an existing ObservableStream (clickStream). The new stream will emit the same values as the source stream. ```js // Example 1: Creating an ObservableStream from a user event stream const clickStream = new ObservableStream(subscriber => { document.addEventListener('click', event => subscriber.next(event)); }); const observableStream = ObservableStream.from(clickStream); ``` -------------------------------- ### DemoPopoverElement template Method Source: https://github.com/kennyfrc/cami.js/blob/master/docs/learn_by_example/popover.md Defines the HTML template for the `DemoPopoverElement` using Cami.js `html` tagged template literal. It includes a dialog for the popover content and buttons to control the popover's placement and open/close state. ```javascript template() { return html` event.stopPropagation()} role="dialog" aria-modal=${this.isOpen} aria-labelledby="dialog-title">
Hi! I'm a Popover
`; } } ``` -------------------------------- ### Handling Component Connection (JavaScript) Source: https://github.com/kennyfrc/cami.js/blob/master/docs/learn_by_example/anchored_popover.md Implements the `onConnect` lifecycle method for the `AnchoredPopover` component. It triggers an initial render, initializes properties, and sets up a `mousemove` event listener on the document. The listener updates the component's `referenceElement` to the current mouse position and calls `positionPopover` to reposition the popover. ```javascript onConnect() { this.render(); // early render because we need the dialog to be in the DOM this.referenceElement = null; this.placement = 'top'; document.addEventListener('mousemove', (event) => { this.referenceElement = this.createVirtualElement(event.clientX, event.clientY); this.positionPopover(); }); } ``` -------------------------------- ### HTML Structure for Flight Booker Component Source: https://github.com/kennyfrc/cami.js/blob/master/docs/learn_by_example/flight_booker.md This HTML snippet defines the custom element tag and includes the necessary script tags to load the cami.js library and the component's JavaScript code. It shows how to use the custom element in the page. ```html ``` -------------------------------- ### Setting Up Input Validation Streams in Cami.js Source: https://github.com/kennyfrc/cami.js/blob/master/docs/features/streams.md Configures stream subscriptions in the `onConnect` lifecycle method. It maps input events to validation functions, debounces the results, and updates the component's state based on validation outcomes and email availability checks. ```JavaScript onConnect() { this.inputValidation$ .map(e => this.validateEmail(e.target.value)) .debounce(300) .subscribe(({ isEmailValid, emailError, email }) => { this.emailError = emailError; this.isEmailValid = isEmailValid; this.email = email; this.isEmailAvailable = this.queryEmail(this.email) }); this.passwordValidation$ .map(e => this.validatePassword(e.target.value)) .debounce(300) .subscribe(({ isValid, password }) => { this.passwordError = isValid ? '' : 'Password must be at least 8 characters long.'; this.password = password; }); } ``` -------------------------------- ### Initializing Form State Observables - JavaScript Source: https://github.com/kennyfrc/cami.js/blob/master/docs/features/streams.md This snippet shows the declaration of observable properties within a class extending `ReactiveElement`. These properties are used to manage the state of the form, including input values and validation errors. ```javascript const { html, ReactiveElement } = cami; class FormElement extends ReactiveElement { emailError = ''; passwordError = ''; email = ''; password = ''; emailIsValid = null; isEmailAvailable = null; // ... } ``` -------------------------------- ### Querying Email Availability via API in Cami.js Source: https://github.com/kennyfrc/cami.js/blob/master/docs/features/streams.md Uses the Cami.js `query` method to asynchronously fetch user data from an API based on the email address. Configures a query key and a fetch function, setting a stale time for caching. ```JavaScript queryEmail(email) { return this.query({ queryKey: ['Email', email], queryFn: () => { return fetch(`https://api.camijs.com/users?email=${email}`).then(res => res.json()) }, staleTime: 1000 * 60 * 5 }) } ``` -------------------------------- ### Initializing ObservableState in Cami.js (JavaScript) Source: https://github.com/kennyfrc/cami.js/blob/master/docs/api/observable_state.md This example demonstrates how to create a new instance of the ObservableState class from the 'cami-js' library, initializing it with a value (10 in this case), and then accessing its current value using the '.value' property. ```javascript import { ObservableState } from 'cami-js'; const observable = new ObservableState(10); console.log(observable.value); // 10 ``` -------------------------------- ### Defining Cami.js Reactive Form Element and State Source: https://github.com/kennyfrc/cami.js/blob/master/docs/features/streams.md Defines the `FormElement` class extending `ReactiveElement` to manage form state reactively. Initializes properties for email, password, validation errors, validity status, and streams for handling input events. ```JavaScript class FormElement extends ReactiveElement { emailError = '' passwordError = '' email = ''; password = ''; emailIsValid = null; isEmailAvailable = null; inputValidation$ = this.stream(); passwordValidation$ = this.stream(); // ... rest of the class methods } ``` -------------------------------- ### Defining Basic HTML Form Template - JavaScript Source: https://github.com/kennyfrc/cami.js/blob/master/docs/features/streams.md This snippet defines the initial HTML structure for a form using a JavaScript template literal. It includes input fields for email and password, placeholders for validation messages, and a submit button. ```js template() { return html`
`; } ``` -------------------------------- ### JavaScript Custom Element for Temperature Converter (Cami) Source: https://github.com/kennyfrc/cami.js/blob/master/docs/learn_by_example/temperature_converter.md Defines the `TemperatureConverterElement` custom element using the Cami library. It manages the Celsius and Fahrenheit values, provides conversion logic, and renders the input fields using Cami's reactive template system. ```JavaScript const { html, ReactiveElement } = cami; class TemperatureConverterElement extends ReactiveElement { celsius = ''; fahrenheit = ''; convertToFahrenheit(celsius) { if (!isNaN(celsius) && celsius !== '') { this.fahrenheit = celsius * (9/5) + 32; } } convertToCelsius(fahrenheit) { if (!isNaN(fahrenheit) && fahrenheit !== '') { this.celsius = (fahrenheit - 32) * (5/9); } } template() { return html` `; } } customElements.define('temperature-converter', TemperatureConverterElement); ``` -------------------------------- ### Implementing Optimistic UI Blog Component with Cami.js Source: https://github.com/kennyfrc/cami.js/blob/master/docs/learn_by_example/blog.md This snippet defines a custom web component `blog-component` using Cami.js. It fetches blog posts using a `query` and adds new posts using a `mutation`. The mutation includes `onMutate` for optimistic updates, `onError` for rollback, and `onSettled` for cache invalidation, demonstrating optimistic UI patterns. ```javascript
```