### Post to Twitter Example Source: https://github.com/mithriljs/mithril.js/wiki/Components Demonstrates how to post content to Twitter using JavaScript. No specific setup is mentioned, but it implies interaction with the Twitter API. ```javascript function postToTwitter() { var url = 'https://twitter.com/intent/tweet'; var text = 'Mithril.js is awesome!'; var shareUrl = url + '?text=' + encodeURIComponent(text); window.open(shareUrl, '_blank'); } ``` -------------------------------- ### Login Form Example Source: https://github.com/mithriljs/mithril.js/wiki/Recipes-and-Snippets A basic login form implementation using Mithril.js. This example demonstrates how to structure a form with input fields and a submit button. ```javascript var LoginForm = { username: '', password: '', view: function() { return m('form', [ m('label', 'Username:'), m('input[type=text]', { oninput: m.withAttr('value', (value) => this.username = value), value: this.username }), m('label', 'Password:'), m('input[type=password]', { oninput: m.withAttr('value', (value) => this.password = value), value: this.password }), m('button[type=submit]', 'Login') ]); } }; m.mount(document.body, LoginForm); ``` -------------------------------- ### Install Mithril.js via npm Source: https://context7.com/mithriljs/mithril.js/llms.txt Install Mithril.js and its TypeScript types using npm for project integration. ```bash # npm npm install mithril --save # TypeScript types npm install @types/mithril --save-dev ``` -------------------------------- ### Video Chooser/Player Example Source: https://github.com/mithriljs/mithril.js/wiki/Components An example that includes a video chooser and player, along with an illustration of paging a long list. This snippet likely demonstrates UI interaction and data handling. ```javascript // Example of paging a long list var list = Array.apply(null, Array(100)).map(function (_, i) { return 'Item ' + (i + 1); }); var pageSize = 10; var currentPage = 1; var paginatedList = { view: function() { var start = (currentPage - 1) * pageSize; var end = start + pageSize; var items = list.slice(start, end); return m('div', [ m('ul', items.map(function(item) { return m('li', item); })), m('div', [ m('button', { onclick: function() { if (currentPage > 1) currentPage--; } }, 'Prev'), m('span', ' Page ' + currentPage + ' of ' + Math.ceil(list.length / pageSize) + ' '), m('button', { onclick: function() { if (currentPage * pageSize < list.length) currentPage++; } }, 'Next') ]) ]); } }; m.mount(document.getElementById('list-container'), paginatedList); ``` -------------------------------- ### Post to Twitter Example (CoffeeScript) Source: https://github.com/mithriljs/mithril.js/wiki/Components Demonstrates how to post content to Twitter using CoffeeScript. This is an alternative to the JavaScript example, implying similar functionality. ```coffeescript # Post to Twitter postToTwitter = -> url = 'https://twitter.com/intent/tweet' text = 'Mithril.js is awesome!' shareUrl = url + '?text=' + encodeURIComponent(text) window.open shareUrl, '_blank' ``` -------------------------------- ### Basic Routing Example in Mithril.js Source: https://github.com/mithriljs/mithril.js/wiki/Recipes-and-Snippets Demonstrates how to switch between different routes within a Mithril.js component. Useful for creating single-page applications with navigation. ```javascript m.route(document.body, "/", { "/": Home, "/about": About, "/contact": Contact }); var root = document.body; m.route(root, "/", { "/": { render: function() { return m("div", "This is the home page.") } }, "/about": { render: function() { return m("div", "This is the about page.") } }, "/contact": { render: function() { return m("div", "This is the contact page.") } } }); ``` -------------------------------- ### Nested Components Example (Deprecated) Source: https://github.com/mithriljs/mithril.js/wiki/Components An example demonstrating nested components in Mithril.js. This example is marked as deprecated and suggests using modern component architecture instead. ```javascript var nestedComponent = { view: function() { return m('div', [ m('h3', 'Parent Component'), m(childComponent) ]); } }; var childComponent = { view: function() { return m('div', 'Child Component'); } }; m.mount(document.body, nestedComponent); ``` -------------------------------- ### Content Editable Example Source: https://github.com/mithriljs/mithril.js/wiki/Recipes-and-Snippets A Mithril.js example demonstrating the use of content editable elements. This snippet shows how to manage the content of an editable div. ```javascript var editableDiv = { view: function(vnode) { return m("div[contenteditable=true]", { oninput: function(e) { vnode.state.content = e.target.innerText; }, innerText: vnode.state.content }); } }; m.mount(document.body, editableDiv); ``` -------------------------------- ### Install Mithril.js TypeScript Definitions Source: https://github.com/mithriljs/mithril.js/blob/main/README.md Install the TypeScript type definitions for Mithril.js from DefinitelyTyped. This is necessary for using Mithril.js with TypeScript. ```bash $ npm install @types/mithril --save-dev ``` -------------------------------- ### Run Mithril.js Tests Source: https://github.com/mithriljs/mithril.js/blob/main/docs/contributing.md Command to execute all tests after installing dependencies. Use `o.only()` to focus on a specific test during debugging. ```bash npm run test ``` ```javascript o.only(description, test) ``` -------------------------------- ### Install Mithril.js via CDN Source: https://github.com/mithriljs/mithril.js/blob/main/README.md Include Mithril.js in your project using a CDN link. Choose between development and production versions, and select your preferred CDN provider (unpkg or jsDelivr). ```html ``` -------------------------------- ### POST with a CORS XHR Source: https://github.com/mithriljs/mithril.js/wiki/Recipes-and-Snippets Example of making a POST request with CORS XHR to avoid preflight OPTIONS conflicts. Useful for cross-origin communication. ```javascript var xhr = m.request({ method: "POST", url: "/foo/bar", data: { "a": 1 }, withCredentials: true, headers: { "Content-Type": "application/x-www-form-urlencoded" } }); ``` -------------------------------- ### Install Mithril.js via npm Source: https://github.com/mithriljs/mithril.js/blob/main/README.md Install Mithril.js as a project dependency using npm. This command adds Mithril.js to your project's node_modules directory and updates your package.json file. ```bash npm install mithril --save ``` -------------------------------- ### Basic CSS Animation in Mithril.js Source: https://github.com/mithriljs/mithril.js/wiki/Recipes-and-Snippets Applies a basic CSS animation to an element within a Mithril.js application. This example shows how to integrate CSS transitions or keyframe animations with Mithril components. ```javascript function AnimatedComponent() { return { view: function() { return m("div", { class: "animated-box" }, "Animate Me"); } } } m.mount(document.body, AnimatedComponent); ``` -------------------------------- ### Checkout and Create Feature Branch Source: https://github.com/mithriljs/mithril.js/blob/main/docs/contributing.md Steps to prepare your local repository for making changes by checking out the main branch and creating a new feature branch. ```bash git checkout main git checkout -b the-feature-branch-name ``` -------------------------------- ### m.route() - Client-side Router Initialization Source: https://context7.com/mithriljs/mithril.js/llms.txt Initialize Mithril's client-side router. Supports path parameters and configurable prefixes. Use programmatic navigation with m.route.set() and read current parameters with m.route.param(). ```javascript const Home = { view: () => m("h1", "Home") } const UserProfile = { view: (vnode) => m("div", m("h1", `User: ${vnode.attrs.id}`), m("p", `Tab: ${vnode.attrs.tab || "overview"}`) ) } const NotFound = { view: () => m("h1", "404 Not Found") } // Change prefix to use real pathnames (requires server-side catch-all) m.route.prefix = "" // Initialize router m.route(document.body, "/", { "/": Home, "/users/:id": UserProfile, "/users/:id/:tab": UserProfile, "/:404...": NotFound, // variadic catch-all }) // Programmatic navigation m.route.set("/users/42") m.route.set("/users/:id", {id: 42}) // with interpolation m.route.set("/users/42", null, {replace: true}) // replace history entry m.route.set("/users/42", null, {state: {from: "home"}}) // push history state // Read current route console.log(m.route.get()) // → "/users/42" // route.param() — read current params (inside a routed component) const UserPage = { view: () => m("p", `id = ${m.route.param("id")}`) } ``` -------------------------------- ### Mount a Component with m.mount() Source: https://context7.com/mithriljs/mithril.js/llms.txt Use m.mount() to render a component into a DOM element and enable automatic redraws. Pass null to unmount. ```js const Counter = { count: 0, view() { return m("div", m("p", `Count: ${Counter.count}`), m("button", { onclick: () => Counter.count++ // triggers automatic redraw }, "Increment"), m("button", { onclick: () => Counter.count-- }, "Decrement") ) } } // Mount to a DOM node m.mount(document.getElementById("app"), Counter) // Unmount (removes DOM content and subscriptions) m.mount(document.getElementById("app"), null) ``` -------------------------------- ### Form Validation with Parsley.js Source: https://github.com/mithriljs/mithril.js/wiki/Recipes-and-Snippets An example of integrating form validation using Parsley.js, including the use of regular expressions for validation rules. ```javascript var form = document.querySelector('form'); var parsley = new Parsley(form); form.addEventListener('submit', function(event) { if (parsley.isValid()) { // Submit form } else { event.preventDefault(); } }); ``` -------------------------------- ### Build Mithril.js Source: https://github.com/mithriljs/mithril.js/blob/main/docs/contributing.md Commands to generate development and minified builds of Mithril.js. ```bash npm run dev ``` ```bash npm run build ``` -------------------------------- ### m.route() — Client-side Router Source: https://context7.com/mithriljs/mithril.js/llms.txt Initializes client-side routing. Routes are keyed by path patterns (supporting :param and :variadic... segments). The default prefix is #! (hashbang) but can be changed. ```APIDOC ## m.route() ### Description Initializes client-side routing. Routes are keyed by path patterns (supporting `:param` and `:variadic...` segments). The default prefix is `#!` (hashbang) but can be changed to pathname or search-based routing. ### Parameters - **root** (DOMElement) - The root DOM element to mount the router to. - **defaultRoute** (string) - The default route to navigate to if no match is found. - **routes** (object) - An object mapping route patterns to components. ### Configuration - **m.route.prefix** (string) - Sets the routing prefix (e.g., `"#!"`, `"/"`). ### Methods - **m.route.set(route, params?, options?)** - Programmatically navigates to a route. - **m.route.get()** - Returns the current route. - **m.route.param(name)** - Reads a current route parameter (inside a routed component). ### Request Example ```javascript const Home = { view: () => m("h1", "Home") } const UserProfile = { view: (vnode) => m("div", m("h1", `User: ${vnode.attrs.id}`), m("p", `Tab: ${vnode.attrs.tab || "overview"}`) ) } const NotFound = { view: () => m("h1", "404 Not Found") } // Change prefix to use real pathnames (requires server-side catch-all) m.route.prefix = "" // Initialize router m.route(document.body, "/", { "/": Home, "/users/:id": UserProfile, "/users/:id/:tab": UserProfile, "/:404...": NotFound, // variadic catch-all }) // Programmatic navigation m.route.set("/users/42") m.route.set("/users/:id", {id: 42}) // with interpolation m.route.set("/users/42", null, {replace: true}) // replace history entry m.route.set("/users/42", null, {state: {from: "home"}}) // push history state // Read current route console.log(m.route.get()) // → "/users/42" // route.param() — read current params (inside a routed component) const UserPage = { view: () => m("p", `id = ${m.route.param("id")}`) } ``` ### Response #### Success Response (200) - **void** - Initializes the router and sets up navigation. ``` -------------------------------- ### m.mount() - Mount a Component to the DOM Source: https://context7.com/mithriljs/mithril.js/llms.txt The `m.mount()` function renders a given component into a specified DOM element and manages its lifecycle, including automatic redraws. Passing `null` as the component will unmount and clean up the element. ```APIDOC ## m.mount() - Mount a Component to the DOM ### Description Renders a component into a real DOM element and subscribes it to automatic redraws. Unmounts the component when `null` is passed as the component. ### Usage `m.mount(element, Component)` ### Parameters - **element** (DOMElement | null) - The DOM element to mount the component to, or `null` to unmount. - **Component** (object | function | null) - The component to mount, or `null` to unmount. ### Examples ```javascript const Counter = { count: 0, view() { return m("div", m("p", `Count: ${Counter.count}`), m("button", {onclick: () => Counter.count++}, "Increment") ) } } // Mount to a DOM node m.mount(document.getElementById("app"), Counter) // Unmount m.mount(document.getElementById("app"), null) ``` ``` -------------------------------- ### Parse and Build URL Pathnames with m.parsePathname and m.buildPathname Source: https://context7.com/mithriljs/mithril.js/llms.txt Use m.parsePathname to extract the path and parameters from a URL. Use m.buildPathname to construct URLs by interpolating parameters into a template, optionally appending remaining parameters as a query string. Supports variadic parameters. ```javascript // Parse pathname + query string m.parsePathname("/users/42?tab=settings#profile") // → {path: "/users/42", params: {tab: "settings"}} m.parsePathname("/search?q=mithril&page=1") // → {path: "/search", params: {q: "mithril", page: "1"}} // Build — interpolate :param tokens, append remainder as query string m.buildPathname("/users/:id/posts", {id: 42, page: 2, sort: "desc"}) // → "/users/42/posts?page=2&sort=desc" m.buildPathname("/api/:resource/:id", {resource: "comments", id: 7}) // → "/api/comments/7" // Variadic parameter (no encoding) m.buildPathname("/files/:path...", {path: "docs/guide/intro.md"}) // → "/files/docs/guide/intro.md" ``` -------------------------------- ### Import Mithril.js Source: https://context7.com/mithriljs/mithril.js/llms.txt Import Mithril.js into your project using CommonJS or ES module syntax. ```js // CommonJS const m = require("mithril") // ES module (bundler) import m from "mithril" ``` -------------------------------- ### Execute Multiple Functions on Event Source: https://github.com/mithriljs/mithril.js/wiki/Recipes-and-Snippets Demonstrates how to execute multiple functions when only one is expected, often useful for event handling. See comments in the original gist for a specific use case. ```javascript m("input", { oninput: [m.withAttr("value", vnode.state.setValue), m.withAttr("value", vnode.state.save)] }); ``` -------------------------------- ### Pathname Utilities Source: https://context7.com/mithriljs/mithril.js/llms.txt Utilities for parsing URL pathnames and query strings into objects, and for building URL pathnames from templates with parameters. ```APIDOC ## m.parsePathname() / m.buildPathname() Parse a URL into `{path, params}`, or interpolate a URL template with parameters, appending leftovers as a query string. ### Usage ```javascript // Parse pathname + query string m.parsePathname("/users/42?tab=settings#profile") // → {path: "/users/42", params: {tab: "settings"}} m.parsePathname("/search?q=mithril&page=1") // → {path: "/search", params: {q: "mithril", page: "1"}} // Build — interpolate :param tokens, append remainder as query string m.buildPathname("/users/:id/posts", {id: 42, page: 2, sort: "desc"}) // → "/users/42/posts?page=2&sort=desc" m.buildPathname("/api/:resource/:id", {resource: "comments", id: 7}) // → "/api/comments/7" // Variadic parameter (no encoding) m.buildPathname("/files/:path...", {path: "docs/guide/intro.md"}) // → "/files/docs/guide/intro.md" ``` ``` -------------------------------- ### Initialize Mithril Component Manually Source: https://github.com/mithriljs/mithril.js/wiki/Helpers-and-Extensions Allows manual creation and management of a component instance for explicit lifecycle control. Useful when you need to control when the controller is initialized. ```javascript m.initComponent = function (component, defaultOptions, defaultContent) { var ctrl = new component.controller(options) ctrl.render = function (options, content) { return component.view( controller, options || defaultOptions, content || defaultContent) } return ctrl } ``` ```javascript var UserList = { controller: function () { /* ... */ }, view: function () { /* ... */ } } var App = { controller: function () { this.userList = m.initComponent(UserList, {users: [ /* ... */ ]}) }, view: function (ctrl) { return m('.app', [ m('h1', "My App"), ctrl.userList.render() ]) } } ``` -------------------------------- ### m.route() with onmatch — Async Route Resolution / Code Splitting Source: https://context7.com/mithriljs/mithril.js/llms.txt Route components can have `onmatch` (async guard/resolver) and `render` (wrapper) hooks, enabling lazy loading and authentication guards. ```APIDOC ## m.route() with onmatch ### Description Route components may be objects with `onmatch` (async guard/resolver) and `render` (wrapper) hooks, enabling lazy loading and authentication guards. ### Route Component Hooks - **onmatch(params, requestedPath, route)**: Called when a route is matched. Can return a component or a Promise resolving to a component. Used for guards and async loading. - **render(vnode)**: A wrapper function that receives the resolved vnode from `onmatch` and can wrap it. ### Request Example ```javascript m.route(document.body, "/login", { "/login": { view: () => m("h1", "Login") }, // Authenticated route with lazy-loaded component "/dashboard": { onmatch(params, requestedPath, route) { if (!localStorage.getItem("token")) { // Redirect to login — returning m.route.SKIP falls through to next route m.route.set("/login") return m.route.SKIP } // Lazy-load the component return import("./Dashboard.js").then(mod => mod.default) }, render(vnode) { return m("main", vnode) // wrap resolved component } }, // Async data preloading "/reports/:id": { onmatch({id}) { return fetch(`/api/reports/${id}`) .then(r => r.json()) .then(data => ({ view: () => m("div", m("h2", data.title), m("p", data.body)) })) } } }) ``` ``` -------------------------------- ### Define a Catch-All Route in Mithril Source: https://github.com/mithriljs/mithril.js/wiki/FAQ Use a route parameter with '...' to define a catch-all route for handling 404s or other unhandled paths. ```javascript m.route(body, '/', { '/': home, '/test': test, '/:...other': my404component }); ``` -------------------------------- ### Mounting a Mithril Component Source: https://github.com/mithriljs/mithril.js/wiki/FAQ Ensures the DOM element exists before rendering. Use Option 1 for direct mounting or Option 2 for deferred mounting on page load. ```html
``` -------------------------------- ### m.fragment() Source: https://context7.com/mithriljs/mithril.js/llms.txt Creates a fragment (grouping of vnodes with no wrapper element). Unlike an array, a fragment can carry a `key` attribute for keyed diffing and lifecycle hooks. ```APIDOC ## m.fragment() ### Description Creates a fragment (grouping of vnodes with no wrapper element). Unlike an array, a fragment can carry a `key` attribute for keyed diffing and lifecycle hooks. ### Method Signature m.fragment(attrs, ...children) ### Parameters #### attrs (object) - **key** (any) - A key attribute for keyed diffing. - Other attributes are passed down to the fragment. #### children (...vnodes) - The child vnodes to be included in the fragment. ### Usage Example ```javascript const items = ["Alpha", "Beta", "Gamma"] m.mount(document.body, { view: () => m("div", // Fragment with a key — safe for use in keyed lists m.fragment({key: "list"}, m("h2", "Items"), items.map((item, i) => m("p", {key: i}, item)) ), // Equivalent using m.Fragment tag string m(m.Fragment, {key: "footer"}, m("hr"), m("small", "End of list") ) ) }) ``` ``` -------------------------------- ### m.vnode - Direct Vnode Construction Source: https://context7.com/mithriljs/mithril.js/llms.txt A low-level factory for creating virtual DOM nodes (vnodes). Prefer using the `m()` hyperscript function for general use. ```APIDOC ## m.vnode — Direct Vnode Construction `m.vnode(tag, key, attrs, children, text, dom)` is the low-level vnode factory. Prefer `m()` for everyday use; this is exposed for advanced cases and library authors. ### Usage ```javascript const Vnode = require("mithril/render/vnode") // Text vnode (tag "#") Vnode("#", undefined, undefined, "Hello", undefined, undefined) // HTML trust vnode (tag "<") Vnode("<", undefined, undefined, "bold", undefined, undefined) // Fragment vnode (tag "[") Vnode("[", "list-key", undefined, [Vnode("#", undefined, undefined, "item")], undefined, undefined) // Normalize a raw value to a vnode Vnode.normalize("plain text") // → text vnode Vnode.normalize(null) // → null (removed from DOM) Vnode.normalize([v1, v2]) // → fragment vnode ``` ``` -------------------------------- ### Create Virtual DOM Nodes with m() Source: https://context7.com/mithriljs/mithril.js/llms.txt Use the m() function to create virtual DOM nodes (vnodes) with selectors, attributes, and children. Supports CSS shorthand and component objects/functions. ```js // Simple element m("h1", "Hello, Mithril!") // →