Context sensitive context menu


Maybe I just didn’t understand the Docs, but it doesn’t seem like there is a way to add commands to the context menu that are context sensitive - for instance that only apply to a given scope by using a CSS selector. It says you attach to DOM elements but in practice it seems only atom core DOM actually works, e.g. you can attach to atom-text-editor, but no . These doesn’t even seem a way to make language-specific context menus

Has anyone managed to make the context menu more context sensitive?


You can take a look at what I do in pigments:

I do attach a context menu to a text editor and I do contextualize its visibility using the shouldDisplay option. But it’s true you won’t be able to target a specific scope using the selector as it won’t traverse the shadow DOM (and ::shadow isn’t supported in these pseudo selectors).

If you just want to target a text editor for a specific language you should be able to do so with a selector like this atom-text-editor[data-grammar="source css"] (up to you to find the value of the data-grammar attribute for the grammar you want to target).

If you need to contextualize even more, you can still retrieve the scope at mouse coordinates in the shouldDisplay function.


super helpful, thanks