Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quiet-spiders-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Update the `AnchoredOverlay` component so that the `ref` value is not overridden when spreading props
32 changes: 31 additions & 1 deletion packages/react/src/AnchoredOverlay/AnchoredOverlay.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {act, useCallback, useState} from 'react'
import {act, createRef, useCallback, useRef, useState} from 'react'
import {describe, expect, it, vi} from 'vitest'
import {render} from '@testing-library/react'
import {userEvent} from 'vitest/browser'
Expand Down Expand Up @@ -124,7 +124,7 @@

it('should render consistently when open', () => {
const {container} = render(<AnchoredOverlayTestComponent initiallyOpen={true} />)
expect(container).toMatchSnapshot()

Check failure on line 127 in packages/react/src/AnchoredOverlay/AnchoredOverlay.test.tsx

View workflow job for this annotation

GitHub Actions / test (react-19)

src/AnchoredOverlay/AnchoredOverlay.test.tsx > AnchoredOverlay > should render consistently when open

Error: Snapshot `AnchoredOverlay > should render consistently when open 1` mismatched - Expected + Received @@ -2,19 +2,19 @@ <div class="prc-src-BaseStyles-9LBd2" data-portal-root="true" > <button - aria-describedby=":r8:-loading-announcement" + aria-describedby="_r_8_-loading-announcement" aria-expanded="true" aria-haspopup="true" class="prc-Button-ButtonBase-Eb8-K" data-loading="false" data-no-visuals="true" data-size="medium" data-variant="default" - id=":r8:" + id="_r_8_" tabindex="0" type="button" > <span class="prc-Button-ButtonContent-KZ5Bz" ❯ toMatchSnapshot src/AnchoredOverlay/AnchoredOverlay.test.tsx:127:22
})

it('should call onPositionChange when provided', async () => {
Expand All @@ -145,4 +145,34 @@
},
})
})

it('should support a `ref` through `overlayProps` on the overlay element', () => {
const ref = createRef<HTMLDivElement>()

function Test() {
const anchorRef = useRef(null)
return (
<AnchoredOverlay
overlayProps={{
ref,
id: 'overlay',
}}
open
renderAnchor={props => {
return (
<button {...props} ref={anchorRef} type="button">
anchor
</button>
)
}}
>
<div>content</div>
</AnchoredOverlay>
)
}

render(<Test />)

expect(document.getElementById('overlay')).toBe(ref.current)
})
})
18 changes: 17 additions & 1 deletion packages/react/src/AnchoredOverlay/AnchoredOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ export const AnchoredOverlay: React.FC<React.PropsWithChildren<AnchoredOverlayPr
onClickOutside={onClickOutside}
ignoreClickRefs={[anchorRef]}
onEscape={onEscape}
ref={updateOverlayRef}
role="none"
visibility={position ? 'visible' : 'hidden'}
height={height}
Expand All @@ -262,6 +261,12 @@ export const AnchoredOverlay: React.FC<React.PropsWithChildren<AnchoredOverlayPr
className={className}
preventOverflow={preventOverflow}
{...overlayProps}
ref={node => {
if (overlayProps?.ref) {
assignRef(overlayProps.ref, node)
}
updateOverlayRef(node)
}}
>
{showXIcon ? (
<div className={classes.ResponsiveCloseButtonContainer}>
Expand All @@ -288,4 +293,15 @@ export const AnchoredOverlay: React.FC<React.PropsWithChildren<AnchoredOverlayPr
)
}

function assignRef<T>(
ref: React.MutableRefObject<T | null> | ((instance: T | null) => void) | null | undefined,
value: T | null,
) {
if (typeof ref === 'function') {
ref(value)
} else if (ref) {
ref.current = value
}
}
Comment on lines +296 to +305
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assignRef utility function is duplicating functionality that already exists in the codebase. Consider using the existing useRefObjectAsForwardedRef hook or extracting assignRef to a shared utility location since it's a common ref-handling pattern that could be reused across components.

Copilot uses AI. Check for mistakes.

AnchoredOverlay.displayName = 'AnchoredOverlay'
Loading