-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(Toaster): fix incompatibility of different Toaster APIs #1987
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,11 @@ | ||
'use client'; | ||
|
||
import React from 'react'; | ||
|
||
import get from 'lodash/get'; | ||
import ReactDOM from 'react-dom'; | ||
|
||
import {block} from '../utils/cn'; | ||
|
||
import {ToasterProvider} from './Provider/ToasterProvider'; | ||
import {ToasterComponent} from './ToasterComponent/ToasterComponent'; | ||
import type {ToastProps, ToasterArgs, ToasterPublicMethods} from './types'; | ||
import type {InternalToastProps, ToastProps} from './types'; | ||
import {getToastIndex} from './utilities/getToastIndex'; | ||
import {hasToast} from './utilities/hasToast'; | ||
import {removeToast} from './utilities/removeToast'; | ||
|
||
const TOASTER_KEY: unique symbol = Symbol('Toaster instance key'); | ||
const bToaster = block('toaster'); | ||
let ReactDOMClient: any; | ||
|
||
declare global { | ||
interface Window { | ||
|
@@ -22,95 +14,86 @@ declare global { | |
} | ||
|
||
export class ToasterSingleton { | ||
static injectReactDOMClient(client: any) { | ||
ReactDOMClient = client; | ||
} | ||
private toasts: InternalToastProps[] = []; | ||
private listeners: ((toasts: InternalToastProps[]) => void)[] = []; | ||
|
||
private rootNode!: HTMLDivElement; | ||
private reactRoot!: any; | ||
private className = ''; | ||
private mobile = false; | ||
private componentAPI: null | ToasterPublicMethods = null; | ||
constructor() { | ||
if (window[TOASTER_KEY] instanceof ToasterSingleton) { | ||
return window[TOASTER_KEY]; | ||
} | ||
|
||
constructor(args?: ToasterArgs) { | ||
const className = get(args, ['className'], ''); | ||
const mobile = get(args, ['mobile'], false); | ||
window[TOASTER_KEY] = this; | ||
} | ||
|
||
if (window[TOASTER_KEY] instanceof ToasterSingleton) { | ||
const me = window[TOASTER_KEY]; | ||
me.className = className; | ||
me.mobile = mobile; | ||
me.setRootNodeClassName(); | ||
return me; | ||
add(toast: ToastProps) { | ||
let nextToasts = this.toasts; | ||
|
||
if (hasToast(nextToasts, toast.name)) { | ||
nextToasts = removeToast(nextToasts, toast.name); | ||
} | ||
|
||
this.className = className; | ||
this.mobile = mobile; | ||
this.createRootNode(); | ||
this.createReactRoot(); | ||
this.render(); | ||
this.toasts = [ | ||
...nextToasts, | ||
{ | ||
...toast, | ||
addedAt: Date.now(), | ||
ref: {current: null}, | ||
}, | ||
]; | ||
|
||
window[TOASTER_KEY] = this; | ||
this.notify(); | ||
} | ||
|
||
destroy() { | ||
// eslint-disable-next-line react/no-deprecated | ||
ReactDOM.unmountComponentAtNode(this.rootNode); | ||
document.body.removeChild(this.rootNode); | ||
remove(name: string) { | ||
this.toasts = removeToast(this.toasts, name); | ||
|
||
this.notify(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to maintain |
||
|
||
add = (options: ToastProps) => { | ||
this.componentAPI?.add(options); | ||
}; | ||
removeAll() { | ||
this.toasts = []; | ||
|
||
remove = (name: string) => { | ||
this.componentAPI?.remove(name); | ||
}; | ||
this.notify(); | ||
} | ||
|
||
removeAll = () => { | ||
this.componentAPI?.removeAll(); | ||
}; | ||
update(name: string, overrideOptions: Partial<ToastProps>) { | ||
if (!hasToast(this.toasts, name)) { | ||
return; | ||
} | ||
|
||
update = (name: string, overrideOptions: Partial<ToastProps>) => { | ||
this.componentAPI?.update(name, overrideOptions); | ||
}; | ||
const index = getToastIndex(this.toasts, name); | ||
|
||
has = (name: string) => { | ||
return this.componentAPI?.has(name) ?? false; | ||
}; | ||
this.toasts = [ | ||
...this.toasts.slice(0, index), | ||
{ | ||
...this.toasts[index], | ||
...overrideOptions, | ||
}, | ||
...this.toasts.slice(index + 1), | ||
]; | ||
|
||
private createRootNode() { | ||
this.rootNode = document.createElement('div'); | ||
this.setRootNodeClassName(); | ||
document.body.appendChild(this.rootNode); | ||
this.notify(); | ||
} | ||
|
||
private createReactRoot() { | ||
if (ReactDOMClient) { | ||
this.reactRoot = ReactDOMClient.createRoot(this.rootNode); | ||
} | ||
has(name: string) { | ||
return hasToast(this.toasts, name); | ||
} | ||
|
||
private render() { | ||
const container = ( | ||
<ToasterProvider | ||
ref={(api) => { | ||
this.componentAPI = api; | ||
}} | ||
> | ||
<ToasterComponent hasPortal={false} mobile={this.mobile} /> | ||
</ToasterProvider> | ||
); | ||
|
||
if (this.reactRoot) { | ||
this.reactRoot.render(container); | ||
} else { | ||
// eslint-disable-next-line react/no-deprecated | ||
ReactDOM.render(container, this.rootNode, () => Promise.resolve()); | ||
subscribe(listener: (toasts: InternalToastProps[]) => void) { | ||
if (typeof listener === 'function') { | ||
this.listeners.push(listener); | ||
} | ||
|
||
return () => { | ||
this.listeners = this.listeners.filter( | ||
(currentListener) => listener !== currentListener, | ||
); | ||
}; | ||
} | ||
|
||
private setRootNodeClassName() { | ||
this.rootNode.className = bToaster({mobile: this.mobile}, this.className); | ||
private notify() { | ||
for (const listener of this.listeners) { | ||
listener(this.toasts); | ||
} | ||
} | ||
Comment on lines
+94
to
98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please separate subscribe pattern from Toaster class to separate base class |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is definitely breaking change, please change target branch to
next