Skip to content

Instantly share code, notes, and snippets.

View developit's full-sized avatar
🦊
write, the codes

Jason Miller developit

🦊
write, the codes
View GitHub Profile
@developit
developit / _readme.md
Last active February 3, 2017 15:01
Like linkState(), but for Refs.

Simple, unobtrusive add-on to support String refs in Preact, without needing to pull in preact-compat.

Option 1: Add-on

Calling linkRef(componentInstance, refName) returns a "ref proxy" function, which sets this.refs[refName] when called.

import { h, Component } from 'preact';
import linkRef from './linked-ref';
import { h } from 'preact';
export default function cloneElement(vnode, props, ...children) {
return h(
vnode.nodeName,
extend(extend({}, vnode.attributes || {}), props),
children.length ? children : vnode.children
);
}
@developit
developit / fire-event.js
Last active September 13, 2017 14:05
DOM fireEvent
function fireEvent(element, type) {
let e = document.createEvent('Event');
e.initEvent(type);
element.dispatchEvent(e);
}
@tj
tj / update.js
Last active April 29, 2023 14:53
shouldComponentUpdate utility
let rows = {}
export default function(props = [], state = []) {
return function(target) {
const proto = Object.create(target.prototype)
proto.shouldComponentUpdate = function(newProps, newState) {
let id = (this._update_id = this._update_id || Math.random())
@developit
developit / document-shim.js
Last active August 4, 2022 02:27
Minimal stand-in for a full DOM. Mocks out basic DOM 1 node and attribute manipulation.
var document = global.document = createDocument();
export default document;
function createDocument() {
const NODE_TYPES = {
ELEMENT_NODE: 1,
ATTRIBUTE_NODE: 2,
TEXT_NODE: 3,
CDATA_SECTION_NODE: 4,
ENTITY_REFERENCE_NODE: 5,
@iammerrick
iammerrick / children-as-function.js
Created May 24, 2016 23:41
Which API do you prefer? Passing a function into the Ratio component or making a higher order component called Ratio you can use to configure a component.
<Ratio width={Ratio.OPTIONS.FLUID} x={3} y={4}>
{(width, height) => (
<Chart id={this.props.id} width={width} height={height} />
)}
</Ratio>
import { h, Component } from 'preact';
import { describe, it } from 'mocha';
import chai, { expect } from 'chai';
import jsxAssert from 'preact-jsx-chai';
chai.use(jsxAssert);
// example stateful list component
class List extends Component {
state = {
items: this.props.items || []
@developit
developit / preact-deep-force-update.js
Last active March 21, 2017 14:05
Recursively invoke forceUpdate() on a tree of components. Demo: https://jsfiddle.net/developit/642ctu04/
/** Invoke `.forceUpdate()` on a component and all descendants. */
export default function deepForceUpdate(component) {
component.forceUpdate();
// high-order child recursion
if (component._component) {
deepForceUpdate(component._component);
}
else if (component.base) {
updateComponentsFromDom(component.base);
@PaulBGD
PaulBGD / hacky-preact-inject.ts
Created June 10, 2016 19:45
Files required to use preact in typescript
/// <reference path="typings/preact.d.ts"/>
import {h} from 'preact';
export function createElement<P>(type: any, props?: P, ...children: any[]): any {
return h(type, props, children);
}
const markdownIt = require('markdown-it')
const frontMatter = require('front-matter')
const Prism = require('prismjs')
const aliases = {
'js': 'jsx',
'html': 'markup'
}
const highlight = (str, lang) => {