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

Add JSR support #402

Open
wants to merge 2 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions jsr.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@bureaudouble/sonner",
"version": "0.0.2",
"compilerOptions": {
"jsx": "react-jsx",
"lib": [
"es2022",
"dom",
"dom.iterable",
"dom.asynciterable"
]
},
"exclude": ["**/*", "!README.md", "!LICENSE.md", "!src", "!jsr.jsonc"],
"imports": {
"react": "npm:react@^18.2.0",
"react-dom": "npm:react-dom@^18.2.0",
"amuchina": "npm:amuchina@^1.0.12",
"react-style-singleton": "jsr:@bureaudouble-forks/react-style-singleton@^0.0.1"
},
"exports": {
".": "./src/index.tsx"
}
}

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"@types/dompurify": "^3.0.5",
"@types/node": "^18.11.13",
"@types/react": "^18.0.26",
"dompurify": "^3.0.9",
"amuchina": "^1.0.12",
"react-style-singleton": "^2.2.1",
"prettier": "^2.8.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
11 changes: 6 additions & 5 deletions src/assets.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use client';
// @deno-types="npm:@types/react@^18.2.0"
import React from 'react';
import type { ToastTypes } from './types';
import type { ToastTypes } from './types.ts';

export const getAsset = (type: ToastTypes): JSX.Element | null => {
switch (type) {
Expand Down Expand Up @@ -35,7 +36,7 @@ export const Loader = ({ visible }: { visible: boolean }) => {
);
};

const SuccessIcon = (
const SuccessIcon: React.JSX.Element = (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" height="20" width="20">
<path
fillRule="evenodd"
Expand All @@ -45,7 +46,7 @@ const SuccessIcon = (
</svg>
);

const WarningIcon = (
const WarningIcon: React.JSX.Element = (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" height="20" width="20">
<path
fillRule="evenodd"
Expand All @@ -55,7 +56,7 @@ const WarningIcon = (
</svg>
);

const InfoIcon = (
const InfoIcon: React.JSX.Element = (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" height="20" width="20">
<path
fillRule="evenodd"
Expand All @@ -65,7 +66,7 @@ const InfoIcon = (
</svg>
);

const ErrorIcon = (
const ErrorIcon: React.JSX.Element = (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" height="20" width="20">
<path
fillRule="evenodd"
Expand Down
5 changes: 3 additions & 2 deletions src/hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// @deno-types="npm:@types/react@^18.2.0"
import React from 'react';

export const useIsDocumentHidden = () => {
const [isDocumentHidden, setIsDocumentHidden] = React.useState(false);
export const useIsDocumentHidden = (): boolean => {
const [isDocumentHidden, setIsDocumentHidden] = React.useState<boolean>(false);

React.useEffect(() => {
const callback = () => {
Expand Down
25 changes: 16 additions & 9 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
'use client';

// @deno-types="npm:@types/react@^18.2.0"
import React from 'react';
// @deno-types="npm:@types/react-dom@^18.2.0"
import ReactDOM from 'react-dom';

import DOMPurify from 'dompurify';
import { getAsset, Loader } from './assets';
import { useIsDocumentHidden } from './hooks';
import { toast, ToastState } from './state';
import './styles.css';
import { styleHookSingleton } from 'react-style-singleton';
import Amuchina from 'amuchina';
import { getAsset, Loader } from './assets.tsx';
import { useIsDocumentHidden } from './hooks.tsx';
import { toast, ToastState } from './state.ts';
import styles from "./styles.ts";
import {
isAction,
type ExternalToast,
Expand All @@ -16,7 +19,7 @@ import {
type ToastProps,
type ToastT,
type ToastToDismiss,
} from './types';
} from './types.ts';

// Visible toasts amount
const VISIBLE_TOASTS_AMOUNT = 3;
Expand All @@ -43,7 +46,9 @@ function _cn(...classes: (string | undefined)[]) {
return classes.filter(Boolean).join(' ');
}

const Toast = (props: ToastProps) => {
const useStyle: ReturnType<typeof styleHookSingleton> = styleHookSingleton();
const Toast = (props: ToastProps): React.JSX.Element => {
useStyle(styles);
const {
invert: ToasterInvert,
toast,
Expand Down Expand Up @@ -250,7 +255,9 @@ const Toast = (props: ToastProps) => {
}

function sanitizeHTML(html: string): { __html: string } {
return { __html: DOMPurify.sanitize(html) };
const amuchina = globalThis.document ? new Amuchina() : null;
const parser = amuchina ? new DOMParser() : null;
return { __html: amuchina?.sanitize(parser?.parseFromString(html, 'text/html'))?.documentElement.innerHTML ?? "" };
}

return (
Expand Down Expand Up @@ -467,7 +474,7 @@ function getDocumentDirection(): ToasterProps['dir'] {
return dirAttribute as ToasterProps['dir'];
}

const Toaster = (props: ToasterProps) => {
const Toaster = (props: ToasterProps): React.JSX.Element => {
const {
invert,
position = 'bottom-right',
Expand Down
47 changes: 29 additions & 18 deletions src/state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @deno-types="npm:@types/react@^18.2.0"
import React from 'react';
import type { ExternalToast, ToastT, PromiseData, PromiseT, ToastToDismiss, ToastTypes } from './types';
import type { ExternalToast, ToastT, PromiseData, PromiseT, ToastToDismiss, ToastTypes } from './types.ts';

let toastsCounter = 1;

Expand All @@ -13,7 +14,7 @@ class Observer {
}

// We use arrow functions to maintain the correct `this` reference
subscribe = (subscriber: (toast: ToastT | ToastToDismiss) => void) => {
subscribe = (subscriber: (toast: ToastT | ToastToDismiss) => void): () => void => {
this.subscribers.push(subscriber);

return () => {
Expand All @@ -22,11 +23,11 @@ class Observer {
};
};

publish = (data: ToastT) => {
publish = (data: ToastT): void => {
this.subscribers.forEach((subscriber) => subscriber(data));
};

addToast = (data: ToastT) => {
addToast = (data: ToastT): void => {
this.publish(data);
this.toasts = [...this.toasts, data];
};
Expand All @@ -38,7 +39,7 @@ class Observer {
promise?: PromiseT;
jsx?: React.ReactElement;
},
) => {
): string | number => {
const { message, ...rest } = data;
const id = typeof data?.id === 'number' || data.id?.length > 0 ? data.id : toastsCounter++;
const alreadyExists = this.toasts.find((toast) => {
Expand Down Expand Up @@ -68,7 +69,7 @@ class Observer {
return id;
};

dismiss = (id?: number | string) => {
dismiss = (id?: number | string): string | number => {
if (!id) {
this.toasts.forEach((toast) => {
this.subscribers.forEach((subscriber) => subscriber({ id: toast.id, dismiss: true }));
Expand All @@ -79,31 +80,31 @@ class Observer {
return id;
};

message = (message: string | React.ReactNode, data?: ExternalToast) => {
message = (message: string | React.ReactNode, data?: ExternalToast): string | number => {
return this.create({ ...data, message });
};

error = (message: string | React.ReactNode, data?: ExternalToast) => {
error = (message: string | React.ReactNode, data?: ExternalToast): string | number => {
return this.create({ ...data, message, type: 'error' });
};

success = (message: string | React.ReactNode, data?: ExternalToast) => {
success = (message: string | React.ReactNode, data?: ExternalToast): string | number => {
return this.create({ ...data, type: 'success', message });
};

info = (message: string | React.ReactNode, data?: ExternalToast) => {
info = (message: string | React.ReactNode, data?: ExternalToast): string | number => {
return this.create({ ...data, type: 'info', message });
};

warning = (message: string | React.ReactNode, data?: ExternalToast) => {
warning = (message: string | React.ReactNode, data?: ExternalToast): string | number => {
return this.create({ ...data, type: 'warning', message });
};

loading = (message: string | React.ReactNode, data?: ExternalToast) => {
loading = (message: string | React.ReactNode, data?: ExternalToast): string | number => {
return this.create({ ...data, type: 'loading', message });
};

promise = <ToastData>(promise: PromiseT<ToastData>, data?: PromiseData<ToastData>) => {
promise = <ToastData>(promise: PromiseT<ToastData>, data?: PromiseData<ToastData>): string | number => {
if (!data) {
// Nothing to show
return;
Expand Down Expand Up @@ -166,17 +167,17 @@ class Observer {
return id;
};

custom = (jsx: (id: number | string) => React.ReactElement, data?: ExternalToast) => {
custom = (jsx: (id: number | string) => React.ReactElement, data?: ExternalToast): string | number => {
const id = data?.id || toastsCounter++;
this.create({ jsx: jsx(id), id, ...data });
return id;
};
}

export const ToastState = new Observer();
export const ToastState: Observer = new Observer();

// bind this to the toast function
const toastFunction = (message: string | React.ReactNode, data?: ExternalToast) => {
const toastFunction = (message: string | React.ReactNode, data?: ExternalToast): string | number => {
const id = data?.id || toastsCounter++;

ToastState.addToast({
Expand All @@ -187,10 +188,20 @@ const toastFunction = (message: string | React.ReactNode, data?: ExternalToast)
return id;
};

const basicToast = toastFunction;
const basicToast: typeof toastFunction = toastFunction;

// We use `Object.assign` to maintain the correct types as we would lose them otherwise
export const toast = Object.assign(basicToast, {
export const toast: ((message: string | React.ReactNode, data?: ExternalToast) => string | number) & {
success: typeof ToastState.success,
info: typeof ToastState.info,
warning: typeof ToastState.warning,
error: typeof ToastState.error,
custom: typeof ToastState.custom,
message: typeof ToastState.message,
promise: typeof ToastState.promise,
dismiss: typeof ToastState.dismiss,
loading: typeof ToastState.loading,
} = Object.assign(basicToast, {
success: ToastState.success,
info: ToastState.info,
warning: ToastState.warning,
Expand Down