Skip to content

Commit aa2a034

Browse files
author
jingwenzheng
committed
feat: 增加 toast 组件
1 parent 48ed960 commit aa2a034

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.body {
2+
position: absolute;
3+
4+
bottom: 20%;
5+
left: 50%;
6+
7+
background-color: rgba(0, 0, 0, 0.8);
8+
color: #fff;
9+
10+
font-size: 15px;
11+
padding: 8px 12px;
12+
border-radius: 4px;
13+
14+
max-width: 50%;
15+
white-space: pre-line;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { defineComponent } from 'vue'
2+
import { Popup } from '@0x30/vue-navigation-layout'
3+
import styles from './index.module.scss'
4+
import anime from 'animejs'
5+
6+
const Toast = defineComponent({
7+
name: 'Toast',
8+
props: {
9+
title: String,
10+
className: String,
11+
},
12+
setup: (props) => {
13+
return () => <div class={props.className ?? styles.body}>{props.title}</div>
14+
},
15+
})
16+
17+
type ToastOptoions = {
18+
duration?: number
19+
className?: string
20+
}
21+
22+
export const useToast = (title: string, options?: ToastOptoions) => {
23+
const [show, close] = Popup({
24+
onEnter(el, done) {
25+
anime.set(el, { translateX: '-50%' })
26+
anime({
27+
targets: el,
28+
opacity: [0, 1],
29+
scale: [0.8, 1],
30+
translateX: '-50%',
31+
duration: 300,
32+
easing: 'easeOutExpo',
33+
complete: done,
34+
})
35+
},
36+
onLeave(el, done) {
37+
anime({
38+
targets: el,
39+
opacity: [1, 0],
40+
duration: 300,
41+
easing: 'easeInQuad',
42+
complete: done,
43+
})
44+
},
45+
})
46+
show(<Toast title={title} className={options?.className} />)
47+
window.setTimeout(close, options?.duration ?? 2000)
48+
}

packages/layout/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * from './components/Safearea'
44
export { default as SidePage } from './components/SidePage'
55
export * from './components/Loading'
66
export * from './util/Popup'
7+
export * from './components/Toast'

src/views/home/index.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import DetailPage from '../detail'
1111

1212
import styles from './index.module.scss'
1313
import { useHooks, wait } from '../../util'
14+
import { useToast } from '@0x30/vue-navigation-layout'
1415

1516
const Component = defineComponent({
1617
name: 'HomePage',
@@ -68,6 +69,13 @@ const Component = defineComponent({
6869
>
6970
popup
7071
</button>
72+
<button
73+
onClick={async () => {
74+
useToast(`Hello world.`)
75+
}}
76+
>
77+
toast
78+
</button>
7179
</div>
7280
<br />
7381
<span>SidePage</span>

0 commit comments

Comments
 (0)