Skip to content

Instantly share code, notes, and snippets.

// Type definitions for mithril.js 1.0
// Project: https://github.com/lhorie/mithril.js
// Definitions by: Mike Linkovich <https://github.com/spacejack>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Typescript 2.0.2
declare namespace Mithril {
interface Hyperscript {
// Loader that provides a dictionary of named assets
import xhr from './xhr'
export interface Assets {
images: {[id: string]: HTMLImageElement}
geometries: {[id: string]: THREE.Geometry}
text: {[id: string]: string}
textures: {[id: string]: THREE.Texture}
sounds: {[id: string]: Howler.Howl}
}
@spacejack
spacejack / m-select.js
Created December 21, 2016 15:12
m-select ES6
import * as m from 'mithril';
/**
* Standalone event handler that can be bound, then attached
* and detached on component remove.
*/
function onFocus(el, e) {
console.log('global focus event element:', e.target);
if (e.target instanceof Node && el.contains(e.target)) {
console.log('child focus event');
this.isFocused = true;
declare namespace Reddit {
interface Item {
domain: string;
banned_by: any;
media_embed: any;
subreddit: string;
selftext_html: string;
selftext: string;
likes: any;

deserialize

A deserialization method to be applied to the xhr.responseText. Defaults to a small wrapper around JSON.parse that returns null for empty responses. If extract is defined, deserialize will be skipped.

extract

A hook to specify how the XMLHttpRequest response should be read. Useful for processing response data, reading headers and cookies. By default this is a function that returns xhr.responseText, which is in turn passed to deserialize. If a custom extract callback is provided, the xhr parameter is the XMLHttpRequest instance used for the request, and options is the object that was passed to the m.request call. Additionally, deserialize will be skipped and the value returned from the extract callback will not automatically be parsed as JSON.

declare namespace Mithril {
// ...
/** A redraw boolean property can be set on all events received by event handlers created by hyperscript */
export interface VEvent {
redraw: boolean
}
/** This represents the attributes available for configuring virtual elements, beyond the applicable DOM attributes. */
export interface Attributes extends Lifecycle<any, any> {
function createToken() {
let resolve
const promise = new Promise(r => {resolve = r})
return [promise, resolve]
}
const c = function() {
const timeline = Timeline(async (delay) => {
const state = stmu({
// ts2fable 0.5.2
module rec Mithril
open System
open Fable.Core
open Fable.Import.JS
let [<Import("*","mithril")>] Mithril: Mithril.Static = jsNative
type [<AllowNullLiteral>] IExports =
/// Manually triggers a redraw of mounted components.
@spacejack
spacejack / mithril-obserer-component.js
Last active July 19, 2021 11:48
Mithril "Observer" Component
/*
This 'Observer' component expects the attrs described below.
The vdom rendered by `view` will re-render normally within
Mithril's redraw cycle.
The vdom rendered by `render` will only re-render when the
data stream updates. This will not trigger global redraws.
`render` renders into the dom node described by `selector`,
otherwise defaults to the component's root node.
@spacejack
spacejack / modal-example.ts
Created April 3, 2018 21:02
Example of Mithril Modal component
import * as m from 'mithril'
// Modal state
let isOpen = false
let options: ModalOptions = {}
export interface ModalButton {
id: string
text: string
}