Skip to content

Instantly share code, notes, and snippets.

View TCotton's full-sized avatar
🏠
Working from home

Andrew Walpole TCotton

🏠
Working from home
View GitHub Profile
@TCotton
TCotton / html_linting.css
Created March 7, 2017 22:11
Lint HTML with CSS
/**
Inline styles
**/
/**
This selector will target any element on the page that has inline styles applied to it.
As a general rule, inline styles should be avoided as they are difficult to override
due to their increased level of specificity. Although inline
styles may be necessary in some cases, this selector will help
highlight them so a decision can be made on a case-by-case basis.
@TCotton
TCotton / mediator-pattern.js
Last active March 20, 2017 16:42
Mediator pattern es3 to es6
// es3: https://gist.github.com/addyosmani/1837327
var mediator = (function() {
// Subscribe to an event, supply a callback to be executed
// when that event is broadcast
var subscribe = function(channel, fn) {
if (!mediator.channels[channel]) mediator.channels[channel] = [];
mediator.channels[channel].push({
context: this,
callback: fn
});
@TCotton
TCotton / fancy-tabs-demo.html
Created February 20, 2017 17:17 — forked from ebidel/fancy-tabs-demo.html
Fancy tabs web component - shadow dom v1, custom elements v1, full a11y
<script>
function execPolyfill() {
(function(){
// CustomElementsV1.min.js v1 polyfill from https://github.com/webcomponents/webcomponentsjs/tree/v1/src/CustomElements/v1.
/*
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
@TCotton
TCotton / react-change-state-view-dyn.js
Created February 13, 2017 12:36
React change state view dynamically
export default class SetValueExample extends React.Component {
constructor() {
super();
this.state = {
myName: '',
};
}
setValue(e) {
this.setState({[e.target.name]: e.target.value});
@TCotton
TCotton / render-if.js
Last active February 23, 2017 08:28
React render if function
// Also available as a NPM library: https://github.com/ajwhite/render-if
'use strict';
const isFunction = input => typeof input === 'function';
export default predicate => elemOrThunk =>
predicate ? (isFunction(elemOrThunk) ? elemOrThunk() : elemOrThunk) : null;
import renderIf from '../utils/renderif'
export default class RenderIfExample extends Component {
@TCotton
TCotton / react-create-reducer-dynamically.js
Created February 13, 2017 12:31
Dynamically create a react redux reducer
export function createReducer(initialState, reducerMap) {
return (state = initialState, action) => {
const reducer = reducerMap[action.type];
return reducer
? reducer(state, action.payload)
: state;
};
}
@TCotton
TCotton / react-deep-object-comp.js
Created February 13, 2017 12:29
React deep object comparison -> used in shouldComponentUpdate
export const isObjectEqual = (obj1, obj2) => {
if(!isObject(obj1) || !isObject(obj2)) {
return false;
}
if (obj1 === obj2) {
return true;
}
@TCotton
TCotton / generator-object-values-keys.js
Created February 12, 2017 14:27
find object values and arrays -> shallow object
function* objectEntries(obj, findKeys = false) {
let keys = Reflect.ownKeys(obj);
if(findKeys) {
yield keys;
} else {
for (let key of keys) {
yield [key, obj[key]];
}
}
@TCotton
TCotton / css-starter-kit.css
Last active June 27, 2020 17:40
CSS reset / starter
*:focus:not(:focus-visible),
*::before:focus:not(:focus-visible),
*::after:focus:not(:focus-visible) {
outline: none;
}
/* https://medium.com/@matuzo/writing-css-with-accessibility-in-mind-8514a0007939 */
.visually-hidden {
position: absolute;
white-space: nowrap;
@TCotton
TCotton / quality-promises.js
Last active February 23, 2017 08:29
promise patterns and anti-promise patterns
// http://blog.runnable.com/post/154695425511/promise-anti-patterns
// BAD!! variable nesting
// Defining a variable to store later
var user
var accountInfo
fetchUser()
.then((fetchedUser) => {
// Setting the variable that we will use later