Skip to content

Instantly share code, notes, and snippets.

View nathansearles's full-sized avatar
✌️

Nathan Searles nathansearles

✌️
View GitHub Profile
@RenaudRohlinger
RenaudRohlinger / GsapTicker.jsx
Created September 8, 2022 06:15
Synchronize gsap and r3f raf loop
import React, { useEffect } from 'react'
import gsap from 'gsap'
import { useFrame } from '@react-three/fiber'
// sync gsap raf to r3f raf
gsap.ticker.remove(gsap.updateRoot)
export const GsapTicker = () => {
const pg = React.useRef(0)
gsap.ticker.remove(gsap.updateRoot)
@northamerican
northamerican / simple-caching-logical-nullish-assignment.js
Created February 11, 2021 23:50
Simple caching in Javascript using the new Logical nullish assignment (??=) operator
// Simple caching in Javascript using the new Logical nullish assignment (??=) operator
class FibClass {
// Private field to store a value
#fib = null
// Fibonacci sequence method (recursive calculation)
f (n) {
return n < 3 ? 1 : this.f(n - 1) + this.f(n - 2)
}
import React from "react";
export type ColorScheme = "light" | "dark";
export default function useColorSchemePreference(
defaultColorScheme: ColorScheme = "light"
) {
let darkQuery = "(prefers-color-scheme: dark)";
let [colorScheme, setColorScheme] = React.useState<ColorScheme>(
typeof window === "object" && window.matchMedia
@coleturner
coleturner / framer-motion-use-viewport-scroll-with-element-container.md
Last active February 5, 2024 09:46
(Framer Motion): useViewportScroll with element container

Demo

Context

useViewportScroll is a great way to create a parallax effect as the page scrolls. In some cases however, we only want to scroll when an element is in the viewport area.

So for example, if we have a "landscape" scene, and want to animate the Sun object only when it's in view, we start with our useViewportScroll implementation:

function Sun(props) {
 const { scrollY, scrollYProgress } = useViewportScroll();
@theodorosploumis
theodorosploumis / Nework_throttling_profiles.md
Last active May 11, 2024 04:18
Web development - Custom network throttling profiles
Profile download (kb/s) upload (kb/s) latency (ms)
Native 0 0 0
GPRS 50 20 500
56K Dial-up 50 30 120
Mobile EDGE 240 200 840
2G Regular 250 50 300
2G Good 450 150 150
3G Slow 780 330 200
@slikts
slikts / advanced-memo.md
Last active April 27, 2024 02:40
Advanced memoization and effects in React

nelabs.dev

Advanced memoization and effects in React

Memoization is a somewhat fraught topic in the React world, meaning that it's easy to go wrong with it, for example, by [making memo() do nothing][memo-pitfall] by passing in children to a component. The general advice is to avoid memoization until the profiler tells you to optimize, but not all use cases are general, and even in the general use case you can find tricky nuances.

Discussing this topic requires some groundwork about the technical terms, and I'm placing these in once place so that it's easy to skim and skip over:

  • Memoization means caching the output based on the input; in the case of functions, it means caching the return value based on the arguments.
  • Values and references are unfortunately overloaded terms that can refer to the low-level implementation details of assignments in a language like C++, for example, or to memory

Everything I Know About UI Routing

Definitions

  1. Location - The location of the application. Usually just a URL, but the location can contain multiple pieces of information that can be used by an app
    1. pathname - The "file/directory" portion of the URL, like invoices/123
    2. search - The stuff after ? in a URL like /assignments?showGrades=1.
    3. query - A parsed version of search, usually an object but not a standard browser feature.
    4. hash - The # portion of the URL. This is not available to servers in request.url so its client only. By default it means which part of the page the user should be scrolled to, but developers use it for various things.
    5. state - Object associated with a location. Think of it like a hidden URL query. It's state you want to keep with a specific location, but you don't want it to be visible in the URL.
@mjackson
mjackson / useMediaQuery.js
Created July 25, 2019 01:05
A React hook for listening to the state of a CSS media query
import { useEffect, useState } from 'react';
export default function useMediaQuery(query, defaultValue) {
const [matches, setMatches] = useState(defaultValue);
useEffect(() => {
const mql = window.matchMedia(query);
setMatches(mql.matches);
@rybon
rybon / compressGraphqlDocument.js
Last active August 25, 2022 19:48
Remove unnecessary whitespace from a GraphQL query / mutation / subscription
const compressGraphqlDocument = graphqlDocument =>
graphqlDocument
.replace(/#.*\n/g, '')
.replace(/[\s|,]*\n+[\s|,]*/g, ' ')
.replace(/:\s/g, ':')
.replace(/,\s/g, ',')
.replace(/\)\s\{/g, '){')
.replace(/\}\s/g, '}')
.replace(/\{\s/g, '{')
.replace(/\s\}/g, '}')
@remy
remy / ActiveLink.js
Last active April 12, 2024 08:33
Next.js version of `activeClassName` support.