mirror of
https://github.com/dzeiocom/components.git
synced 2025-06-07 16:49:55 +00:00
Add NotificationManager
a Component that manage Notifications for you Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
parent
ae7dde8802
commit
f957500580
@ -0,0 +1,38 @@
|
|||||||
|
@import '../config.styl'
|
||||||
|
|
||||||
|
.section
|
||||||
|
position fixed
|
||||||
|
bottom 0
|
||||||
|
left 0
|
||||||
|
padding 0 16px
|
||||||
|
max-width 25%
|
||||||
|
@media (max-width $tablet)
|
||||||
|
max-width 50%
|
||||||
|
@media (max-width $mobile)
|
||||||
|
width 100%
|
||||||
|
max-width 100%
|
||||||
|
> div
|
||||||
|
margin-bottom 16px
|
||||||
|
animation spawn 1 forwards ease-in-out .3s
|
||||||
|
|
||||||
|
&.remove
|
||||||
|
animation despawn 1 both ease-in-out .3s
|
||||||
|
|
||||||
|
.title
|
||||||
|
font-weight normal
|
||||||
|
margin 0
|
||||||
|
@keyframes spawn
|
||||||
|
from
|
||||||
|
opacity 0
|
||||||
|
transform translateY(100%)
|
||||||
|
to
|
||||||
|
opacity 1
|
||||||
|
transform translateY(0)
|
||||||
|
|
||||||
|
@keyframes despawn
|
||||||
|
from
|
||||||
|
opacity 1
|
||||||
|
transform translateY(0)
|
||||||
|
to
|
||||||
|
opacity 0
|
||||||
|
transform translateY(100%)
|
@ -0,0 +1,18 @@
|
|||||||
|
import { Meta } from '@storybook/react/types-6-0'
|
||||||
|
import React from 'react'
|
||||||
|
import { Zap } from 'react-feather'
|
||||||
|
import Component from '.'
|
||||||
|
|
||||||
|
export default {
|
||||||
|
title: 'DZEIO/NotificationManager',
|
||||||
|
component: Component
|
||||||
|
} as Meta
|
||||||
|
|
||||||
|
export const Basic = (args: any) => <Component {...args} />
|
||||||
|
Basic.args = {
|
||||||
|
ttl: 999999999999,
|
||||||
|
notifications: [
|
||||||
|
'Test',
|
||||||
|
'LArge text lorem ipsum dolor sit amet, i dont know what to type yolo :D'
|
||||||
|
]
|
||||||
|
}
|
179
src/dzeio/NotificationManager/index.tsx
Normal file
179
src/dzeio/NotificationManager/index.tsx
Normal file
@ -0,0 +1,179 @@
|
|||||||
|
import { buildClassName } from '../Util'
|
||||||
|
import Button from '../Button'
|
||||||
|
import Box from '../Box'
|
||||||
|
import Col from '../Col'
|
||||||
|
import Text from '../Text'
|
||||||
|
import Router from 'next/router'
|
||||||
|
import React from 'react'
|
||||||
|
import { X } from 'react-feather'
|
||||||
|
|
||||||
|
import css from './NotificationManager.module.styl'
|
||||||
|
|
||||||
|
export interface Notification {
|
||||||
|
message: string
|
||||||
|
actions?: Array<{
|
||||||
|
txt: string
|
||||||
|
action: (this: HTMLInputElement, event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void
|
||||||
|
}>
|
||||||
|
ttl?: number
|
||||||
|
internal?: {
|
||||||
|
timeRemaining: number
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
manageRoutes?: boolean
|
||||||
|
ttl?: number
|
||||||
|
notifications?: Array<string | Notification>
|
||||||
|
}
|
||||||
|
|
||||||
|
interface State {
|
||||||
|
notifications: Array<Notification | undefined>
|
||||||
|
}
|
||||||
|
|
||||||
|
export default class NotificationManager extends React.Component<Props, State> {
|
||||||
|
|
||||||
|
private static instance: NotificationManager
|
||||||
|
|
||||||
|
|
||||||
|
public state: State = {
|
||||||
|
notifications: []
|
||||||
|
}
|
||||||
|
|
||||||
|
private interval?: NodeJS.Timeout
|
||||||
|
private freezedNotification?: number
|
||||||
|
|
||||||
|
public constructor(props: Props | Readonly<Props>) {
|
||||||
|
super(props)
|
||||||
|
NotificationManager.instance = this
|
||||||
|
}
|
||||||
|
|
||||||
|
public static addNotification(notif: Omit<Notification, 'internal'> | string): number {
|
||||||
|
const realNotif: Notification = typeof notif === 'string' ? { message: notif, ttl: this.instance.props.ttl ?? 2000 } : notif
|
||||||
|
|
||||||
|
if (realNotif.ttl) {
|
||||||
|
realNotif.ttl /= 100
|
||||||
|
realNotif.internal = { timeRemaining: realNotif.ttl }
|
||||||
|
}
|
||||||
|
const notifs = this.instance.state.notifications
|
||||||
|
const id = notifs.push(realNotif) - 1
|
||||||
|
this.instance.setState({
|
||||||
|
notifications: notifs
|
||||||
|
})
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
|
||||||
|
public static removeNotification(id: number, array?: Array<Notification|undefined>) {
|
||||||
|
const notifs = array || this.instance.state.notifications
|
||||||
|
notifs[id] = undefined
|
||||||
|
if (array) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
this.instance.setState({
|
||||||
|
notifications: notifs
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public componentDidMount() {
|
||||||
|
if (this.props.notifications) {
|
||||||
|
for (const notif of this.props.notifications) {
|
||||||
|
NotificationManager.addNotification(notif)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (this.props.manageRoutes) {
|
||||||
|
Router.events.on('routeChangeComplete', this.checkRouteForMessage)
|
||||||
|
this.checkRouteForMessage()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public componentDidUpdate() {
|
||||||
|
if (this.state.notifications.length > 0 && !this.interval) {
|
||||||
|
this.interval = setInterval(() => {
|
||||||
|
const notifs = this.state.notifications
|
||||||
|
for (let id = 0; id < notifs.length; id++) {
|
||||||
|
const notif = notifs[id]
|
||||||
|
if (this.freezedNotification === id && notif?.ttl) {
|
||||||
|
notif.internal = {timeRemaining: notif.ttl}
|
||||||
|
}
|
||||||
|
if (!notif || typeof notif.internal?.timeRemaining !== 'number' || id === this.freezedNotification) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if (notif.internal.timeRemaining < 1) {
|
||||||
|
NotificationManager.removeNotification(id)
|
||||||
|
}
|
||||||
|
notif.internal.timeRemaining -= 1
|
||||||
|
}
|
||||||
|
this.setState({
|
||||||
|
notifications: notifs
|
||||||
|
})
|
||||||
|
}, 100)
|
||||||
|
}
|
||||||
|
if (this.state.notifications.length === 0 && this.interval) {
|
||||||
|
clearInterval(this.interval)
|
||||||
|
this.interval = undefined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public componentWillUnMount() {
|
||||||
|
if (this.interval) {
|
||||||
|
clearInterval(this.interval)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public render = () => (
|
||||||
|
<section className={css.section}>
|
||||||
|
{this.state.notifications.map((el, index) => {
|
||||||
|
if (el === undefined) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
key={index}
|
||||||
|
onMouseEnter={this.onMouseEnter(index)}
|
||||||
|
onMouseLeave={this.onMouseExit(index)}
|
||||||
|
className={buildClassName([css.remove, typeof el.internal?.timeRemaining === 'number' && el.internal.timeRemaining <= 3])}
|
||||||
|
>
|
||||||
|
<Box
|
||||||
|
title={el.message}
|
||||||
|
titleClassName={css.title}
|
||||||
|
headerButtons={(
|
||||||
|
<Col nogrow>
|
||||||
|
<Text><X onClick={() => NotificationManager.removeNotification(index)} /></Text>
|
||||||
|
</Col>
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{el.actions && (
|
||||||
|
<div>
|
||||||
|
{el.actions.map((btn, aIndex) => (
|
||||||
|
<Button
|
||||||
|
onClick={btn.action as unknown as ((event: React.MouseEvent<HTMLButtonElement | HTMLAnchorElement, MouseEvent>) => void)}
|
||||||
|
key={aIndex}
|
||||||
|
>{btn.txt}</Button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</Box>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</section>
|
||||||
|
)
|
||||||
|
|
||||||
|
private onMouseEnter = (id: number) => () => {
|
||||||
|
this.freezedNotification = id
|
||||||
|
}
|
||||||
|
|
||||||
|
private onMouseExit = (id: number) => () => {
|
||||||
|
if (this.freezedNotification === id) {
|
||||||
|
this.freezedNotification = undefined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private checkRouteForMessage = () => {
|
||||||
|
const msg = Router.query.msg
|
||||||
|
console.log(msg)
|
||||||
|
if (typeof msg === 'string') {
|
||||||
|
NotificationManager.addNotification(decodeURI(msg))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -20,6 +20,7 @@ import Input from './dzeio/Input'
|
|||||||
import Link from './dzeio/Link'
|
import Link from './dzeio/Link'
|
||||||
import Loader from './dzeio/Loader'
|
import Loader from './dzeio/Loader'
|
||||||
import Navbar from './dzeio/Navbar'
|
import Navbar from './dzeio/Navbar'
|
||||||
|
import NotificationManager from './dzeio/NotificationManager'
|
||||||
import Overflow from './dzeio/Overflow'
|
import Overflow from './dzeio/Overflow'
|
||||||
import Popup from './dzeio/Popup'
|
import Popup from './dzeio/Popup'
|
||||||
import Row from './dzeio/Row'
|
import Row from './dzeio/Row'
|
||||||
@ -41,8 +42,8 @@ export {
|
|||||||
Input,
|
Input,
|
||||||
Link,
|
Link,
|
||||||
Loader,
|
Loader,
|
||||||
Menu,
|
|
||||||
Navbar,
|
Navbar,
|
||||||
|
NotificationManager,
|
||||||
Overflow,
|
Overflow,
|
||||||
Popup,
|
Popup,
|
||||||
Row,
|
Row,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user