Skip to content

Instantly share code, notes, and snippets.

@markmur
markmur / render.js
Created December 5, 2017 21:20
Render Components as String
const renderProp = (key, val) => {
const type = typeof val;
if (type === 'string' && !val) return null;
if (type === 'object' && !Object.keys(val).length) return null;
switch (type) {
case 'number':
return `${key}={${val}}`;
case 'string':
@markmur
markmur / snippets.cson
Last active January 8, 2018 09:30
Atom / React Snippets
'.source.js':
'constructor':
'prefix': 'construct'
'body': '''
constructor(props) {
super(props);
$1
}
'''
@markmur
markmur / styled.js
Created January 28, 2018 23:17
Omit props from styled component
import pickBy from 'lodash.pickby';
import { createElement } from 'react';
export const createComponent = ({
tag: defaultTag = 'div',
prop = 'tag',
omit = []
} = {}) => {
return ({ children, ...otherProps }) => {
const tag = otherProps[prop] || defaultTag;
@markmur
markmur / generate_docs.js
Created March 26, 2018 14:53
Generate a documentation JSON file for React Components
const docgen = require('react-docgen');
const fs = require('fs');
const glob = require('glob');
const chalk = require('chalk');
const chokidar = require('chokidar');
const _ = require('lodash/fp');
const fmap = require('lodash/map');
const { flow, mapValues } = _;
@markmur
markmur / dangerfile.js
Created March 26, 2018 15:24
Example Dangerfile
const jest = require('danger-plugin-jest').default;
const { message, danger, warn } = require('danger');
// Output all modified files in the PR
const modified = danger.git.modified_files;
message('Changed Files in this PR: \n' + modified.map(file => `- ${file}\n`));
// Encourage smaller PRs
let errorCount = 0;
const bigPRThreshold = 600;
@markmur
markmur / snippets.cson
Created July 2, 2018 11:26
Atom React Snippets
# Your snippets
#
# Atom snippets allow you to enter a simple prefix in the editor and hit tab to
# expand the prefix into a larger code block with templated values.
#
# You can create a new snippet in this file by typing "snip" and then hitting
# tab.
#
# An example CoffeeScript snippet to expand log to console.log:
#
@markmur
markmur / .zshrc
Last active June 6, 2023 12:11
Add script to package.json via command line
# Reusable bash function you can add to your ~/.zshrc or ~/.bashrc file
#
# Usage: pkg-script start "node index.js"
#
function pkg-script () {
echo $(jq --arg key "${1}" --arg val "${2}" '.scripts[$key]=$val' package.json) | jq . | > package.json
}
@markmur
markmur / is-in-viewport.js
Created July 13, 2018 08:45
Is Element In Viewport
/**
* Check if an element is within the viewport or considered visible
* @param {Object} bounds { top, left, bottom, right } - distances from container
* @param {Object} container - container object with { width, height }
* @return {Boolean} returns visibility state
*/
const isInViewport = ({ top, left, bottom, right }, container) {
const isTallerThanViewport = top <= 0 && bottom >= container.height
const isWiderThanViewport = right >= container.width && left <= 0
@markmur
markmur / get.js
Created July 16, 2018 13:27
Lookup a deeply nested object property by dot.notation
/**
* Lookup an object property by dot notation
* @param {Object} obj - object to perform lookup
* @param {String} key - property location
* @param {Any} fallback - fallback if not found
* @return {Any} returns value of lookup if found, otherwise undefined
*/
const get = (obj, key, fallback) =>
key
.split('.')
@markmur
markmur / Container.js
Last active July 18, 2018 21:45
Provide Firebase store data + subscription method to child routes
import React, { Component } from 'react'
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom'
import FirebaseProvider, { Consumer as FirebaseConsumer } from './FirebaseProvider'
class Container extends Component {
render() {
return (
<Router>
<FirebaseProvider>
<FirebaseConsumer>