-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: fix table column data is passed into chlidren is undefined or null errorr * feat: add px2rem in cssinjs
- Loading branch information
1 parent
cacbde3
commit 80a5b53
Showing
4 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { Linter } from '..'; | ||
import { lintWarning } from './utils'; | ||
|
||
const linter: Linter = (_key, _value, info) => { | ||
if ( | ||
info.parentSelectors.some(selector => { | ||
const selectors = selector.split(','); | ||
return selectors.some(item => item.split('&').length > 2); | ||
}) | ||
) { | ||
lintWarning('Should not use more than one `&` in a selector.', info); | ||
} | ||
}; | ||
|
||
export default linter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* respect https://github.com/cuth/postcss-pxtorem | ||
*/ | ||
import unitless from '@emotion/unitless'; | ||
import type { CSSObject } from '..'; | ||
import type { Transformer } from './interface'; | ||
|
||
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); | ||
// covenant: pixels <= 1, not transform to rem @zombieJ | ||
if (pixels <= 1) return m; | ||
const fixedVal = toFixed(pixels / rootValue, precision); | ||
return `${fixedVal}rem`; | ||
}; | ||
|
||
const visit = (cssObj: CSSObject): CSSObject => { | ||
const clone: CSSObject = { ...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 }; | ||
}; | ||
|
||
export default transform; |