Skip to content

Instantly share code, notes, and snippets.

View treshugart's full-sized avatar
😅
🍊 ⛸

Trey Shugart treshugart

😅
🍊 ⛸
  • Looker
  • Santa Cruz
View GitHub Profile
@treshugart
treshugart / install-postgres-travisci
Created March 26, 2013 23:05
Installing versions of Postgres other than the default version in TravisCI.
#!/bin/sh
TRAVIS_PGVERSION='9.1'
if [ $PGVERSION != $TRAVIS_PGVERSION ]
then
sudo -u postgres pg_dropcluster --stop $TRAVIS_PGVERSION main
export PGCLUSTER=$PGVERSION/main
fi
@treshugart
treshugart / git-cleanup.sh
Created November 17, 2013 22:16
Sync and cleanup all local branches that have been merged (except master).
git pull
git remote prune origin
git branch --merged | grep -v "\*" | grep -v "master" | xargs -n 1 git branch -d
@treshugart
treshugart / jsx-anywhere.js
Last active May 6, 2024 04:58
Use JSX with the native DOM.
function shouldBeProp (item) {
return typeof item === 'function' || typeof item === 'object';
}
window.React = {
createElement (name, attrs, ...chren) {
const node = typeof name === 'function' ? name() : document.createElement(name);
Object.keys(attrs || []).forEach(attr => shouldBeProp(attrs[attr]) ? node[attr] = attrs[attr] : node.setAttribute(attr, attrs[attr]));
chren.forEach(child => node.appendChild(child instanceof Node ? child : document.createTextNode(child)));
return node;
@treshugart
treshugart / chrome-canary-wc-v1
Last active May 6, 2024 04:58
Launch Chrome Canary with Web Components V1 enabled
/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary --enable-blink-features=CustomElementsV1,ShadowDOMV1
@treshugart
treshugart / server-side-rendering-web-components.md
Last active May 6, 2024 04:58
Server-side rendering web components in Electron

To render a component:

electron server.js --html "<x-app></x-app>" --scripts ./entry-point

Where --html is the HTML that you want to render and --scripts is the entry point that will be loaded prior to rendering the HTML. The entry point should contain all your component definitions that you need for the HTML to be rendered.

@treshugart
treshugart / rules.js
Last active May 6, 2024 04:59
JSS rules for polyfilling Shadow DOM selectors
const jss = window.jss.default;
const native = fn => (fn || '').toString().indexOf('[native code]') > -1;
const div = document.createElement('div');
const supportsShadowDOM = native(ShadowRoot);
const supportsShadowDOMV0 = div.createShadowRoot && native(HTMLContentElement);
const supportsShadowDOMV1 = div.attachShadow && native(HTMLSlotElement);
// Polyfill :host
// --------------
@treshugart
treshugart / slot.js
Created September 5, 2016 02:48
SkateJS stateless function for doing stuff with slotted nodes on first render and subsequent renders (slotchange event only fires on updates)
const $slotRendered = Symbol();
const Slot = (props, chren) => {
const { changed } = props;
function onSlotchange(e) {
const slot = e.target;
if (slot[$slotRendered]) {
if (changed) {
changed(slot);
}
@treshugart
treshugart / roadmap-ideas.md
Last active May 6, 2024 04:59
notebook-skatejs

Roadmap Ideas

Virtual DOM Functions

Right now, we have vdom.element() and vdom.text(). Currently they map directly off to Incremental DOM. However, we could create adaptors that could map to any backend. For example, we could serialise data to JSON that could be consumed by a web worker that returns diff / patch instructions for a different virtual DOM implementation.

In the same way we could keep support for Incremental DOM directly via a separate adaptor. This would allow us to create a server-renderer all without changing anything in our components' render() functions. Consumers could leverage whatever vDOM implementation they want via separate modules that they just plug in to their components, or we could just create base-classes / composable functions they call on their definitions to apply support for whatever the plugin is.

Our functions could also attain the same API as Hyperscript / React so it'd inherently be compatible with current JSX transpilers.

@treshugart
treshugart / document-reduce.js
Last active May 6, 2024 04:59
document.reduce() - querySelectorAll() but with a callback instead of a selector
document.reduce = Element.prototype.reduce = function(cb) {
const filter = node => cb(node) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
// Old IE requires a function other browsers require { acceptNode }.
filter.acceptNode = filter;
// Old IE requires the last parameter.
const walker = document.createTreeWalker(this, NodeFilter.SHOW_ELEMENT, null, false);
const nodels = [];
@treshugart
treshugart / yarn-install.sh
Created November 7, 2016 01:37
Yarn install with Lerna
yarn install && rm -rf yarn.lock && lerna exec -- yarn install && lerna exec -- rm -rf yarn.lock