Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support rootFontSize for rem2px #301

Merged
merged 1 commit into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Browse your utilities usages, have an overview of your design system, identify "
| `windicss.enableAttrUtilityCompletion` | boolean | true | Enable Utility Completion For Attributify Mode. |
| `windicss.enableAttrVariantCompletion` | boolean | true | Enable Variant Completion For Attributify Mode. |
| `windicss.enableRemToPxPreview` | boolean | true | Enable Rem to Px Preview. |
| `windicss.rootFontSize` | number | 16 | Used to convert rem CSS values to their px equivalents. |
| `windicss.enableEmmetCompletion` | boolean | false | Enable . trigger for emmet. |
| `windicss.enableCodeFolding` | boolean | false | Enable ClassNames Code Folding. |
| `windicss.colorDecoratorsType` | string | 'cube' | Configure the type of color decorations, optional ['border', 'bg', 'cube', 'picker'], default is 'cube'. |
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@
"default": true,
"description": "Enable rem to px preview."
},
"windicss.rootFontSize": {
"type": "number",
"default": 16,
"description": "Used to convert rem CSS values to their px equivalents."
},
"windicss.enableCodeFolding": {
"type": "boolean",
"default": false,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function toggleConfig(key: string) {
}

export function buildStyle(styleSheet?: StyleSheet) {
return styleSheet ? highlightCSS(getConfig('windicss.enableRemToPxPreview') ? rem2px(styleSheet.build()) : styleSheet.build()) : undefined;
return styleSheet ? highlightCSS(getConfig('windicss.enableRemToPxPreview') ? rem2px(styleSheet.build(), getConfig('windicss.rootFontSize')) : styleSheet.build()) : undefined;
}

export function buildEmptyStyle(style: Style) {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ export function combineSeparators(separators: string[], sortedP: string[]) {
}


export function rem2px(str?: string) {
export function rem2px(str?: string, rootFontSize: number = 16) {
if (!str) return;
let index = 0;
const output: string[] = [];

while (index < str.length) {
const rem = str.slice(index,).match(/-?[\d.]+rem;/);
if (!rem || !rem.index) break;
const px = ` /* ${parseFloat(rem[0].slice(0, -4)) * 16}px */`;
const px = ` /* ${parseFloat(rem[0].slice(0, -4)) * rootFontSize}px */`;
const end = index + rem.index + rem[0].length;
output.push(str.slice(index, end));
output.push(px);
Expand Down