Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 13 additions & 4 deletions source/03-components/Accordion/Accordion.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Meta, StoryObj } from '@storybook/react';
import parse from 'html-react-parser';
import AccordionComponent from './Accordion';
import accordionArgs from './accordion.yml';
import { AccordionItemProps } from './AccordionItem';

const meta: Meta<typeof AccordionComponent> = {
title: 'Components/Accordion',
Expand All @@ -11,10 +12,18 @@ const meta: Meta<typeof AccordionComponent> = {

type Story = StoryObj<typeof AccordionComponent>;

accordionArgs.accordionItems = accordionArgs.accordionItems.map(item => {
item.content = parse(item.content);
return item;
});
accordionArgs.accordionItems = accordionArgs.accordionItems.map(
(
item:
| (Omit<AccordionItemProps, 'content'> & { content: string })
| AccordionItemProps,
) => {
if (typeof item.content === 'string') {
item.content = parse(item.content) as string;
}
return item;
},
);

const Accordion: Story = {
render: args => <AccordionComponent {...args} />,
Expand Down
24 changes: 16 additions & 8 deletions source/03-components/Accordion/Accordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,25 @@ function Accordion({
}, [accordionItemsStatus]);

const openAccordionItem = (items: AccordionItemProps[], index: number) => {
return items.with(index, {
...items[index],
isOpen: true,
});
return [
...items.slice(0, index),
{
...items[index],
isOpen: true,
},
...items.slice(index + 1),
];
};

const closeAccordionItem = (items: AccordionItemProps[], index: number) => {
return items.with(index, {
...items[index],
isOpen: false,
});
return [
...items.slice(0, index),
{
...items[index],
isOpen: false,
},
...items.slice(index + 1),
];
};

const handleClick = (id: string, isOpen = false) => {
Expand Down
10 changes: 8 additions & 2 deletions source/03-components/Accordion/AccordionItem.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import clsx from 'clsx';
import { GessoComponent } from 'gesso';
import { ElementType, MouseEventHandler, useEffect, useRef } from 'react';
import {
ElementType,
MouseEventHandler,
ReactElement,
useEffect,
useRef,
} from 'react';
import styles from './accordion-item.module.css';
import { slideCollapse, slideExpand } from '../../06-utility/slide';

export interface AccordionItemProps extends GessoComponent {
id: string;
title: string;
content: string;
content: ReactElement;
titleElement?: ElementType;
isOpen?: boolean;
accordionSpeed?: string;
Expand Down
3 changes: 1 addition & 2 deletions source/06-utility/slide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export const slideCollapse = (
target.style.paddingBottom = '0';
target.style.marginTop = '0';
target.style.marginBottom = '0';
target.style.marginHeight = 'auto';

window.requestAnimationFrame(() => {
function hideTarget() {
Expand Down Expand Up @@ -70,7 +69,7 @@ export const slideCollapse = (
*
* @name slideExpand
* @param {HTMLElement} target - The element expanding.
* @param {integer} duration - The duration of the animation, defaults to gesso token standard.
* @param {number} duration - The duration of the animation, defaults to gesso token standard.
* @param {string} easing - The easing of the animation, defaults to gesso token ease-in-out.
* @param {boolean} hideContent - Whether to hide collapsed content from screen readers, defaults to true.
*/
Expand Down