question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ContextMenu & transform

See original GitHub issue

Hi fkhadra,

I got next problem: I have the parent element with style transform. When I add context menu on child element, then its behaviour incorrectly.

Please, find example below.

import React from 'react';
import { ContextMenu, Item, ContextMenuProvider } from 'react-contexify';
import './App.css';

import 'react-contexify/dist/ReactContexify.min.css' 


export default class App extends React.Component<any, any> {
  childrenShow: boolean;
  constructor(props) {
    super(props);
    this.childrenShow = true;

    this.handleMenuSelect = this.handleMenuSelect.bind(this);
    this.handleMenuUnSelect = this.handleMenuUnSelect.bind(this);
    this.handleMenuDelete = this.handleMenuDelete.bind(this);
  }

  render() {
    if ( this.childrenShow ) {
      return (
      <div className="App" style={{marginTop: '200px', transform: 'translate(0)'}}>
        <ContextMenuClass handleMenuSelect={this.handleMenuSelect} handleMenuUnSelect={this.handleMenuUnSelect} handleMenuDelete={this.handleMenuDelete} />
      </div>
      );
  }

    return (
      <div className="App">
      Hiden
    </div>
    );  
  }


  handleMenuUnSelect() {
    this.childrenShow = false;
    console.log('unSelect triggered');
  }

  handleMenuSelect() {
    this.childrenShow = false;
    console.log('select triggered');
  }

  handleMenuDelete() {
    this.childrenShow = false;
    console.log('delete triggered');
    this.forceUpdate();
  }
}

export class ContextMenuClass extends React.Component<any, any> {
  render() {
    return (
      <div>
        <ContextMenuProvider id={'context_menu'}>
        ContextMenu
      </ContextMenuProvider>
      <ContextMenu theme="dark" id={'context_menu'}>
        <Item onClick={this.props.handleMenuUnSelect} disabled>Unselect</Item>
        <Item onClick={this.props.handleMenuSelect} disabled={true}>select</Item>
        <Item onClick={this.props.handleMenuDelete}>Delete</Item>
      </ContextMenu>  
    </div>
  )
  }
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:39 (18 by maintainers)

github_iconTop GitHub Comments

1reaction
fkhadracommented, Sep 3, 2017

I pushed the new version. You won’t get the “Did you forgot to bind an event” message.

@brezetsky regarding your snippet won’t work, only the parent menu will be displayed. To fix it move the <ContextNestedMenuClass /> as follow:

class ContextMenuClass extends React.Component {
  render() {
    const fn = () => { };
    return (
      <div>
        <ContextMenuProvider id={'context_menu'}>
          ContextMenu
      <ContextNestedMenuClass /> -> Remove this
      </ContextMenuProvider>
        <ContextMenu theme="dark" id={'context_menu'}>
          <Item onClick={fn} disabled>Unselect</Item>
          <Item onClick={fn} disabled={true}>select</Item>
          <Item onClick={fn}>Delete</Item>
        </ContextMenu>
{**Put the  <ContextNestedMenuClass /> here instead**}
      </div>
    )
  }
}

Sorry for the late release btw

0reactions
ekilahcommented, Sep 24, 2021

I ran into the same issue here, can’t tell if it was resolved and regressed from the old discussion above or not, but I was able to fix it by throwing the <Menu> render inside a React Portal:

        {ReactDOM.createPortal(
          this.renderContextMenu(), // this renders the `<Menu>`
          document.body
        )}
Read more comments on GitHub >

github_iconTop Results From Across the Web

context menu position with transform - CodeSandbox
context menu position with transform. css transform will create new stacking context and will mess up with right click x and y when...
Read more >
CSS transform: translate() on Parent Breaks Context Menu #147
Context menu appears next to cursor when right-clicking. Actual Behavior. Context menu shifted twice as much as the parent's transform away from ...
Read more >
Is there a way to convert a Controls.ContextMenu to a Forms ...
No - the controls are for completely different platforms (Winforms vs WPF). There is no "conversion" between the two. Either use a different...
Read more >
ContextMenu Class (System.Windows.Controls)
Gets or sets transform information that affects the rendering position of this element. This is a dependency property. (Inherited from UIElement).
Read more >
Using ContextMenu and ContextMenuItem in Unity 3D
There are a lot of options available to you when you want to test your project in Unity3D and validate that your code...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found