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

feat(accordion): clickable title #1033

Merged
merged 4 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/components/Accordion.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export function AccordionVariations() {
Our modern approach to homebuilding makes the process easier and more personal than ever before.
</div>
</Accordion>
<Accordion title="Clickable Title" titleOnClick={() => {}}>
<div css={Css.sm.$}>
Our modern approach to homebuilding makes the process easier and more personal than ever before.
</div>
</Accordion>
</>
);
}
Expand Down
34 changes: 33 additions & 1 deletion src/components/Accordion.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render } from "src/utils/rtl";
import { click, render } from "src/utils/rtl";
import { Accordion } from "./Accordion";

describe(Accordion, () => {
Expand Down Expand Up @@ -72,4 +72,36 @@ describe(Accordion, () => {
// And the content is not displayed
expect(r.query.accordion_content).not.toBeInTheDocument();
});

it("calls the titleOnClick function when the title is clicked", async () => {
// Given an accordion component with titleOnClick set
const titleOnClick = jest.fn();
// When rendered
const r = await render(
<Accordion title="Test title" titleOnClick={titleOnClick}>
Test description
</Accordion>,
);

// Then the titleOnClick function is called when the title is clicked
click(r.accordion_titleText);
expect(titleOnClick).toHaveBeenCalled();
});

it("alters expando behavior when titleOnClick is provided", async () => {
// When rendered with a titleOnClick set
const r = await render(
<Accordion title="Test title" titleOnClick={() => {}}>
Test description
</Accordion>,
);

click(r.accordion_titleText);
// And the content is not displayed
expect(r.query.accordion_content).not.toBeInTheDocument();

// Then the content is displayed when the title is clicked
click(r.accordion_title);
expect(r.accordion_content).toHaveTextContent("Test description");
});
});
20 changes: 18 additions & 2 deletions src/components/Accordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export interface AccordionProps<X = AccordionXss> {
*/
index?: number;
setExpandedIndex?: Dispatch<SetStateAction<number | undefined>>;
/** Turns the title into a button. If provided, disables expand/collapse on title text */
titleOnClick?: VoidFunction;
/** Used by Accordion list. Sets default padding to 0 for nested accordions */
omitPadding?: boolean;
/** Styles overrides for padding */
Expand All @@ -43,6 +45,7 @@ export function Accordion<X extends Only<AccordionXss, X>>(props: AccordionProps
bottomBorder = false,
index,
setExpandedIndex,
titleOnClick,
omitPadding = false,
xss,
} = props;
Expand Down Expand Up @@ -98,11 +101,24 @@ export function Accordion<X extends Only<AccordionXss, X>>(props: AccordionProps
...xss,
}}
onClick={() => {
setExpanded(!expanded);
setExpanded((prior) => !prior);
if (setExpandedIndex) setExpandedIndex(index);
}}
>
<span css={Css.fg1.tl.$}>{title}</span>
{titleOnClick ? (
<button
Copy link
Contributor

@bdow bdow May 25, 2024

Choose a reason for hiding this comment

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

Suggestion: We should try to avoid nesting interactive elements. Per HTML standards, nesting of interactive elements is not permitted- it's possible, just not advisable.

Content model:
Phrasing content, but there must be no interactive content descendant and no descendant with the tabindex attribute specified.

https://html.spec.whatwg.org/multipage/form-elements.html#the-button-element

Please work with design to determine a way forward where we do not nest interactive elements. We could have buttons side by side, or just make the chevron the click target for expanding/collapsing.

I used to be pretty adamant with the old design crew about avoiding these patterns, and we've been successful so far. I'd like us to keep up with enforcing HTML standards back onto design and share the knowledge.

Fwiw, here's what GPT had to say about it:

Nesting a <button> element inside another <button> element is invalid HTML according to the HTML5 specification. This is because:

  1. Semantic Incorrectness: Buttons are meant to be interactive elements, and nesting one interactive element inside another can create confusion about which element should be interactive.

  2. Accessibility Issues: Screen readers and other assistive technologies may have difficulty interpreting nested buttons, leading to poor user experiences for individuals relying on these tools.

  3. Browser Compatibility: Different browsers may handle nested buttons inconsistently. Some browsers may ignore the nested button, while others may render it in unexpected ways, leading to unpredictable behavior.

  4. Event Handling Confusion: Nesting buttons can create complications in event handling. For instance, click events might not propagate as expected, causing issues in the functionality of the nested buttons.

To maintain valid, accessible, and predictable HTML, it's best to avoid nesting <button> elements inside each other.

{...testIds.titleText}
css={Css.fg0.tl.$}
onClick={(e) => {
e.stopPropagation();
titleOnClick();
}}
>
{title}
</button>
) : (
<span css={Css.fg1.tl.$}>{title}</span>
)}
<span
css={{
...Css.fs0.$,
Expand Down
Loading