|
1
|
"use client" |
|
2
|
|
|
3
|
import clsx from "clsx" |
|
4
|
import React, { Children, ReactElement, useRef } from "react" |
|
5
|
import { NotificationItemLayoutDefault } from "./Layout/Default" |
|
6
|
// @ts-expect-error can't install the types package because it doesn't support React v19 |
|
7
|
import { CSSTransition } from "react-transition-group" |
|
8
|
|
|
9
|
export type NotificationItemProps = { |
|
10
|
layout?: "default" | "empty" |
|
11
|
type?: "info" | "error" | "warning" | "success" | "custom" | "none" |
|
12
|
CustomIcon?: React.ReactNode |
|
13
|
title?: string |
|
14
|
text?: string |
|
15
|
className?: string |
|
16
|
children?: ReactElement |
|
17
|
isClosable?: boolean |
|
18
|
placement?: "top" | "bottom" |
|
19
|
show?: boolean |
|
20
|
setShow?: (value: boolean) => void |
|
21
|
onClose?: () => void |
|
22
|
closeButtonText?: string |
|
23
|
} & React.HTMLAttributes<HTMLDivElement> |
|
24
|
|
|
25
|
type EmptyLayoutProps = { |
|
26
|
onClose?: () => void |
|
27
|
} |
|
28
|
|
|
29
|
export const NotificationItem = ({ |
|
30
|
className = "", |
|
31
|
placement = "bottom", |
|
32
|
show = true, |
|
33
|
layout = "default", |
|
34
|
setShow, |
|
35
|
onClose, |
|
36
|
children, |
|
37
|
...rest |
|
38
|
}: NotificationItemProps) => { |
|
39
|
const ref = useRef<HTMLDivElement>(null) |
|
40
|
const handleClose = () => { |
|
41
|
setShow?.(false) |
|
42
|
onClose?.() |
|
43
|
} |
|
44
|
|
|
45
|
return ( |
|
46
|
<CSSTransition |
|
47
|
timeout={200} |
|
48
|
classNames={{ |
|
49
|
enter: "animate-slideInRight animate-fast", |
|
50
|
exit: "animate-slideOutRight animate-fast", |
|
51
|
}} |
|
52
|
nodeRef={ref} |
|
53
|
> |
|
54
|
<div |
|
55
|
className={clsx( |
|
56
|
"md:max-w-[320px] md:w-[320px] w-full", |
|
57
|
"fixed md:right-docs_1 left-0 md:m-docs_1", |
|
58
|
placement === "bottom" && "md:bottom-docs_1 bottom-0", |
|
59
|
placement === "top" && "md:top-docs_1 top-0", |
|
60
|
"opacity-100 transition-opacity duration-200 ease-ease", |
|
61
|
!show && "!opacity-0", |
|
62
|
className |
|
63
|
)} |
|
64
|
ref={ref} |
|
65
|
data-testid="notification-item" |
|
66
|
> |
|
67
|
{layout === "default" && ( |
|
68
|
<NotificationItemLayoutDefault {...rest} handleClose={handleClose}> |
|
69
|
{children} |
|
70
|
</NotificationItemLayoutDefault> |
|
71
|
)} |
|
72
|
{layout === "empty" && |
|
73
|
Children.map(children, (child) => { |
|
74
|
if (child) { |
|
75
|
return React.cloneElement<EmptyLayoutProps>( |
|
76
|
child as React.ReactElement< |
|
77
|
EmptyLayoutProps, |
|
78
|
React.FunctionComponent<EmptyLayoutProps> |
|
79
|
>, |
|
80
|
{ |
|
81
|
onClose: handleClose, |
|
82
|
} |
|
83
|
) |
|
84
|
} |
|
85
|
})} |
|
86
|
</div> |
|
87
|
</CSSTransition> |
|
88
|
) |
|
89
|
} |