Skip to content

Replace forwarded ref instances of useProvidedRefOrCreate with useMergedRefs#7644

Open
iansan5653 wants to merge 49 commits into
mainfrom
replace-use-provided-ref-or-create
Open

Replace forwarded ref instances of useProvidedRefOrCreate with useMergedRefs#7644
iansan5653 wants to merge 49 commits into
mainfrom
replace-use-provided-ref-or-create

Conversation

@iansan5653

@iansan5653 iansan5653 commented Mar 9, 2026

Copy link
Copy Markdown
Contributor

Closes https://github.com/github/primer/issues/6788

Per-component changes covered by new feature flag: https://devportal.githubapp.com/feature-flags/primer_react_merged_forwarded_refs/overview


In #7638 I created a useMergedRefs hook and migrated all instances of useRefObjectAsForwardedRef to it.

A similar pattern is using useProvidedRefOrCreate for this. It's typically used with a type cast and a ts-expect-error comment (❗):

const ref = useProvidedRefOrCreate(forwardedRef as RefObject<HTMLElement | null>)

// @ts-expect-error
return <div ref={ref} />

This is obviously problematic. The type errors are trying to point out the problem with this pattern: it will break if consumers pass ref callbacks. Ref callbacks are likely to become more and more common as React 19 has introduced ref callback cleanup functions, making ref callbacks a viable alternative to effects.

A much better pattern is to be consistent and use the same approach as in #7638:

const ref = useRef<HTMLElement>(null)
const combinedRef = useCombinedRefs(ref, forwardedRef)

return <div ref={combinedRef} />

Note, however, that I did not deprecate useProvidedRefOrCreate or remove all calls to it. There are still some valid use cases where refs can be passed in to refer to elements rendered outside the component. We most often see this in anchor refs. There are also some utility hooks that can create a ref internally or use a passed ref. These two cases are still valid; the case I'm targeting here is specifically around forwarded refs.

Changelog

New

Changed

Removed

Rollout strategy

  • Patch release
  • Minor release
  • Major release; if selected, include a written rollout or migration plan
  • None; if selected, include a brief description as to why

Testing & Reviewing

Merge checklist

@changeset-bot

changeset-bot Bot commented Mar 9, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 7d5e493

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions Bot added the integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm label Mar 9, 2026
@github-actions

github-actions Bot commented Mar 9, 2026

Copy link
Copy Markdown
Contributor

⚠️ Action required

👋 Hi, this pull request contains changes to the source code that github/github-ui depends on. If you are GitHub staff, test these changes with github/github-ui using the integration workflow. Or, apply the integration-tests: skipped manually label to skip these checks.

@github-actions github-actions Bot requested a deployment to storybook-preview-7644 March 9, 2026 15:33 Abandoned
@github-actions github-actions Bot temporarily deployed to storybook-preview-7644 March 9, 2026 15:43 Inactive
@github-actions github-actions Bot requested a deployment to storybook-preview-7644 March 9, 2026 15:51 Abandoned
@github-actions github-actions Bot temporarily deployed to storybook-preview-7644 March 9, 2026 16:01 Inactive
Comment thread packages/react/src/Checkbox/Checkbox.tsx Outdated
@iansan5653 iansan5653 force-pushed the replace-use-provided-ref-or-create branch from 422c4e4 to 006cc27 Compare March 17, 2026 17:01
@iansan5653 iansan5653 changed the base branch from create-use-combined-refs to update-use-merged-refs March 17, 2026 17:01
Base automatically changed from update-use-merged-refs to main March 18, 2026 19:40
@github-actions github-actions Bot requested a deployment to storybook-preview-7644 March 24, 2026 18:55 Abandoned
@primer

primer Bot commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

🤖 Lint issues have been automatically fixed and committed to this PR.

@github-actions github-actions Bot requested a deployment to storybook-preview-7644 June 26, 2026 21:55 Abandoned
@github-actions github-actions Bot requested a deployment to storybook-preview-7644 June 26, 2026 22:04 Abandoned
@github-actions github-actions Bot temporarily deployed to storybook-preview-7644 June 26, 2026 22:14 Inactive
@primer

primer Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

🤖 Lint issues have been automatically fixed and committed to this PR.

@github-actions

Copy link
Copy Markdown
Contributor

Uh oh! @primer-integration[bot], at least one image you shared is missing helpful alt text. Check #7644 (comment) to fix the following violations:

  • Images should have alternate text (alt text) at line 6

Alt text is an invisible description that helps screen readers describe images to blind or low-vision users. If you are using markdown to display images, add your alt text inside the brackets of the markdown image.

Learn more about alt text at Basic writing and formatting syntax: images on GitHub Docs.

🤖 Beep boop! This comment was added automatically by github/accessibility-alt-text-bot.

5 similar comments
@github-actions

Copy link
Copy Markdown
Contributor

Uh oh! @primer-integration[bot], at least one image you shared is missing helpful alt text. Check #7644 (comment) to fix the following violations:

  • Images should have alternate text (alt text) at line 6

Alt text is an invisible description that helps screen readers describe images to blind or low-vision users. If you are using markdown to display images, add your alt text inside the brackets of the markdown image.

Learn more about alt text at Basic writing and formatting syntax: images on GitHub Docs.

🤖 Beep boop! This comment was added automatically by github/accessibility-alt-text-bot.

@github-actions

Copy link
Copy Markdown
Contributor

Uh oh! @primer-integration[bot], at least one image you shared is missing helpful alt text. Check #7644 (comment) to fix the following violations:

  • Images should have alternate text (alt text) at line 6

Alt text is an invisible description that helps screen readers describe images to blind or low-vision users. If you are using markdown to display images, add your alt text inside the brackets of the markdown image.

Learn more about alt text at Basic writing and formatting syntax: images on GitHub Docs.

🤖 Beep boop! This comment was added automatically by github/accessibility-alt-text-bot.

@github-actions

Copy link
Copy Markdown
Contributor

Uh oh! @primer-integration[bot], at least one image you shared is missing helpful alt text. Check #7644 (comment) to fix the following violations:

  • Images should have alternate text (alt text) at line 6

Alt text is an invisible description that helps screen readers describe images to blind or low-vision users. If you are using markdown to display images, add your alt text inside the brackets of the markdown image.

Learn more about alt text at Basic writing and formatting syntax: images on GitHub Docs.

🤖 Beep boop! This comment was added automatically by github/accessibility-alt-text-bot.

@github-actions

Copy link
Copy Markdown
Contributor

Uh oh! @primer-integration[bot], at least one image you shared is missing helpful alt text. Check #7644 (comment) to fix the following violations:

  • Images should have alternate text (alt text) at line 6

Alt text is an invisible description that helps screen readers describe images to blind or low-vision users. If you are using markdown to display images, add your alt text inside the brackets of the markdown image.

Learn more about alt text at Basic writing and formatting syntax: images on GitHub Docs.

🤖 Beep boop! This comment was added automatically by github/accessibility-alt-text-bot.

@github-actions

Copy link
Copy Markdown
Contributor

Uh oh! @primer-integration[bot], at least one image you shared is missing helpful alt text. Check #7644 (comment) to fix the following violations:

  • Images should have alternate text (alt text) at line 6

Alt text is an invisible description that helps screen readers describe images to blind or low-vision users. If you are using markdown to display images, add your alt text inside the brackets of the markdown image.

Learn more about alt text at Basic writing and formatting syntax: images on GitHub Docs.

🤖 Beep boop! This comment was added automatically by github/accessibility-alt-text-bot.

@primer

primer Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

🤖 Lint issues have been automatically fixed and committed to this PR.

1 similar comment
@primer

primer Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

🤖 Lint issues have been automatically fixed and committed to this PR.

@iansan5653

Copy link
Copy Markdown
Contributor Author

The integration CI appears to be failing because of a tooltip (which was previously broken by bad refs) getting in the way of an Escape keypress event when dragging and dropping. Fixed downstream by explicitly disabling the tooltip in that scenario in https://github.com/github/github-ui/pull/26124

@primer-integration

primer-integration Bot commented Jul 10, 2026

Copy link
Copy Markdown

Integration test results from github/github-ui PR:

Failed  CI   Failed
Running  VRT   Running
Waiting  Projects   Waiting

CI check runs linting, type checking, and unit tests. Check the workflow logs for specific failures.

Need help? If you believe this failure is unrelated to your changes, please reach out to the Primer team for assistance.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Canary Release Apply this label when you want CI to create a canary release of the current PR integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm integration-tests: skipped manually Changes in this PR do not require an integration test skip changeset This change does not need a changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants