Comparing version 1.0.1 to 1.0.2
import * as react from 'react'; | ||
import { HTMLAttributes, FC, ComponentProps } from 'react'; | ||
import { FC, ComponentProps, HTMLAttributes } from 'react'; | ||
@@ -10,5 +10,7 @@ declare const Reason: { | ||
declare function useDiscardableEffect(fn?: (discard: () => void | Promise<void>) => void): void; | ||
declare function Container({ id, ...props }: Omit<HTMLAttributes<HTMLDivElement>, 'id' | 'children'> & { | ||
interface ContainerProps extends Omit<HTMLAttributes<HTMLDivElement>, 'id' | 'children'> { | ||
id: string; | ||
}): react.ReactPortal; | ||
} | ||
declare function InlineContainer({ id: idProp, ...props }: ContainerProps): JSX.Element; | ||
declare function Container(props: ContainerProps): react.ReactPortal | null; | ||
type SettlerReturnValue<T extends FC, K extends 'onResolve' | 'onReject'> = ComponentProps<T> extends Partial<Record<K, (value: infer R) => void>> ? R : void; | ||
@@ -21,2 +23,2 @@ interface Toaster<T extends FC<any>> { | ||
export { Container, Reason, Toaster, toaster, useDiscardableEffect }; | ||
export { Container, InlineContainer, Reason, Toaster, toaster, useDiscardableEffect }; |
55
index.js
import EventEmitter from 'eventemitter3'; | ||
import uniqueId from 'lodash/uniqueId'; | ||
import { createContext, useRef, useEffect, useContext, useState } from 'react'; | ||
import { createContext, useRef, useEffect, useContext, useState, useMemo } from 'react'; | ||
import { createPortal } from 'react-dom'; | ||
@@ -57,3 +57,2 @@ import { jsx } from 'react/jsx-runtime'; | ||
let emitter; | ||
const containers = {}; | ||
function getEmitter() { | ||
@@ -65,22 +64,10 @@ if (!emitter) { | ||
} | ||
function Container({ | ||
id, | ||
function InlineContainer({ | ||
id: idProp, | ||
...props | ||
}) { | ||
const containerRef = useRef(document.createElement("div")); | ||
containerRef.current.id = id; | ||
const [metadatas, setMetadatas] = useState({}); | ||
const id = useMemo(() => uniqueId(idProp), [idProp]); | ||
useEffect(() => { | ||
let mounted = true; | ||
const { | ||
current: container | ||
} = containerRef; | ||
const { | ||
id: id2 | ||
} = container; | ||
if (Object.prototype.hasOwnProperty.call(containers, id2) && containers[id2]) { | ||
throw new Error("Container id already taken"); | ||
} | ||
containers[id2] = true; | ||
document.body.appendChild(container); | ||
let cache = {}; | ||
@@ -115,9 +102,7 @@ async function onEvent(metadata) { | ||
} | ||
const en = eventName(id2); | ||
const en = eventName(idProp); | ||
getEmitter().on(en, onEvent); | ||
return () => { | ||
mounted = false; | ||
document.body.removeChild(container); | ||
getEmitter().off(en, onEvent); | ||
delete containers[id2]; | ||
for (const key in cache) { | ||
@@ -132,5 +117,6 @@ if (!Object.prototype.hasOwnProperty.call(cache, key)) { | ||
}; | ||
}, []); | ||
return /*#__PURE__*/createPortal( /*#__PURE__*/jsx("div", { | ||
}, [idProp]); | ||
return /*#__PURE__*/jsx("div", { | ||
...props, | ||
id: id, | ||
children: Object.entries(metadatas).map(([key, { | ||
@@ -162,4 +148,25 @@ component: C, | ||
}) | ||
}), containerRef.current); | ||
}); | ||
} | ||
function Container(props) { | ||
const containerRef = useRef(typeof document === "undefined" ? void 0 : document.createElement("div")); | ||
useEffect(() => { | ||
const { | ||
current: container | ||
} = containerRef; | ||
if (!container) { | ||
return () => void 0; | ||
} | ||
document.body.appendChild(container); | ||
return () => { | ||
document.body.removeChild(container); | ||
}; | ||
}, []); | ||
if (containerRef.current) { | ||
return /*#__PURE__*/createPortal( /*#__PURE__*/jsx(InlineContainer, { | ||
...props | ||
}), containerRef.current); | ||
} | ||
return null; | ||
} | ||
function toaster(component, id) { | ||
@@ -205,2 +212,2 @@ let metadata; | ||
export { Container, Reason, toaster, useDiscardableEffect }; | ||
export { Container, InlineContainer, Reason, toaster, useDiscardableEffect }; |
{ | ||
"name": "toasterhea", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Promise-driven toast poppin' library.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16488
435