Skip to content

Commit 40a484f

Browse files
committed
fix: toast close
Signed-off-by: Innei <tukon479@gmail.com>
1 parent 3c63abf commit 40a484f

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

packages/kami-design/components/Icons/shared.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,17 @@ export function FluentList16Filled(props: SVGProps<SVGSVGElement>) {
1818
</svg>
1919
)
2020
}
21+
22+
export function HumbleiconsTimes(props: SVGProps<SVGSVGElement>) {
23+
return (
24+
<svg width="1em" height="1em" viewBox="0 0 24 24" {...props}>
25+
<path
26+
fill="none"
27+
stroke="currentColor"
28+
strokeLinecap="round"
29+
strokeWidth="2"
30+
d="M6 18L18 6m0 12L6 6"
31+
/>
32+
</svg>
33+
)
34+
}

src/components/widgets/Toast/card.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { observer } from 'mobx-react-lite'
22
import type { FC } from 'react'
33
import { useRef } from 'react'
4+
import { toast } from 'react-toastify'
45

56
import { Avatar } from '@mx-space/kami-design/components/Avatar'
67

8+
import { HumbleiconsTimes } from '~/../packages/kami-design/components/Icons'
79
import { ImpressionView } from '~/components/biz/ImpressionView'
810
import { RelativeTime } from '~/components/universal/RelativeTime'
911
import { useInitialData } from '~/hooks/use-initial-data'
@@ -15,6 +17,8 @@ export const ToastCard: FC<{
1517
description?: string
1618
text?: string
1719
avatar?: string
20+
onClick?: (e: MouseEvent) => void
21+
getToastId?: () => string
1822
}> = observer((props) => {
1923
const { userStore, appStore } = store
2024
const isPadOrMobile = appStore.isPadOrMobile
@@ -26,6 +30,7 @@ export const ToastCard: FC<{
2630
trackerMessage={`Toast 曝光 - ${title} · ${description} · ${text}`}
2731
>
2832
<div
33+
onClick={(e) => props.onClick?.(e.nativeEvent)}
2934
className="relative flex space-x-4 items-center p-4 w-full text-[12px] bg-bg-opacity overflow-hidden
3035
text-inherit border border-shallow border-opacity-50
3136
rounded-xl backdrop-filter backdrop-brightness-150 backdrop-brightness-110 backdrop-saturate-150 backdrop-blur-md
@@ -34,6 +39,17 @@ export const ToastCard: FC<{
3439
width: isPadOrMobile ? 'calc(100% - 16px - 16px)' : '350px',
3540
}}
3641
>
42+
<div
43+
role={'button'}
44+
tabIndex={0}
45+
className="flex items-center justify-center absolute z-10 top-2 left-2 h-4 w-4 rounded-full overflow-hidden bg-gray-6 bg-opacity-80 dark:bg-dark-100 text-dark-50 dark:text-white"
46+
onClick={(e) => {
47+
e.stopPropagation()
48+
props.getToastId && toast.dismiss(props.getToastId())
49+
}}
50+
>
51+
<HumbleiconsTimes />
52+
</div>
3753
<div className="flex-shrink-0">
3854
<Avatar
3955
useRandomColor={false}

src/utils/notice.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,23 @@ export class Notice {
132132
action: TrackerAction.Interaction,
133133
label: '内嵌通知触发',
134134
})
135-
toast(
136-
React.createElement(ToastCard, { text, title, description, avatar }),
135+
136+
const id = toast(
137+
React.createElement(ToastCard, {
138+
text,
139+
title,
140+
description,
141+
avatar,
142+
getToastId: () => id,
143+
onClick(e) {
144+
onClick?.(e)
145+
toast.dismiss(id)
146+
},
147+
}),
137148
{
138149
autoClose: duration,
150+
closeOnClick: false,
139151
position: store.appUIStore.isPadOrMobile ? 'top-center' : 'top-right',
140-
onClick(e) {
141-
onClick?.(e.nativeEvent)
142-
},
143152
},
144153
)
145154
}

0 commit comments

Comments
 (0)