Skip to content

feat(tools): add Alert component - #43835

Merged
ahmaxed merged 20 commits into
freeCodeCamp:mainfrom
sitek94:ui-components-alert
Nov 27, 2021
Merged

ahmaxed merged 20 commits into
freeCodeCamp:mainfrom
sitek94:ui-components-alert

Conversation

@sitek94

@sitek94 sitek94 commented Oct 13, 2021

Copy link
Copy Markdown
Contributor

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.

Is part of #41580

Hello 👋

Last days I've been working on creating an alert component for #41580.

It's still a work in progress, but there are some things that I'd like to discuss, and some questions that I'd like to ask, so I'm opening this draft PR, and I'll also add comments to the code :)

  • From a functionality perspective, I don't think that there is much to add. The component:
    • is accessible,
    • can have 4 different variants,
    • accepts className prop to customize its style,
    • renders the children,
    • conditionally shows a close button.
  • I've written some test cases to address mentioned functionality.
  • In the PR, you can find a temporary alert.css where I placed all the old Alert related CSS, so that we can think about how and where these styles should be extracted.
  • For now, I placed all alert related files in src/alert directory, since there will be at least 3 files (.tsx, .story.tsx, .test.tsx) I thought that this will keep things more organized. What do you think about it?

Development steps

Below are the steps I followed, when working on this. Hopefully, someone will find it useful, when starting to work on other components.

Research

  • Find component usage in the project and list all the props being used.
    • children
    • bsStyle - success, info, etc.
    • className
    • onDismiss
      • when present, show the close button
      • it should be passed down to the close button onClick handler
  • List out all the classNames passed as a prop
    • donation-completion
    • flash-message
    • text-center
  • Compare usage and props with React-Bootstrap documentation.
  • Find out how does close button work.
    • Alert doesn't handle its own visibility state.
    • It simply accepts onDismiss prop, and when it's there it shows close button, and invoke onDismiss when it's clicked.
  • Find out which Bootstrap classes are applied by default.
  • Find where alert related classes are being used.
    • Cypress tests - some selectors target alert class
    • Donation.css

Development

  • Initialize Alert component.
  • Add children prop
  • Add className prop
  • Add variant - replacing old bsStyle(success, info, etc.)
  • Add onDismiss and dismissText props
    • Alternatively, they could be called, e.g. onClose and closeText

Useful links


cc: @huyenltnguyen

@gitpod-io

gitpod-io Bot commented Oct 13, 2021

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 Oct 13, 2021
@ghost

ghost commented Oct 13, 2021 •

Copy link
Copy Markdown

👀 Review this PR in a CodeSee Review Map

Preview the Review Map

Review these changes using an interactive CodeSee Map

Legend

CodeSee Map Legend

@sitek94 sitek94 mentioned this pull request Oct 13, 2021
7 of 43 tasks
Comment on lines +4 to +12
type AlertVariant = 'success' | 'info' | 'warning' | 'danger';

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should these types be extracted to alert.types.ts?

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.

Since they are a few, they could stay in the components.

import React from 'react';
import './alert.css';

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Maybe it should be an enum?

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.

Either way should work.

Comment thread tools/ui-components/src/alert/alert.css Outdated
Comment on lines +98 to +126
.alert .alert-link {
font-weight: bold;
}

.alert-success .alert-link {
color: #2b542c;
}
.alert-info .alert-link {
color: #245269;
}
.alert-warning .alert-link {
color: #66512c;
}
.alert-danger .alert-link {
color: #843534;
}

.alert-success hr {
border-top-color: #c9e2b3;
}
.alert-info hr {
border-top-color: #a6e1ec;
}
.alert-warning hr {
border-top-color: #f7e1b5;
}
.alert-danger hr {
border-top-color: #e4b9c0;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I haven't found any usage of alert-link and hr, so these probably are not needed.

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.

We can remove it and add it if/when needed.

Comment thread tools/ui-components/src/alert/alert.css Outdated
Comment on lines +74 to +93
.alert-success {
color: #3c763d;
background-color: #dff0d8;
border-color: #d6e9c6;
}
.alert-info {
color: #31708f;
background-color: #d9edf7;
border-color: #bce8f1;
}
.alert-warning {
color: #8a6d3b;
background-color: #fcf8e3;
border-color: #faebcc;
}
.alert-danger {
color: #a94442;
background-color: #f2dede;
border-color: #ebccd1;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These hard coded color values are coming from _variables.scss in Bootstrap 3.3.7. It looks like this:

//== Form states and alerts
//
//## Define colors for form feedback states and, by default, alerts.

$state-success-text:             #3c763d !default;
$state-success-bg:               #dff0d8 !default;
$state-success-border:           darken(adjust-hue($state-success-bg, -10), 5%) !default;

$state-info-text:                #31708f !default;
$state-info-bg:                  #d9edf7 !default;
$state-info-border:              darken(adjust-hue($state-info-bg, -10), 7%) !default;

$state-warning-text:             #8a6d3b !default;
$state-warning-bg:               #fcf8e3 !default;
$state-warning-border:           darken(adjust-hue($state-warning-bg, -10), 5%) !default;

$state-danger-text:              #a94442 !default;
$state-danger-bg:                #f2dede !default;
$state-danger-border:            darken(adjust-hue($state-danger-bg, -10), 5%) !default;

We probably want to reuse the exact same colors and add them to tailwind.config.js, right?

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.

Yes, we can define them as hex in colors.css and add them to tailwind.config.js

Comment thread tools/ui-components/src/alert/alert.css Outdated
Comment on lines +18 to +28
.alert h4 {
margin-top: 0;
color: inherit;
}
.alert > p,
.alert > ul {
margin-bottom: 0;
}
.alert > p + p {
margin-top: 5px;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should we go this way for styling the children components, or maybe do something like Material UI and create a helper AlertTitle component?

<Alert>
  <AlertTitle>Error</AlertTitle>
  This is an error alert — <strong>check it out!</strong>
</Alert>

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.

You mean attaching classes to those sub-components and style them directly?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually, now that I've taken a closer look at this, I see that this won't be necessary, and we can simply remove these
Bootstrap styles :)

This is just a simple reset, that we're getting out of the box from tailwind:

.alert h4 {
  margin-top: 0;
  color: inherit;
}
.alert > p,
.alert > ul {
  margin-bottom: 0;
}

And this is never used:

.alert > p + p {
  margin-top: 5px;
}

@ahmaxed

ahmaxed commented Oct 15, 2021 •

Copy link
Copy Markdown
Member

@sitek94, thank you for setting the standard for the UI component development. 🔥

Once this pr is ready, we can update our docs with the description you provided above, build component library, and replace the alert components in the learn client to see if there are any incompatibilities.

Comment thread tools/ui-components/src/alert/alert.tsx Outdated
Comment on lines +43 to +50
<button
aria-label={dismissText}
className='close'
onClick={onDismiss}
type='button'
>
×
</button>

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 is alright for now. If ended up building our own modal, we might extract this into its own component.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It was pretty straightforward, so I extracted it already as a part of this PR

@ahmaxed

ahmaxed commented Oct 15, 2021

Copy link
Copy Markdown
Member

From a functionality perspective, I don't think that there is much to add. The component:
is accessible,
can have 4 different variants,
accepts className prop to customize its style,
renders the children,
conditionally shows a close button.
I've written some test cases to address mentioned functionality.

Yes, it seems to be a straight forward component. Tests for different props and accessibility should be sufficient.

In the PR, you can find a temporary alert.css where I placed all the old Alert related CSS, so that we can think about how and where these styles should be extracted.

Ultimately we would like have the tailwind equivalent of the styles that the browser computes for each element minus the normalizer styles(tailwind will add those).

Here are a few options moving forward:

  • Dissecting the minified bootstrap for each component addition and replacement so we could remove the related styles to make sure the component is compatible by its own.

  • Create all components and remove minified bootstrap at the end when replacing them.

  • Keep the bootstrap styles in each component and replace it immediately.

The topic is open to discussion, so let us know if a better idea comes to mind.

For now, I placed all alert related files in src/alert directory, since there will be at least 3 files (.tsx, .story.tsx, .test.tsx) I thought that this will keep things more organized. What do you think about it?

This was brought to my attention by @hamzawaleed0102 as well and it seems to be the convention, so yes.

@sitek94

sitek94 commented Oct 18, 2021

Copy link
Copy Markdown
Contributor Author

@ahmadabdolsaheb thanks for review and helping me out with all the questions I've had :)

After recent changes:

  • the component is now completely independent of Bootstrap styles and uses only Tailwind classes,
  • I extracted Bootstrap's SCSS variables, defined our own in colors.css and added them in Tailwind config,
  • As suggested, I extracted Close Button to a separate component

@sitek94
sitek94 marked this pull request as ready for review October 18, 2021 18:49
@sitek94
sitek94 requested a review from a team October 18, 2021 18:49

@ahmaxed ahmaxed left a comment •

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.

Thank you for your patience with the review. This is a well-researched pull request and I cannot wait to see it in productions.

Here are a few links for the latest developments on the bootstrap Alert component for our reference:
https://github.com/react-bootstrap/react-bootstrap/blob/master/src/Alert.tsx
https://github.com/react-bootstrap/react-bootstrap/blob/master/test/AlertSpec.js
Note: The Alert component would need a mounting and closing transition, but that seems out of scope for this pull-request.

Here are a few considerations to make this pr even better:

  1. It would be great if you could hide the unnecessary options such as "undefined" variant from the stories.
  2. The dark/light toggle should be added to the story.

Comment thread tools/ui-components/src/alert/alert.tsx Outdated
children,
className,
variant = 'info',
dismissText = 'Close',

@ahmaxed ahmaxed Oct 28, 2021 •

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.

Since this is the text of a label, dismissLabel or closeLabel seems more expressive.

Comment thread tools/ui-components/src/colors.css Outdated
Comment on lines +35 to +49
--success-10: #dff0d8;
--success-20: #d6e9c6;
--success-90: #3c763d;

--info-10: #d9edf7;
--info-20: #bce8f1;
--info-90: #31708f;

--warning-10: #fcf8e3;
--warning-20: #faebcc;
--warning-90: #8a6d3b;

--danger-10: #f2dede;
--danger-20: #ebccd1;
--danger-90: #a94442;

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.

The colors are named by hue here then converted to semantic names e.g. --green-10
Note if a variable name is taken, please choose a number that is close enough.
Our convention is adding numbers to hue color names based on their darkness from 00-90 ( 90 being the darkest )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I see, thanks for clarifying that. Initially I went with variant based names, because there are some existing colors defined, and I didn't want to mix them up.

Here's how they currently look compared:

Screen Shot 2021-11-04 at 07 03 08

Because existing values (--yellow10, etc.) are not yet used anywhere, I was thinking about modifying them slightly. Something like:

Screen Shot 2021-11-04 at 07 00 14

What do you think about it?

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.

Looks good.

component: Alert
};

const Template: Story<AlertProps> = args => <Alert {...args} />;

@ahmaxed ahmaxed Oct 28, 2021 •

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.

Stories should reflect the actual behavior of the component, so the closing functionality should also be added.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do you mean something like this:

export const TrueStory = (): JSX.Element => {
  const [show, setShow] = React.useState(false);

  return (
    <div>
      <button onClick={() => setShow(true)}>Show Alert</button>
      {show && (
        <Alert onDismiss={() => setShow(false)} variant='danger'>
          Something went wrong
        </Alert>
      )}
    </div>
  );
};

So that we can actually see the alert visibility being toggled?

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.

To my understanding, the alert component should internally have the close functionality so it would work once it is imported in another component.

In the Bootstrap alert component, there is an in internal transition wrapper which should handle the display. It would be great if you could research that and replicate a similar functionality so Alert would work out of the box.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

To my understanding, the alert component should internally have the close functionality so it would work once it is imported in another component.

Yeah, I see your point. I haven't implemented that in our alert, because it seemed like we're not using anywhere that functionality. When looking for "Alert" references in our codebase, I found that most of the time the alert component is used without closing functionality, and the only time we actually pass onDismiss handler to it, is in Flash component, which handles both closing logic and transitions:

https://github.com/freeCodeCamp/freeCodeCamp/blob/main/client/src/components/Flash/index.tsx

flash and alert

What do you think?

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.

That is a valid point. I leave it up to you. we could either combine the two components into one and modify its functionality with props, or have them be two separate things.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I decided to keep them separate for now. I think that we should try to keep the transition to Tailwind as seamless as possible, ideally it should be possible to just swap the imports, and it seems like combining Flash and Alert wouldn't be so straightforward.

className: '',
variant: 'info'
};

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.

An alert without the close button should also be added.

@sitek94

sitek94 commented Nov 8, 2021

Copy link
Copy Markdown
Contributor Author

Hey @ahmadabdolsaheb, once again, thanks for very insightful review!

Regarding your comments:

  1. It would be great if you could hide the unnecessary options such as "undefined" variant from the stories.

Sure, I'll simply make the variant prop required, since it's always passed anyway.

  1. The dark/light toggle should be added to the story.

When it comes to dark/light mode, I have some suggestions based on current implementation in Button component.

  1. The theme wrapper that we're using is definitely too big - it takes 100vh
    Screen Shot 2021-11-08 at 19 57 06
  2. It seems like, Storybook treats all the argTypes as props, that are passed to the component, so when we click "Show code", it gives false results:
    theme prop
const story = {
  title: 'Example/Button',
  component: Button,
  argTypes: {
    theme: {
      options: ['dark', 'light'],
      control: { type: 'radio' },
      defaultValue: 'light'
    }
  }
};
  1. This is the current theme switching logic. Even if we extract it to a separate component, we will have to include it in each and every story.
const Template: Story<ButtonProps> = args => {
  return (
    <div
      className={`flex h-screen justify-center items-center ${
        args.theme === 'dark'
          ? 'dark bg-dark-theme-background'
          : 'light bg-light-theme-background'
      }`}
    >
      <Button {...args} />
    </div>
  );
};

I have some ideas for addressing all these things, but maybe I should open a separate PR, so we can discuss it there, just to keep things organized. What do you think?

@ahmaxed

ahmaxed commented Nov 10, 2021

Copy link
Copy Markdown
Member

@sitek94 the changes to the wrapper sense to me. The current theme wrapper is just a proof of concept. Feel free to optimize it as needed. Having it in another pr would be a good call.

@sitek94

sitek94 commented Nov 21, 2021

Copy link
Copy Markdown
Contributor Author

@ahmadabdolsaheb, I added some small updates regarding the things we've discussed, and opened a separate PR with a theme toggle :)

@ahmaxed

ahmaxed commented Nov 27, 2021

Copy link
Copy Markdown
Member

@sitek94 Thanks for your patience with this. LGMT. I am happy that our first component in the library is a well built one. 🔥

Now that you have the know how, we would appreciate it if you could develop a few more of the components if you are interested.

Also since you have pioneered the building process, feel free to document it in docs similar to: https://contribute.freecodecamp.org/#/how-to-work-on-coding-challenges
https://github.com/freeCodeCamp/freeCodeCamp/blob/main/docs/how-to-work-on-coding-challenges.md

@ahmaxed
ahmaxed merged commit 58701d5 into freeCodeCamp:main Nov 27, 2021
@sitek94

sitek94 commented Dec 7, 2021

Copy link
Copy Markdown
Contributor Author

Thanks @ahmadabdolsaheb for all your time and help with this!

I should have much more free time in January, so I'll try to contribute more then, because to be honest Christmas Spirit is keeping me quite busy right now 😀

@sitek94
sitek94 deleted the ui-components-alert branch December 7, 2021 18:30
@huyenltnguyen

Copy link
Copy Markdown
Member

Hey @sitek94, I really appreciate your time and your contribution. We have new components finally! And the details in the PR description is just beautiful ❤️

Also, thank you @ahmadabdolsaheb for helping with the component library work!


Sincerely apologize for disappearing for so long. I have been slowly catching up with codebase changes as well as organizing my schedule for 2022. I hope I can contribute and collaborate with you guys more 🙂

Happy Holidays!

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.

3 participants