Skip to content

Instantly share code, notes, and snippets.

View icyJoseph's full-sized avatar

Joseph icyJoseph

View GitHub Profile
@icyJoseph
icyJoseph / functional.js
Created June 13, 2018 04:36
Functional Pack
export const curry = f => a => (...b) => f(a,...b)
export const pipe = (...funcs) => (...args) => funcs.reduce((res, func, i) => (i === 0 ? func(...res) : func(res)), args);
export const mapf = (func, arr) => arr.map(func);
export const filterf = (func, arr) => arr.filter(func);
export const flatten = arr => [].concat(...arr);
@icyJoseph
icyJoseph / Responsive.js
Last active June 14, 2018 17:45
Responsive React Function
import React from 'react';
import Media from 'react-media';
const Responsive = () => (
<Media query={{ maxWidth: 1023 }}>
{
matches =>
matches
? <div>Less than 1023</div>
: <div>More than 1023</div>
@icyJoseph
icyJoseph / ResponsiveWithProps.js
Last active June 15, 2018 06:28
Responsive content passing props
import React from 'react';
import Media from 'react-media';
const Mobile = ({ title }) => <div>{title} Mobile</div>;
const Desktop = ({title}) => <div>{title} Desktop</div>;
const Responsive = ({ ...props }) => (
<Media query={{ maxWidth: 1023 }}>
{matches => (matches ? <Mobile {...props} /> : <Desktop {...props} />)}
</Media>
@icyJoseph
icyJoseph / ResponsiveWithCurriedProps.js
Last active June 15, 2018 06:28
React Media using a curried function to manage responsive content.
import React from 'react';
import Media from 'react-media';
// functional helper
const curry = f => a => (...b) => f(a,...b)
const Mobile = ({ title }) => <div>{title} Mobile</div>;
const Desktop = ({title}) => <div>{title} Desktop</div>;
const MediaContent = (props, matches) => {
@icyJoseph
icyJoseph / ReactReader.js
Last active June 17, 2018 06:44
React Component to read its own source code, demo at: https://codesandbox.io/s/kmlmzlvn5v
import React, { Component } from "react";
import ReactDOM from "react-dom";
class App extends Component {
constructor(props) {
super(props);
this.state = {
result: ""
};
}
@icyJoseph
icyJoseph / ReduceFun.js
Created August 24, 2018 12:59
Fun with reduce! Everything here is made with reduce!
/**
* Map
* Map a function fn over every element of arr
* */
const map = (arr, fn) =>
arr.reduce((acc, val, index) => acc.concat(fn(val, index)), []);
/**
* Filter
* Remove elements that do not return truthy when evaluated against the function fn
@icyJoseph
icyJoseph / Portal.jsx
Last active September 12, 2018 16:07
An example of using the ReactDOM's createPortal method.
import React from "react";
import ReactDOM from "react-dom";
export default ({ children, closeCallback }) => {
const composed = (
<div className="Modal-container">
<div className="Modal-content">
{children}
<button onClick={closeCallback}> Close Basic Modal </button>
</div>
@icyJoseph
icyJoseph / BasicModal.jsx
Created September 12, 2018 16:06
Basic usage of a React Portal
import React, { Component, Fragment } from "react";
import Portal from "./Portal";
export default class BasicModal extends Component {
state = { open: false };
componentDidMount() {
this.listener = document.body.addEventListener("click", () => {
this.closeModal();
});
@icyJoseph
icyJoseph / App.jsx
Created September 12, 2018 16:08
Putting the portable BasicModal in a React App
import React, { Component } from "react";
import BasicModal from "../components/BasicModal";
import FixButton from "../components/FixButton";
import { MyModalContent, ReactHeader } from "../components/Commons";
import { bodyClickFix } from "../utils";
import "./App.css";
export default class App extends Component {
state = { open: false, counter: 0, fixed: false };
@icyJoseph
icyJoseph / iosBodyClickFix.js
Created September 12, 2018 16:38
Make the body in iOS touch screens clickable again
export const bodyClickFix = cb => {
if ("ontouchstart" in document.documentElement) {
const highlight = "-webkit-tap-highlight-color";
document.body.style.cursor = "pointer";
document.body.style[highlight] = "transparent";
}
return cb();
};