10up / component-library

A library of barebones front-end components built with WordPress and accessibility in mind.
https://baseline.10up.com/
MIT License
59 stars 12 forks source link

New Feature: Accordion - expanded state control #82

Open ncoetzer opened 3 years ago

ncoetzer commented 3 years ago

Is your feature request related to a problem? Please describe. We've recently had a request from a client (Campbell's) to extend the accordion component with functionality to control its initial expanded state upon page load. By default, this feature is not built-in and would probably have to be added manually (which I'm almost sure has been done several times in the past). This seemed to be a handy feature that could benefit other projects/clients as well.

Describe the solution you'd like As a solution, we've implemented ToggleControls on two levels:

As a result, not only would administrators have more control to customize the initial state of the accordion, but it would also improve the user experience in the editor at the same time.

devinle commented 3 years ago

Hey @ncoetzer thanks for opening this issue.

A few questions, or rather, discussion points:

As a user, when clicking to expand an accordion item, the accordion should ...

As an editor, when enabling the toggle "Expand by default" on an item, the accordion should ...

The multiple vs singular items mentioned in the User and Editor criteria definitely relate. Oftentimes I'm on the fence with either only allowing a single accordion item to remain opened–but this can often force the collapse of other accordion items the user may still want opened.

Overall, I like your approach!

References:

ncoetzer commented 3 years ago

Thanks, @devinle really appreciate your feedback on this.

As a user, when clicking to expand an accordion item, the accordion should ...

The current behaviour of the accordion component is to keep all other expanded items open. I'm also with you on the fence here. The behaviour of only allowing a single accordion open at a time feels kind of forced. Perhaps this could be an additional option or setting e.g. "Tabbed" or "Multi-select", which would further allow the user to select the type of accordion they would want. However, this would interfere with the toggle approach, since more than one item could be expanded on load.

As an editor, when enabling the toggle "Expand by default" on an item, the accordion should ...

With my approach, the toggle for each individual item only becomes available and effective if the global option is disabled. The purpose of the global option is a quick way to force the behaviour on all items, regardless of their individual setting. Both the global and individual item options are by default set to false. Only if the global option is false/off, will the option become available on each individual item, otherwise, it inherits the global setting. I made a short screen recording to showcase what I've done on a past project for Campbell's Corporate, that might explain things a bit better 😉

https://user-images.githubusercontent.com/3490023/130070025-c66c8f32-f9f4-41be-9403-0c46f7763dcd.mp4

ncoetzer commented 3 years ago

We'll preferably introduce this new feature to the component here (CSS + JS) and the block library (InspectorControls) in tandem. This will ensure the new settings are also available to administrators in the admin.