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(core): auto px addition to css properties for unitless numbers #5426

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 13 additions & 2 deletions packages/qwik/src/core/render/execute-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { handleError } from './error-handling';
import { HOST_FLAG_DIRTY, HOST_FLAG_MOUNTED, type QContext } from '../state/context';
import { isSignal, SignalUnassignedException } from '../state/signal';
import { isJSXNode } from './jsx/jsx-runtime';
import { isUnitlessNumber } from '../util/unitless_number';

export interface ExecuteComponentOutput {
node: JSXNode | null;
Expand Down Expand Up @@ -164,8 +165,11 @@ export const stringifyStyle = (obj: any): string => {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
const value = obj[key];
if (value != null) {
const normalizedKey = key.startsWith('--') ? key : fromCamelToKebabCase(key);
chunks.push(normalizedKey + ':' + value);
if (key.startsWith('--')) {
chunks.push(key + ':' + value);
} else {
chunks.push(fromCamelToKebabCase(key) + ':' + setValueForStyle(key, value));
}
}
}
}
Expand All @@ -175,6 +179,13 @@ export const stringifyStyle = (obj: any): string => {
return String(obj);
};

const setValueForStyle = (styleName: string, value: any) => {
if (typeof value === 'number' && value !== 0 && !isUnitlessNumber(styleName)) {
return value + 'px';
}
return value;
};

export const getNextIndex = (ctx: RenderContext) => {
return intToStr(ctx.$static$.$containerState$.$elementIndex$++);
};
Expand Down
51 changes: 51 additions & 0 deletions packages/qwik/src/core/util/unitless_number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/** CSS properties which accept numbers but are not in units of "px". */
gioboa marked this conversation as resolved.
Show resolved Hide resolved
const unitlessNumbers = new Set([
'animationIterationCount',
'aspectRatio',
'borderImageOutset',
'borderImageSlice',
'borderImageWidth',
'boxFlex',
'boxFlexGroup',
'boxOrdinalGroup',
'columnCount',
'columns',
'flex',
'flexGrow',
'flexShrink',
'gridArea',
'gridRow',
'gridRowEnd',
'gridRowStart',
'gridColumn',
'gridColumnEnd',
'gridColumnStart',
'fontWeight',
'lineClamp',
'lineHeight',
'opacity',
'order',
'orphans',
'scale',
'tabSize',
'widows',
'zIndex',
'zoom',
'MozAnimationIterationCount', // Known Prefixed Properties
'MozBoxFlex', // TODO: Remove these since they shouldn't be used in modern code
'msFlex',
'msFlexPositive',
'WebkitAnimationIterationCount',
'WebkitBoxFlex',
'WebkitBoxOrdinalGroup',
'WebkitColumnCount',
'WebkitColumns',
'WebkitFlex',
'WebkitFlexGrow',
'WebkitFlexShrink',
'WebkitLineClamp',
]);

export const isUnitlessNumber = (name: string): boolean => {
return unitlessNumbers.has(name);
};
Loading