From 035afe13409a9ee724ccc32a764ed60b3c6b32bb Mon Sep 17 00:00:00 2001 From: Andrea Forni <1233582+andreaforni@users.noreply.github.com> Date: Mon, 22 Jan 2024 16:30:20 +0100 Subject: [PATCH] Fix/311: accordion (#319) * chore: remove unuseful comments * refactor: rename Docx to Docs * fix: move Accordion example inside the story * fix: add different sizes * docs: remove example and update headers * fix: update paddings based on the designer's feedbacks * fix: fix horizontal divider width --------- Co-authored-by: Andrea Forni --- src/components/Accordion/index.tsx | 38 +++++++++--- stories/Accordion/Accordion.example.tsx | 37 ------------ stories/Accordion/Accordion.stories.tsx | 73 +++++++++++++++++++----- stories/Accordion/{Docx.mdx => Docs.mdx} | 65 +++++---------------- 4 files changed, 102 insertions(+), 111 deletions(-) delete mode 100644 stories/Accordion/Accordion.example.tsx rename stories/Accordion/{Docx.mdx => Docs.mdx} (63%) diff --git a/src/components/Accordion/index.tsx b/src/components/Accordion/index.tsx index b25b59e6..680ce004 100644 --- a/src/components/Accordion/index.tsx +++ b/src/components/Accordion/index.tsx @@ -2,17 +2,37 @@ import * as React from 'react' import * as AccordionPrimitive from '@radix-ui/react-accordion' -import { BsChevronDown } from '@/assets' +import { cva, type VariantProps } from 'class-variance-authority' +import { BsChevronDown } from '@/assets' import { cn } from '@/lib/utils' +const sizeVariants = cva('', { + variants: { + size: { + sm: ['text-sm'], + md: ['text-base'], + lg: ['text-lg'] + } + }, + defaultVariants: { + size: 'md' + } +}) + +type AccordionProps = React.ComponentPropsWithoutRef< + typeof AccordionPrimitive.Root +> & + VariantProps + const Accordion = React.forwardRef< React.ElementRef, - React.ComponentPropsWithoutRef ->(({ children, className, ...props }, ref) => ( + AccordionProps +>(({ children, className, size, ...props }, ref) => ( (({ children, className, ...props }, ref) => ( >(({ children, className, ...props }, ref) => ( @@ -61,7 +81,7 @@ const AccordionTrigger = React.forwardRef< (({ children, className, ...props }, ref) => ( -
{children}
+
{children}
)) AccordionContent.displayName = AccordionPrimitive.Content.displayName diff --git a/stories/Accordion/Accordion.example.tsx b/stories/Accordion/Accordion.example.tsx deleted file mode 100644 index d8df2c6c..00000000 --- a/stories/Accordion/Accordion.example.tsx +++ /dev/null @@ -1,37 +0,0 @@ -import { - Accordion, - AccordionItem, - AccordionTrigger, - AccordionContent -} from '@/components/Accordion' - -export const AccordionDemo = () => ( - - - Item 1 - - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse - facilisis ac ligula ut luctus. Nulla sed tortor tempor, auctor mi at, - mollis nulla. - - - - - Item 2 - -
- Replace this component with your content -
-
-
- - - Item 3 - -
- Replace this component with your content -
-
-
-
-) diff --git a/stories/Accordion/Accordion.stories.tsx b/stories/Accordion/Accordion.stories.tsx index fdfdf4e5..ecca8f7a 100644 --- a/stories/Accordion/Accordion.stories.tsx +++ b/stories/Accordion/Accordion.stories.tsx @@ -1,29 +1,74 @@ import type { Meta, StoryObj } from '@storybook/react' -import { AccordionDemo } from './Accordion.example' +import { + Accordion, + AccordionItem, + AccordionTrigger, + AccordionContent +} from '@/components' -// More on how to set up stories at: https://storybook.js.org/docs/react/writing-stories/introduction#default-export const meta = { title: 'Components/Accordion', - component: AccordionDemo, + component: Accordion, parameters: { - // Optional parameter to center the component in the Canvas. More info: https://storybook.js.org/docs/react/configure/story-layout layout: 'centered' }, - // This component will have an automatically generated Autodocs entry: https://storybook.js.org/docs/react/writing-docs/autodocs argTypes: { - className: { - controle: 'text', - description: 'Alter the className to change the style' + type: { + options: ['single', 'multiple'], + control: 'radio' + }, + size: { + options: ['sm', 'md', 'lg'], + control: 'radio' } - } - // More on argTypes: https://storybook.js.org/docs/react/api/argtypes -} satisfies Meta + }, + render: props => ( + + + Item 1 + + Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse + facilisis ac ligula ut luctus. Nulla sed tortor tempor, auctor mi at, + mollis nulla. + + + + + Item 2 + +
+ Replace this component with your content +
+
+
+ + + Item 3 + +
+ Replace this component with your content +
+
+
+
+ ) +} satisfies Meta export default meta type Story = StoryObj -// More on writing stories with args: https://storybook.js.org/docs/react/writing-stories/args -export const Default: Story = { - args: {} +export const Single: Story = { + args: { + type: 'single', + collapsible: true, + size: 'md' + } +} + +export const Multiple: Story = { + args: { + type: 'multiple', + size: 'md' + } } diff --git a/stories/Accordion/Docx.mdx b/stories/Accordion/Docs.mdx similarity index 63% rename from stories/Accordion/Docx.mdx rename to stories/Accordion/Docs.mdx index e697dbfb..97922fc2 100644 --- a/stories/Accordion/Docx.mdx +++ b/stories/Accordion/Docs.mdx @@ -6,15 +6,19 @@ import * as AccordionStories from './Accordion.stories' # Accordion -An Accordion is a vertically stacked set of interactive headings that each reveal an associated section of content. +An accordion component is an interactive element with headers and collapsible content panels. +It's used to present information in a compact, organised way, typically for FAQs, menus, or content sections. +Users can click on headers to expand or collapse the associated content, improving space efficiency and user experience. -## Default - +## Single + -## Attributes +## Multiple + -#### Accordion +## Attributes +### Accordion Contains all the parts of an accordion @@ -88,7 +92,7 @@ Contains all the parts of an accordion -#### AccordionItem +### AccordionItem Contains all the parts of a collapsible section @@ -117,7 +121,7 @@ Contains all the parts of a collapsible section -#### AccordionItem +### AccordionItem Contains header & content of a item the Accordion @@ -146,7 +150,7 @@ Contains header & content of a item the Accordion -#### AccordionHeader +### AccordionHeader Wraps an Accordion.Trigger. Use the asChild prop to update it to the appropriate heading level for your page. @@ -165,7 +169,7 @@ Wraps an Accordion.Trigger. Use the asChild prop to update it to the appropriate -#### AccordionTrigger +### AccordionTrigger Toggles the collapsed state of its associated item. It should be nested inside of an Accordion.Header. @@ -184,7 +188,7 @@ Toggles the collapsed state of its associated item. It should be nested inside o -#### AccordionContent +### AccordionContent Contains the collapsible content for an item. @@ -207,44 +211,3 @@ Contains the collapsible content for an item. - -## Example - -```tsx -import { - Accordion, - AccordionItem, - AccordionHeader, - AccordionTrigger, - AccordionContent -} from '@/components/Accordion' - -export const AccordionDemo = () => ( - - - Item 1 - - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse facilisis ac ligula ut luctus. Nulla sed tortor tempor, auctor mi at, mollis nulla. - - - - - Item 2 - -
- Replace this component with your content -
-
-
- - - Item 3 - -
- Replace this component with your content -
-
-
-
-); -``` \ No newline at end of file