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

feat: add preventClickOnDrag option to PanInput #199

Merged
merged 6 commits into from
Aug 30, 2022

Conversation

malangfox
Copy link
Contributor

@malangfox malangfox commented Aug 16, 2022

Details

This adds the option to cancel the click event when a valid drag occurs in PanInput and RotatePanInput.

We currently implemented this feature on Flicking that uses PanInput.
However, there are cases when you want to use this preventClickOnDrag feature in other UIs that uses PanInput.

After we add preventClickOnDrag to PanInputOption, Flicking can exclude implementation about preventClickOnDrag and handle it by PanInputOption.

@daybrush
Copy link
Member

There are a few things to check.

  1. Prevent the click event of the element's parent? vs Prevent click event on child inside vs element? vs prevent everything?
  2. What is the need for the preventClickOnDragStart option? Option to block clicks as soon as you start dragging.
  3. Check the process in which the click event occurs in touch.

@@ -353,6 +358,9 @@ export class PanInput implements InputType {
this._observer = observer;
this._enabled = true;
this._activeEvent = activeEvent;
if (this.options.preventClickOnDrag) {
this.element?.addEventListener("click", this._preventClickWhenDragged);
Copy link
Member

Choose a reason for hiding this comment

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

I think it shouldn't be activated if the element doesn't exist.
Like,

if (!activeEvent || !this.element) {
  return;
}

Actually, I wonder in what case the element be null, as it receives the element in the constructor.
Please check whether the type is incorrect for this case.

Copy link
Contributor Author

@malangfox malangfox Aug 26, 2022

Choose a reason for hiding this comment

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

This seems to be unnecessary since existence of the element should be guaranteed by user at the timing of _attachElementEvent, unlike _detachElementEvent which element may not exist while input is disconnected.
I think we can throw an error if the target element does not exist at _attachElementEvent.

Copy link
Member

@daybrush daybrush left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -359,14 +359,14 @@ export class PanInput implements InputType {
this._enabled = true;
this._activeEvent = activeEvent;
if (this.options.preventClickOnDrag) {
this.element?.addEventListener("click", this._preventClickWhenDragged);
this.element.addEventListener("click", this._preventClickWhenDragged);
Copy link
Member

Choose a reason for hiding this comment

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

Subtract the frequently used this property with a constant.

const element = this.element;

Copy link
Member

@WoodNeck WoodNeck left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines 214 to 217
const element = this.element;
element?.removeEventListener("keydown", this._onKeydown, false);
element?.removeEventListener("keypress", this._onKeydown, false);
element?.removeEventListener("keyup", this._onKeyup, false);
Copy link
Member

Choose a reason for hiding this comment

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

You'd rather do it like:

const element = this.element;
if (element) {
  element.removeEventListener("keydown", this._onKeydown, false);
  element.removeEventListener("keypress", this._onKeydown, false);
  element.removeEventListener("keyup", this._onKeyup, false);
}

Because simply, the one you wrote turns like this on tsc compiler:

var element = this.element;
element === null || element === void 0 ? void 0 : element.removeEventListener("keydown", this._onKeydown, false);
element === null || element === void 0 ? void 0 : element.removeEventListener("keypress", this._onKeydown, false);
element === null || element === void 0 ? void 0 : element.removeEventListener("keyup", this._onKeyup, false);

@malangfox malangfox merged commit b66481d into naver:master Aug 30, 2022
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.

None yet

3 participants