From e97325a2c174a87c29593d1b42b9a1cc1eaf11af Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Wed, 4 Oct 2023 15:06:29 +0200 Subject: refactor(components): rewrite HelpButton component --- .../buttons/help-button/help-button.module.scss | 24 +++++++++++ .../buttons/help-button/help-button.stories.tsx | 50 ++++++++++++++++++++++ .../buttons/help-button/help-button.test.tsx | 13 ++++++ .../molecules/buttons/help-button/help-button.tsx | 42 ++++++++++++++++++ .../molecules/buttons/help-button/index.ts | 1 + 5 files changed, 130 insertions(+) create mode 100644 src/components/molecules/buttons/help-button/help-button.module.scss create mode 100644 src/components/molecules/buttons/help-button/help-button.stories.tsx create mode 100644 src/components/molecules/buttons/help-button/help-button.test.tsx create mode 100644 src/components/molecules/buttons/help-button/help-button.tsx create mode 100644 src/components/molecules/buttons/help-button/index.ts (limited to 'src/components/molecules/buttons/help-button') diff --git a/src/components/molecules/buttons/help-button/help-button.module.scss b/src/components/molecules/buttons/help-button/help-button.module.scss new file mode 100644 index 0000000..2dbba69 --- /dev/null +++ b/src/components/molecules/buttons/help-button/help-button.module.scss @@ -0,0 +1,24 @@ +@use "../../../../styles/abstracts/functions" as fun; +@use "../../../../styles/abstracts/mixins" as mix; + +.btn { + padding: fun.convert-px(7); + + @include mix.pointer("fine") { + padding: fun.convert-px(4); + } + + &[aria-pressed="true"] { + background: var(--color-primary-dark); + border-color: var(--color-primary-dark); + box-shadow: fun.convert-px(1) fun.convert-px(1) 0 0 var(--color-shadow); + + .icon { + :global { + path { + fill: var(--color-fg-inverted); + } + } + } + } +} diff --git a/src/components/molecules/buttons/help-button/help-button.stories.tsx b/src/components/molecules/buttons/help-button/help-button.stories.tsx new file mode 100644 index 0000000..556dd74 --- /dev/null +++ b/src/components/molecules/buttons/help-button/help-button.stories.tsx @@ -0,0 +1,50 @@ +import type { ComponentMeta, ComponentStory } from '@storybook/react'; +import { HelpButton as HelpButtonComponent } from './help-button'; + +/** + * HelpButton - Storybook Meta + */ +export default { + title: 'Molecules/Buttons', + component: HelpButtonComponent, + argTypes: { + className: { + control: { + type: 'text', + }, + description: 'Set additional classnames to the button wrapper.', + table: { + category: 'Options', + }, + type: { + name: 'string', + required: false, + }, + }, + onClick: { + control: { + type: null, + }, + description: 'A callback function to handle click on button.', + table: { + category: 'Events', + }, + type: { + name: 'function', + required: false, + }, + }, + }, +} as ComponentMeta; + +const Template: ComponentStory = (args) => ( + +); + +/** + * Help Button Stories - Level 1 + */ +export const HelpButton = Template.bind({}); +HelpButton.args = { + label: 'Help', +}; diff --git a/src/components/molecules/buttons/help-button/help-button.test.tsx b/src/components/molecules/buttons/help-button/help-button.test.tsx new file mode 100644 index 0000000..f36fba7 --- /dev/null +++ b/src/components/molecules/buttons/help-button/help-button.test.tsx @@ -0,0 +1,13 @@ +import { describe, expect, it } from '@jest/globals'; +import { render, screen as rtlScreen } from '@testing-library/react'; +import { HelpButton } from './help-button'; + +describe('Help', () => { + it('renders a help button', () => { + const label = 'hic'; + + render(); + + expect(rtlScreen.getByRole('button')).toHaveAccessibleName(label); + }); +}); diff --git a/src/components/molecules/buttons/help-button/help-button.tsx b/src/components/molecules/buttons/help-button/help-button.tsx new file mode 100644 index 0000000..1951b4d --- /dev/null +++ b/src/components/molecules/buttons/help-button/help-button.tsx @@ -0,0 +1,42 @@ +import { forwardRef, type ForwardRefRenderFunction } from 'react'; +import { Button, VisuallyHidden, type ButtonProps, Icon } from '../../../atoms'; +import styles from './help-button.module.scss'; + +export type HelpButtonProps = Omit< + ButtonProps, + 'aria-label' | 'children' | 'kind' | 'shape' +> & { + /** + * Define an accessible name for the button. + */ + label: string; +}; + +const HelpButtonWithRef: ForwardRefRenderFunction< + HTMLButtonElement, + HelpButtonProps +> = ({ className = '', isPressed = false, label, ...props }, ref) => { + const btnClass = `${styles.btn} ${className}`; + + return ( + + ); +}; + +/** + * HelpButton component + * + * Render a button with an interrogation mark icon. + */ +export const HelpButton = forwardRef(HelpButtonWithRef); diff --git a/src/components/molecules/buttons/help-button/index.ts b/src/components/molecules/buttons/help-button/index.ts new file mode 100644 index 0000000..952119e --- /dev/null +++ b/src/components/molecules/buttons/help-button/index.ts @@ -0,0 +1 @@ +export * from './help-button'; -- cgit v1.2.3