Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 677
Replace useRefObjectAsForwardedRef with useMergedRefs internally#7638
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
6e882eb288dee98fea4b73b9804fcef559e0e14a42f71d6337a905ab9361cd5c53642d1079845f6bc0e5c5cc9a53f60a7729fbb1fece5160bdb0e3980f14aa4f9ca439d449fbdcae302bb26a623d854c98e77831973f18486bedb140ef6b3105e866bbfc0cb7349459715c45b8dc08e0File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@primer/react': patch | ||
| --- | ||
| Update internal implementations of combined refs to improve performance and add support for React 19 callback refs |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import {clsx} from 'clsx' | ||
| import React, {forwardRef, useEffect} from 'react' | ||
| import {useRefObjectAsForwardedRef} from '../hooks' | ||
| import {useMergedRefs} from '../hooks' | ||
| import type {ComponentProps} from '../utils/types' | ||
| import type {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic' | ||
| import classes from './Heading.module.css' | ||
| @@ -14,7 +14,7 @@ type StyledHeadingProps = { | ||
| const Heading = forwardRef(({as: Component = 'h2', className, variant, ...props}, forwardedRef) => { | ||
| const innerRef = React.useRef<HTMLHeadingElement>(null) | ||
| useRefObjectAsForwardedRef(forwardedRef, innerRef) | ||
| const mergedRef = useMergedRefs(forwardedRef, innerRef) | ||
iansan5653 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (__DEV__) { | ||
| /** | ||
| @@ -32,7 +32,7 @@ const Heading = forwardRef(({as: Component = 'h2', className, variant, ...props} | ||
| }, [innerRef]) | ||
| } | ||
| return <Component className={clsx(className, classes.Heading)} data-variant={variant} {...props} ref={innerRef} /> | ||
| return <Component className={clsx(className, classes.Heading)} data-variant={variant} {...props} ref={mergedRef} /> | ||
| }) as PolymorphicForwardRefComponent<HeadingLevels, StyledHeadingProps> | ||
| Heading.displayName = 'Heading' | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2,7 +2,7 @@ import React, {forwardRef, useRef, type HTMLAttributes} from 'react' | ||
| import {IconButton} from '../../Button' | ||
| import useDialog from '../../hooks/useDialog' | ||
| import type {ComponentProps} from '../../utils/types' | ||
| import {useRefObjectAsForwardedRef} from '../../hooks/useRefObjectAsForwardedRef' | ||
| import {useMergedRefs} from '../../hooks/useMergedRefs' | ||
| import {XIcon} from '@primer/octicons-react' | ||
| import {clsx} from 'clsx' | ||
| import classes from './Dialog.module.css' | ||
| @@ -48,7 +48,7 @@ const Dialog = forwardRef<HTMLDivElement, InternalDialogProps>( | ||
| ) => { | ||
| const overlayRef = useRef(null) | ||
| const modalRef = useRef<HTMLDivElement>(null) | ||
| useRefObjectAsForwardedRef(forwardedRef, modalRef) | ||
| const mergedRef = useMergedRefs(forwardedRef, modalRef) | ||
| const closeButtonRef = useRef(null) | ||
iansan5653 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| const onCloseClick = () => { | ||
| @@ -73,7 +73,7 @@ const Dialog = forwardRef<HTMLDivElement, InternalDialogProps>( | ||
| <span className={classes.Overlay} ref={overlayRef} /> | ||
| <Component | ||
| tabIndex={-1} | ||
| ref={modalRef} | ||
| ref={mergedRef} | ||
| role="dialog" | ||
| aria-modal="true" | ||
| {...props} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -17,18 +17,18 @@ import {useCallback} from 'react' | ||
| * // React 18 | ||
| * const Example = forwardRef<HTMLButtonElement, {}>((props, forwardedRef) => { | ||
| * const ref = useRef<HTMLButtonElement>(null) | ||
| * const combinedRef = useMergedRefs(forwardedRef, ref) | ||
| * const mergedRef = useMergedRefs(forwardedRef, ref) | ||
| * | ||
| * return <button ref={combinedRef} /> | ||
| * return <button ref={mergedRef} /> | ||
| * }) | ||
| * | ||
| * @example | ||
| * // React 19 | ||
| * const Example = ({ref: externalRef}: {ref?: Ref<HTMLButtonElement>}) => { | ||
| * const ref = useRef<HTMLButtonElement>(null) | ||
| * const combinedRef = useMergedRefs(externalRef, ref) | ||
| * const mergedRef = useMergedRefs(externalRef, ref) | ||
| * | ||
| * return <button ref={combinedRef} /> | ||
| * return <button ref={mergedRef} /> | ||
| * } | ||
iansan5653 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| */ | ||
| export function useMergedRefs<T>(refA: Ref<T | null>, refB: Ref<T | null>) { | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's different here that this needed to be added? 🤔