Skip to content

refactor(tools): Alert component - #45015

Merged
ahmaxed merged 3 commits into
freeCodeCamp:mainfrom
huyenltnguyen:chore/alert
Feb 9, 2022
Merged

ahmaxed merged 3 commits into
freeCodeCamp:mainfrom
huyenltnguyen:chore/alert

Conversation

@huyenltnguyen

Copy link
Copy Markdown
Member

Checklist:

  • I have read freeCodeCamp's contribution guidelines.
  • My pull request has a descriptive title (not a vague title like Update index.md)
  • My pull request targets the main branch of freeCodeCamp.
  • I have tested these changes either locally on my machine, or GitPod.

This PR:

  • Swaps the order of the alert message and the close button. At the moment the close button is rendered first in the DOM. This is not an issue for sighted users, but for non-sighted users, screen readers will announce it before the alert message, which might be confusing for them.
  • Refactors the Storybook page of the Alert component. I reckon it would be easier to follow the changes with the code, so I will explain the changes and the motivation in the comments.
  • Removes a default Storybook config that automatically injects an action to each on* prop. This is part of the storybook refactoring, I will elaborate in the comments.

The changes were split into separate commits to help make it easier to review.

Screenshots

Storybook improvements

Before After
Component examples Screen Shot 2022-02-04 at 18 25 01 Screen Shot 2022-02-04 at 17 49 38
Code snippet Screen Shot 2022-02-04 at 18 23 40 Screen Shot 2022-02-04 at 18 24 04

@gitpod-io

gitpod-io Bot commented Feb 4, 2022

Copy link
Copy Markdown

@github-actions github-actions Bot added the scope: tools/scripts Scripts for supporting dev work, generating config and build artifacts, etc. label Feb 4, 2022
Comment on lines -17 to -18
export const Basic = Template.bind({});
Basic.args = {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the Basic story because the name may imply that this is the default state of the component. But that would be misleading because our component does not have a default state as variant, children, and the dismissable state are required to be specified.

Instead, I added a story for each variant we support, as well as renaming the "With Close Button" story to "Dismissable". The rename is just me being nit-picky, I reckoned that the dismissable state is associated with the onDismiss prop, so I thought the naming would make it more self-explanatory.

Note that I did not include onDismiss as a prop in the stories, except for the "Dismissable" one, since onDismiss is undefined by default.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in the codebase flash messages are dismissible but not alert themselves. That is why we have build some traditions and on handleClose in the Flash component. It might make sense to think of flash as a separate parent component at the moment.

We could combine the two at some point in the future once we have rolled out the library.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pr should be good to go as soon we remove the Dismissable variation. Please feel free to discuss the matter further if necessary if you don't find the point made above convincing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will get this pr in for now so we could keep moving forward, and we could further discuss the Dismissable variation if needed in details.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is my proposal:

  • We will remove dismissability-related props from the Alert component, to make it non-dismissable
  • Build a Flash component with props for dismiss action. The props should be required to give a clear distinction between the two

As for the implementation approach, we can either:

  1. Create a BaseAlert component that has the alert styling applied and supports all props. Then have Alert built upon it but prohibit dismissability-related props (via the props interface, thanks to TypeScript). Flash would also be built upon BaseAlert, but have stricter props interface to enforce the dismissability; or
  2. Basically just duplicate the code and keep Alert and Flash not related to each other.

(2) is simpler to build while (1) is a bit more involved, but with mobile and dark theme support, I think going with (1) might be worth it as it allows us to keep the styling between the two components consistent.


Here is the interfaces I have in mind:

type AlertVariant = 'success' | 'info' | 'warning' | 'danger';
type FlashVariant = AlertVariant;

interface BaseAlertProps {
  children: React.ReactNode;
  className?: string;
  variant: AlertVariant;
  dismissLabel?: string;
  onDismiss?: () => void;
} 

interface AlertProps {
  children: React.ReactNode;
  className?: string;
  variant: AlertVariant;
}

interface FlashProps {
  children: React.ReactNode;
  className?: string;
  variant: FlashVariant; // This can totally be AlertVariant, but the naming might be confusing
  dismissLabel: string; // <- required prop
  onDismiss: () => void; // <- required prop
}

Comment on lines +41 to +46
export const LongText = Template.bind({});
LongText.args = {
variant: 'success',
children:
'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Amet animi commodi cumque dicta ducimus eum iure, maiores mollitia, odit porro quas quod rerum soluta sunt tempora unde, vel voluptas voluptates.'
};

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also wrote stories using the args object instead of rendering JSX. This is to help improve the code snippets, as shown in the screenshot.

@@ -1,5 +1,4 @@
export const parameters = {
actions: { argTypesRegex: '^on[A-Z].*' },

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a default Storybook config. It automatically adds a dummy function to every prop that starts with on*. This means every example of our Alert component is dismissable regardless of whether or not we pass the onDismiss prop as an arg.

I removed this config to give us more control.

Comment on lines +42 to +44
<div className='grow'>{children}</div>
{isDismissable && (
<CloseButton
className='absolute right-4'
label={dismissLabel}
onClick={onDismiss}
/>
<CloseButton label={dismissLabel} onClick={onDismiss} />

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from swapping the DOM order, I took the opportunity to change the styling approach a bit.

Currently we are positioning the close button using absolute and spacing classes (right-4 and pr-10). Personally I would avoid playing with spacing values when I can, just because I think the layout would be easier to break.

I changed the implementation a bit and use Flex instead. The wrapper div is a flex container with its items (the message and the button) aligned at the start of the cross axis (via the flex and items-start classes). The message is wrapped in a div that has flex-grow set to 1 (via the grow class) in order for it to stretch and fill the container.

There should be no regressions or visual changes as a result of this.

@huyenltnguyen
huyenltnguyen marked this pull request as ready for review February 4, 2022 12:19
@huyenltnguyen
huyenltnguyen requested a review from a team February 4, 2022 12:19
@ahmaxed
ahmaxed merged commit 95e473d into freeCodeCamp:main Feb 9, 2022
@huyenltnguyen
huyenltnguyen deleted the chore/alert branch February 9, 2022 07:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: tools/scripts Scripts for supporting dev work, generating config and build artifacts, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants