From a4c0d88cf6d2137f61fc6005ad2c6339c7f3c9d8 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Fri, 12 Mar 2021 16:10:01 +0100 Subject: toast context working --- components/toast.tsx | 77 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 29 deletions(-) (limited to 'components') diff --git a/components/toast.tsx b/components/toast.tsx index 03a6255..5bfc0ae 100644 --- a/components/toast.tsx +++ b/components/toast.tsx @@ -1,10 +1,11 @@ -import { CSSProperties, ReactNode, Component } from "react"; +import { CSSProperties, ReactNode, useState, createContext } from "react"; import CloseIcon from '@material-ui/icons/Close'; -export function ToastArea(props: { +function ToastArea(props: { style?: CSSProperties children?: ReactNode + rerender?: boolean; }) { return
{props.children}
} -export class Toast extends Component<{ +function Toast(props: { text?: string icon?: ReactNode children?: ReactNode type?: "normal"|"confirmation"|"error" style?: CSSProperties -}> { - state = { render: true } +}) { + var [visible, setVisibility] = useState(true); - close = () => this.setState({ render: false }) + setTimeout(() => setVisibility(false), 10e3); - render () { - if (!this.state.render) return null; - return
+ return visible &&
{ - this.props.children ? - this.props.children : + props.children ||
{this.props.icon}
+ }}>{props.icon}

{this.props.text}

+ fontSize: 20, + userSelect: "none" + }}>{props.text}
+ }} onClick={() => setVisibility(false)}>
} -
- } + +} + +export var ToastContext = createContext<{ toast?: (message: string, + type: "confirmation"|"normal"|"error", + icon?: ReactNode ) => void }>({}); +var toasts: Array = []; + +export function ToastContextWrapper(props: { children?: ReactNode }) { + var [dummyState, rerender] = useState(false); + + return { + toasts.push(); + rerender(!dummyState); + } }}> + { props.children } + + {toasts} + + } -- cgit v1.2.3