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

[NoJira][BpkSlider] - Fix native event handling for the Slider #3645

Merged
merged 14 commits into from
Oct 25, 2024

Conversation

metalix2
Copy link
Contributor

@metalix2 metalix2 commented Oct 17, 2024

Description

Resolve native event bug where dragging over many slider steps result in multiple change events.

This change fixes that once a users mousedown event is lifted the "click" will then register a final change event.

Works for keyboard as well the "keyup" event where a user can hold down the arrow key and the change sent at that moment.

Tested across Chrome, Firefox and Safari.

OldSlider.ChangeEvents.mov
Slider.ChangeEvent.mov

Remember to include the following changes:

  • Ensure the PR title includes the name of the component you are changing so it's clear in the release notes for consumers of the changes in the version e.g [KOA-123][BpkButton] Updating the colour
  • README.md (If you have created a new component)
  • Component README.md
  • Tests
  • Accessibility tests
    • The following checks were performed:
      • Ability to navigate using a keyboard only
      • Zoom functionality (Deque University explanation):
        • The page SHOULD be functional AND readable when only the text is magnified to 200% of its initial size
        • Pages must reflow as zoom increases up to 400% so that content continues to be presented in only one column i.e. Content MUST NOT require scrolling in two directions (both vertically and horizontally)
      • Ability to navigate using a screen reader only
  • Storybook examples created/updated
  • For breaking changes or deprecating components/properties, migration guides added to the description of the PR. If the guide has large changes, consider creating a new Markdown page inside the component's docs folder and link it here

@metalix2 metalix2 added patch Patch production bug javascript Pull requests that update Javascript code labels Oct 17, 2024
Copy link

Visit https://backpack.github.io/storybook-prs/3645 to see this build running in a browser.

Copy link

github-actions bot commented Oct 17, 2024

Warnings
⚠️

Package source files (e.g. packages/package-name/src/Component.js) were updated, but snapshots weren't. Have you checked that the tests still pass?

⚠️

Package source files (e.g. packages/package-name/src/Component.tsx) were updated, but type files weren't. Have you checked that no types have changed?

Browser support

If this is a visual change, make sure you've tested it in multiple browsers.

Generated by 🚫 dangerJS against 0fe8e51

Copy link

Visit https://backpack.github.io/storybook-prs/3645 to see this build running in a browser.

Copy link

Visit https://backpack.github.io/storybook-prs/3645 to see this build running in a browser.

1 similar comment
Copy link

Visit https://backpack.github.io/storybook-prs/3645 to see this build running in a browser.

@metalix2 metalix2 changed the title [NoJira] - Fix native event handling for the Slider [NoJira][BpkSlider] - Fix native event handling for the Slider Oct 18, 2024
packages/bpk-component-slider/src/BpkSlider.tsx Outdated Show resolved Hide resolved
// needed on document as users can drag the thumb while out of the thumb elements mouse area
document.addEventListener(
'click',
() => interactionEndHandler(value as number),
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need to cast the value? Shouldn't interactionEndHandler expect to handle all the diferent types of values that come from the event listener?

Copy link
Contributor

Choose a reason for hiding this comment

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

General comment on this area of the code is that the nested nature of the options/functions etc make it a little difficult to read, is there a way to make it easier? I think, if we are able to not have to pass the casting there is a small increase in readability, eg:

document.addEventListener('click', interactionEndHandler, options)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why do we need to cast the value? Shouldn't interactionEndHandler expect to handle all the diferent types of values that come from the event listener?

I thought we had originally needed them but the value remains the same once instanciated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

General comment on this area of the code is that the nested nature of the options/functions etc make it a little difficult to read, is there a way to make it easier? I think, if we are able to not have to pass the casting there is a small increase in readability, eg:

I'll see If I can refactor it a little.

Comment on lines 170 to 172
['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown'].includes(
e.key,
) && interactionEndHandler(value as number);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this logic not be within the handler itself?
Even if there are different types of handlers with shared common logic:

// common & shared
function interactionValueParsing(){}

function keyupInteractionHandler (e){
   if(!['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown'].includes( e.key)) return;
   const value = interactionValueParsing(e)
   // stuff
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well It would mean checking for key events when it was a click event. This just sort of wraps and takes care of the custom keyboard logic.

Copy link
Contributor

Choose a reason for hiding this comment

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

It would mean checking for key events when it was a click event

I don't understand. Why? Don't we attach the handler twice, once for click and once for keyup - these could just be different handlers, no?

packages/bpk-component-slider/src/form-test.tsx Outdated Show resolved Hide resolved
Copy link

Visit https://backpack.github.io/storybook-prs/3645 to see this build running in a browser.

Copy link

Visit https://backpack.github.io/storybook-prs/3645 to see this build running in a browser.

Copy link

Visit https://backpack.github.io/storybook-prs/3645 to see this build running in a browser.

// needed on document as users can drag the thumb while out of the thumb elements mouse area
signal: controller.signal,
});
thumb.addEventListener('touchend', interactionEndHandler, {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

touchend needed for mobile devices 👍 Tested in storybook

Copy link

Visit https://backpack.github.io/storybook-prs/3645 to see this build running in a browser.

Copy link

Visit https://backpack.github.io/storybook-prs/3645 to see this build running in a browser.

Copy link

Visit https://backpack.github.io/storybook-prs/3645 to see this build running in a browser.

@metalix2 metalix2 merged commit 6d53c2e into main Oct 25, 2024
9 checks passed
@metalix2 metalix2 deleted the SliderNativeEventFixes branch October 25, 2024 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
javascript Pull requests that update Javascript code patch Patch production bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants