Skip to content

Instantly share code, notes, and snippets.

View PaulieScanlon's full-sized avatar

Paul Scanlon PaulieScanlon

View GitHub Profile
@PaulieScanlon
PaulieScanlon / test.js
Created November 23, 2021 18:41
client side data with fetch
import React, { useEffect, useState } from 'react';
const Page = () => {
const [response, setResponse] = useState(false);
useEffect(() => {
const getData = async () => {
const response = await fetch(
'https://api.oceandrivers.com/api/ODWeather'
);
@PaulieScanlon
PaulieScanlon / use-local-storage.js
Last active January 1, 2022 18:15
Sync state to local storage so that it persists through a page refresh. Usage is similar to useState except we pass in a local storage key so that we can default to that value on page load instead of the specified initial value.
import { useEffect, useState } from 'react'
const useLocalStorage = (key, initialValue) => {
const [storedValue, setStoredValue] = useState(initialValue)
const setValue = (value) => {
try {
const valueToStore =
value instanceof Function ? value(storedValue) : value
setStoredValue(valueToStore)
window.localStorage.setItem(key, JSON.stringify(valueToStore))
@PaulieScanlon
PaulieScanlon / hybrid-svg.js
Created January 13, 2022 18:32
A WIP hybrid Svg using Gatsby Image
import React from 'react';
import { useStaticQuery, graphql } from 'gatsby';
import { GatsbyImage, getImage } from 'gatsby-plugin-image';
const Test = () => {
const {
file: {
childImageSharp: { gatsbyImageData }
}
} = useStaticQuery(graphql`
@PaulieScanlon
PaulieScanlon / gatsby-browser.js
Created January 20, 2022 12:37
onRouteUpdate - jumplinks
export const onRouteUpdate = ({ location }) => {
const jumplink = document.getElementById(location.hash)
if (jumplink) {
window.scrollTo({
top: jumplink.offsetTop,
})
}
return true
}
@PaulieScanlon
PaulieScanlon / gatsby-browser.js
Last active January 29, 2022 15:34
Conditional Header
import React, { Fragment } from 'react';
export const wrapPageElement = ({ element, props }) => {
const {
location: { pathname },
} = props
return (
<Fragment>
<header className={`${pathname === '/' ? 'some-class' : 'some-other-class'} `}>
import { useState, useEffect } from 'react'
const QUERY_STRING = '(prefers-reduced-motion: no-preference)'
const EVENT = 'change'
const usePrefersReducedMotion = () => {
const [prefersReducedMotion, setPrefersReducedMotion] = useState(true)
useEffect(() => {
const query = window.matchMedia(QUERY_STRING)
{
"contentTypes": [
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "816y14uaxxqf"
}
@PaulieScanlon
PaulieScanlon / micromarkAbb.md
Last active July 8, 2022 19:54
High level prose micromark-extension-abbr

micromarkAbbr

Usage

Pass markdown and config to function

micromarkAbbr(markdown, config)

Example Markdown

@PaulieScanlon
PaulieScanlon / sample-data.js
Created October 1, 2022 08:57
nextjs-ssr-line-chart-sample-data
const sampleData = [
{
date: 'Sun Jan 02 2022 00:00:00 GMT+0000 (Greenwich Mean Time)',
year: 2022,
additions: 9932,
deletions: -7938
},
{
date: 'Sun Jan 09 2022 00:00:00 GMT+0000 (Greenwich Mean Time)',
year: 2022,
@PaulieScanlon
PaulieScanlon / sample-data.js
Last active October 3, 2022 08:32
gatsby-ssr-line-chart-sample-data
const sampleData = [
{
total: 84,
date: 'Sun Oct 10 2021 01:00:00 GMT+0100 (British Summer Time)'
},
{
total: 91,
date: 'Sun Oct 17 2021 01:00:00 GMT+0100 (British Summer Time)'
},
{