-
Notifications
You must be signed in to change notification settings - Fork 655
Create new useCombinedRefs hook to deprecate and replace useRefObjectAsForwardedRef
#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
base: main
Are you sure you want to change the base?
Changes from all commits
6e882eb
288dee9
8fea4b7
3b9804f
cef559e
0e14a42
f71d633
7a905ab
9361cd5
c53642d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,7 +7,7 @@ import {XIcon} from '@primer/octicons-react' | |||||
| import {useFocusZone} from '../hooks/useFocusZone' | ||||||
| import {FocusKeys} from '@primer/behaviors' | ||||||
| import Portal from '../Portal' | ||||||
| import {useRefObjectAsForwardedRef} from '../hooks/useRefObjectAsForwardedRef' | ||||||
| import {useCombinedRefs} from '../hooks/useCombinedRefs' | ||||||
| import {useId} from '../hooks/useId' | ||||||
| import {ScrollableRegion} from '../ScrollableRegion' | ||||||
| import type {ResponsiveValue} from '../hooks/useResponsiveValue' | ||||||
|
|
@@ -288,7 +288,7 @@ const _Dialog = React.forwardRef<HTMLDivElement, React.PropsWithChildren<DialogP | |||||
| }) | ||||||
|
|
||||||
| const dialogRef = useRef<HTMLDivElement>(null) | ||||||
| useRefObjectAsForwardedRef(forwardedRef, dialogRef) | ||||||
| useCombinedRefs(forwardedRef, dialogRef) | ||||||
|
||||||
| useCombinedRefs(forwardedRef, dialogRef) | |
| const combinedDialogRef = useCombinedRefs(forwardedRef, dialogRef) |
| 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 {useCombinedRefs} 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) | ||
| useCombinedRefs(forwardedRef, innerRef) | ||
|
|
||
|
Comment on lines
15
to
18
|
||
| if (__DEV__) { | ||
| /** | ||
|
|
||
| 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 {useCombinedRefs} from '../../hooks/useCombinedRefs' | ||
| 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) | ||
| useCombinedRefs(forwardedRef, modalRef) | ||
| const closeButtonRef = useRef(null) | ||
|
Comment on lines
49
to
52
|
||
|
|
||
| const onCloseClick = () => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,137 @@ | ||||||
| import {render, renderHook} from '@testing-library/react' | ||||||
| import React, {forwardRef, type RefObject} from 'react' | ||||||
| import {describe, expect, it, vi} from 'vitest' | ||||||
| import {useCombinedRefs} from '../useCombinedRefs' | ||||||
|
|
||||||
| type InputOrButtonRef = RefObject<HTMLInputElement & HTMLButtonElement> | ||||||
|
|
||||||
| const Component = forwardRef<HTMLInputElement & HTMLButtonElement, {asButton?: boolean}>(({asButton}, forwardedRef) => { | ||||||
| const ref: InputOrButtonRef = React.useRef(null) | ||||||
|
|
||||||
| const combinedRef = useCombinedRefs(forwardedRef, ref) | ||||||
|
|
||||||
| return asButton ? <button type="button" ref={combinedRef} /> : <input ref={combinedRef} /> | ||||||
| }) | ||||||
|
|
||||||
| describe('useCombinedRefs', () => { | ||||||
| describe('combining a forwarded ref with an internal ref', () => { | ||||||
| describe('object refs', () => { | ||||||
| it('fowards the ref to the underlying element', async () => { | ||||||
|
||||||
| it('fowards the ref to the underlying element', async () => { | |
| it('forwards the ref to the underlying element', async () => { |
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.
useCombinedRefs(forwardedRef, innerRef)is called but its return value isn't used; the component still passesref={innerRef}. This breaks external/forwarded refs. Assign the combined ref and pass it to the renderedComponent’srefprop.