1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-17 16:53:34 +00:00
Files
browser/libs/components/src/toast/utils.ts
Will Martin d5f503a0d6 [CL-18] toast component and service (#6490)
Update toast styles and new service to CL.
2024-04-18 13:23:35 -04:00

15 lines
578 B
TypeScript

/**
* Given a toast message, calculate the ideal timeout length following:
* a minimum of 5 seconds + 1 extra second per 120 additional words
*
* @param message the toast message to be displayed
* @returns the timeout length in milliseconds
*/
export const calculateToastTimeout = (message: string | string[]): number => {
const paragraphs = Array.isArray(message) ? message : [message];
const numWords = paragraphs
.map((paragraph) => paragraph.split(/\s+/).filter((word) => word !== ""))
.flat().length;
return 5000 + Math.floor(numWords / 120) * 1000;
};