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

WIP Pattern input field #17472

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

brunopagno
Copy link
Contributor

@brunopagno brunopagno commented Dec 16, 2024

Ticket

https://community.openproject.org/projects/document-workflows-stream/work_packages/59999

What are you trying to accomplish?

Fancy input

Screenshots

Screencast.From.2025-01-14.10-36-22.mp4

What approach did you choose and why?

The component was built with rails leveraging stimulus + primer where available. There is minimal custom styling. That said, the component does not look like Figma because we wanted to favour primer components.

A few decisions were made here:

  • Using a contenteditable field for the input
  • A few hacks were necessary (e.g. adding spaces around the editable field so that they are still editable)

@brunopagno brunopagno self-assigned this Dec 16, 2024
@brunopagno brunopagno force-pushed the impl/59999-pattern-autocompleter-input branch 9 times, most recently from f82ef17 to 896dbd3 Compare December 24, 2024 14:04
@brunopagno brunopagno force-pushed the impl/59999-pattern-autocompleter-input branch 5 times, most recently from 8f324e2 to 428ede5 Compare January 7, 2025 08:17
@brunopagno brunopagno force-pushed the impl/59999-pattern-autocompleter-input branch 4 times, most recently from 02b24d2 to 17ccd5e Compare January 10, 2025 11:13
@github-actions github-actions bot temporarily deployed to gh-6899875-pr-17472 January 11, 2025 07:02 Inactive
@brunopagno brunopagno force-pushed the impl/59999-pattern-autocompleter-input branch from 2f587af to e20ace7 Compare January 13, 2025 09:12
@github-actions github-actions bot temporarily deployed to gh-6899875-pr-17472 January 13, 2025 09:12 Inactive
@brunopagno brunopagno force-pushed the impl/59999-pattern-autocompleter-input branch from e20ace7 to 90e816f Compare January 13, 2025 09:13
@github-actions github-actions bot temporarily deployed to gh-6899875-pr-17472 January 13, 2025 09:25 Inactive
@brunopagno brunopagno force-pushed the impl/59999-pattern-autocompleter-input branch from 90e816f to e6cd61c Compare January 13, 2025 18:14
@github-actions github-actions bot temporarily deployed to gh-6899875-pr-17472 January 13, 2025 18:15 Inactive
@brunopagno brunopagno force-pushed the impl/59999-pattern-autocompleter-input branch from e6cd61c to 9f03cd4 Compare January 13, 2025 18:19
@github-actions github-actions bot temporarily deployed to gh-6899875-pr-17472 January 13, 2025 18:27 Inactive
@brunopagno brunopagno force-pushed the impl/59999-pattern-autocompleter-input branch from 9f03cd4 to 8ca26a9 Compare January 14, 2025 09:35
@github-actions github-actions bot temporarily deployed to gh-6899875-pr-17472 January 14, 2025 09:36 Inactive
@brunopagno brunopagno marked this pull request as ready for review January 14, 2025 09:41
@brunopagno brunopagno requested review from HDinger, Kharonus and a team January 14, 2025 09:41
@brunopagno brunopagno changed the title WIP pattern autocompleter input Pattern autocompleter input Jan 14, 2025
Copy link
Contributor

@NobodysNightmare NobodysNightmare left a comment

Choose a reason for hiding this comment

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

I've done some testing mostly from the UI side (i.e. not looking at the code first, but checking behaviour first) and left some comments.

Feel free to give me feedback on whether you agree or disagree to the findings, but also whenever you think that addressing a finding takes too much effort and should rather be extracted to a separate PR.

}

// move up and down the suggestions selection
if (event.key === 'ArrowUp') {
Copy link
Contributor

Choose a reason for hiding this comment

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

What I see is that we implement the behaviour of a select box manually here. This is tricky. For example I noticed that when using a screen reader, the current implementation does not read out my selection when I change it using the arrow keys. A normal select box (e.g. the color selection in the settings tab) does this.

That's an accessibility concern as far as I can tell. However, I realize that the same problem can be observed for two other places in our UI: The project selection in the top left and our emoji autocompleter. So maybe we never solved this so far. (for comparison, the GitHub emoji autocompleter behaves fine)

Sadly I am by no means an expert in implementing accessible custom components. As far as I can tell, the usually best practice is to start with a standard component that is already accessible, e.g. using a real select box in the background.

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 am not sure how to handle this, though. I will reach out to other people on the dev team to see if it helps.

But I was more afraid of attempting to change focus/force navigate with something that is supposed to be a shortcut.

Maybe there's an obvious solution.

Copy link
Contributor

Choose a reason for hiding this comment

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

The idea with using Primer was to fully spport accessibility again, as the Primer components are claimed to be accessible. However, the ActionList in Primer is not meant to be used standalone afaik, so we are more or less misusing it here. Looking at other components where the actionList is used, it is possible to navigate via arrows. Primer has an internal helper called focus-group for that, see:

Maybe, we simply have to add that around our ActionList as well and we can get rid of the custom selection logic. But that is just an educated guess, I don't know what that really works.

This commit introduces a new type of input field which can handle autocomplete with 'tokens', so that we can build
patterns like `Example pattern {{token}} value`.
@brunopagno brunopagno force-pushed the impl/59999-pattern-autocompleter-input branch from 8ca26a9 to 685b5e1 Compare January 14, 2025 12:43
@github-actions github-actions bot temporarily deployed to gh-6899875-pr-17472 January 14, 2025 12:44 Inactive
}

// close the suggestions
if (event.key === 'Escape' || event.key === 'ArrowLeft' || event.key === 'ArrowRight') {
Copy link
Contributor

Choose a reason for hiding this comment

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

(Very optional)

Small suggestion after realizing that using Esc we can close the auto-completer: What do you think about adding Ctrl+Space as a combo to open the list again?

After the latest change this can also be useful for cases where the menu accidentially closed during keyboard navigation.

@NobodysNightmare
Copy link
Contributor

NobodysNightmare commented Jan 14, 2025

Because the PR template always reminds me about it: Should we add our new component also to the Lookbook?

I am not quite sure how this works, but I guess we need to add a file like lookbook/previews/open_project/common/pattern_autocomplete_preview.rb, maybe not in "common", because our component has a pretty narrow use case. But unsure.


def select_item_action
{
action: "click->pattern-autocompleter#suggestions_select",
Copy link
Contributor

@NobodysNightmare NobodysNightmare Jan 15, 2025

Choose a reason for hiding this comment

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

I just spotted this one and noticed that I didn't try using the mouse to select an autocomplete entry yet.

For me it doesn't work to click on an autoselect entry. The pop-up closes, but nothing gets selected.

Copy link
Contributor

Choose a reason for hiding this comment

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

New learning: Selecting with the mouse works after opening the suggestion list via the arrow down button, but not when using the mouse after starting autocompletion via typing.

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 shall investigate. Maybe the focus group solution suggested above fixes all things and I can move on with life 👀

Copy link
Contributor

@HDinger HDinger left a comment

Choose a reason for hiding this comment

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

Hi @brunopagno

I don't know which detail of review you want at that point. I will just write down the issues I found, feel free to ignore when you already aware of that.

  • When loading the page, the field is shortly shown. Sometimes, it there so long that I even could start typing before it vanished
Bildschirmfoto 2025-01-15 um 07 54 54
  • The search results are only selectable via keyboard, not via mouse click

  • The label text and closing "x" are not alligned

Bildschirmfoto 2025-01-15 um 07 56 45
  • The labels can overflow the input
Bildschirmfoto 2025-01-15 um 08 00 57
  • When there are no search results, the headlines are still shown, which feels weird
Bildschirmfoto 2025-01-15 um 07 56 22
  • I can't reliably reproduce it, but sometimes there are issues with the cursor position which is jumping around. For example when you select a label as the first element, it jumps to the right after pressing space.
    Jan-15-2025 08-09-38

In general it works nicely. Looking at the code, it still feels however as if we should have solved that differently from the UX side from the beginning. We are implementing a lot of custom stuff here and misusing components for that. I know that it is too late now, but we should keep that in mind for later similar cases.

)
) do
%>
<%= content_tag(:span, style: "cursor: default;", "data-role": "token-text") { "__VALUE__" } %>
Copy link
Contributor

Choose a reason for hiding this comment

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

Please do not use inline styles. Either use system_arguments or if the style is not supported, add a class and write the styles in the CSS file.

<%=
render(
Primer::Beta::Octicon.new(
icon: :x, size: :xsmall, style: "cursor: pointer;margin: 0.2em;",
Copy link
Contributor

Choose a reason for hiding this comment

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

Same, please do not use inline styles. Further, 0.2 is somehow a magic number, that does not even align the cross to the center:
Bildschirmfoto 2025-01-15 um 07 56 45

Please try to resolve that with CSS alignment mechanism (e.g. vertica-align or a flex)

<% end %>
</template>

<%= content_tag(:div, style: "position: relative;") do %>
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
<%= content_tag(:div, style: "position: relative;") do %>
<%= render(Primer::BaseComponent.new(tag: :div, position: :relative)) do %>

Primer::Beta::Octicon.new(
icon: "triangle-down",
size: :small,
style: "cursor: pointer;position: absolute;right: 1em;top: 0.5em;",
Copy link
Contributor

Choose a reason for hiding this comment

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

Position absolute has some drawbacks in this case. You are enforcing it to lay at that specific place no matter what the content of the input is. Thus the content and the arrow can overlap:

Bildschirmfoto 2025-01-15 um 08 18 12

Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't see that arrow down at all until now 🙈

Devils advocate: Do we need this arrow at all? It makes the component look like a select, when it is mostly like a text input. E.g. we'd also not show an arrow down in a text input that allows autocompleting emojis (which I think is a good analogy for what we are doing here).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The arrow allows for a mouse only interaction of adding tokens so I think it might be necessary. 🤔

@@ -0,0 +1,5 @@
.pattern-autocompleter
.selected
color: var(--list-item-hover--color)
Copy link
Contributor

Choose a reason for hiding this comment

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

Looking at the ActionList component, they use --fgColor-default for hovered elements. As we try to mimic that style, I'd suggest that we use the same here.

}

// move up and down the suggestions selection
if (event.key === 'ArrowUp') {
Copy link
Contributor

Choose a reason for hiding this comment

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

The idea with using Primer was to fully spport accessibility again, as the Primer components are claimed to be accessible. However, the ActionList in Primer is not meant to be used standalone afaik, so we are more or less misusing it here. Looking at other components where the actionList is used, it is possible to navigate via arrows. Primer has an internal helper called focus-group for that, see:

Maybe, we simply have to add that around our ActionList as well and we can get rid of the custom selection logic. But that is just an educated guess, I don't know what that really works.

@HDinger
Copy link
Contributor

HDinger commented Jan 15, 2025

Some other things:

  • When I selected and item from the list via Enter, and the press Enter again, the element is added again to the input
    Jan-15-2025 09-24-18

  • In Safari, after I added an item, the cursor jumps to the beginning of the input
    Jan-15-2025 09-24-10

  • Currently it is not possible to delete a label via keyboard, only with a mouse. That is also an accessibility issue.

@brunopagno brunopagno changed the title Pattern autocompleter input WIP pattern autocompleter input Jan 16, 2025
@brunopagno brunopagno changed the title WIP pattern autocompleter input WIP Pattern input field Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

4 participants