-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into fix/client-state-desync
- Loading branch information
Showing
12 changed files
with
304 additions
and
178 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { createContext } from 'preact' | ||
import { useState, useContext, useCallback, useEffect } from 'preact/hooks' | ||
import withStyles from './jss' | ||
|
||
const ToastCtx = createContext() | ||
|
||
// Use sequential IDs | ||
let toastIdCounter = 0 | ||
|
||
function genToastId () { | ||
return toastIdCounter++ | ||
} | ||
|
||
function Toast ({ children, remove, type, id }) { | ||
const wrappedRemove = useCallback(() => remove(id), [remove, id]) | ||
useEffect(() => { | ||
const duration = 1000 * 5 | ||
const timeout = setTimeout(wrappedRemove, duration) | ||
|
||
return () => clearTimeout(timeout) | ||
}, [wrappedRemove]) | ||
|
||
return ( | ||
<div className={`toast toast--${type}`}> | ||
{children} | ||
<button onClick={wrappedRemove} className='btn-close' /> | ||
</div> | ||
) | ||
} | ||
|
||
const ToastContainer = withStyles({ | ||
container: { | ||
position: 'fixed', | ||
top: '1em', | ||
right: '1em', | ||
zIndex: 9999, | ||
width: '320px' | ||
} | ||
}, ({ classes, ...props }) => <div class={classes.container} {...props} />) | ||
|
||
export function ToastProvider ({ children }) { | ||
const [toasts, setToasts] = useState([]) | ||
|
||
const remove = useCallback((id) => { | ||
setToasts(toasts => toasts.filter(t => id !== t.id)) | ||
}, []) | ||
const add = useCallback((data) => { | ||
const id = genToastId() | ||
const toast = { | ||
...data, | ||
id | ||
} | ||
setToasts(toasts => [...toasts, toast]) | ||
return () => remove(id) | ||
}, [remove]) | ||
|
||
return ( | ||
<ToastCtx.Provider value={add}> | ||
{children} | ||
{ toasts.length > 0 && | ||
<ToastContainer> | ||
{ toasts.map(({ id, type, body }) => | ||
<Toast type={type} key={id} id={id} remove={remove}> | ||
{body} | ||
</Toast> | ||
)} | ||
</ToastContainer> | ||
} | ||
</ToastCtx.Provider> | ||
) | ||
} | ||
|
||
export function useToast () { | ||
const toast = useContext(ToastCtx) | ||
|
||
return { toast } | ||
} | ||
|
||
export function withToast (Component) { | ||
return function Toasted (props) { | ||
const { toast } = useToast() | ||
return ( | ||
<Component {...props} toast={toast} /> | ||
) | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
module.exports = { | ||
strings: require('./strings'), | ||
toasts: require('./toasts') | ||
strings: require('./strings') | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.