-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the new NotificationCarousel component Added a duration timeout feature and a delete callback to the existing Notification component
- Loading branch information
Showing
7 changed files
with
232 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/components/NotificationCarousel/NotificationCarousel.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React from 'react'; | ||
import { StoryFn, Meta } from '@storybook/react'; | ||
import { NotificationCarousel } from './NotificationCarousel'; | ||
|
||
export default { | ||
title: 'Components/NotificationCarousel', | ||
component: NotificationCarousel, | ||
args: { | ||
// Shaping the stories through args composition. | ||
carouselItems: [ | ||
{ | ||
title: 'First notification', | ||
closeButtonAriaLabel: 'Close the first notification', | ||
hidden: false, | ||
}, | ||
{ | ||
title: 'Second notification', | ||
message: 'Should disappear in 10 seconds', | ||
duration: 10000, | ||
closeButtonAriaLabel: 'Close the second notification', | ||
}, | ||
{ | ||
title: 'Third notification', | ||
closeButtonAriaLabel: 'Close the third notification', | ||
}, | ||
], | ||
maxNotifications: 2, | ||
}, | ||
parameters: { | ||
design: [ | ||
{ | ||
name: 'light', | ||
type: 'figma', | ||
url: '', | ||
}, | ||
{ | ||
name: 'dark', | ||
type: 'figma', | ||
url: '', | ||
}, | ||
], | ||
}, | ||
} as Meta<typeof NotificationCarousel>; | ||
|
||
// More on component templates: https://storybook.js.org/docs/react/writing-stories/introduction#using-args | ||
const Template: StoryFn<typeof NotificationCarousel> = (args) => { | ||
return <NotificationCarousel {...args} />; | ||
}; | ||
|
||
/** | ||
* Default | ||
*/ | ||
export const Default = Template.bind({}); |
105 changes: 105 additions & 0 deletions
105
src/components/NotificationCarousel/NotificationCarousel.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import React, { useState } from 'react'; | ||
import styled from 'styled-components'; | ||
import { generateRandomString } from '../../shared'; | ||
import { Notification } from '../Notification/Notification'; | ||
import { Wrapper, WrapperProps } from '../Wrapper'; | ||
import { notificationSpacing } from './styles'; | ||
|
||
/** | ||
* Notification carousel component properties | ||
*/ | ||
export interface NotificationCarouselProps extends WrapperProps { | ||
/** | ||
* List of items (notifications) to stack to each other | ||
*/ | ||
carouselItems?: CarouselItem[]; | ||
|
||
/** | ||
* Maximum amount of notifications to be shown at once | ||
*/ | ||
maxNotifications?: number; | ||
} | ||
|
||
/** | ||
* Each notification shown on the carousel is specified thru CarouselItem | ||
*/ | ||
export interface CarouselItem { | ||
/** | ||
* ID for the carousel item, or randomly generated if undefined | ||
*/ | ||
id?: string; | ||
|
||
/** | ||
* Title for the notification | ||
*/ | ||
title?: string; | ||
|
||
/** | ||
* Message for the notification (optional) | ||
*/ | ||
message?: string; | ||
|
||
/** | ||
* Aria-label for the close button | ||
*/ | ||
closeButtonAriaLabel?: string; | ||
|
||
/** | ||
* How long the notification is shown (indefinitely if undefined) | ||
*/ | ||
duration?: number; | ||
} | ||
|
||
/** | ||
* Wrap notifications to each other vertically with padding | ||
*/ | ||
const NotificationCarouselItemWrapper = styled.div` | ||
padding-bottom: ${notificationSpacing}; | ||
pointer-events: auto; | ||
`; | ||
|
||
/** | ||
* NotificationCarousel component | ||
* @param props NotificationCarousel props | ||
* @returns NotificationCarousel component | ||
*/ | ||
export const NotificationCarousel = ({ | ||
carouselItems, | ||
maxNotifications, | ||
}: NotificationCarouselProps) => { | ||
const [carouselItemsState, setCarouselItemsState] = useState( | ||
carouselItems || [] | ||
); | ||
|
||
return ( | ||
<Wrapper | ||
style={{ | ||
overflow: 'hidden', | ||
pointerEvents: 'none', | ||
}} | ||
> | ||
{carouselItemsState | ||
.filter((_, index) => | ||
maxNotifications ? index < maxNotifications : true | ||
) | ||
.map((item, index) => ( | ||
<NotificationCarouselItemWrapper | ||
key={item.id || generateRandomString(5)} | ||
> | ||
<Notification | ||
title={item.title} | ||
message={item.message} | ||
closeButtonAriaLabel={item.closeButtonAriaLabel} | ||
duration={item.duration} | ||
index={index} | ||
deleteNotification={(deleteIndex) => { | ||
setCarouselItemsState( | ||
carouselItemsState.filter((_, index) => index !== deleteIndex) | ||
); | ||
}} | ||
/> | ||
</NotificationCarouselItemWrapper> | ||
))} | ||
</Wrapper> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { pxToRem } from '../../shared/styles'; | ||
|
||
/** | ||
* Spacing between two notifications in the notification carousel | ||
*/ | ||
export const notificationSpacing = pxToRem(16); |