-
Notifications
You must be signed in to change notification settings - Fork 6
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
Allow to register an event when the contextual menu is hidden #49
Comments
Thanks for your feedback, that's a very valid feature request. I am currently in the middle of a bigger refactoring, so I will need to investigate whether the implementation for this can be done before the refactoring is finished. I thought of alternate ways to achieve this and while also not very pretty, I came up with this: It's not as simple as a dedidacted API, but maybe it's easier than the MutationObserver. Some questions about your feature request:
I think option 1 is my preferred option, although it is the most effort. I guess the other options may have limitations for menus that are opened via Please let me know your thoughts. |
#1 indeed seems to be the best option. There's already a way to register on events for the menuItems; it would fit with the current patterns. For #2 (and even with the current implementation), what would help is to be able to assign an id to the contextual menu or some custom class. When I tried to find the menu, I realized the "ctxmenu" class was already used by another node in my app. Luckily, it wasn't on the body. If you do add the onClose, I imagine others will want the onShow! :) Great work, I enjoy using your plugin. It is simple and flexible. Allowed me to clean up a lot of code that was used to create our previous menu. |
Thanks for the kind words, simplicity always was - and still is - one of the main goals of this project!
Yes, I also thought about that, although these differ from your use-case in two important ways:
I think I'm going to deprecate the current signature of // deprecated:
declare function attach(target: string, ctxmenu: Array, beforeRender?: (menu: Array, event: MouseEvent) => Array)
declare function update(target: string, ctxmenu?: Array, beforeRender?: (menu: Array, event: MouseEvent) => Array)
// new:
declare function attach(target: string, ctxmenu: Array, config?: {
onBeforeShow?: (menu: Array, event: MouseEvent) => Array;
onShow?: Function,
onBeforeHide?: Function,
onHide?: Function,
// etc. ?
}) This opens up the possibility to have more lifecycle handlers (you mentioned onShow, which basically was the beforeRender, but now we can have handlers before and after the menu is attached to the DOM) and the config object is infinitely expandable, we could also have browser event handlers (like onHover) in an I'm not sure if I'm going to implement all of these explicitly, maybe I'm gonna add a generic This config option will of course also be available for the I am going to build a prototype in the next days and see where it leads me... |
config is a good way to go. Method parameters that become huge is a bad habit in my own app that I'm trying to break! Regarding the attributes member that would be applied to the ctx menu dom, it would solve another of my issues which is to customize the styling for some contextual menus by giving them ids (could use class but you would need to consider this a bit different not to remove your default classes). For the menuItems, I used div only to be able to add a class to it, so those too could benefit from this attribute. It would allow me to simplify my code. But I'm diverting from my original issue! ;) I'd love to help but right now the timing isn't good. However, I'd be happy to test your solution in my environment if needed. |
there's the There is a bunch of stuff I haven't considered initially cause I just needed a super simple context menu utility. There's a bunch of stuff I still have planned and didn't start on, but I've made some progress in the refactoring and might begin a prototype implementation on sunday, alas I'm also very busy at the moment, so it might take a bit. I'll keep you updated 😊 |
adding a generic attribute support would also allow me to remove the The changes discussed so far, including deprecation changes, breaking changes and all the things that have already changed internally during the refactoring (not to mention a few bugs i found) feel like this all should be a major version bump, which I don't want to rush, but I'll try to get a solution ready for you as fast as possible... |
Hi @MissChocoe, I've implemented a rough draft in the above mentioned commit, you can find it on the onHide branch. I've basically implemented all we've disussed so far, all members of the both hide events receive the menu root element, however, they should already be able to know it from when it was attached. I haven't had much time to test these changes myself, but I wanted to get something out of the door for you. I am very interested if this works for you and which of the above options are most useful to you, so please let me know your thoughts :) |
ok, fixed some bugs already... 🤣 |
Thanks! I will try it out today! |
Somehow I missed that comment. Using style value to target elements is very creative! I would not have thought about it. I prefer my solution of adding a small div, looks less hacked!!! I could not use the style attributes directly because the style varies depending on the context. |
onHide works as expected and does fulfill my need. onBeforeHide and onShow are also called properly (and I see that you've already added the dom as a parameter of the onShow!) However, onBeforeShow doesn't seem to work. |
thanks for the feedback. onBeforeShow currently is only called for menus that are permanently registered via I need to test it a bit more and I'll check how much effort it would be to implement the generic attribute config we talked about. I'll probably release an update in the next few days :) |
Do as you see fit. We have working solutions for both (onHide and class attribute) so we will adjust. Using the new attributes config will require a bit more work so it won't be done for our current release but on the next one. Thanks for your work! |
Hi @MissChocoe, I have also written some unit tests for the entire new feature on the refactoring branch (which is also WIP), before cherry-picking the fix to the onHide branch, so I have a good feeling about the robustness of the implementation. I have decided to postpone the attributes feature until the refactoring and all the tests are merged. I assume I'll have the release ready in the next few days. |
Hi @MissChocoe I've just officially released version Happy coding 😄 |
Thank you very much, will include that in our next release! |
I need to perform something when the contextual menu is closed even without any action taken. I used a MutationObserver on my side right now to achieve that but find it a bit ugly. Would be nice to be able to know when a menu displayed with ctxmenu.show is hidden.
The text was updated successfully, but these errors were encountered: