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

Fix FormTokenField suggestions broken scrollbar when __experimentalExpandOnFocus is defined #56426

Merged
merged 6 commits into from
Nov 27, 2023

Conversation

talldan
Copy link
Contributor

@talldan talldan commented Nov 22, 2023

Fixes #56419

What?

Fixes an issue with FormTokenField whereby when the __experimentalExpandOnFocus prop is defined, the suggestions list can't be scrolled by dragging it with the mouse.

How?

The issue seemed to be that the isExpanded prop was being repeatedly switched between true and false. This remounting of the suggestions was causing the scroll position to be fixed in place.

Notably the onFocusHandler handler checks both input focus and container focus, while the onBlur handler didn't, so the fix was to replicate that in onBlur to prevent isExpanded being set to false.

(note: I'm not sure if this is a good enough explanation, as I don't know why it works ok normally 😄 ).

Testing Instructions

  1. Pop open the storybook at the FormTokenField component (https://wordpress.github.io/gutenberg/?path=/docs/components-formtokenfield--docs)
  2. Enable __experimentalExpandOnFocus
  3. Click on the input and try scrolling the suggestions by dragging on the scrollbar

Testing Instructions for Keyboard

I don't think this is reproducible using the keyboard

Screenshots or screencast

Before

Kapture.2023-11-22.at.18.18.29.mp4

After

Kapture.2023-11-22.at.18.14.58.mp4

@talldan talldan added [Type] Bug An existing feature does not function as intended [Feature] UI Components Impacts or related to the UI component system labels Nov 22, 2023
@talldan talldan self-assigned this Nov 22, 2023
@talldan talldan requested a review from ajitbohra as a code owner November 22, 2023 10:19
@talldan
Copy link
Contributor Author

talldan commented Nov 22, 2023

I have this obtuse typescript error that I'll need some help resolving!

packages/components/src/form-token-field/index.tsx:662:5 - error TS2322: Type '{ onChange: ((event: { value: string; }) => void) | undefined; ref: RefObject<HTMLInputElement>; instanceId: number; autoCapitalize: string | undefined; ... 7 more ...; selectedSuggestionIndex: number; }' is not assignable to type 'Omit<Pick<DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>, "key" | keyof InputHTMLAttributes<...>>, "children" | ... 1 more ... | keyof TokenInputProps>'.
  Types of property 'onBlur' are incompatible.
    Type '(event: FocusEvent) => void' is not assignable to type 'FocusEventHandler<HTMLInputElement>'.
      Types of parameters 'event' and 'event' are incompatible.
        Type 'FocusEvent<HTMLInputElement, Element>' is missing the following properties from type 'FocusEvent': detail, view, which, initUIEvent, and 11 more.

662    <TokenInput

@talldan talldan requested review from ciampo, a team, glendaviesnz, kevin940726 and aaronrobertshaw and removed request for ajitbohra and ciampo November 22, 2023 10:24
@t-hamano
Copy link
Contributor

Just wanted to let you know that with this PR, #50514 can also be closed.

@glendaviesnz
Copy link
Contributor

This tested well for me. I pushed a fix for the type issue, but probably needs someone with more experience with the components typing to take a look at it. It seems like the onFocus method is expecting the native FocusEvent but onBlur the react event version - but not sure why this is.

@aaronrobertshaw
Copy link
Contributor

It seems like the onFocus method is expecting the native FocusEvent but onBlur the react event version - but not sure why this is.

At a quick glance, the focus and key down/up handlers are applied to the wrapping div rather than the input.

@talldan talldan changed the title Fix FormTokenField suggestions broke scrollbar when __experimentalExpandOnFocus is defined Fix FormTokenField suggestions broken scrollbar when __experimentalExpandOnFocus is defined Nov 23, 2023
@talldan talldan force-pushed the fix/form-token-field-suggestions-unscrollable branch from f9a413c to bbfd447 Compare November 23, 2023 02:13
Copy link
Contributor

@ciampo ciampo left a comment

Choose a reason for hiding this comment

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

Thank you for finding this bug and working on a solution! It's interesting that the only way to reproduce seems to be to click and drag the scrollbar — using the keyboard or the mousewheel seems to work fine.

Anyway, the changes are testing well for me (tested in Chrome/Safari/Firefox on MacOS).

I have this obtuse typescript error that I'll need some help resolving!

This definitely bit me in the past — FocusEvent and React.FocusEvent!

It seems like the onFocus method is expecting the native FocusEvent but onBlur the react event version - but not sure why this is.

I took a look and I believe that both the onFocus prop and the onFocusHandler also should be typed using ReactFocusEvent (instead of the native one). I logged the event object and it definitely looks like a react synthetic event.

@talldan talldan force-pushed the fix/form-token-field-suggestions-unscrollable branch from bbfd447 to 8281f8d Compare November 27, 2023 02:00
@talldan
Copy link
Contributor Author

talldan commented Nov 27, 2023

I took a look and I believe that both the onFocus prop and the onFocusHandler also should be typed using ReactFocusEvent (instead of the native one). I logged the event object and it definitely looks like a react synthetic event.

Thanks for the feedback Marco, good catch! I've updated that now.

Copy link
Contributor

@glendaviesnz glendaviesnz left a comment

Choose a reason for hiding this comment

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

Tested well for me, and scrolling categories works with scroll wheel and when dragging scrollbar.

Looks good to go once changelog conflict resolved.

@talldan talldan force-pushed the fix/form-token-field-suggestions-unscrollable branch from 7d8f583 to 910a69f Compare November 27, 2023 03:53
@talldan talldan enabled auto-merge (squash) November 27, 2023 03:53
@talldan talldan merged commit 2d4000f into trunk Nov 27, 2023
52 checks passed
@talldan talldan deleted the fix/form-token-field-suggestions-unscrollable branch November 27, 2023 05:58
@github-actions github-actions bot added this to the Gutenberg 17.2 milestone Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] UI Components Impacts or related to the UI component system [Type] Bug An existing feature does not function as intended
Projects
None yet
5 participants