-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(Modal|Portal|Popup): use React.forwardRef() (#4253)
* chore(Modal|Portal): use React.forwardRef() * remove redundant statics * migrate Popup
- Loading branch information
1 parent
275dad1
commit d6e43a0
Showing
16 changed files
with
849 additions
and
705 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,30 @@ | ||
import React from 'react' | ||
import ReactIs from 'react-is' | ||
|
||
import { useMergedRefs } from '../../lib' | ||
|
||
/** | ||
* Assigns merged ref to an existing element is possible or wraps it with an additional "div". | ||
* | ||
* @param {React.ReactNode} node | ||
* @param {React.Ref} userRef | ||
*/ | ||
export default function usePortalElement(node, userRef) { | ||
const ref = useMergedRefs(node.ref, userRef) | ||
|
||
if (React.isValidElement(node)) { | ||
if (ReactIs.isForwardRef(node)) { | ||
return React.cloneElement(node, { ref }) | ||
} | ||
|
||
if (typeof node.type === 'string') { | ||
return React.cloneElement(node, { ref }) | ||
} | ||
} | ||
|
||
return ( | ||
<div data-suir-portal='true' ref={ref}> | ||
{node} | ||
</div> | ||
) | ||
} |
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,25 @@ | ||
import React from 'react' | ||
|
||
import { useMergedRefs } from '../../../lib' | ||
import validateTrigger from './validateTrigger' | ||
|
||
/** | ||
* @param {React.ReactNode} trigger | ||
* @param {React.Ref} triggerRef | ||
*/ | ||
function useTrigger(trigger, triggerRef) { | ||
const ref = useMergedRefs(trigger?.ref, triggerRef) | ||
|
||
if (trigger) { | ||
/* istanbul ignore else */ | ||
if (process.env.NODE_ENV !== 'production') { | ||
validateTrigger(trigger) | ||
} | ||
|
||
return [ref, React.cloneElement(trigger, { ref })] | ||
} | ||
|
||
return [ref, null] | ||
} | ||
|
||
export default useTrigger |
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,18 @@ | ||
import * as React from 'react' | ||
|
||
/** | ||
* Hook keeping track of a given value from a previous execution of the component the Hook is used in. | ||
* | ||
* @see https://reactjs.org/docs/hooks-faq.html#how-to-get-the-previous-props-or-state | ||
*/ | ||
function usePrevious(value) { | ||
const ref = React.useRef() | ||
|
||
React.useEffect(() => { | ||
ref.current = value | ||
}) | ||
|
||
return ref.current | ||
} | ||
|
||
export default usePrevious |
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.