From fa046c4e7c34ceebd10602dd167bca678494abdf Mon Sep 17 00:00:00 2001 From: Mark Lawlor Date: Tue, 18 Oct 2022 12:57:59 +1000 Subject: [PATCH] fix: font weight parsing --- .../__tests__/tailwind-serialization/kitchen-sink.ts | 10 ++++++++++ packages/nativewind/src/style-sheet/resolve.ts | 11 +++++------ .../nativewind/src/transform-css/encode-value.ts | 9 ++------- packages/nativewind/src/transform-css/index.ts | 4 ++++ .../src/transform-css/transforms/font-weight.ts | 3 ++- .../nativewind/src/transform-css/transforms/push.ts | 12 +++++++++--- 6 files changed, 32 insertions(+), 17 deletions(-) diff --git a/packages/nativewind/__tests__/tailwind-serialization/kitchen-sink.ts b/packages/nativewind/__tests__/tailwind-serialization/kitchen-sink.ts index 4a33dda..ae269fa 100644 --- a/packages/nativewind/__tests__/tailwind-serialization/kitchen-sink.ts +++ b/packages/nativewind/__tests__/tailwind-serialization/kitchen-sink.ts @@ -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: [ { diff --git a/packages/nativewind/src/style-sheet/resolve.ts b/packages/nativewind/src/style-sheet/resolve.ts index 9dcc3e8..7ff0df7 100644 --- a/packages/nativewind/src/style-sheet/resolve.ts +++ b/packages/nativewind/src/style-sheet/resolve.ts @@ -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; } diff --git a/packages/nativewind/src/transform-css/encode-value.ts b/packages/nativewind/src/transform-css/encode-value.ts index 3002b87..1a29013 100644 --- a/packages/nativewind/src/transform-css/encode-value.ts +++ b/packages/nativewind/src/transform-css/encode-value.ts @@ -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)) { diff --git a/packages/nativewind/src/transform-css/index.ts b/packages/nativewind/src/transform-css/index.ts index 9477f4c..8a6c816 100644 --- a/packages/nativewind/src/transform-css/index.ts +++ b/packages/nativewind/src/transform-css/index.ts @@ -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).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; diff --git a/packages/nativewind/src/transform-css/transforms/font-weight.ts b/packages/nativewind/src/transform-css/transforms/font-weight.ts index eb69cc6..526907c 100644 --- a/packages/nativewind/src/transform-css/transforms/font-weight.ts +++ b/packages/nativewind/src/transform-css/transforms/font-weight.ts @@ -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") { diff --git a/packages/nativewind/src/transform-css/transforms/push.ts b/packages/nativewind/src/transform-css/transforms/push.ts index 736daf7..6b4779b 100644 --- a/packages/nativewind/src/transform-css/transforms/push.ts +++ b/packages/nativewind/src/transform-css/transforms/push.ts @@ -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;