Skip to content
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

[WIP] feat(Toaster): add ToasterSingletonNew #1591

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/components/Toaster/ToasterSingletonNew.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React from 'react';

import {ToasterProvider} from './Provider/ToasterProvider';
import {ToasterComponent} from './ToasterComponent/ToasterComponent';
import type {ToastProps, ToasterPublicMethods} from './types';

const TOASTER_NEW_KEY: unique symbol = Symbol('Toaster new instance key');

declare global {
interface Window {
[TOASTER_NEW_KEY]: ToasterSingletonNew;
}
}

export class ToasterSingletonNew {
private componentAPI: null | ToasterPublicMethods = null;

constructor() {
if (window[TOASTER_NEW_KEY] instanceof ToasterSingletonNew) {
return window[TOASTER_NEW_KEY];
}

window[TOASTER_NEW_KEY] = this;
}

add = (options: ToastProps) => {
this.componentAPI?.add(options);
};

remove = (name: string) => {
this.componentAPI?.remove(name);
};

removeAll = () => {
this.componentAPI?.removeAll();
};

update = (name: string, overrideOptions: Partial<ToastProps>) => {
this.componentAPI?.update(name, overrideOptions);
};

has = (name: string) => {
return this.componentAPI?.has(name) ?? false;
};

render = () => {
return (
<ToasterProvider
ref={(api) => {
this.componentAPI = api;
}}
>
<ToasterComponent />
</ToasterProvider>
);
};
}
34 changes: 34 additions & 0 deletions src/components/Toaster/__stories__/Toaster.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import React from 'react';

import type {Meta, StoryObj} from '@storybook/react';

import {Button} from '../../Button';
import type {ButtonView} from '../../Button';
import {useTheme} from '../../theme';
import {ToasterProvider} from '../Provider/ToasterProvider';
import {Toast} from '../Toast/Toast';
import {ToasterSingletonNew} from '../ToasterSingletonNew';

import {ToasterDemo} from './ToasterShowcase';

Expand Down Expand Up @@ -93,3 +96,34 @@ export const Default: Story = {
args: {},
render: (props) => <ToasterDemo {...props} />,
};

const toasterSingletonNew1 = new ToasterSingletonNew();

const ToastContent = () => {
const theme = useTheme();
return `Current theme: ${theme}`;
};

let index = 0;

const someFnOutsideReact = () => {
const toasterSingletonNew2 = new ToasterSingletonNew();
toasterSingletonNew2.add({
name: `id-${index++}`,
theme: 'info',
title: 'Hey, toaster!',
content: <ToastContent />,
});
};

export const NewSingleton: Story = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll delete this story after review

args: {},
render: () => {
return (
<React.Fragment>
<Button onClick={someFnOutsideReact}>Add toast</Button>
{toasterSingletonNew1.render()}
</React.Fragment>
);
},
};
Loading