Skip to content

Instantly share code, notes, and snippets.

View olecksamdr's full-sized avatar

Oleksandr olecksamdr

View GitHub Profile
// curry :: ((a, b, ...) -> c) -> a -> b -> ... -> c
function curry(fn) {
const arity = fn.length;
return function $curry(...args) {
if (args.length < arity) {
return $curry.bind(null, ...args);
}
return fn.call(null, ...args);
@olecksamdr
olecksamdr / ifProp.js
Created March 6, 2019 09:20
ifProp helper for styled-components
export const ifProp = (prop, pass, fail) => props => props[prop] ? pass : fail;
@olecksamdr
olecksamdr / Media.js
Created February 19, 2019 08:47
Media component
import React, { Component } from 'react';
import { arrayOf, node, oneOfType } from 'prop-types';
class Media extends Component {
constructor(props) {
super(props);
this.state = {
matches: window.matchMedia(this.props.query).matches,
};
export const getScrollbarWidth = () => {
const scrollDiv = document.createElement('div');
scrollDiv.style.cssText = `
position: absolute;
top: -9999px;
width: 50px;
height: 50px;
overflow: scroll
`;
export function putContent(el: HTMLElement, currentPosition: string, isOpen: boolean): string {
let height = el.offsetHeight
var rect = el.getBoundingClientRect()
var elemTop = (isOpen ? rect.top : rect.top - height)
var elemBottom = (isOpen ? rect.bottom : rect.bottom + height)
if (elemTop <= 0) { return 'below' }
if (elemBottom >= window.innerHeight) { return 'above' }
return (isOpen ? currentPosition : 'below')
}
@olecksamdr
olecksamdr / debounce.js
Created December 11, 2018 14:26
debounce
// debounce :: Number -> (Args -> Any) -> (Args -> Undefined)
// Args = Any
export const debounce = time => (fn) => {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => fn(...args), time);
};
};
// Escapes a string for use in a regular expression.
str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
componentDidUpdate(prevProps, prevState) {
if (isBrowser()) {
const { open, bodyAlign } = this.state;
if (prevState.open !== open) {
const { right } = this.body.current.getBoundingClientRect();
if (right > Root.innerWidth && bodyAlign === 'left') {
// Do not go beyond the right window border
this.setState({ bodyAlign: 'right' });
export const withContext = propName => Component => props => (
<Context.Consumer>
{value => (
<Component
{...{[propName]: value}}
{...props}
/>
)}
</Context.Consumer>
);
@olecksamdr
olecksamdr / recomposeThrottle.js
Created October 25, 2018 05:39
Throttle recompose
withPropsOnChange(
['handlerName'],
({ handlerName }) => ({
handlerName: throttle(100, handlerName),
})
),