mirror of
https://github.com/zhigang1992/nativewind.git
synced 2026-06-20 03:48:31 +08:00
fix: font weight parsing
This commit is contained in:
@@ -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: [
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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") {
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user