Skip to content

Commit

Permalink
Merge pull request eleme#113 from hwaphon/fix/20230914_animation_exce…
Browse files Browse the repository at this point in the history
…ption

fix: animation 动画因用户指定单位导致双单位异常问题
  • Loading branch information
hwaphon committed Sep 14, 2023
2 parents b661815 + 07e9756 commit 9c41ad2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/runtime-web/src/components/src/utils/animation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { addUnit } from './unit'
export const KEY_ANIMATION = 'animation'

const propertyRegex = (key) => new RegExp(key + '\\(([^)]+)\\)', 'i')
Expand Down Expand Up @@ -64,7 +65,7 @@ const formateTransformValue = (key, value) => {
return value
} else if (isTranslate || isSkew) {
const unit = isSkew ? 'deg' : 'px'
value = value.map((item) => item + unit)
value = value.map((item) => addUnit(item, unit))
return value
}

Expand Down
20 changes: 20 additions & 0 deletions packages/runtime-web/src/components/src/utils/unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function isNumeric(val: string): boolean {
return /^\d+(\.\d+)?$/.test(val)
}

function isDef<T>(val: T): val is NonNullable<T> {
return val !== undefined && val !== null
}

export function addUnit(
value: string | number,
unit = 'px'
): string | undefined {
if (!isDef(value)) {
return undefined
}

// eslint-disable-next-line no-param-reassign
value = String(value)
return isNumeric(value) ? `${value}${unit}` : value
}

0 comments on commit 9c41ad2

Please sign in to comment.