Common front-end components for Web using the Tesseract design system, written for React. https://make.modal.sh/tesseract/web/react/common
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

splitValueAndUnit.ts 598 B

12345678910111213141516171819202122232425
  1. export type Unit = 'px' | '%' | 'rem'
  2. export interface ValueAndUnit {
  3. magnitude: number,
  4. unit: Unit,
  5. }
  6. interface SplitValueAndUnit {
  7. (value: any): ValueAndUnit
  8. }
  9. const splitValueAndUnit: SplitValueAndUnit = value => {
  10. if (!['string', 'number'].includes(typeof value)) {
  11. throw TypeError('Argument must be a valid CSS number')
  12. }
  13. const valueString = typeof value! === 'number' ? `${value}px` : String(value)
  14. const magnitude = parseFloat(valueString)
  15. return {
  16. magnitude,
  17. unit: valueString.slice(String(magnitude).length) as Unit,
  18. }
  19. }
  20. export default splitValueAndUnit