Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
77 changes: 60 additions & 17 deletions src/lib/accordion/accordion-item.tsx
Original file line number Diff line number Diff line change
@@ -1,56 +1,99 @@
import React, { ReactNode } from "react";
import React, { ReactNode, useMemo } from "react";
import { useElementSize } from "../../hooks/useElementSize";
import Plus from "../../assets/svgs/accordion/plus.svg";
import Minus from "../../assets/svgs/accordion/minus.svg";

import { Button } from "react-aria-components";
import { cn } from "../../utils";

interface AccordionItemProps {
export interface AccordionItemProps {
setExpanded: React.Dispatch<React.SetStateAction<number>>;
index: number;
/**
* The title displayed in the accordion header.
*
* This is usually text, but can be any ReactNode.
*/
title: ReactNode;
/**
* The body/content of the accordion section.
* This is only visible when the item is expanded.
*/
body: ReactNode;
expanded?: boolean;
/**
* Custom render function for the expand/collapse button.
*
* This function receives:
* - `expanded`: boolean => whether the item is currently expanded
* - `toggle`: function => expands/collapses this item when called
*
* Example:
* ```
* expandButton={({ expanded, toggle }) => (
* <button onClick={toggle}>
* {expanded ? "-" : "+"}
* </button>
* )}
* ```
*
* If not provided, a default + or − icon will be shown.
*
* This button does NOT need to manage its own state — calling `toggle()`
* properly expands/collapses the item.
*/
expandButton?: (options: {
expanded: boolean;
toggle: () => void;
}) => ReactNode;
expanded: boolean;
}

const AccordionItem: React.FC<AccordionItemProps> = ({
title,
body,
expandButton,
index,
expanded,
setExpanded,
}) => {
const [ref, { height }] = useElementSize();
const ExpandButton = useMemo(
() =>
expandButton ? (
expandButton({
expanded,
toggle: () => setExpanded(expanded ? -1 : index),
})
) : expanded ? (
<Minus
className={cn("fill-klerosUIComponentsPrimaryText size-4 shrink-0")}
/>
) : (
<Plus
className={cn("fill-klerosUIComponentsPrimaryText size-4 shrink-0")}
/>
),

Check warning on line 75 in src/lib/accordion/accordion-item.tsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Extract this nested ternary operation into an independent statement.

See more on https://sonarcloud.io/project/issues?id=kleros_ui-components-library&issues=AZsNP1nkaKGY6TxDWubX&open=AZsNP1nkaKGY6TxDWubX&pullRequest=85
[expanded, expandButton, index, setExpanded],
);
return (
<div className="my-2">
<Button
id="expand-button"
aria-expanded={expanded}
className={cn(
"bg-klerosUIComponentsWhiteBackground border-klerosUIComponentsStroke border",
"hover-medium-blue hover-short-transition hover:cursor-pointer",
"rounded-[3px] px-4 py-[11.5px] md:px-8",
"flex w-full items-center justify-between",
"flex w-full items-center justify-between gap-4",
)}
onPress={() => setExpanded(expanded ? -1 : index)}
>
{title}
{expanded ? (
<Minus
className={cn("fill-klerosUIComponentsPrimaryText size-4 shrink-0")}
/>
) : (
<Plus
className={cn("fill-klerosUIComponentsPrimaryText size-4 shrink-0")}
/>
)}
{ExpandButton}
</Button>
<div
style={{ height: expanded ? `${height.toString()}px` : 0 }}
className={cn(
expanded ? `overflow-visible` : "overflow-hidden",
"transition-[height] duration-(--klerosUIComponentsTransitionSpeed) ease-initial",
)}
className="overflow-hidden transition-[height] duration-(--klerosUIComponentsTransitionSpeed) ease-in-out"
>
<div className="p-4 md:p-8" id="body-wrapper" ref={ref}>
{body}
Expand Down
64 changes: 52 additions & 12 deletions src/lib/accordion/custom.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,69 @@
import React, { ReactNode, useState } from "react";
import AccordionItem from "./accordion-item";
import React, { useState } from "react";
import AccordionItem, { AccordionItemProps } from "./accordion-item";
import { cn, isUndefined } from "../../utils";

interface AccordionItem {
title: ReactNode;
body: ReactNode;
}
export interface CustomAccordionProps {
/**
* Array of accordion items.
*
* Each item can optionally define its own `expandButton`.
* If omitted, the parent-level `expandButton` (if provided) is used.
*/

interface AccordionProps {
items: AccordionItem[];
items: Pick<AccordionItemProps, "title" | "body" | "expandButton">[];
className?: string;

/**
* Index of the item to expand by default.
*
* - Set to a number (0-based index) to expand an item on mount
* - Leave undefined to start with all items collapsed
*/

defaultExpanded?: number;
/**
* A global expand/collapse button renderer applied to every item
* **unless that item provides its own expandButton**.
*
* Signature:
* ```
* expandButton?: ({ expanded, toggle }) => ReactNode;
* ```
*
* Example:
* ```
* expandButton={({ expanded, toggle }) => (
* <Button onPress={toggle}>
* {expanded ? <ChevronUp /> : <ChevronDown />}
* <Button>
* )}
* ```
*/
expandButton?: AccordionItemProps["expandButton"];
}

const CustomAccordion: React.FC<AccordionProps> = ({
/**
* @description This component manages a list of collapsible accordion items,
* where only one item can be expanded at a time.
* @param props - CustomAccordionProps
* @returns JSX.Element
*/
function CustomAccordion({
items,
className,
defaultExpanded,
expandButton,
...props
}) => {
}: Readonly<CustomAccordionProps>) {
const [expanded, setExpanded] = useState(
!isUndefined(defaultExpanded) ? defaultExpanded : -1,

Check warning on line 59 in src/lib/accordion/custom.tsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Unexpected negated condition.

See more on https://sonarcloud.io/project/issues?id=kleros_ui-components-library&issues=AZsNP1qXaKGY6TxDWubZ&open=AZsNP1qXaKGY6TxDWubZ&pullRequest=85
);
return (
<div
className={cn("box-border flex w-[1000px] flex-col", className)}
className={cn(
"box-border flex w-full max-w-[1000px] flex-col",
className,
)}
{...props}
>
{items.map((item, index) => (
Expand All @@ -33,12 +72,13 @@
index={index}
title={item.title}
body={item.body}
expandButton={item.expandButton ?? expandButton}
setExpanded={setExpanded}
expanded={expanded === index}
/>
))}
</div>
);
};
}

export default CustomAccordion;
34 changes: 26 additions & 8 deletions src/lib/accordion/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,56 @@
import AccordionItem from "./accordion-item";
import { cn, isUndefined } from "../../utils";

interface AccordionItem {
export interface AccordionItemProps {
title: string;
body: ReactNode;
Icon?: React.FC<React.SVGAttributes<SVGElement>>;
icon?: ReactNode;
}

interface AccordionProps {
items: AccordionItem[];
export interface AccordionProps {
/**
* Array of accordion items.
*/
items: AccordionItemProps[];
/**
* Index of the item to expand by default.
*
* - Set to a number (0-based index) to expand an item on mount
* - Leave undefined to start with all items collapsed
*/
defaultExpanded?: number;
className?: string;
}

const DefaultTitle: React.FC<{ item: AccordionItem }> = ({ item }) => (
const DefaultTitle: React.FC<{ item: AccordionItemProps }> = ({ item }) => (
<>
{item.icon ?? (item.Icon && <item.Icon />)}
<p className="w-fit text-center text-base font-semibold">{item.title}</p>
</>
);

const Accordion: React.FC<AccordionProps> = ({
/**
* @description This component manages a list of collapsible accordion items,
* where only one item can be expanded at a time.
* @param props - AccordionProps
* @returns JSX.Element
*/
function Accordion({
items,
defaultExpanded,
className,
...props
}) => {
}: Readonly<AccordionProps>) {
const [expanded, setExpanded] = useState(
!isUndefined(defaultExpanded) ? defaultExpanded : -1,

Check warning on line 47 in src/lib/accordion/index.tsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Unexpected negated condition.

See more on https://sonarcloud.io/project/issues?id=kleros_ui-components-library&issues=AZsNP1qDaKGY6TxDWubY&open=AZsNP1qDaKGY6TxDWubY&pullRequest=85
);
return (
<div
className={cn("box-border flex w-[1000px] flex-col", className)}
className={cn(
"box-border flex w-full max-w-[1000px] flex-col",
className,
)}
{...props}
>
{items.map((item, index) => (
Expand All @@ -48,6 +66,6 @@
))}
</div>
);
};
}

export default Accordion;
8 changes: 4 additions & 4 deletions src/stories/accordion.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import type { Meta, StoryObj } from "@storybook/react";

import { IPreviewArgs } from "./utils";

import Accordion from "../lib/accordion/index";
import AccordionComponent from "../lib/accordion/index";

const meta = {
component: Accordion,
component: AccordionComponent,
title: "Accordion",
tags: ["autodocs"],
} satisfies Meta<typeof Accordion>;
} satisfies Meta<typeof AccordionComponent>;

export default meta;

type Story = StoryObj<typeof meta> & IPreviewArgs;

export const DarkTheme: Story = {
export const Accordion: Story = {
args: {
className: "max-w-[80dvw]",

Expand Down
Loading