Skip to content

Instantly share code, notes, and snippets.

View guillermodlpa's full-sized avatar

Guillermo guillermodlpa

View GitHub Profile
@guillermodlpa
guillermodlpa / SearchableGrommetSelect.tsx
Created April 25, 2022 18:02
Component I made while developing Travelmap, but ended up not keeping. This could be useful in the future.
import { Select, SelectExtendedProps } from 'grommet';
import { useState } from 'react';
export interface Option {
label: string;
value: string;
}
interface SearchableSelectProps extends SelectExtendedProps {
options: Option[];
}
@guillermodlpa
guillermodlpa / ErrorBoundary.jsx
Created April 28, 2022 17:46
React Error Boundary (TypeScript), keeping it in a gist for convenience
import { Component, ReactNode } from 'react';
// Error boundaries currently have to be classes.
class ErrorBoundary extends Component<{ fallback: ReactNode }> {
state = { hasError: false, error: null };
static getDerivedStateFromError(error: Error) {
return {
hasError: true,
error,
@guillermodlpa
guillermodlpa / share-button-messenger.svg
Created April 29, 2022 10:13
Messenger App SVG icon, for sharing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@guillermodlpa
guillermodlpa / NoSsr.tsx
Created June 9, 2022 10:11
NoSsr React component with TypeScript
import { useEffect, useState } from 'react';
export default function NoSsr({ children }: { children: React.ReactNode }) {
const [mountedState, setMountedState] = useState(false);
useEffect(() => {
setMountedState(true);
}, []);
return <>{mountedState ? children : null}</>;
@guillermodlpa
guillermodlpa / ConfirmationDialogWrapper.tsx
Created August 25, 2022 07:48
Pattern to add a confirmation dialog to confirm a button action, with TypeScript. Dialog not implemented here, as it depends on the UI library
import { EventHandler, useCallback, useState } from 'react';
import ConfirmationDialog from './ConfirmationDialog';
export default function WrappingDialogConfirmation({
children,
...props
}: {
children: (handleClick: EventHandler<any>) => React.ReactNode;
title: string;
description: string;
@guillermodlpa
guillermodlpa / gist:9c60cf3f7cafe0b511685fdd1e5c779d
Created September 13, 2022 16:40
Aside bio code for À l'équilibre Squarespace blog
// Custom code for Squarespace
// To add bio aside to https://www.alequilibre-nutrition.com/
(() => {
try {
const imageSrc = 'https://images.squarespace-cdn.com/content/v1/61d747e813d12769b8b2ae58/e4ba1672-bd46-42e2-8980-f2c19bd84bf3/Romane+A+l%27equilibre+meditation+self+care.jpg?format=120w';
const linkUrl = '/apropos';
const linkText = 'À propos';
const body = 'alal la';
const mobileBreakpoint = 768;
@guillermodlpa
guillermodlpa / generateImageSizesProp.ts
Created October 28, 2022 17:14
Generates a string to be passed to img sizes for performance
/**
* Generates a string to be passed to img sizes, for performance.
* @see https://nextjs.org/docs/api-reference/next/image#sizes
*/
export default function generateImageSizesProp(responsiveValues: {
base: string;
sm?: string;
md?: string;
lg?: string;
xl?: string;
@guillermodlpa
guillermodlpa / useThrottledResizeObserver.tsx
Created December 23, 2022 11:41
Reusable hook to observe a node and run a throttled callback when its dimensions change
import throttle from 'lodash.throttle';
import React, { useEffect } from 'react';
/**
* Reusable hook to observe a node and run a throttled callback when its dimensions change
* Example usage can be to change the text within a flex grow container between
* a short and a long version depending on available space
*/
export default function useThrottledResizeObserver<T extends HTMLElement>(
ref: React.MutableRefObject<T>,
@guillermodlpa
guillermodlpa / InstagramLogo.tsx
Created December 31, 2022 17:36
Instagram logo svg wrapped with Chakra UI's `createIcon`
import { createIcon } from "@chakra-ui/react";
export default createIcon({
displayName: "InstagramLogo",
viewBox: "0 0 512 512",
path: (
<>
<title>Instagram</title>
<path d="M256,49.471c67.266,0,75.233.257,101.8,1.469,24.562,1.121,37.9,5.224,46.778,8.674a78.052,78.052,0,0,1,28.966,18.845,78.052,78.052,0,0,1,18.845,28.966c3.45,8.877,7.554,22.216,8.674,46.778,1.212,26.565,1.469,34.532,1.469,101.8s-0.257,75.233-1.469,101.8c-1.121,24.562-5.225,37.9-8.674,46.778a83.427,83.427,0,0,1-47.811,47.811c-8.877,3.45-22.216,7.554-46.778,8.674-26.56,1.212-34.527,1.469-101.8,1.469s-75.237-.257-101.8-1.469c-24.562-1.121-37.9-5.225-46.778-8.674a78.051,78.051,0,0,1-28.966-18.845,78.053,78.053,0,0,1-18.845-28.966c-3.45-8.877-7.554-22.216-8.674-46.778-1.212-26.564-1.469-34.532-1.469-101.8s0.257-75.233,1.469-101.8c1.121-24.562,5.224-37.9,8.674-46.778A78.052,78.052,0,0,1,78.458,78.458a78.053,78.053,0,0,1,28.966-18.845c8.877-3.45,22.216-7.554,46.778-8.674,26.565-1.212,34.532-1.469,101.8-1.469m0-45.391c-68.418,0
@guillermodlpa
guillermodlpa / useKeepBrowserPathUpdated.ts
Created January 12, 2023 07:37
React hook guideline implementation to update the URL with a debounce in Next.js
function getBrowserPath(params) {
// your implementation here
}
const URL_UPDATE_DELAY = 750;
export default function useKeepBrowserPathUpdated(params) {
const path = getBrowserPath(params);
useEffect(() => {