Skip to content

Commit 3ebdcd5

Browse files
committed
Toast code updates & feedback applied
1 parent 7757eb0 commit 3ebdcd5

File tree

7 files changed

+28
-30
lines changed

7 files changed

+28
-30
lines changed

packages/lib/src/toast/Toast.accessibility.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const ToastPage = () => {
2525
/>
2626
);
2727
};
28+
2829
const TestExample = () => (
2930
<DxcToastsQueue>
3031
<ToastPage />

packages/lib/src/toast/Toast.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import ToastPropsType from "./types";
77
import useTimeout from "../utils/useTimeout";
88
import { HalstackLanguageContext } from "../HalstackContext";
99
import { responsiveSizes } from "../common/variables";
10-
import { getSemantic } from "./utils";
10+
import getSemantic from "./utils";
1111
import ToastIcon from "./ToastIcon";
1212

1313
const fadeInUp = keyframes`
@@ -104,12 +104,12 @@ const DxcToast = ({
104104
);
105105

106106
return (
107-
<Toast semantic={semantic} isClosing={isClosing} role="status">
107+
<Toast isClosing={isClosing} role="status" semantic={semantic}>
108108
<ContentContainer loading={loading} semantic={semantic}>
109109
<ToastIcon hideSemanticIcon={hideSemanticIcon} icon={icon} loading={loading} semantic={semantic} />
110110
<Message>{message}</Message>
111111
</ContentContainer>
112-
<DxcFlex alignItems="center" gap="0.25rem">
112+
<DxcFlex alignItems="center" gap="var(--spacing-gap-s)">
113113
{action && (
114114
<DxcButton
115115
icon={action.icon}

packages/lib/src/toast/ToastIcon.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { memo } from "react";
22
import { DxcSpinner } from "..";
33
import DxcIcon from "../icon/Icon";
44
import ToastPropsType from "./types";
5-
import { getSemantic } from "./utils";
5+
import getSemantic from "./utils";
66

77
const ToastIcon = memo(
88
({

packages/lib/src/toast/ToastsQueue.tsx

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import DxcToast from "./Toast";
66
import { QueuedToast, Semantic, ToastsQueuePropsType, ToastType } from "./types";
77
import { responsiveSizes } from "../common/variables";
88
import ToastContext from "./ToastContext";
9+
import { generateUniqueToastId } from "./utils";
910

1011
const ToastsQueue = styled.section`
1112
box-sizing: border-box;
@@ -25,17 +26,7 @@ const ToastsQueue = styled.section`
2526
}
2627
`;
2728

28-
const generateUniqueToastId = (toasts: QueuedToast[]) => {
29-
let id = "";
30-
let exists = true;
31-
while (exists) {
32-
id = `${performance.now()}-${Math.random().toString(36).slice(2, 9)}`;
33-
exists = toasts.some((toast) => toast.id === id);
34-
}
35-
return id;
36-
};
37-
38-
const DxcToastsQueue = ({ children, duration = 3000 }: ToastsQueuePropsType) => {
29+
export default function DxcToastsQueue({ children, duration = 3000 }: ToastsQueuePropsType) {
3930
const [toasts, setToasts] = useState<QueuedToast[]>([]);
4031
const [isMounted, setIsMounted] = useState(false); // Next.js SSR mounting issue
4132
const adjustedDuration = useMemo(() => (duration > 5000 ? 5000 : duration < 3000 ? 3000 : duration), [duration]);
@@ -78,6 +69,4 @@ const DxcToastsQueue = ({ children, duration = 3000 }: ToastsQueuePropsType) =>
7869
{children}
7970
</ToastContext.Provider>
8071
);
81-
};
82-
83-
export default DxcToastsQueue;
72+
}

packages/lib/src/toast/types.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,16 @@ type ToastPropsType = {
6565
hideSemanticIcon?: boolean;
6666
};
6767

68-
type ToastsQueuePropsType = {
68+
type ToastsQueuePropsType = {
69+
/**
70+
* Tree of components from which the useToast hook can be triggered.
71+
*/
72+
children: ReactNode;
6973
/**
7074
* Duration in milliseconds before a toast automatically hides itself.
7175
* The range goes from 3000ms to 5000ms, any other value will not be taken into consideration.
7276
*/
7377
duration?: number;
74-
/**
75-
* Tree of components from which the useToast hook can be triggered.
76-
*/
77-
children: ReactNode;
7878
};
7979

8080
export default ToastPropsType;

packages/lib/src/toast/useToast.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useContext, useMemo } from "react";
22
import ToastContext from "./ToastContext";
33
import { DefaultToast, SemanticToast, LoadingToast } from "./types";
44

5-
const useToast = () => {
5+
export default function useToast() {
66
const add = useContext(ToastContext);
77

88
const toast = useMemo(
@@ -15,8 +15,6 @@ const useToast = () => {
1515
}),
1616
[add]
1717
);
18-
18+
1919
return toast;
20-
};
21-
22-
export default useToast;
20+
}

packages/lib/src/toast/utils.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import ToastPropsType from "./types";
1+
import ToastPropsType, { QueuedToast } from "./types";
22

3-
export const getSemantic = (semantic: ToastPropsType["semantic"]) => {
3+
export default function getSemantic(semantic: ToastPropsType["semantic"]) {
44
switch (semantic) {
55
case "default":
66
return {
@@ -27,4 +27,14 @@ export const getSemantic = (semantic: ToastPropsType["semantic"]) => {
2727
icon: "filled_warning",
2828
};
2929
}
30+
}
31+
32+
export function generateUniqueToastId(toasts: QueuedToast[]) {
33+
let id = "";
34+
let exists = true;
35+
while (exists) {
36+
id = `${performance.now()}-${Math.random().toString(36).slice(2, 9)}`;
37+
exists = toasts.some((toast) => toast.id === id);
38+
}
39+
return id;
3040
};

0 commit comments

Comments
 (0)