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

implement #27 - adds an api for more direct usage #29

Merged
merged 3 commits into from
Oct 4, 2021

Conversation

jkorrek
Copy link
Contributor

@jkorrek jkorrek commented Oct 1, 2021

As promised, a pull request to add apis for controlling a context menus lifecycle directly.

I think I got to all the points you mentioned in #27.

One note on the implementation:
As suggested, I merged the private closeMenu method with the new public hide function. I choose not to expose the optional menu parameter in the public interface. As far as I can tell, this is only really used when closing a submenu, so I don't think we need in the public api.

Let me know what you think.

src/ctxmenu.ts Outdated
}
}

private openMenu(ctxMenu: CTXMenu, e: MouseEvent) {
Copy link
Owner

Choose a reason for hiding this comment

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

I would move the content of openMenu back to the show function if possible, to avoid confusion (as discussed before)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

openMenu is gone :)

src/ctxmenu.ts Outdated

const newMenu = beforeRender([...ctxMenu], e);
this.menu = this.generateDOM(newMenu, e);
document.body.appendChild(this.menu);
this.openMenu(newMenu, e);
Copy link
Owner

Choose a reason for hiding this comment

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

I'd try replacing this with the show function

src/ctxmenu.ts Outdated
@@ -196,18 +205,33 @@ class ContextMenu implements CTXMenuSingleton {
t.removeEventListener("contextmenu", o.handler);
delete this.cache[target];
}

public show(ctxMenu: CTXMenu, e: MouseEvent) {
Copy link
Owner

Choose a reason for hiding this comment

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

The only thing I don't like about the current implementation of your PR is the necessity to provide this MouseEvent in a public API. This forces the caller to carry around an event. In case of a button press it might be simple enough, but with some complex logic I can imagine this getting cumbersome very quickly. Also the user of this function might want to open a menu in other cases where there's no MouseEvent, too (key presses for example)...

Since subsequently generateDOM will be called, which itself supports an HTMLElement instead of an event, we might want to provide this possibility here, too by overloading the show function.
Providing just the target coordinates or a DOMRect might be handy, too, although this might require some further changes.
In case you need to change the type of the second parameter for generateDOM from HTMLLIElement to HTMLElement, feel free to do so, it doesn't seem to cause any issues.

Copy link
Owner

Choose a reason for hiding this comment

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

Let me know your thoughts on this! 😊

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think adding an overload to the show method using a HTMLElement instead of a MouseEvent is a good idea, especially since it does not require any real effort :)
Directly passing coordinates also sounds interesting, but I would assume that you would usually always have a reference to an event or Element. So I think I would leave that extension for a future pull request ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did change it as described. However I switched to using just a union type on the parameter instead of using overloads. I am not sure if your were using overloads for any particular reason, as far as I can tell, using a union type for the parameter should be enough - let me know if you want to change this back if this was intended for some special reason.

Copy link
Owner

Choose a reason for hiding this comment

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

I'm not so sure, why, either 🤣
I think what I like most about overloads over union types is that the type hints are concise, since it only shows what you are using instead of all possibilities at once.
grafik

No need to change it though. ( I'll do it myself later, if it bothers me...)
I'm having a look right now and if all looks fine, I'll merge the PR!

@nkappler
Copy link
Owner

nkappler commented Oct 4, 2021

I agree that the menu parameter of the hide function should not be exposed.

@nkappler nkappler added this to the 1.4.0 milestone Oct 4, 2021
@nkappler nkappler linked an issue Oct 4, 2021 that may be closed by this pull request
- always use show method instead of private openMenu method
- show method should also work with a HTMLElement instead of a MouseEvent
@nkappler nkappler merged commit b4ae481 into nkappler:master Oct 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Usage without attaching to specific elements
2 participants