Skip to content

Instantly share code, notes, and snippets.

@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 / .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 / 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 / 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 / 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 / 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 / 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 / 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 / preloadImages.js
Created June 6, 2017 18:38
Preload Images with Promises
const preloadImages = (images) => Promise.all(images.map(image =>
new Promise(resolve => {
const img = new Image();
img.onload = () => (resolve(image));
img.onerror = () => (resolve(image));
img.src = image;
})
));
@markmur
markmur / imagePreloader.js
Created April 26, 2017 10:21
Preload images
const preloadImages = (images, callback) => {
const loaded = [];
const failed = [];
const imageRequests = images.map(image => {
const img = new Image();
img.src = image;
return new Promise((resolve) => {
img.onload = () => {