Repository navigation
feat(tools): add Alert component - #43835
Conversation
# Conflicts: # tools/ui-components/tailwind.config.js
|
👀 Review this PR in a CodeSee Review Map |
| type AlertVariant = 'success' | 'info' | 'warning' | 'danger'; | ||
|
|
||
| export interface AlertProps { | ||
| children: React.ReactNode; | ||
| className?: string; | ||
| variant?: AlertVariant; | ||
| dismissText?: string; | ||
| onDismiss?: () => void; | ||
| } |
There was a problem hiding this comment.
Should these types be extracted to alert.types.ts?
There was a problem hiding this comment.
Since they are a few, they could stay in the components.
| import React from 'react'; | ||
| import './alert.css'; | ||
|
|
||
| type AlertVariant = 'success' | 'info' | 'warning' | 'danger'; |
There was a problem hiding this comment.
Maybe it should be an enum?
| .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; | ||
| } |
There was a problem hiding this comment.
I haven't found any usage of alert-link and hr, so these probably are not needed.
There was a problem hiding this comment.
We can remove it and add it if/when needed.
| .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; | ||
| } |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Yes, we can define them as hex in colors.css and add them to tailwind.config.js
| .alert h4 { | ||
| margin-top: 0; | ||
| color: inherit; | ||
| } | ||
| .alert > p, | ||
| .alert > ul { | ||
| margin-bottom: 0; | ||
| } | ||
| .alert > p + p { | ||
| margin-top: 5px; | ||
| } |
There was a problem hiding this comment.
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>There was a problem hiding this comment.
You mean attaching classes to those sub-components and style them directly?
There was a problem hiding this comment.
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;
}|
@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. |
| <button | ||
| aria-label={dismissText} | ||
| className='close' | ||
| onClick={onDismiss} | ||
| type='button' | ||
| > | ||
| × | ||
| </button> |
There was a problem hiding this comment.
This is alright for now. If ended up building our own modal, we might extract this into its own component.
There was a problem hiding this comment.
It was pretty straightforward, so I extracted it already as a part of this PR
Yes, it seems to be a straight forward component. Tests for different props and accessibility should be sufficient.
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:
The topic is open to discussion, so let us know if a better idea comes to mind.
This was brought to my attention by @hamzawaleed0102 as well and it seems to be the convention, so yes. |
|
@ahmadabdolsaheb thanks for review and helping me out with all the questions I've had :) After recent changes:
|
There was a problem hiding this comment.
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:
- It would be great if you could hide the unnecessary options such as "undefined" variant from the stories.
- The dark/light toggle should be added to the story.
| children, | ||
| className, | ||
| variant = 'info', | ||
| dismissText = 'Close', |
There was a problem hiding this comment.
Since this is the text of a label, dismissLabel or closeLabel seems more expressive.
| --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; |
There was a problem hiding this comment.
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 )
There was a problem hiding this comment.
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:
Because existing values (--yellow10, etc.) are not yet used anywhere, I was thinking about modifying them slightly. Something like:
What do you think about it?
| component: Alert | ||
| }; | ||
|
|
||
| const Template: Story<AlertProps> = args => <Alert {...args} />; |
There was a problem hiding this comment.
Stories should reflect the actual behavior of the component, so the closing functionality should also be added.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
What do you think?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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' | ||
| }; | ||
|
|
There was a problem hiding this comment.
An alert without the close button should also be added.
|
@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. |
|
@ahmadabdolsaheb, I added some small updates regarding the things we've discussed, and opened a separate PR with a theme toggle :) |
|
@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 |
|
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 😀 |
|
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! |






Checklist:
Update index.md)mainbranch of freeCodeCamp.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 :)
classNameprop to customize its style,alert.csswhere I placed all the old Alert related CSS, so that we can think about how and where these styles should be extracted.src/alertdirectory, 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
childrenbsStyle- success, info, etc.classNameonDismissonClickhandlerclassNamespassed as a proponDismissprop, and when it's there it shows close button, and invokeonDismisswhen it's clicked.alertclassDevelopment
childrenpropclassNamepropvariant- replacing oldbsStyle(success, info, etc.)variant- used by ReactBootstrap v2type- used by AntDesignseverity- used by MaterialUIstatus- used by ChakraUIonDismissanddismissTextpropsonCloseandcloseTextUseful links
cc: @huyenltnguyen