Skip to content

Commit

Permalink
feat(utils): added getPx function to dom utils
Browse files Browse the repository at this point in the history
  • Loading branch information
hirotomoyamada committed Dec 27, 2023
1 parent 38cfa0b commit e28a099
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-years-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@yamada-ui/utils": patch
---

Added `getPx` function
20 changes: 20 additions & 0 deletions packages/utils/src/dom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type React from "react"
import { isNumber, isUndefined } from "."

export const createdDom = (): boolean =>
!!(
Expand Down Expand Up @@ -61,6 +62,25 @@ export const isContains = (
return parent === child || parent?.contains(child)
}

export const getPx = (value: string | number | undefined): number => {
if (isNumber(value)) return value

if (isUndefined(value)) return 0

if (value.includes("px")) return parseFloat(value)

const isBrowser = createdDom()
let fontSize = 16

if (isBrowser) {
const style = window.getComputedStyle(document.documentElement)

fontSize = parseFloat(style.fontSize)
}

return parseFloat(value) * fontSize
}

export const getEventRelatedTarget = (
ev: React.FocusEvent | React.MouseEvent,
) =>
Expand Down

0 comments on commit e28a099

Please sign in to comment.