Repository navigation
refactor(tools): Alert component - #45015
Conversation
fd90ad7 to
4e23081
Compare
| export const Basic = Template.bind({}); | ||
| Basic.args = { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Here is my proposal:
- We will remove dismissability-related props from the
Alertcomponent, to make it non-dismissable - Build a
Flashcomponent 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:
- Create a
BaseAlertcomponent that has the alert styling applied and supports all props. Then haveAlertbuilt upon it but prohibit dismissability-related props (via the props interface, thanks to TypeScript).Flashwould also be built uponBaseAlert, but have stricter props interface to enforce the dismissability; or - Basically just duplicate the code and keep
AlertandFlashnot 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
}| 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.' | ||
| }; |
There was a problem hiding this comment.
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].*' }, | |||
There was a problem hiding this comment.
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.
| <div className='grow'>{children}</div> | ||
| {isDismissable && ( | ||
| <CloseButton | ||
| className='absolute right-4' | ||
| label={dismissLabel} | ||
| onClick={onDismiss} | ||
| /> | ||
| <CloseButton label={dismissLabel} onClick={onDismiss} /> |
There was a problem hiding this comment.
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.
Checklist:
Update index.md)mainbranch of freeCodeCamp.This PR:
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