Ant Design Vue v4版本如何解决1px没有被postcss-px2rem转成rem的问题

背景说明

如果你的 Ant Design Vue 项目有要做适配的需求,那首先要选择一种适配方案。笔者选择的是用 postcss-px2rem 进行适配。笔者在配置了 postcss-px2rem的相关配置后,发现 postcss-px2rem 没有对 Ant Design Vue 进行适配。在网上看了一些文章之后,发现想对 Ant Design Vue 进行适配需要走它自己的规则:给 a-style-provider 组件传入 transformerspropsAnt Design Vue 官方自己提供了一套 transformers 方法,用这套方法可以对大于1px的单位进行转换,而不凑巧的是笔者的项目有对1px进行适配的需求,所以笔者必须解决这个问题。

解决方案

不直接用官方的方案,而是用官方方案的修改版。

编写一个px2rem的方法,然后在 a-style-provider 里引进去。

typescript 复制代码
// 这应该是一个文件,例如px2Rem.ts
import unitless from '@emotion/unitless'
export interface Options {
  /**
   * The root font size.
   * @default 16
   */
  rootValue?: number
  /**
   * The decimal numbers to allow the REM units to grow to.
   * @default 5
   */
  precision?: number
  /**
   * Whether to allow px to be converted in media queries.
   * @default false
   */
  mediaQuery?: boolean
}

const pxRegex = /url\([^)]+\)|var\([^)]+\)|(\d*\.?\d+)px/g

function toFixed(number: number, precision: number) {
  const multiplier = Math.pow(10, precision + 1),
    wholeNumber = Math.floor(number * multiplier)
  return (Math.round(wholeNumber / 10) * 10) / multiplier
}

const transform = (options: Options = {}): Transformer => {
  const { rootValue = 16, precision = 5, mediaQuery = false } = options

  const pxReplace = (m: string, $1: any) => {
    if (!$1) return m
    const pixels = parseFloat($1)
    if (pixels < 1) return m
    const fixedVal = toFixed(pixels / rootValue, precision)
    return `${fixedVal}rem`
  }

  const visit = (cssObj: any): any => {
    const clone: any = { ...cssObj }

    Object.entries(cssObj).forEach(([key, value]) => {
      if (typeof value === 'string' && value.includes('px')) {
        const newValue = value.replace(pxRegex, pxReplace)
        clone[key] = newValue
      }

      // no unit
      if (!unitless[key] && typeof value === 'number' && value !== 0) {
        clone[key] = `${value}px`.replace(pxRegex, pxReplace)
      }

      // Media queries
      const mergedKey = key.trim()
      if (mergedKey.startsWith('@') && mergedKey.includes('px') && mediaQuery) {
        const newKey = key.replace(pxRegex, pxReplace)

        clone[newKey] = clone[key]
        delete clone[key]
      }
    })

    return clone
  }

  return { visit } as Transformer<any, any>
}

export default transform

App.vue该如下

html 复制代码
import px2rem from "./px2Rem.ts" // 自行修改
<template>
  <a-config-provider>
    <a-style-provider :transformers="[px2rem]">
          <component />
    </a-style-provider>
  </a-config-provider>
</template>
相关推荐
Mintopia18 分钟前
计算机图形学的奇幻之旅:第三天探索
前端·javascript·计算机图形学
Mintopia21 分钟前
Three.js 物理材质:打造 3D 世界的 “魔法皮肤”
前端·javascript·three.js
知识分享小能手43 分钟前
Typescript学习教程,从入门到精通,TypeScript 泛型与类型操作详解(二)(17)
前端·javascript·学习·typescript·jquery·前端网页学习
stark张宇1 小时前
Web - Javascript 函数与DOM、BOM
前端
上海张律师1 小时前
组件截图sdk -- screenshot_hm介绍 ##三方SDK##
前端·harmonyos
lineo_1 小时前
抛弃陈旧写法,你的uniapp定义globalData的正确姿势(setup语法糖)
前端·javascript
spionbo1 小时前
Vue 结合 D3 实现可拖拽拓扑图的技术方案及具体应用实例解析
前端·javascript
GIS之路1 小时前
OpenLayers 图形交互编辑
前端
ViceBoy_1 小时前
前端混淆的概念-缓存&存储
前端·面试
中微子1 小时前
从C++看JavaScript闭包:执行上下文与作用域的跨语言对比
前端·c++