Skip to content

Instantly share code, notes, and snippets.

View tbranyen's full-sized avatar

Tim Branyen tbranyen

View GitHub Profile
@tbranyen
tbranyen / web-component-jsx.js
Created May 21, 2017 17:17
Web Component using JSX Demo
import { innerHTML, createTree } from 'diffhtml';
import WebComponent from 'diffhtml-components/web-component';
import PropTypes from 'proptypes';
customElements.define('jsx-test', class extends WebComponent {
render({ message }) {
return (
<div>{message}</div>
);
}
@tbranyen
tbranyen / importScript.js
Last active April 23, 2018 14:47
Native `import()` polyfill (Note: can't name it `import` due to reserved word limitations)
Object.defineProperty(window, Symbol.for('registry'), {
value: new Map(),
});
window.importScript = src => {
const registry = window[Symbol.for('registry')];
if (registry.has(src)) {
return registry.get(src).promise;
}
@tbranyen
tbranyen / es-modules-diffhtml.html
Last active June 1, 2017 17:50
Loading some middleware and diffHTML with ES Modules in Chromium 60
<style>
body { display: flex; align-items: center; justify-content: center; }
h1 { font-size: 60px; font-family: Helvetica; }
</style>
<script type="module">
import { innerHTML, html, use } from 'https://diffhtml.org/master/diffhtml';
import createLogger from 'https://diffhtml.org/master/diffhtml-middleware-logger';
import inlineTransitions from 'https://diffhtml.org/master/diffhtml-middleware-inline-transitions';
@tbranyen
tbranyen / es-modules.html
Created May 6, 2017 00:19
Loading diffHTML + Internals with real ES Modules in Chrome 60!
<body></body>
<script type="module">
import { innerHTML, html, use } from 'https://diffhtml.org/es/runtime';
import Pool from 'https://diffhtml.org/es/util/pool';
use(transaction => {
console.log('Started transaction');
console.log(transaction);
@tbranyen
tbranyen / toReactElement.js
Last active May 3, 2017 05:39
Consume a DOM Node in React
{
const { Component, cloneElement, createElement: jsx } = React;
const { findDOMNode, render } = ReactDOM;
const map = new WeakMap();
const attrMap = {
htmlFor: 'for',
className: 'class',
};
const toReactElement = (domNode, props) => {
@tbranyen
tbranyen / rr.js
Created May 1, 2017 18:36
Router Router Help!
// FIXME This logic is potentially super overkill for our needs. Is there
// a way to change the URL based off changing a param (we could simply
// update the tab param)?
@autobind
handleTabSwitch(index) {
this.setState({ index });
const { router: { params: { tab }, location } } = this.props;
const identifier = this.identifierMap[index];
const { search } = location;
@tbranyen
tbranyen / how.md
Last active April 18, 2017 16:33
Idiomatically navigate Reach Router programmatically.

I've been really upset at the following code:

@autobind
handleTabSwitch(index) {
  this.setState({ index });

  const { router: { params: { tab }, location } } = this.props;
  const identifier = this.identifierMap[index];
 const { search, pathname } = location;
@tbranyen
tbranyen / simple-redux-helpers.js
Created March 27, 2017 22:40
Simple Redux Helpers
import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import { mapStateToProps, mapDispatchToProps } from 'simple-redux-helpers';
class MyComponent extends Component {
static propTypes = {
slice: PropTypes.shape({
def: PropTypes.string,
}),
}
@tbranyen
tbranyen / _readme.md
Last active February 23, 2017 17:59
Passes in Node 6.8 fails in Node 7.x

The following code fails in Node 7, while strangely passes in Node 6. I couldn't find any release notes or changelog entries that would account for this breakage.

@tbranyen
tbranyen / promise-test-failure-node-7.js
Created February 23, 2017 16:21
Issue with Promises (potentially) in Node 7
it('will hold off rendering until attributeChanged promise resolves', () => {
const promise = Promise.resolve();
const { use, innerHTML, addTransitionState } = diff;
const { fixture } = this;
const assertions = [
() => {
ok(fixture.querySelector('p'));
equal(fixture.querySelector('p').className, '');
},