forked from TEN-framework/ten-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
36 lines (26 loc) · 1 KB
/
Copy pathutils.ts
File metadata and controls
36 lines (26 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import * as React from "react"
import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
export function useIsMobileScreen(breakpoint?: string) {
const [isMobileScreen, setIsMobileScreen] = React.useState(false)
React.useEffect(() => {
const mql = window.matchMedia(`(max-width: ${breakpoint ?? "768px"})`)
setIsMobileScreen(mql.matches)
const listener = () => setIsMobileScreen(mql.matches)
mql.addEventListener("change", listener)
return () => mql.removeEventListener("change", listener)
}, [breakpoint])
return isMobileScreen
}
export function formatNumber(num: number, decimals: number = 1): string {
if (num === 0) return "0"
const k = 1000
const sizes = ["", "K", "M", "B", "T"]
const i = Math.floor(Math.log(Math.abs(num)) / Math.log(k))
if (i === 0) return num.toString()
const scaled = num / Math.pow(k, i)
return `${scaled.toFixed(decimals)}${sizes[i]}`
}