ItsJonQ / g2

✨ An experimental reimagining of WordPress components
http://g2-components.com/
MIT License
105 stars 12 forks source link

SelectDropdown: Add Component #161

Closed ItsJonQ closed 3 years ago

ItsJonQ commented 3 years ago

For integration with Gutenberg, we may need to replace the current FontSizePicker component from @wordpress/components (as it related to Typography Tools in Global Styles).

As such, we'll need some sort of custom select component:

Screen Shot 2020-11-23 at 2 11 07 PM

I'm not a fan of the name CustomSelect (from @wordpress/components). Ultimately, we can go with that if it's the best option.

I feel like either SelectDropdown or SelectMenu is a better description.

ItsJonQ commented 3 years ago

Sigh... It looks like Downshift doesn't really play nicely with Portal (via Popover) (at least, I haven't figured out a good way to do this yet).

At this point, we either massage Downshift useSelect... or we massage Reakit Menu 😂 .

ItsJonQ commented 3 years ago

Calling in reinforcements! (aka, @diegohaz ❤️ )

Haiiiii!! I'm a bit stuck 😊 .

I basically want to remake the CustomSelect component in G2. Something we can use for this: https://wordpress.github.io/gutenberg/iframe.html?id=components-fontsizepicker--default&viewMode=story

It doesn't look like the Reakit Menu should be used for this. Alternatively, there's the experimental Combobox component. However, for this particular select, we won't need to input part of Combobox.

From your perspective, should we use Menu or Combobox? :).

Ideally, I'd use something Reakit based because I'll be able to reuse other parts of G2. Otherwise... I'll have to rebuild many things to make Downshift happy (e.g. Dropdown menus, portal handling, Popper.js positioning, etc...)

Any thoughts and advice would be much appreciated 🙏 !!!

ItsJonQ commented 3 years ago

From my understanding, there was a lot of collaborative work to craft the CustomSelect component from @wordpress/components (a11y, dev, etc...).

For that reason, perhaps the SelectDropdown could use that code as it's foundation as a start?

The biggest feature it's missing at the moment is Portal'ing/Autopositioning.

Perhaps that and other adjustments to sync. it with Dropdown code can be enhanced later on.

ItsJonQ commented 3 years ago

It's coming together 🌈

Screen Capture on 2020-11-24 at 09-46-11

ItsJonQ commented 3 years ago

Copied @diegohaz 's setup with Popper in Reakit's usePopoverState: https://github.com/reakit/reakit/blob/master/packages/reakit/src/Popover/PopoverState.ts

Got it working roughly with downshift. Neat!

We don't have Portal'ing, but we have better positioning + size control.

ItsJonQ commented 3 years ago

Getting closer:

Screen Shot 2020-11-24 at 4 03 03 PM

Some much needed enhancements:

At the moment, the select Element is rendering a Button. Ideally, it should be rendering the same UI as a regular Select. Going to work on that next.

ItsJonQ commented 3 years ago

Update: We did it! ✨

In addition to reusing 99% of existing components and styles for SelectDropdown, I also have Portaling + Popper working.

Check out this demo: https://d.pr/v/RIRg1p

In it, you can see the Dropdown repositioning itself to the left/right if there's a chance it may be "cut off" from view in the browser (this is thanks to the positioning engine that is popper.js). The content is also Portal'ed. So it doesn't absorb lineHeight/fontSize styles from the inline text content.

diegohaz commented 3 years ago

From your perspective, should we use Menu or Combobox? :).

A bit late here! 😅

That's a great question. I've seen people using Menu for that in the past: https://codesandbox.io/s/gaudi-0bp08?file=/src/components/Listbox/Listbox.tsx

But I think Combobox is the best base for that, although it would still require a lot of work to make a select completely accessible with that.

Anyway, the best resources I know for accessible select components are these articles by Sarah Higley: