-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic mechanism for surfacing toasts.
- Loading branch information
Showing
7 changed files
with
162 additions
and
2 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
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,121 @@ | ||
import React, { | ||
// cloneElement, | ||
Component, | ||
} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import 'ngreact'; | ||
import { uiModules } from 'ui/modules'; | ||
|
||
import { | ||
EuiGlobalToastList, | ||
EuiGlobalToastListItem, | ||
EuiLink, | ||
EuiToast, | ||
} from '@elastic/eui'; | ||
|
||
const TOAST_LIFE_TIME_MS = 4000; | ||
const TOAST_FADE_OUT_MS = 250; | ||
|
||
class GlobalToastList extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
toastIdToDismissedMap: {} | ||
}; | ||
|
||
this.timeoutIds = []; | ||
this.toastsScheduledForDismissal = {}; | ||
} | ||
|
||
static propTypes = { | ||
toasts: PropTypes.array, | ||
dismissToast: PropTypes.func.isRequired, | ||
}; | ||
|
||
static defaultProps = { | ||
toasts: [], | ||
}; | ||
|
||
scheduleToastForDismissal = (toast, isImmediate = false) => { | ||
const lifeTime = isImmediate ? TOAST_FADE_OUT_MS : TOAST_LIFE_TIME_MS; | ||
|
||
this.timeoutIds.push(setTimeout(() => { | ||
this.props.dismissToast(toast); | ||
this.setState(prevState => { | ||
const toastIdToDismissedMap = { ...this.state.toastIdToDismissedMap }; | ||
delete toastIdToDismissedMap[toast.id]; | ||
|
||
return { | ||
toastIdToDismissedMap, | ||
}; | ||
}); | ||
}, lifeTime)); | ||
|
||
this.timeoutIds.push(setTimeout(() => { | ||
this.startDismissingToast(toast); | ||
}, lifeTime - TOAST_FADE_OUT_MS)); | ||
}; | ||
|
||
startDismissingToast(toast) { | ||
this.setState(prevState => { | ||
const toastIdToDismissedMap = { ...this.state.toastIdToDismissedMap }; | ||
toastIdToDismissedMap[toast.id] = true; | ||
|
||
return { | ||
toastIdToDismissedMap, | ||
}; | ||
}); | ||
} | ||
|
||
componentWillUnmount() { | ||
this.timeoutIds.forEach(timeoutId => clearTimeout(timeoutId)); | ||
} | ||
|
||
componentDidUpdate(prevProps) { | ||
prevProps.toasts.forEach(toast => { | ||
if (!this.toastsScheduledForDismissal[toast.id]) { | ||
this.scheduleToastForDismissal(toast); | ||
} | ||
}); | ||
} | ||
|
||
render() { | ||
const { | ||
toasts, | ||
dismissToast, | ||
} = this.props; | ||
|
||
const renderedToasts = toasts.map(toast => { | ||
const { text, ...rest } = toast; | ||
return ( | ||
<EuiGlobalToastListItem | ||
key={toast.id} | ||
isDismissed={this.state.toastIdToDismissedMap[toast.id]} | ||
> | ||
<EuiToast | ||
onClose={this.scheduleToastForDismissal.bind(toast, true)} | ||
{...rest} | ||
> | ||
{text} | ||
</EuiToast> | ||
</EuiGlobalToastListItem> | ||
); | ||
}); | ||
|
||
return ( | ||
<EuiGlobalToastList> | ||
{renderedToasts} | ||
</EuiGlobalToastList> | ||
); | ||
} | ||
} | ||
|
||
const app = uiModules.get('app/kibana', ['react']); | ||
|
||
app.directive('globalToastList', function (reactDirective) { | ||
return reactDirective(GlobalToastList, [ | ||
'toasts', | ||
['dismissToast', { watchDepth: 'reference' }], | ||
]); | ||
}); |
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,2 @@ | ||
import './global_toast_list'; | ||
export { toastNotifications } from './toast_notifications'; |
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 @@ | ||
let toastCounter = 0; | ||
|
||
class ToastNotifications { | ||
constructor() { | ||
this.list = []; | ||
} | ||
|
||
add = toast => { | ||
this.list.push({ | ||
id: toastCounter++, | ||
...toast | ||
}); | ||
}; | ||
|
||
remove = toast => { | ||
const index = this.list.indexOf(toast); | ||
this.list.splice(index, 1); | ||
}; | ||
} | ||
|
||
export const toastNotifications = new ToastNotifications(); |
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