fix: font weight parsing

This commit is contained in:
Mark Lawlor
2022-10-18 12:57:59 +10:00
parent 49ed195698
commit fa046c4e7c
6 changed files with 32 additions and 17 deletions

View File

@@ -253,6 +253,8 @@ testCompile(
// Typography - Font Style
italic
not-italic
// Typography - Font Weight
font-bold
// Typography - Line Height
leading-3
leading-tight
@@ -804,6 +806,14 @@ testCompile(
],
topics: ["--window-width"],
},
"font-bold": {
styles: [
{
fontWeight: "700",
},
],
},
"z-10": {
styles: [
{

View File

@@ -7,12 +7,11 @@ export function resolve(
): string | number | ColorValue | undefined {
if (!style) return;
if (typeof style === "string" || typeof style === "number") {
if (typeof style === "string") {
if (style.endsWith("%")) return style;
const maybeNumber = Number.parseFloat(style);
return Number.isNaN(maybeNumber) ? style : maybeNumber;
}
if (typeof style === "string") {
return style;
}
if (typeof style === "number") {
return style;
}

View File

@@ -9,14 +9,9 @@ import {
FunctionValue,
} from "./types";
export interface EncodeValueOptions {
forceString?: boolean;
}
export function encodeValue(
node: StyleValue | null | undefined,
topics: string[],
{ forceString = false }: EncodeValueOptions = {}
topics: string[]
): StyleValue | StyleValue[] | undefined {
if (!node) return;
@@ -26,7 +21,7 @@ export function encodeValue(
}
if (typeof node === "number") {
return forceString ? node : node.toString();
return node;
}
if (Array.isArray(node)) {

View File

@@ -15,6 +15,7 @@ import { textDecoration } from "./transforms/text-decoration";
import { textDecorationLine } from "./transforms/text-decoration-line";
import { textShadow } from "./transforms/text-shadow";
import { transform } from "./transforms/transform";
import { fontWeight } from "./transforms/font-weight";
const skip = (walk as unknown as Record<string, unknown>).skip;
@@ -188,6 +189,9 @@ function getDeclarations(block: Block, meta: SelectorMeta) {
case "font-family":
styles.push(...fontFamily(node, meta));
break;
case "font-weight":
styles.push(...fontWeight(node, meta));
break;
case "place-content":
styles.push(...placeContent(node, meta));
break;

View File

@@ -2,7 +2,8 @@ import { Declaration } from "css-tree";
import { AtomStyle, SelectorMeta } from "../types";
import { pushStyle } from "./push";
export function fontFamily(node: Declaration, meta: SelectorMeta) {
// This only exists to force fontWeight into a string :(
export function fontWeight(node: Declaration, meta: SelectorMeta) {
const styles: AtomStyle[] = [];
if (node.value.type !== "Value") {

View File

@@ -1,17 +1,23 @@
import { StyleProperty, validProperties } from "./valid-styles";
import { AtomStyle, SelectorMeta, StyleValue } from "../types";
import { encodeValue, EncodeValueOptions } from "../encode-value";
import { encodeValue } from "../encode-value";
export interface PushStyleOptions {
// This only exists to force fontWeight into a string :(
forceString?: boolean;
}
export function pushStyle(
styles: AtomStyle[],
property: string,
meta: SelectorMeta,
node: StyleValue | null | undefined,
options?: EncodeValueOptions
{ forceString = false }: PushStyleOptions = {}
) {
if (!node) return;
const value = encodeValue(node, meta.topics, options);
let value = encodeValue(node, meta.topics);
if (forceString) value = value?.toString();
if (value === undefined || value === null) return;