Skip to content

Commit

Permalink
remove es-toolkit
Browse files Browse the repository at this point in the history
  • Loading branch information
talentlessguy committed Aug 28, 2024
1 parent f536e4e commit c647b90
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 30 deletions.
1 change: 0 additions & 1 deletion components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
},
"dependencies": {
"clsx": "^1.1.1",
"es-toolkit": "^1.16.0",
"react-transition-state": "^2.1.1",
"ts-pattern": "^4.3.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Portal } from '../Portal'
import { Box, BoxProps } from '../Box/Box'
import { getValueForTransitionState } from './utils/getValueForTransitionState'
import { container } from './style.css'
import { debounceWithWait } from './utils/debounce'
import { debounce } from '../../../utils/debounce'

export type DynamicPopoverSide = 'top' | 'right' | 'bottom' | 'left'

Expand Down Expand Up @@ -362,7 +362,7 @@ export const DynamicPopover: React.FC<DynamicPopoverProps> = ({
onShowCallback?.()
}

const debouncedMouseMove = debounceWithWait(
const debouncedMouseMove = debounce(
(e: MouseEvent) => {
const cursorXY = { x: e.clientX, y: e.clientY }
const targetRect = targetElement?.getBoundingClientRect()
Expand All @@ -380,7 +380,7 @@ export const DynamicPopover: React.FC<DynamicPopoverProps> = ({
document.removeEventListener('mousemove', handleMouseMove)
},
100,
1000,
{ maxWait: 1000 },
)

handleMouseMove = (e: MouseEvent) => {
Expand Down
2 changes: 1 addition & 1 deletion components/src/components/molecules/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from 'react'
import { P, match } from 'ts-pattern'
import { debounce } from 'es-toolkit'

import { TransitionState } from 'react-transition-state'

Expand All @@ -15,6 +14,7 @@ import { DownChevronSVG, DynamicPopover, ScrollBox } from '../..'
import { ActionSheet } from './ActionSheet'
import { Box, BoxProps } from '../../atoms/Box/Box'
import { PopoverProps } from '../../atoms/DynamicPopover'
import { debounce } from '@/src/utils/debounce'

type Align = 'left' | 'right'
type LabelAlign = 'flex-start' | 'flex-end' | 'center'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
export function debounceWithWait<T extends (...args: any[]) => any>(
type DebounceOptions = {
maxWait?: number
}

export function debounce<T extends (...args: any[]) => any>(
func: T,
wait: number,
maxWait: number,
options?: DebounceOptions,
): (...args: Parameters<T>) => void {
let timer: NodeJS.Timeout | null = null
let maxTimer: NodeJS.Timeout | null = null
const { maxWait } = options || {}

return function (...args: Parameters<T>): void {
// @ts-expect-error some magic
// @ts-expect-error because this in a func
// eslint-disable-next-line @typescript-eslint/no-this-alias
const context = this

if (timer) {
clearTimeout(timer)
}

// Clear the maxWait timer if it exists
if (maxTimer) {
clearTimeout(maxTimer)
}

// Set the regular debounce timer
timer = setTimeout(() => {
if (maxTimer) {
clearTimeout(maxTimer)
}
func.apply(context, args)
}, wait)

// Set the maxWait timer
maxTimer = setTimeout(() => {
func.apply(context, args)
// Clear the regular timer since maxWait has fired
if (timer) {
clearTimeout(timer)
}
}, maxWait)
if (maxWait) {
maxTimer = setTimeout(() => {
func.apply(context, args)
if (timer) {
clearTimeout(timer)
}
}, maxWait)
}
}
}
16 changes: 4 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c647b90

Please sign in to comment.