Skip to content
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

feat(useContextMenu): add ref support #13210

Merged

Conversation

janhassel
Copy link
Member

When working on #13209 I noticed that the useContextMenu currently only supports DOM elements (or window or document) as its trigger which means developers would need to wrap useContextMenu in a useEffect in order to pass it the correct ref.current at all times.

Since this feels a bit cumbersome and the useContextMenu hook already uses a useEffect with trigger as its dependency, I updated the hook to directly support react refs.

Changelog

New

  • Added support for react refs as trigger argument in useContextMenu

Testing / Reviewing

function TestComponent() {
  const el = useRef(null);
  const menuProps = useContextMenu(el);

  return (
    <>
      <div ref={el}>
        Right click this element
      </div>
      <Menu {...menuProps}>
        <MenuItem label="Edit" />
        <MenuItem label="Delete" kind="danger" />
      </Menu>
    </>
  );
};

Menu should appear when right-clicking on the div, but not when right-clicking anywhere else on the screen.

@janhassel janhassel requested a review from a team as a code owner February 22, 2023 14:52
@janhassel janhassel changed the title Usecontextmenu ref support feat(useContextMenu): add ref support Feb 22, 2023
@netlify
Copy link

netlify bot commented Feb 22, 2023

Deploy Preview for carbon-components-react ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit b744cb2
🔍 Latest deploy log https://app.netlify.com/sites/carbon-components-react/deploys/63f7bbaeca8e1d000822df5e
😎 Deploy Preview https://deploy-preview-13210--carbon-components-react.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

Copy link
Collaborator

@francinelucca francinelucca left a comment

Choose a reason for hiding this comment

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

lgtm

@kodiakhq kodiakhq bot merged commit f32a1b3 into carbon-design-system:main Feb 23, 2023
@janhassel janhassel deleted the usecontextmenu-ref-support branch February 24, 2023 07:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants