Skip to content

Add toggleMode to accordion and accordion section #2559

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

maddy531
Copy link

@maddy531 maddy531 commented Apr 16, 2025

Summary:

This PR updates the Accordion and AccordionSection so that sections can be nested and exposes an internal toggle handler.

Updates:

  • Nesting is now supported.
  • The toggle handler is exposed for the consumer.
  • They can programmatically control open/close states.

Screenshots:

Screen.Recording.2025-04-15.at.9.50.50.PM.mov

Issue: CLASS-11631

Test plan:

@maddy531 maddy531 self-assigned this Apr 16, 2025
Copy link

changeset-bot bot commented Apr 16, 2025

🦋 Changeset detected

Latest commit: b5a04ab

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@khanacademy/wonder-blocks-accordion Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

github-actions bot commented Apr 16, 2025

Size Change: +240 B (+0.23%)

Total Size: 103 kB

Filename Size Change
packages/wonder-blocks-accordion/dist/es/index.js 3.8 kB +240 B (+6.74%) 🔍
ℹ️ View Unchanged
Filename Size
packages/wonder-blocks-announcer/dist/es/index.js 2.04 kB
packages/wonder-blocks-banner/dist/es/index.js 1.58 kB
packages/wonder-blocks-birthday-picker/dist/es/index.js 1.88 kB
packages/wonder-blocks-breadcrumbs/dist/es/index.js 886 B
packages/wonder-blocks-button/dist/es/index.js 4.25 kB
packages/wonder-blocks-cell/dist/es/index.js 2.36 kB
packages/wonder-blocks-clickable/dist/es/index.js 3.12 kB
packages/wonder-blocks-core/dist/es/index.js 2.85 kB
packages/wonder-blocks-data/dist/es/index.js 6.25 kB
packages/wonder-blocks-dropdown/dist/es/index.js 19.7 kB
packages/wonder-blocks-form/dist/es/index.js 6.03 kB
packages/wonder-blocks-grid/dist/es/index.js 1.36 kB
packages/wonder-blocks-icon-button/dist/es/index.js 2.27 kB
packages/wonder-blocks-icon/dist/es/index.js 873 B
packages/wonder-blocks-labeled-field/dist/es/index.js 1.26 kB
packages/wonder-blocks-layout/dist/es/index.js 1.82 kB
packages/wonder-blocks-link/dist/es/index.js 1.98 kB
packages/wonder-blocks-modal/dist/es/index.js 5.48 kB
packages/wonder-blocks-pill/dist/es/index.js 1.5 kB
packages/wonder-blocks-popover/dist/es/index.js 4.85 kB
packages/wonder-blocks-progress-spinner/dist/es/index.js 1.52 kB
packages/wonder-blocks-search-field/dist/es/index.js 1.34 kB
packages/wonder-blocks-styles/dist/es/index.js 468 B
packages/wonder-blocks-switch/dist/es/index.js 2.02 kB
packages/wonder-blocks-tabs/dist/es/index.js 1.92 kB
packages/wonder-blocks-testing-core/dist/es/index.js 3.96 kB
packages/wonder-blocks-testing/dist/es/index.js 1.07 kB
packages/wonder-blocks-theming/dist/es/index.js 679 B
packages/wonder-blocks-timing/dist/es/index.js 1.79 kB
packages/wonder-blocks-tokens/dist/es/index.js 2.85 kB
packages/wonder-blocks-toolbar/dist/es/index.js 923 B
packages/wonder-blocks-tooltip/dist/es/index.js 7.02 kB
packages/wonder-blocks-typography/dist/es/index.js 1.23 kB

compressed-size-action

Copy link
Contributor

github-actions bot commented Apr 16, 2025

A new build was pushed to Chromatic! 🚀

https://5e1bf4b385e3fb0020b7073c-snxieqtpnq.chromatic.com/

Chromatic results:

Metric Total
Captured snapshots 7
Tests with visual changes 3
Total stories 605
Inherited (not captured) snapshots [TurboSnap] 372
Tests on the build 379

@maddy531 maddy531 requested review from jandrade and nishasy April 16, 2025 02:01
@maddy531 maddy531 marked this pull request as ready for review April 16, 2025 02:01
@khan-actions-bot khan-actions-bot requested a review from a team April 16, 2025 02:01
@khan-actions-bot
Copy link
Contributor

Gerald

Required Reviewers
  • @Khan/wonder-blocks for changes to .changeset/smooth-actors-mix.md, __docs__/wonder-blocks-accordion/accordion.stories.tsx, packages/wonder-blocks-accordion/src/components/accordion-section-header.tsx, packages/wonder-blocks-accordion/src/components/accordion-section.tsx, packages/wonder-blocks-accordion/src/components/accordion.tsx, packages/wonder-blocks-accordion/src/components/__tests__/accordion.test.tsx

Don't want to be involved in this pull request? Comment #removeme and we won't notify you of further changes.

Copy link
Contributor

npm Snapshot: Published

🎉 Good news!! We've packaged up the latest commit from this PR (f57fd50) and published all packages with changesets to npm.

You can install the packages in webapp by running:

./services/static/dev/tools/deploy_wonder_blocks.js --tag="PR2559"

Packages can also be installed manually by running:

pnpm add @khanacademy/wonder-blocks-<package-name>@PR2559

key={`nested-section-1`}
caretPosition="start"
>
<AccordionSection
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a small margin around the inner nested sections so it's visually more clear that they're nested?

Copy link
Author

Choose a reason for hiding this comment

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

Absolutely!

Copy link
Member

@marcysutton marcysutton 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 so much for contributing this PR! The toggleMode functionality is going to be super useful. I had some feedback primarily on nested accordions and tests!

key={`nested-section-1`}
caretPosition="start"
>
<AccordionSection
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: Looking at the rendered markup, it would be helpful to have Accordions output in a nested UL and LIs. That would indicate the structure semantically. Is there a way to handle this under the hood and make it automatic for consumers?

Design-wise, the nested accordions aren't super clear that they're nested beyond the caret icons and a subtle outline. You. might already be working on this, but can we make the nesting more obvious somehow? Some design ideas could include indentation or other spacing, or background/foreground colors.

</View>

<View>
<LabelLarge>Nested Accordion Example</LabelLarge>
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: Even though this uses toggle mode, could the nested example be broken out into a separate story? It would make it easier to find!

expect(await screen.findByText("Content 1")).toBeVisible();
expect(await screen.findByText("Content 2")).toBeVisible();

// Act: now re-render with toggleMode="collapse-all"
Copy link
Member

Choose a reason for hiding this comment

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

question: Can you break this out into a separate test to isolate the assertions? Since the first set of expectations are testing the same visibility, perhaps they could also be combined into one group with a different query selector, like findAllByRole?

There are some tips and reasoning in this doc: https://khanacademy.atlassian.net/wiki/spaces/ENG/pages/98402353/Unit+Testing+Best+Practices#Assert-One-Requirement-Per-Test

@@ -73,6 +83,26 @@ type Props = AriaProps & {
* Custom styles for the overall accordion container.
*/
style?: StyleType;

/**
* Called whenever the accordion’s overall expansion state may have changed,
Copy link
Member

Choose a reason for hiding this comment

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

praise: Thanks for these comments! They are super helpful!

@@ -17,7 +17,7 @@ type Props = {
// Unique ID for this section's button.
id: string;
// Header content.
header: string | React.ReactElement;
header: string | React.ReactElement | TemplateStringsArray;
Copy link
Member

Choose a reason for hiding this comment

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

question: I'm curious, are there specific cases where TemplateStringsArray becomes necessary?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants