diff --git a/theatre/shared/src/utils/react/cloneElementAndMergeCallbacks.ts b/theatre/shared/src/utils/react/cloneElementAndMergeCallbacks.ts deleted file mode 100644 index d990b0b..0000000 --- a/theatre/shared/src/utils/react/cloneElementAndMergeCallbacks.ts +++ /dev/null @@ -1,103 +0,0 @@ -import React from 'react' -import type {$IntentionalAny} from '@theatre/shared/utils/types' - -const simplyMergeCallbacks: Merger = (oldCallback, newCallback) => { - if (!oldCallback) return newCallback - return (...args: $IntentionalAny[]) => { - oldCallback(...args) - newCallback(...args) - } -} - -function initializeMergers() { - const mergables = [ - 'onClick', - 'onContextMenu', - 'onDoubleClick', - 'onDrag', - 'onDragEnd', - 'onDragEnter', - 'onDragExit', - 'onDragLeave', - 'onDragOver', - 'onDragStart', - 'onDrop', - 'onMouseDown', - 'onMouseEnter', - 'onMouseLeave', - 'onMouseMove', - 'onMouseOut', - 'onMouseOver', - 'onMouseUp', - ] - mergables.forEach((propKey) => { - mergerByPropKey[propKey] = simplyMergeCallbacks - }) -} - -let mergersInitialised = false - -type Callback = (...args: $IntentionalAny[]) => void - -type Merger = (originalCb: undefined | Callback, newCb: Callback) => Callback - -const mergerByPropKey: {[propKey: string]: Merger} = { - ref(_, newRef: $IntentionalAny) { - return newRef - }, -} - -function getMerger(propKey: string) { - if (!mergersInitialised) { - mergersInitialised = true - initializeMergers() - } - return mergerByPropKey[propKey] -} - -type MergableProps = Pick< - React.AllHTMLAttributes<$IntentionalAny>, - | 'onClick' - | 'onContextMenu' - | 'onDoubleClick' - | 'onDrag' - | 'onDragEnd' - | 'onDragEnter' - | 'onDragExit' - | 'onDragLeave' - | 'onDragOver' - | 'onDragStart' - | 'onDrop' - | 'onMouseDown' - | 'onMouseEnter' - | 'onMouseLeave' - | 'onMouseMove' - | 'onMouseOut' - | 'onMouseOver' - | 'onMouseUp' -> & { - ref: React.Ref<$IntentionalAny> -} - -const cloneElementAndMergeCallbacks = ( - node: React.ReactElement<$IntentionalAny>, - additionalProps: Partial, -) => { - const newProps: Partial = {} - const keys = Object.keys(additionalProps) - - for (const key of keys) { - const merger = getMerger(key) - if (!merger) { - throw new Error(`Cannot merge react prop '${key}'`) - } - ;(newProps as $IntentionalAny)[key] = merger( - node.props[key], - (additionalProps as $IntentionalAny)[key], - ) - } - - return React.cloneElement(node, newProps) -} - -export default cloneElementAndMergeCallbacks diff --git a/theatre/shared/src/utils/react/hooks.ts b/theatre/shared/src/utils/react/hooks.ts deleted file mode 100644 index 55c59aa..0000000 --- a/theatre/shared/src/utils/react/hooks.ts +++ /dev/null @@ -1,22 +0,0 @@ -import logger from '@theatre/shared/logger' -import type {$IntentionalAny} from '@theatre/shared/utils/types' -import {useCallback, useEffect, useState} from 'react' - -const EMPTY_ARRAY: $IntentionalAny[] = [] - -export function useUnmount(fn: () => void) { - useEffect(() => fn, EMPTY_ARRAY) -} - -export function useForceUpdate(debugLabel?: string) { - const [, setTick] = useState(0) - - const update = useCallback(() => { - if (process.env.NODE_ENV !== 'production' && debugLabel) - logger.log(debugLabel, 'forceUpdate', {trace: new Error()}) - - setTick((tick) => tick + 1) - }, []) - - return update -}