Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix confetti bug #446

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/components/confetti.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Index as ConfettiShower } from 'confetti-react'
import { ClientOnly } from 'remix-utils'
import { useWindowSize } from '#app/utils/useWindowSize.ts'

export function Confetti({ id }: { id?: string | null }) {
const { width, height } = useWindowSize()
if (!id) return null

return (
Expand All @@ -12,8 +14,8 @@ export function Confetti({ id }: { id?: string | null }) {
run={Boolean(id)}
recycle={false}
numberOfPieces={500}
width={window.innerWidth}
height={window.innerHeight}
width={width}
height={height}
/>
)}
</ClientOnly>
Expand Down
81 changes: 81 additions & 0 deletions app/utils/useEventListener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { type RefObject, useEffect, useRef } from 'react'
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect.ts'

// MediaQueryList Event based useEventListener interface
function useEventListener<K extends keyof MediaQueryListEventMap>(
eventName: K,
handler: (event: MediaQueryListEventMap[K]) => void,
element: RefObject<MediaQueryList>,
options?: boolean | AddEventListenerOptions,
): void

// Window Event based useEventListener interface
function useEventListener<K extends keyof WindowEventMap>(
eventName: K,
handler: (event: WindowEventMap[K]) => void,
element?: undefined,
options?: boolean | AddEventListenerOptions,
): void

// Element Event based useEventListener interface
function useEventListener<
K extends keyof HTMLElementEventMap,
T extends HTMLElement = HTMLDivElement,
>(
eventName: K,
handler: (event: HTMLElementEventMap[K]) => void,
element: RefObject<T>,
options?: boolean | AddEventListenerOptions,
): void

// Document Event based useEventListener interface
function useEventListener<K extends keyof DocumentEventMap>(
eventName: K,
handler: (event: DocumentEventMap[K]) => void,
element: RefObject<Document>,
options?: boolean | AddEventListenerOptions,
): void

function useEventListener<
KW extends keyof WindowEventMap,
KH extends keyof HTMLElementEventMap,
KM extends keyof MediaQueryListEventMap,
T extends HTMLElement | MediaQueryList | void = void,
>(
eventName: KW | KH | KM,
handler: (
event:
| WindowEventMap[KW]
| HTMLElementEventMap[KH]
| MediaQueryListEventMap[KM]
| Event,
) => void,
element?: RefObject<T>,
options?: boolean | AddEventListenerOptions,
) {
// Create a ref that stores handler
const savedHandler = useRef(handler)

useIsomorphicLayoutEffect(() => {
savedHandler.current = handler
}, [handler])

useEffect(() => {
// Define the listening target
const targetElement: T | Window = element?.current ?? window

if (!(targetElement && targetElement.addEventListener)) return

// Create event listener that calls handler function stored in ref
const listener: typeof handler = event => savedHandler.current(event)

targetElement.addEventListener(eventName, listener, options)

// Remove event listener on cleanup
return () => {
targetElement.removeEventListener(eventName, listener, options)
}
}, [eventName, element, options])
}

export { useEventListener }
4 changes: 4 additions & 0 deletions app/utils/useIsomorphicLayoutEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { useEffect, useLayoutEffect } from 'react'

export const useIsomorphicLayoutEffect =
typeof window !== 'undefined' ? useLayoutEffect : useEffect
32 changes: 32 additions & 0 deletions app/utils/useWindowSize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useState } from 'react'
import { useEventListener } from './useEventListener.ts'
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect.ts'

interface WindowSize {
width: number
height: number
}

export function useWindowSize(): WindowSize {
const [windowSize, setWindowSize] = useState<WindowSize>({
width: 0,
height: 0,
})

const handleSize = () => {
setWindowSize({
width: window.innerWidth,
height: window.innerHeight,
})
}

useEventListener('resize', handleSize)

// Set size at the first client-side load
useIsomorphicLayoutEffect(() => {
handleSize()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

return windowSize
}