forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backpatch: add StrictMode support react-bootstrap#5687
- Loading branch information
Showing
13 changed files
with
233 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import React, { useCallback, useRef } from 'react'; | ||
import Transition, { | ||
TransitionProps, | ||
TransitionStatus, | ||
} from 'react-transition-group/Transition'; | ||
import safeFindDOMNode from 'react-overlays/safeFindDOMNode'; | ||
import useMergedRefs from '@restart/hooks/useMergedRefs'; | ||
|
||
export type TransitionWrapperProps = TransitionProps & { | ||
childRef?: React.Ref<unknown>; | ||
children: | ||
| React.ReactElement | ||
| (( | ||
status: TransitionStatus, | ||
props: Record<string, unknown>, | ||
) => React.ReactNode); | ||
}; | ||
|
||
// Normalizes Transition callbacks when nodeRef is used. | ||
const TransitionWrapper = React.forwardRef< | ||
Transition<any>, | ||
TransitionWrapperProps | ||
>( | ||
( | ||
{ | ||
onEnter, | ||
onEntering, | ||
onEntered, | ||
onExit, | ||
onExiting, | ||
onExited, | ||
addEndListener, | ||
children, | ||
childRef, | ||
...props | ||
}, | ||
ref, | ||
) => { | ||
const nodeRef = useRef<HTMLElement>(null); | ||
const mergedRef = useMergedRefs(nodeRef, childRef); | ||
|
||
const attachRef = ( | ||
r: React.ComponentClass | Element | null | undefined, | ||
) => { | ||
mergedRef(safeFindDOMNode(r)); | ||
}; | ||
|
||
const normalize = ( | ||
callback?: (node: HTMLElement, param: unknown) => void, | ||
) => (param: unknown) => { | ||
if (callback && nodeRef.current) { | ||
callback(nodeRef.current, param); | ||
} | ||
}; | ||
|
||
const handleEnter = useCallback(normalize(onEnter), [onEnter]); | ||
const handleEntering = useCallback(normalize(onEntering), [onEntering]); | ||
const handleEntered = useCallback(normalize(onEntered), [onEntered]); | ||
const handleExit = useCallback(normalize(onExit), [onExit]); | ||
const handleExiting = useCallback(normalize(onExiting), [onExiting]); | ||
const handleExited = useCallback(normalize(onExited), [onExited]); | ||
const handleAddEndListener = useCallback(normalize(addEndListener), [ | ||
addEndListener, | ||
]); | ||
|
||
return ( | ||
<Transition | ||
ref={ref} | ||
{...props} | ||
onEnter={handleEnter} | ||
onEntered={handleEntered} | ||
onEntering={handleEntering} | ||
onExit={handleExit} | ||
onExited={handleExited} | ||
onExiting={handleExiting} | ||
addEndListener={handleAddEndListener} | ||
nodeRef={nodeRef} | ||
> | ||
{typeof children === 'function' | ||
? (status: TransitionStatus, innerProps: Record<string, unknown>) => | ||
children(status, { | ||
...innerProps, | ||
ref: attachRef, | ||
}) | ||
: React.cloneElement(children as React.ReactElement, { | ||
ref: attachRef, | ||
})} | ||
</Transition> | ||
); | ||
}, | ||
); | ||
|
||
export default TransitionWrapper; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.