Skip to content

Instantly share code, notes, and snippets.

View icyJoseph's full-sized avatar

Joseph icyJoseph

View GitHub Profile
import React from "react";
import useSWR from "swr";
import { GridSpinner } from "react-spinners-kit";
import { SlowSuspense } from "containers/SlowSuspense";
import { UserCard } from "components/UserCard";
const endpoint = "https://api.github.com";
const fetcher = key =>
fetch({ url: `${endpoint}/users/${key}` }).then(res => res.json());
@icyJoseph
icyJoseph / SlowSuspense.js
Last active April 6, 2020 17:12
A pattern to create a delayed suspense fallback
import React, {
useEffect,
useState,
createContext,
useContext,
Suspense,
useMemo
} from "react";
const SlowSuspenseCtx = createContext({});
@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();
};
@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 / 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 / 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 / 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 / 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 / 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 / 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>