From 12a03a9a72f7895d571dbaeeb245d92aa277a610 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Fri, 6 Oct 2023 17:48:03 +0200 Subject: refactor(components): merge HeadingButton and Widget components The HeadingButton component was only used inside Widget component and it is not very useful on its own so I merge the two components in a new Collapsible component. --- .../molecules/buttons/heading-button.module.scss | 44 -------- .../molecules/buttons/heading-button.stories.tsx | 104 ------------------ .../molecules/buttons/heading-button.test.tsx | 33 ------ .../molecules/buttons/heading-button.tsx | 67 ------------ src/components/molecules/buttons/index.ts | 1 - .../molecules/collapsible/collapsible.module.scss | 100 ++++++++++++++++++ .../molecules/collapsible/collapsible.stories.tsx | 72 +++++++++++++ .../molecules/collapsible/collapsible.test.tsx | 83 +++++++++++++++ .../molecules/collapsible/collapsible.tsx | 108 +++++++++++++++++++ src/components/molecules/collapsible/index.ts | 1 + src/components/molecules/index.ts | 1 + src/components/molecules/layout/index.ts | 1 - src/components/molecules/layout/widget.module.scss | 65 ------------ src/components/molecules/layout/widget.stories.tsx | 117 --------------------- src/components/molecules/layout/widget.test.tsx | 20 ---- src/components/molecules/layout/widget.tsx | 61 ----------- 16 files changed, 365 insertions(+), 513 deletions(-) delete mode 100644 src/components/molecules/buttons/heading-button.module.scss delete mode 100644 src/components/molecules/buttons/heading-button.stories.tsx delete mode 100644 src/components/molecules/buttons/heading-button.test.tsx delete mode 100644 src/components/molecules/buttons/heading-button.tsx create mode 100644 src/components/molecules/collapsible/collapsible.module.scss create mode 100644 src/components/molecules/collapsible/collapsible.stories.tsx create mode 100644 src/components/molecules/collapsible/collapsible.test.tsx create mode 100644 src/components/molecules/collapsible/collapsible.tsx create mode 100644 src/components/molecules/collapsible/index.ts delete mode 100644 src/components/molecules/layout/widget.module.scss delete mode 100644 src/components/molecules/layout/widget.stories.tsx delete mode 100644 src/components/molecules/layout/widget.test.tsx delete mode 100644 src/components/molecules/layout/widget.tsx (limited to 'src/components/molecules') diff --git a/src/components/molecules/buttons/heading-button.module.scss b/src/components/molecules/buttons/heading-button.module.scss deleted file mode 100644 index 689f1e6..0000000 --- a/src/components/molecules/buttons/heading-button.module.scss +++ /dev/null @@ -1,44 +0,0 @@ -@use "../../../styles/abstracts/functions" as fun; - -.icon { - transition: all 0.25s ease-in-out 0s; -} - -.wrapper { - display: flex; - flex-flow: row nowrap; - align-items: center; - justify-content: space-between; - gap: var(--spacing-md); - width: 100%; - padding: 0 var(--spacing-2xs); - position: sticky; - top: 0; - background: inherit; - border: none; - border-top: fun.convert-px(2) solid var(--color-primary-dark); - border-bottom: fun.convert-px(2) solid var(--color-primary-dark); - cursor: pointer; - - .heading { - padding: var(--spacing-2xs) 0; - background: none; - font-size: var(--font-size-xl); - font-weight: 500; - text-align: left; - } - - &:hover, - &:focus { - .icon { - background: var(--color-primary-light); - color: var(--color-fg-inverted); - transform: scale(1.25); - - &::before, - &::after { - background: var(--color-bg); - } - } - } -} diff --git a/src/components/molecules/buttons/heading-button.stories.tsx b/src/components/molecules/buttons/heading-button.stories.tsx deleted file mode 100644 index 9beda2b..0000000 --- a/src/components/molecules/buttons/heading-button.stories.tsx +++ /dev/null @@ -1,104 +0,0 @@ -import { ComponentMeta, ComponentStory } from '@storybook/react'; -import { useState } from 'react'; -import { HeadingButton as HeadingButtonComponent } from './heading-button'; - -/** - * HeadingButton - Storybook Meta - */ -export default { - title: 'Molecules/Buttons/HeadingButton', - component: HeadingButtonComponent, - argTypes: { - className: { - control: { - type: 'text', - }, - description: 'Set additional classnames to the button.', - table: { - category: 'Styles', - }, - type: { - name: 'string', - required: false, - }, - }, - expanded: { - control: { - type: null, - }, - description: 'Heading button state (plus or minus).', - type: { - name: 'boolean', - required: true, - }, - }, - level: { - control: { - type: 'number', - min: 1, - max: 6, - }, - description: 'Heading level.', - type: { - name: 'number', - required: true, - }, - }, - setExpanded: { - control: { - type: null, - }, - description: 'Callback function to set heading button state.', - type: { - name: 'function', - required: true, - }, - }, - title: { - control: { - type: 'text', - }, - description: 'Heading title.', - type: { - name: 'string', - required: true, - }, - }, - }, -} as ComponentMeta; - -const Template: ComponentStory = ({ - expanded, - setExpanded: _setExpanded, - ...args -}) => { - const [isExpanded, setIsExpanded] = useState(expanded); - - return ( - - ); -}; - -/** - * Heading Button Stories - Expanded - */ -export const Expanded = Template.bind({}); -Expanded.args = { - expanded: true, - level: 2, - title: 'Your title', -}; - -/** - * Heading Button Stories - Collapsed - */ -export const Collapsed = Template.bind({}); -Collapsed.args = { - expanded: false, - level: 2, - title: 'Your title', -}; diff --git a/src/components/molecules/buttons/heading-button.test.tsx b/src/components/molecules/buttons/heading-button.test.tsx deleted file mode 100644 index 4d3d91e..0000000 --- a/src/components/molecules/buttons/heading-button.test.tsx +++ /dev/null @@ -1,33 +0,0 @@ -import { describe, expect, it } from '@jest/globals'; -import { render, screen } from '../../../../tests/utils'; -import { HeadingButton } from './heading-button'; - -describe('HeadingButton', () => { - it('renders a button to collapse.', () => { - render( - null} - /> - ); - expect( - screen.getByRole('button', { name: 'Collapse The accordion title' }) - ).toBeInTheDocument(); - }); - - it('renders a button to expand.', () => { - render( - null} - /> - ); - expect( - screen.getByRole('button', { name: 'Expand The accordion title' }) - ).toBeInTheDocument(); - }); -}); diff --git a/src/components/molecules/buttons/heading-button.tsx b/src/components/molecules/buttons/heading-button.tsx deleted file mode 100644 index 3c3eef5..0000000 --- a/src/components/molecules/buttons/heading-button.tsx +++ /dev/null @@ -1,67 +0,0 @@ -import { useCallback, type FC, type SetStateAction } from 'react'; -import { useIntl } from 'react-intl'; -import { Heading, type HeadingProps, Icon } from '../../atoms'; -import styles from './heading-button.module.scss'; - -export type HeadingButtonProps = Pick & { - /** - * Set additional classnames to the button. - */ - className?: string; - /** - * Accordion state. - */ - expanded: boolean; - /** - * Callback function to set accordion state on click. - */ - setExpanded: (value: SetStateAction) => void; - /** - * Accordion title. - */ - title: string; -}; - -/** - * HeadingButton component - * - * Render a button as accordion title to toggle body. - */ -export const HeadingButton: FC = ({ - className = '', - expanded, - level, - setExpanded, - title, -}) => { - const intl = useIntl(); - const btnClass = `${styles.wrapper} ${className}`; - const iconState = expanded ? 'minus' : 'plus'; - const titlePrefix = expanded - ? intl.formatMessage({ - defaultMessage: 'Collapse', - description: 'HeadingButton: title prefix (expanded state)', - id: 'UX9Bu8', - }) - : intl.formatMessage({ - defaultMessage: 'Expand', - description: 'HeadingButton: title prefix (collapsed state)', - id: 'bcyOgC', - }); - - const toggleExpand = useCallback( - () => setExpanded((prev) => !prev), - [setExpanded] - ); - - return ( - - ); -}; diff --git a/src/components/molecules/buttons/index.ts b/src/components/molecules/buttons/index.ts index e0a10c1..b2930aa 100644 --- a/src/components/molecules/buttons/index.ts +++ b/src/components/molecules/buttons/index.ts @@ -1,3 +1,2 @@ export * from './back-to-top'; -export * from './heading-button'; export * from './help-button'; diff --git a/src/components/molecules/collapsible/collapsible.module.scss b/src/components/molecules/collapsible/collapsible.module.scss new file mode 100644 index 0000000..3c5a97c --- /dev/null +++ b/src/components/molecules/collapsible/collapsible.module.scss @@ -0,0 +1,100 @@ +@use "../../../styles/abstracts/functions" as fun; +@use "../../../styles/abstracts/mixins" as mix; + +.heading { + display: flex; + flex-flow: row nowrap; + align-items: center; + justify-content: space-between; + gap: var(--spacing-md); + width: 100%; + padding: var(--spacing-2xs); + position: sticky; + top: 0; + z-index: 2; + background: var(--color-bg); + border-top: fun.convert-px(2) solid var(--color-primary-dark); + border-bottom: fun.convert-px(2) solid var(--color-primary-dark); + + &:hover, + &:focus { + .icon { + background: var(--color-primary-light); + color: var(--color-fg-inverted); + transform: scale(1.25); + + &::before, + &::after { + background: var(--color-bg); + } + } + } +} + +.body { + &--has-borders { + border: fun.convert-px(2) solid var(--color-primary-dark); + } + + &--has-padding { + padding-inline: var(--spacing-2xs); + } +} + +.wrapper { + display: flex; + flex-flow: column; + + @include mix.media("screen") { + @include mix.dimensions("lg") { + max-height: calc(100vh - var(--spacing-2xs)); + + &--expanded { + .body { + overflow: hidden; + } + + &:hover, + &:focus-within { + .body { + overflow-y: auto; + } + } + } + } + } + + &--collapsed { + .body { + max-height: 0; + opacity: 0; + overflow: hidden; + visibility: hidden; + transition: + all 0.2s linear 0.2s, + max-height 0.5s cubic-bezier(0, 1, 0, 1); + } + } + + &--expanded { + .body { + max-height: 10000px; // Fixed value needed for transition. + opacity: 1; + visibility: visible; + transition: + all 0.5s ease-in-out 0s, + margin 0.2s ease-in-out 0s, + padding 0.2s ease-in-out 0s, + max-height 1.2s ease-in-out; + + &--has-padding { + margin: var(--spacing-2xs) 0; + padding-block: var(--spacing-2xs); + } + + &--no-padding { + margin: var(--spacing-xs) 0; + } + } + } +} diff --git a/src/components/molecules/collapsible/collapsible.stories.tsx b/src/components/molecules/collapsible/collapsible.stories.tsx new file mode 100644 index 0000000..7cac64d --- /dev/null +++ b/src/components/molecules/collapsible/collapsible.stories.tsx @@ -0,0 +1,72 @@ +import type { ComponentMeta, ComponentStory } from '@storybook/react'; +import { Heading } from '../../atoms'; +import { Collapsible } from './collapsible'; + +/** + * HeadingButton - Storybook Meta + */ +export default { + title: 'Molecules/Collapsible', + component: Collapsible, + argTypes: { + heading: { + control: { + type: 'text', + }, + description: 'Define the collapsible heading.', + type: { + name: 'function', + required: true, + }, + }, + isCollapsed: { + control: { + type: 'boolean', + }, + description: 'Define if the component should be collapsed or expanded.', + table: { + category: 'Options', + defaultValue: { summary: false }, + }, + type: { + name: 'boolean', + required: true, + }, + }, + }, +} as ComponentMeta; + +const Template: ComponentStory = ({ heading, ...args }) => ( + + {heading} + + } + /> +); + +const heading = 'Your title'; +const body = + 'Eius et eum ex voluptas laboriosam aliquid quas necessitatibus. Molestiae eius voluptatem qui voluptas eaque et totam. Ut ipsum ea sit. Quos molestiae id est consequatur. Suscipit illo at. Omnis non suscipit. Qui itaque laboriosam quos ut est laudantium. Iusto recusandae excepturi quia labore voluptatem quod recusandae. Quod ducimus ut rem dolore et.'; + +/** + * Collapsible Stories - Collapsed + */ +export const Collapsed = Template.bind({}); +Collapsed.args = { + children: body, + heading, + isCollapsed: true, +}; + +/** + * Collapsible Stories - Expanded + */ +export const Expanded = Template.bind({}); +Expanded.args = { + children: body, + heading, + isCollapsed: false, +}; diff --git a/src/components/molecules/collapsible/collapsible.test.tsx b/src/components/molecules/collapsible/collapsible.test.tsx new file mode 100644 index 0000000..52fbdd0 --- /dev/null +++ b/src/components/molecules/collapsible/collapsible.test.tsx @@ -0,0 +1,83 @@ +import { describe, expect, it } from '@jest/globals'; +import { render, screen as rtlScreen } from '@testing-library/react'; +import { userEvent } from '@testing-library/user-event'; +import { Collapsible } from './collapsible'; +import { Heading } from 'src/components/atoms'; + +const body = + 'Error autem numquam vero quo cum qui voluptatem est qui. Quasi id rem molestiae. Velit voluptatum dolores et officia. Ut voluptatem quae ut quaerat vel nulla.'; +const heading = 'sed minima sed'; + +describe('Collapsible', () => { + it('renders the collapsible heading and body', () => { + const headingLevel = 2; + + render( + {heading}}> + {body} + + ); + + expect( + rtlScreen.getByRole('heading', { level: headingLevel }) + ).toHaveTextContent(heading); + expect(rtlScreen.getByText(body)).toBeInTheDocument(); + }); + + it('can be collapsed by default', () => { + render( + {heading}} isCollapsed> + {body} + + ); + + expect(rtlScreen.getByRole('button').parentElement).toHaveClass( + 'wrapper--collapsed' + ); + // Neither toBeVisible or toHaveStyle are working. + // expect(rtlScreen.getByText(body)).toHaveStyle({ visibility: 'hidden' }); + // expect(rtlScreen.getByText(body)).not.toBeVisible(); + }); + + it('can be expanded by default', () => { + render( + {heading}} + isCollapsed={false} + > + {body} + + ); + + expect(rtlScreen.getByRole('button').parentElement).toHaveClass( + 'wrapper--expanded' + ); + expect(rtlScreen.getByText(body)).toBeVisible(); + }); + + it('can be collapsed and/or expanded by the user', async () => { + const user = userEvent.setup(); + + render( + {heading}}> + {body} + + ); + + expect(rtlScreen.getByRole('button').parentElement).toHaveClass( + 'wrapper--expanded' + ); + + await user.click(rtlScreen.getByRole('button', { name: heading })); + + expect(rtlScreen.getByRole('button').parentElement).toHaveClass( + 'wrapper--collapsed' + ); + + await user.click(rtlScreen.getByRole('button', { name: heading })); + + expect(rtlScreen.getByRole('button').parentElement).toHaveClass( + 'wrapper--expanded' + ); + }); +}); diff --git a/src/components/molecules/collapsible/collapsible.tsx b/src/components/molecules/collapsible/collapsible.tsx new file mode 100644 index 0000000..e61ccba --- /dev/null +++ b/src/components/molecules/collapsible/collapsible.tsx @@ -0,0 +1,108 @@ +import { + useCallback, + type ForwardRefRenderFunction, + type HTMLAttributes, + type ReactNode, + forwardRef, + useId, + useState, +} from 'react'; +import { Button, Icon } from '../../atoms'; +import styles from './collapsible.module.scss'; + +export type CollapsibleProps = Omit< + HTMLAttributes, + 'children' +> & { + /** + * The collapsible body. + */ + children: ReactNode; + /** + * Should we disable padding around body? + * + * @default false + */ + disablePadding?: boolean; + /** + * Should the body be bordered? + * + * @default false + */ + hasBorders?: boolean; + /** + * The collapsible heading. + */ + heading: ReactNode; + /** + * Should the component be collapsed or expanded by default? + * + * @default false + */ + isCollapsed?: boolean; +}; + +const CollapsibleWithRef: ForwardRefRenderFunction< + HTMLDivElement, + CollapsibleProps +> = ( + { + children, + className = '', + disablePadding = false, + hasBorders = false, + heading, + isCollapsed = false, + ...props + }, + ref +) => { + const bodyId = useId(); + const [isExpanded, setIsExpanded] = useState(!isCollapsed); + const bodyClassNames = [ + styles.body, + hasBorders ? styles['body--has-borders'] : '', + styles[disablePadding ? 'body--no-padding' : 'body--has-padding'], + ]; + const wrapperClassNames = [ + styles.wrapper, + styles[isExpanded ? 'wrapper--expanded' : 'wrapper--collapsed'], + className, + ]; + + const handleState = useCallback(() => { + setIsExpanded((prevState) => !prevState); + }, []); + + return ( +
+ +
+ {children} +
+
+ ); +}; + +/** + * Collapsible component. + * + * Render a heading associated to a collapsible body. + */ +export const Collapsible = forwardRef(CollapsibleWithRef); diff --git a/src/components/molecules/collapsible/index.ts b/src/components/molecules/collapsible/index.ts new file mode 100644 index 0000000..20c0813 --- /dev/null +++ b/src/components/molecules/collapsible/index.ts @@ -0,0 +1 @@ +export * from './collapsible'; diff --git a/src/components/molecules/index.ts b/src/components/molecules/index.ts index dae369b..a62f3bf 100644 --- a/src/components/molecules/index.ts +++ b/src/components/molecules/index.ts @@ -1,4 +1,5 @@ export * from './buttons'; +export * from './collapsible'; export * from './forms'; export * from './images'; export * from './layout'; diff --git a/src/components/molecules/layout/index.ts b/src/components/molecules/layout/index.ts index 9fa1216..1580baa 100644 --- a/src/components/molecules/layout/index.ts +++ b/src/components/molecules/layout/index.ts @@ -5,4 +5,3 @@ export * from './columns'; export * from './meta'; export * from './page-footer'; export * from './page-header'; -export * from './widget'; diff --git a/src/components/molecules/layout/widget.module.scss b/src/components/molecules/layout/widget.module.scss deleted file mode 100644 index 1a601e5..0000000 --- a/src/components/molecules/layout/widget.module.scss +++ /dev/null @@ -1,65 +0,0 @@ -@use "../../../styles/abstracts/functions" as fun; -@use "../../../styles/abstracts/mixins" as mix; - -.widget { - display: flex; - flex-flow: column; - - &__header { - z-index: 2; - background: var(--color-bg); - } - - &__body { - position: relative; - } - - &--has-borders & { - &__body { - border: fun.convert-px(2) solid var(--color-primary-dark); - } - } - - &--collapsed & { - &__body { - max-height: 0; - margin: 0; - visibility: hidden; - opacity: 0; - overflow: hidden; - border: 0 solid transparent; - transition: all 0.1s linear 0.3s, - max-height 0.5s cubic-bezier(0, 1, 0, 1) 0s, margin 0.3s ease-in-out 0s; - } - } - - &--expanded#{&}--has-scroll { - @include mix.media("screen") { - @include mix.dimensions("lg") { - max-height: 95vh; - - .widget__body { - overflow: hidden; - } - - &:hover, - &:focus-within { - .widget__body { - overflow-y: auto; - } - } - } - } - } - - &--expanded & { - &__body { - max-height: 10000px; // needs a fixed value for transition. - margin: var(--spacing-sm) 0; - opacity: 1; - visibility: visible; - transition: all 0.5s ease-in-out 0s, border 0s linear 0s, - max-height 0.6s ease-in-out 0s; - } - } -} diff --git a/src/components/molecules/layout/widget.stories.tsx b/src/components/molecules/layout/widget.stories.tsx deleted file mode 100644 index 8fb868d..0000000 --- a/src/components/molecules/layout/widget.stories.tsx +++ /dev/null @@ -1,117 +0,0 @@ -import { ComponentMeta, ComponentStory } from '@storybook/react'; -import headingButtonStories from '../buttons/heading-button.stories'; -import { Widget } from './widget'; - -/** - * Widget - Storybook Meta - */ -export default { - title: 'Molecules/Layout/Widget', - component: Widget, - args: { - withBorders: false, - withScroll: false, - }, - argTypes: { - children: { - control: { - type: 'text', - }, - description: 'The widget body', - type: { - name: 'string', - required: true, - }, - }, - className: { - control: { - type: 'text', - }, - description: 'Set additional classnames to the widget wrapper.', - table: { - category: 'Styles', - }, - type: { - name: 'string', - required: false, - }, - }, - expanded: { - control: { - type: 'boolean', - }, - description: 'The widget state (expanded or collapsed)', - table: { - category: 'Options', - defaultValue: { summary: true }, - }, - type: { - name: 'boolean', - required: false, - }, - }, - level: headingButtonStories.argTypes?.level, - title: { - control: { - type: 'text', - }, - description: 'The widget title.', - type: { - name: 'string', - required: true, - }, - }, - withBorders: { - control: { - type: 'boolean', - }, - description: 'Define if the content should have borders.', - table: { - category: 'Options', - defaultValue: { summary: false }, - }, - type: { - name: 'boolean', - required: false, - }, - }, - withScroll: { - control: { - type: 'boolean', - }, - description: 'Define if the widget should be scrollable', - table: { - category: 'Options', - defaultValue: { summary: false }, - }, - type: { - name: 'boolean', - required: false, - }, - }, - }, -} as ComponentMeta; - -const Template: ComponentStory = (args) => ; - -/** - * Widget Stories - Expanded - */ -export const Expanded = Template.bind({}); -Expanded.args = { - children: 'Widget body', - expanded: true, - level: 2, - title: 'Widget title', -}; - -/** - * Widget Stories - Collapsed - */ -export const Collapsed = Template.bind({}); -Collapsed.args = { - children: 'Widget body', - expanded: false, - level: 2, - title: 'Widget title', -}; diff --git a/src/components/molecules/layout/widget.test.tsx b/src/components/molecules/layout/widget.test.tsx deleted file mode 100644 index 21c7a3c..0000000 --- a/src/components/molecules/layout/widget.test.tsx +++ /dev/null @@ -1,20 +0,0 @@ -import { describe, expect, it } from '@jest/globals'; -import { render, screen } from '../../../../tests/utils'; -import { Widget } from './widget'; - -const children = 'Widget body'; -const title = 'Widget title'; -const titleLevel = 2; - -describe('Widget', () => { - it('renders the widget title', () => { - render( - - {children} - - ); - expect( - screen.getByRole('heading', { level: titleLevel }) - ).toHaveTextContent(title); - }); -}); diff --git a/src/components/molecules/layout/widget.tsx b/src/components/molecules/layout/widget.tsx deleted file mode 100644 index 0bb04c7..0000000 --- a/src/components/molecules/layout/widget.tsx +++ /dev/null @@ -1,61 +0,0 @@ -import { FC, ReactNode, useState } from 'react'; -import { HeadingButton, type HeadingButtonProps } from '../buttons'; -import styles from './widget.module.scss'; - -export type WidgetProps = Pick< - HeadingButtonProps, - 'expanded' | 'level' | 'title' -> & { - /** - * The widget body. - */ - children: ReactNode; - /** - * Set additional classnames to the widget wrapper. - */ - className?: string; - /** - * Determine if the widget body should have borders. Default: false. - */ - withBorders?: boolean; - /** - * Determine if a vertical scrollbar should be displayed. Default: false. - */ - withScroll?: boolean; -}; - -/** - * Widget component - * - * Render an expandable widget. - */ -export const Widget: FC = ({ - children, - className = '', - expanded = true, - level, - title, - withBorders = false, - withScroll = false, -}) => { - const [isExpanded, setIsExpanded] = useState(expanded); - const stateClass = isExpanded ? 'widget--expanded' : 'widget--collapsed'; - const bordersClass = withBorders - ? 'widget--has-borders' - : 'widget--no-borders'; - const scrollClass = withScroll ? 'widget--has-scroll' : 'widget--no-scroll'; - const widgetClass = `${styles.widget} ${styles[bordersClass]} ${styles[stateClass]} ${styles[scrollClass]} ${className}`; - - return ( -
- -
{children}
-
- ); -}; -- cgit v1.2.3