Skip to content

Commit

Permalink
feat: 增加 toast 组件
Browse files Browse the repository at this point in the history
  • Loading branch information
0x30 committed Aug 7, 2024
1 parent 48ed960 commit aa2a034
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/layout/src/components/Toast/index.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.body {
position: absolute;

bottom: 20%;
left: 50%;

background-color: rgba(0, 0, 0, 0.8);
color: #fff;

font-size: 15px;
padding: 8px 12px;
border-radius: 4px;

max-width: 50%;
white-space: pre-line;
}
48 changes: 48 additions & 0 deletions packages/layout/src/components/Toast/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { defineComponent } from 'vue'
import { Popup } from '@0x30/vue-navigation-layout'
import styles from './index.module.scss'
import anime from 'animejs'

const Toast = defineComponent({
name: 'Toast',
props: {
title: String,
className: String,
},
setup: (props) => {
return () => <div class={props.className ?? styles.body}>{props.title}</div>
},
})

type ToastOptoions = {
duration?: number
className?: string
}

export const useToast = (title: string, options?: ToastOptoions) => {
const [show, close] = Popup({
onEnter(el, done) {
anime.set(el, { translateX: '-50%' })
anime({
targets: el,
opacity: [0, 1],
scale: [0.8, 1],
translateX: '-50%',
duration: 300,
easing: 'easeOutExpo',
complete: done,
})
},
onLeave(el, done) {
anime({
targets: el,
opacity: [1, 0],
duration: 300,
easing: 'easeInQuad',
complete: done,
})
},
})
show(<Toast title={title} className={options?.className} />)
window.setTimeout(close, options?.duration ?? 2000)
}
1 change: 1 addition & 0 deletions packages/layout/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './components/Safearea'
export { default as SidePage } from './components/SidePage'
export * from './components/Loading'
export * from './util/Popup'
export * from './components/Toast'
8 changes: 8 additions & 0 deletions src/views/home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import DetailPage from '../detail'

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

const Component = defineComponent({
name: 'HomePage',
Expand Down Expand Up @@ -68,6 +69,13 @@ const Component = defineComponent({
>
popup
</button>
<button
onClick={async () => {
useToast(`Hello world.`)
}}
>
toast
</button>
</div>
<br />
<span>SidePage</span>
Expand Down

0 comments on commit aa2a034

Please sign in to comment.