-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
498 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
<style type="text/css"> | ||
html { | ||
background-color: black; | ||
color: white; | ||
} | ||
#root { | ||
height: 100vh; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import MdCheckCircle from '@material-design-icons/svg/round/check_circle.svg'; | ||
import MdWarning from '@material-design-icons/svg/round/warning.svg'; | ||
import MdError from '@material-design-icons/svg/round/error.svg'; | ||
import MdInfo from '@material-design-icons/svg/round/info.svg'; | ||
|
||
import type { Component } from 'solid-js'; | ||
import { createEffect, Show } from 'solid-js'; | ||
import { Dynamic } from 'solid-js/web'; | ||
import { setState } from './helper'; | ||
|
||
import type { Toast } from '.'; | ||
import { toast } from './toast'; | ||
|
||
import classes from './index.module.css'; | ||
|
||
const iconMap = { | ||
info: MdInfo, | ||
success: MdCheckCircle, | ||
warn: MdWarning, | ||
error: MdError, | ||
}; | ||
|
||
const colorMap = { | ||
info: '#3a97d7', | ||
success: '#23bb35', | ||
warn: '#f0c53e', | ||
error: '#e45042', | ||
custom: '#1f2936', | ||
}; | ||
|
||
/** 删除 toast */ | ||
const dismissToast = (id: string) => | ||
setState((state) => { | ||
state.map[id].onDismiss?.({ ...state.map[id] }); | ||
const i = state.list.findIndex((t) => t === id); | ||
if (i !== -1) state.list.splice(i, 1); | ||
Reflect.deleteProperty(state.map, id); | ||
}); | ||
|
||
/** 重置 toast 的 update 属性 */ | ||
const resetToastUpdate = (id: string) => | ||
setState((state) => { | ||
Reflect.deleteProperty(state.map[id], 'update'); | ||
}); | ||
|
||
export const ToastItem: Component<Toast> = (props) => { | ||
const dismiss = (e: Event) => { | ||
e.stopPropagation(); | ||
toast.dismiss(props.id); | ||
}; | ||
|
||
// 在退出动画结束后才真的删除 | ||
const handleAnimationEnd = () => { | ||
if (!props.exit) return; | ||
dismissToast(props.id); | ||
}; | ||
|
||
let scheduleRef: HTMLDivElement; | ||
createEffect(() => { | ||
if (!props.update) return; | ||
|
||
resetToastUpdate(props.id); | ||
scheduleRef.getAnimations().forEach((animation) => { | ||
animation.cancel(); | ||
animation.play(); | ||
}); | ||
}); | ||
|
||
return ( | ||
<div | ||
class={classes.item} | ||
style={{ '--theme': colorMap[props.type] }} | ||
data-exit={props.exit} | ||
onClick={dismiss} | ||
onAnimationEnd={handleAnimationEnd} | ||
> | ||
<Dynamic component={iconMap[props.type]} /> | ||
<div class={classes.msg}> | ||
{typeof props.msg === 'string' ? props.msg : <props.msg />} | ||
</div> | ||
<Show when={props.duration !== Infinity}> | ||
<div | ||
ref={scheduleRef!} | ||
class={classes.schedule} | ||
style={{ 'animation-duration': `${props.duration}ms` }} | ||
onAnimationEnd={dismiss} | ||
/> | ||
</Show> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { Component } from 'solid-js'; | ||
import { For, createSignal, onCleanup, onMount } from 'solid-js'; | ||
import { store } from './helper'; | ||
import { ToastItem } from './ToastItem'; | ||
|
||
import classes from './index.module.css'; | ||
|
||
export const Toaster: Component = () => { | ||
const [visible, setVisible] = createSignal( | ||
document.visibilityState === 'visible', | ||
); | ||
|
||
onMount(() => { | ||
const handleVisibilityChange = () => { | ||
console.log(document.visibilityState); | ||
setVisible(document.visibilityState === 'visible'); | ||
}; | ||
document.addEventListener('visibilitychange', handleVisibilityChange); | ||
onCleanup(() => | ||
document.removeEventListener('visibilitychange', handleVisibilityChange), | ||
); | ||
}); | ||
|
||
return ( | ||
<div class={classes.root} data-paused={visible() ? undefined : ''}> | ||
<For each={store.list}>{(id) => <ToastItem {...store.map[id]} />}</For> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 用于测试时显示组件 | ||
*/ | ||
|
||
import { toast } from '.'; | ||
import { Toaster } from './Toaster'; | ||
|
||
export default function Display() { | ||
const duration = 1000 * 3; | ||
|
||
return ( | ||
<div style={{ height: '100vh' }}> | ||
<button onClick={() => toast('加载图片中,请稍候', { duration })}> | ||
文字 | ||
</button> | ||
|
||
<button | ||
onClick={() => toast(() => <div>加载图片中,请稍候</div>, { duration })} | ||
> | ||
JSX | ||
</button> | ||
|
||
<button onClick={() => toast.success('加载图片中,请稍候', { duration })}> | ||
success | ||
</button> | ||
<button onClick={() => toast.warn('加载图片中,请稍候', { duration })}> | ||
warn | ||
</button> | ||
<button onClick={() => toast.error('加载图片中,请稍候', { duration })}> | ||
error | ||
</button> | ||
|
||
<button | ||
onClick={() => | ||
toast.error('加载图片中,请稍候', { duration: Infinity }) | ||
} | ||
> | ||
永久显示 | ||
</button> | ||
|
||
<button | ||
onClick={() => toast.error('加载图片中,请稍候', { duration, id: '1' })} | ||
> | ||
更新 | ||
</button> | ||
|
||
<Toaster /> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { createStore, produce } from 'solid-js/store'; | ||
import type { Toast } from '.'; | ||
|
||
const [_state, _setState] = createStore({ | ||
list: [] as Toast['id'][], | ||
map: {} as Record<Toast['id'], Toast>, | ||
}); | ||
export type State = typeof _state; | ||
|
||
export const setState = (fn: (state: State) => void) => _setState(produce(fn)); | ||
|
||
// eslint-disable-next-line solid/reactivity | ||
export const store: Readonly<State> = _state; | ||
|
||
export const creatId = (): string => { | ||
let id = `${Date.now()}`; | ||
while (Reflect.has(store.map, id)) { | ||
id += '_'; | ||
} | ||
return id; | ||
}; |
Oops, something went wrong.