Skip to content

Instantly share code, notes, and snippets.

View tbranyen's full-sized avatar

Tim Branyen tbranyen

View GitHub Profile
@tbranyen
tbranyen / diffhtml-promise-test.js
Last active February 18, 2017 21:08
Internals test for detached Promise handling
it('will hold off rendering until detached (during replace) promise resolves', () => {
const promise = Promise.resolve();
const { use, innerHTML, addTransitionState } = diff;
const { fixture } = this;
// Run each assertion after a successful render.
const assertions = [
() => ok(fixture.querySelector('p')),
() => {
equal(fixture.querySelector('p'), null);
@tbranyen
tbranyen / hooks-theme-and-refs.js
Last active June 5, 2017 17:40
HRT: Hooks, theme, and refs! Consistent re-usable component schema.
import React, { Component, PropTypes } from 'react';
import htr from 'hooks-theme-refs';
export class Chip extends Component {
render() {
const { label, hooks, theme, refs, children, ...rest } = htr(this);
return (
<div {...rest} className={theme.chip} onClick={hooks.onClick} ref={refs.chip}>
{children}
@tbranyen
tbranyen / middleware.js
Last active March 14, 2017 00:16
diffHTML 1.0 Middleware Specification + Documentation
// Documentation for authoring middleware in diffHTML v1.0
// Usage of the middleware we'll write below, I added it up top, since it's a
// single line:
diff.use(myFirstMiddleware());
// To make a diffHTML middleware, start by creating a normal function that
// accepts options. We don't need to configure our middleware yet, but we want
// to keep all middleware consistent with this convention.
functon myFirstMiddleware(options = {}) {
@tbranyen
tbranyen / panels.js
Last active January 29, 2017 22:31
Web Component Panel (takes in a route and the current activeRoute)
import { html } from 'diffhtml';
import { WebComponent, PropTypes } from 'diffhtml-components';
export default class DevtoolsPanels extends WebComponent {
static propTypes = {
route: PropTypes.string,
activeRoute: PropTypes.string,
}
render() {
@tbranyen
tbranyen / method.js
Created January 28, 2017 19:14
When your web component is also a reactive component...
toggleExpanded() {
this.setState({ isExpanded: !this.state.isExpanded });
// Close siblings w/o needing a store or global container!
[...this.parentNode.children].filter(x => x !== this).forEach(row => {
row.setState({ isExpanded: false });
});
}
@tbranyen
tbranyen / diffhtml-jsx.js
Last active January 12, 2017 00:11
diffHTML working out-of-the-box with JSX
import { createTree, innerHTML } from 'diffhtml';
class Display {
render({ message, children }) {
return [
<li>{message}</li>,
...children,
];
}
}
@tbranyen
tbranyen / web-component.js
Created January 1, 2017 21:25
diffHTML Web Component
import { html } from 'diffhtml';
import { WebComponent } from 'diffhtml-components';
class CocktailEditor extends WebComponent(['recipe']) {
render() {
return html`${this.state.markup}`;
}
constructor() {
super();
@tbranyen
tbranyen / tagged-template.js
Last active December 31, 2016 18:04
Possible way to improve tagged template usability for components
import { html, innerHTML } from 'diffhtml';
// Import arbitrary template engine...
import combyne from 'combyne';
// Create the combyne template instance and set context to be the component's props.
const combyneProcessor = props => text => combyne(text).render(props);
// Experimental stateful components.
class MyComponent {
@tbranyen
tbranyen / make-cancellable.js
Last active December 11, 2018 10:02
Make Cancellable Promise
Promise.makeCancellable = promise => {
const deferred = {};
const promise = new Promise((resolve, reject) => {
deferred.reject = reject;
// Invoke the fetch argument with the matching args... only resolve if not
// aborted.
promise
.then(resp => !deferred.aborted && resolve(resp))
.catch(ex => !deferred.aborted && reject(ex));
@tbranyen
tbranyen / devtools-navigation.js
Created December 18, 2016 18:15
Web Components v1 / diffHTML v1 integration
import { html } from 'diffhtml';
import { ReactiveElement } from 'diffhtml-components';
class DevtoolsNavigation extends ReactiveElement {
styles(props, state) {
return html`
<style>
:host {
display: flex;
height: 100%;