Skip to content

Commit

Permalink
update props instead of overriding
Browse files Browse the repository at this point in the history
  • Loading branch information
fkhadra committed Nov 25, 2023
1 parent 53f8a73 commit e428326
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 31 deletions.
15 changes: 6 additions & 9 deletions src/components/ToastContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import cx from 'clsx';
import React, { useLayoutEffect, useMemo, useRef, useState } from 'react';
import React, { useLayoutEffect, useRef, useState } from 'react';

import { toast } from '../core';
import { useToastContainer } from '../hooks/useToastContainer';
import { ToastContainerProps, ToastPosition } from '../types';
import { Default, Direction, isFn, parseClassName } from '../utils';
import { Toast } from './Toast';
import { Bounce } from './Transitions';
import { toast } from '../core';

export const defaultProps: ToastContainerProps = {
position: 'top-right',
Expand All @@ -23,13 +23,10 @@ export const defaultProps: ToastContainerProps = {
};

export function ToastContainer(props: ToastContainerProps) {
let containerProps: ToastContainerProps = useMemo(
() => ({
...defaultProps,
...props
}),
[props]
);
let containerProps: ToastContainerProps = {
...defaultProps,
...props
};
const stacked = props.stacked;
const [collapsed, setIsCollapsed] = useState(true);
const containerRef = useRef<HTMLDivElement>(null);
Expand Down
20 changes: 9 additions & 11 deletions src/core/containerObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,19 @@ interface ActiveToast {
staleId?: Id;
}

interface ContainerObserverParams {
id: Id;
props: ToastContainerProps;
dispatchChanges: OnChangeCallback;
}

export type ContainerObserver = ReturnType<typeof createContainerObserver>;

export function createContainerObserver({
id,
props,
dispatchChanges
}: ContainerObserverParams) {
export function createContainerObserver(
id: Id,
containerProps: ToastContainerProps,
dispatchChanges: OnChangeCallback
) {
let toastKey = 1;
let toastCount = 0;
let queue: QueuedToast[] = [];
let activeToasts: Id[] = [];
let snapshot: Toast[] = [];
let props = containerProps;
const toasts = new Map<Id, Toast>();
const listeners = new Set<Notify>();

Expand Down Expand Up @@ -218,6 +213,9 @@ export function createContainerObserver({
toasts,
clearQueue,
buildToast,
setProps(p: ToastContainerProps) {
props = p;
},
setToggle: (id: Id, fn: (v: boolean) => void) => {
toasts.get(id)!.toggle = fn;
},
Expand Down
9 changes: 4 additions & 5 deletions src/core/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,7 @@ export function registerContainer(props: ToastContainerProps) {
const id = props.containerId || Default.CONTAINER_ID;
return {
subscribe(notify: () => void) {
const container = createContainerObserver({
id,
props,
dispatchChanges
});
const container = createContainerObserver(id, props, dispatchChanges);

containers.set(id, container);
const unobserve = container.observe(notify);
Expand All @@ -137,6 +133,9 @@ export function registerContainer(props: ToastContainerProps) {
containers.delete(id);
};
},
setProps(p: ToastContainerProps) {
containers.get(id)?.setProps(p);
},
getSnapshot() {
return containers.get(id)?.getSnapshot();
}
Expand Down
12 changes: 6 additions & 6 deletions src/hooks/useToastContainer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useMemo } from 'react';
import { useSyncExternalStore } from 'use-sync-external-store';
import { useRef } from 'react';
import { useSyncExternalStore } from 'use-sync-external-store/shim';
import { isToastActive, registerContainer } from '../core/store';
import { Toast, ToastContainerProps, ToastPosition } from '../types';

export function useToastContainer(props: ToastContainerProps) {
const { subscribe, getSnapshot } = useMemo(
() => registerContainer(props),
[props]
);
const { subscribe, getSnapshot, setProps } = useRef(
registerContainer(props)
).current;
setProps(props);
const snapshot = useSyncExternalStore(subscribe, getSnapshot);

function getToastToRender<T>(
Expand Down

0 comments on commit e428326

Please sign in to comment.