Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toast 组件增加淡出动画,错误文档更新 #710

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions packages/vantui/src/toast/demo/demo2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@ const Toast_ = Toast.createOnlyToast()

export default function Demo() {
const show = () => {
Toast_.success({
message: '成功文案',
})
}

const showLong = () => {
Toast_.fail({
message: '失败文案',
Toast_.loading({
message: '加载中...',
})
}

Expand All @@ -21,9 +15,6 @@ export default function Demo() {
<Button type="primary" onClick={show}>
加载提示
</Button>
<Button type="primary" onClick={showLong}>
自定义加载图标
</Button>
<Toast_ />
</View>
)
Expand Down
2 changes: 1 addition & 1 deletion packages/vantui/src/toast/demo/demo3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function Demo() {
}

const showLong = () => {
Toast.loading({
Toast.fail({
message: '失败文案',
selector: '#vanToast-demo2',
mask: true,
Expand Down
21 changes: 18 additions & 3 deletions packages/vantui/src/toast/toast.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { View, Text, RichText } from '@tarojs/components'
import { useState } from 'react'
import { useState, useEffect } from 'react'
import { ToastProps } from '../../types/toast'
import VanTransition from '../transition/index'
import VanOverlay from '../overlay/index'
Expand All @@ -24,20 +24,35 @@ export function Toast(props: ToastProps) {
id: '',
...props,
})
const [visible, setVisible] = useState(state.show)

useEffect(() => {
let timer: any = null

if (state.duration !== 0) {
timer = setTimeout(() => {
setVisible(false)
}, state.duration)
}

return () => {
clearTimeout(timer)
}
}, [state.duration])

const noop = function () {}

return (
<View>
{(state.mask || state.forbidClick) && (
<VanOverlay
show={state.show}
show={visible}
zIndex={state.zIndex}
style={state.mask ? '' : 'background-color: transparent;'}
></VanOverlay>
)}
<VanTransition
show={state.show}
show={visible}
style={state.zIndex ? { zIndex: state.zIndex } : {}}
className="van-toast__container"
>
Expand Down