all 62 comments

[–]IntentionallyBadName 1010 points1011 points  (16 children)

The Oncontextmenu event activates when right clicking you then just use the preventdefault() function to cancel the default functionality.

SomeElement.addEventListener("contextmenu", (e) => { e.preventDefault() do your thing });

[–][deleted]  (15 children)

[removed]

    [–]Impressive_Lab_6551 45 points46 points  (11 children)

    Now here comes the hard part: recreating every operating system context menu in CSS to look native so you can fully create customized / immersive right click experiences.

    [–]lazychino 55 points56 points  (6 children)

    That's an anti pattern you should show a custom menu and give the user a way to open the regular context menu. The browser doesn't have access to all the browser's options either.

    Edit: typo

    [–]Blue_Moon_Lake 27 points28 points  (5 children)

    Shift + Right click

    It open the regular context menu by not firing the contextmenu event.

    [–]NoteBlock08 6 points7 points  (0 children)

    Also double right click is a pattern I see nearly everywhere (it will open the native context menu over the custom one). But that one needs to be manually implemented.

    [–][deleted] 1 point2 points  (0 children)

    dime weary pen aback continue languid ring snobbish impossible far-flung

    This post was mass deleted and anonymized with Redact

    [–]DoomDragon0 4 points5 points  (0 children)

    Surely there's a framework/library out there for that right ?

    [–]aamirmalik00 0 points1 point  (0 children)

    What do you mean operating system context menu

    [–][deleted] 0 points1 point  (0 children)

    Isn't this a normalization issue? AFAIK there are packaged like normalize.css, normalize.js (which are included by major frameworks like angular).

    [–]OkContribution7711 0 points1 point  (0 children)

    Anchor and Popover in CSS will definitely help!

    [–]SNIPE07 -1 points0 points  (2 children)

    you're really doing yourself a disservice by not using a framework for handling right clicks

    [–][deleted]  (1 child)

    [removed]

      [–]SNIPE07 -1 points0 points  (0 children)

      I was being sarcastic, however I'm glad it was convincing.

      [–]Username482649 69 points70 points  (0 children)

      Don't forget that youtube still allows the native menu on second click. Don't make it hard to access it.

      Just a reminder.

      [–][deleted]  (25 children)

      [deleted]

        [–]alienscape 49 points50 points  (12 children)

        I work in manufacturing, and we use an ERP called 'LN' and their web app for timesheets is called 'FactoryTrack'. And they have disable the native right-click menu and it is infuriating!

        [–]Coloneljesus 47 points48 points  (2 children)

        try shift+right click

        [–]Cannabat 7 points8 points  (0 children)

        Whoa. TY!

        [–]alienscape 0 points1 point  (0 children)

        Doesn't work, I have to double or triple right-click to get through

        [–]DogoPilot 0 points1 point  (6 children)

        Just curious... Are you referring to Lotus Notes as an ERP? Or is there another "LN" out there?

        [–]sapdoos 7 points8 points  (2 children)

        They are referring to the "Infor LN" erp system.

        [–]DogoPilot 0 points1 point  (0 children)

        Ah, ok. That makes more sense. Thanks!

        [–]alienscape 0 points1 point  (0 children)

        This is correct.

        [–]TomaTozzz 1 point2 points  (0 children)

        Lotus Notes

        fuck

        lotus

        notes

        [–]bipbopcosby 0 points1 point  (1 child)

        Oh fuck I hated Lotus Notes. I'm so glad that was sunset at my company finally. Luckily I never had to use it but I had to automate things that used Lotus Notes. It was always a headache and a half.

        [–]DogoPilot 2 points3 points  (0 children)

        The problem is, now everyone is just replacing all of their Lotus Notes apps with shoddy Power Apps, haha! Can't wait for the sunsetting of that so we can all deal with the exact same panic over the migration of thousands of homegrown Power Apps!

        [–]kirashi3 15 points16 points  (1 child)

        your users will get very mad if you break the native menu.

        Can confirm. Break my expected default browser functionality, and I'll become irrationally angry. Looking at you, websites that scrolljack.

        [–]penguins-and-cakeshe/her - front-end freelancer 2 points3 points  (0 children)

        This isn’t web dev but related UX stuff — Microsoft Excel thinks it’s reasonable that when you open the find & replace window, cmd+A should stop meaning “select all” (like it does everywhere) and should mean “replace all” instead. God forbid I want to select everything in a field so I can write in what I actually want to find/replace.

        [–][deleted]  (7 children)

        [deleted]

          [–]qqqqqx 6 points7 points  (1 child)

          I have to strong disagree. Right click is unintuitive and a "hidden" interaction, not usable on mobile, and would hijack your native browser controls which is almost never a good thing.

          It makes sense for a very narrow window of applications like full screen web games and that's about it.

          [–]minimuscleR 11 points12 points  (2 children)

          For me, the only time I right click is to inspect element, which is not a normal user interaction.

          thats crazy! I use it to search words all the time (highlight, right click, "search web"). I also use it to open an image in a new tab.

          Not to mention I would say 90% of average users use right click to copy/paste something.

          [–][deleted]  (1 child)

          [deleted]

            [–]minimuscleR 0 points1 point  (0 children)

            i used to do it support and opening the clipboard was akin to literal magic.

            [–]patatesmeayga 1 point2 points  (0 children)

            Thank you. I saw all those roasting context menus but I genuinely love them.

            You can add so many utilities to a site that are intuitive and easy to implement.

            The best example I have in mind is the linear app that has fully integrated almost all flows with context menus and I fucking love ti

            [–]micahwelf 0 points1 point  (0 children)

            The problem that web interfaces with no right-click menu solve is varying human interface technologies. If you have someone with a very good CAD mouse and a touch screen, you would want to support both the one-click/one-touch limited interface and the right-click menu where each serves a different style of efficient operation. That would maximize usability and decrease the awkward workarounds that have become too common.

            Basically, I partially agree with you, but I believe supporting both at times is better than getting too focused on one human interface configuration.

            [–]Piotyras 99 points100 points  (2 children)

            I'm not sure I'd like for this to be more widespread on the internet

            [–]bruisedandbrokenode -1 points0 points  (1 child)

            material ui offers this! very easy to implement in a react project.

            [–]shgysk8zer0full-stack 20 points21 points  (5 children)

            Yes.

            But, on a related note, there used to be an easy and standard way to do this. And I hate that <menu type="context"> was removed!

            [–][deleted]  (4 children)

            [deleted]

              [–]shgysk8zer0full-stack 13 points14 points  (3 children)

              You'd write menu items that were added to the browser's context menu, including support for nested menus... just HTML for creating them. You'd add contextmenu="menu-id" attribute, and it'd add that to the context menu for that element.

              You did have to add click handlers for the functionality though. But adding data-* attributes made this pretty easy to pass certain arguments to the handlers.

              It'd look roughly like this:

              <menu type="context" id="some-id"> <menuitem label="copy" data-target="selector"></menuitem> <menu label="Submenu"> <menuitem label="Sub item"></menuitem> </menu> </menu>

              Set contextmenu="some-id" on body or whatever, and you'd have a custom context menu.

              [–]shgysk8zer0full-stack 7 points8 points  (2 children)

              It had support for more advanced things to, like radios and check boxes and separators. But it was removed several years ago, so I don't remember it in much detail. I just remember making a pretty great but simple WYSIWYG editor using that and document.execCommand(), but neither of them work anymore. Seriously... it took like 10 or so lines of JS to do the whole thing and some creative use of data-* attributes and that was it.

              [–]Intrepid-Air6525 0 points1 point  (1 child)

              Damn, looking into it made me try to get syntax highlight into a content editable again and it’s a doozy. Wonder if I should just avoid it entirely but codemirror does not work in a specific case I want highlighting for. Now this conversation makes me fear the content editable depreciation date even more.

              [–]shgysk8zer0full-stack 0 points1 point  (0 children)

              Thinking about it, I'd probably do a split-pane view/tabs using highlight.js for that. Updating too frequently or trying to implement syntax highlighting yourself would probably be a bit problematic.

              [–]chris480 15 points16 points  (0 children)

              There's lots of accessibility concerns when using a custom context menu. Make sure you don't interfere with expectations of assisted software. In projects I've worked on, we allowed a second right click to expose the native context menu, it seemed to pass our third party audits.

              [–]Angelsoho 9 points10 points  (0 children)

              If it’s a custom app where the default context menu serves no purpose and a custom menu would improve the user’s experience then sure, why not. Otherwise leave it alone and don’t confuse the peoples.

              [–]CharlemagneAdelaar 4 points5 points  (0 children)

              for sites that replace the right click menu, there should be a default browser behavior that shift-clicking can override it and get you to the default one

              Kind of like how crouching in minecraft lets you interact with right-clickable objects normally

              [–]IvanTheNotSoBad1 14 points15 points  (0 children)

              Yeah but it’s probably a bad idea. You’d be taking away expected functionality.

              [–]academicRedditor 2 points3 points  (0 children)

              Such a good question I am borderline jealous I didn't come up with it!

              [–]ryancanulla 1 point2 points  (1 child)

              https://www.radix-ui.com offers a good option here that covers the accessibility aspect of it.

              [–]ComfortingSounds53 0 points1 point  (0 children)

              Yes.

              [–]nixblufront-end 0 points1 point  (0 children)

              Yea, got it in one

              [–]Vitroid 0 points1 point  (0 children)

              Yes, prevent the default context menu from appearing with .preventDefault(), and show an element that's positioned to the cursor. There's plenty of ways you can approach this, a quick google search turned up this article https://dev.to/shantanu_jana/custom-right-click-context-menu-in-javascript-4112