diff --git a/CHANGELOG.md b/CHANGELOG.md index 29ae36d..13a2ac5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- `box-shadow` maps to Kryon elevation roles, the `font` shorthand sets size, line height, weight and style, and generic font families map to the theme face or the code font role. + - Rules nest `&:state`, `&.class`, `&#id` and `&[attribute]` rules one level deep, flattened into compound selectors and exported with the parent selector. Combinator, `:not()` and child-position errors name the native idiom. - Lengths take em, vw and vh and calc(), min(), max() and clamp(): static ones fold while the sheet is read, viewport ones resolve at install and packs keep them; CssSheetSameMedia reports a change when a new viewport changes an active length. var() fallbacks may nest one var(), and var(--kryon-NAME) reads the installed theme palette. diff --git a/Makefile b/Makefile index 1016ffc..008fd54 100644 --- a/Makefile +++ b/Makefile @@ -58,7 +58,7 @@ diagnostics-test: $(CSS_TOOL) $(CSS_TOOL) check tests/css_diagnostics.css 2>$(BUILD)/css-diagnostics.log @if $(CSS_TOOL) check --strict tests/css_diagnostics.css 2>$(BUILD)/css-diagnostics-strict.log; then exit 1; else test $$? -eq 1; fi cmp $(BUILD)/css-diagnostics.log $(BUILD)/css-diagnostics-strict.log - grep -Fx "tests/css_diagnostics.css:5:3: web-only declaration 'box-shadow': declaration is applied only by the browser" $(BUILD)/css-diagnostics.log + grep -Fx "tests/css_diagnostics.css:5:3: web-only declaration 'text-transform': declaration is applied only by the browser" $(BUILD)/css-diagnostics.log grep -Fx "tests/css_diagnostics.css:14:1: skipped at-rule '@supports': at-rule 'supports' is outside Kryon CSS" $(BUILD)/css-diagnostics.log # Packs are compiled into rule tables apps load without parsing. The diff --git a/README.md b/README.md index b612384..73b30f8 100644 --- a/README.md +++ b/README.md @@ -101,8 +101,18 @@ must match), `-kryon-motion-duration`, `-kryon-motion-multiplier`, `-kryon-motion-easing` (`inherit`, `standard`, `linear`, `instant`) and `-kryon-motion-reduced` (`reduce`, `no-preference`). The web document host -also uses browser-only declarations such as `box-shadow`. Each native widget -uses the fields its paint and layout implement. +also uses browser-only declarations such as `text-transform`. Each native +widget uses the fields its paint and layout implement. +- `box-shadow` selects a Kryon elevation role: `none` clears it, a shadow + whose largest blur is at most 8px is `raised` and a softer one `floating` + (the widgets that draw elevation, such as Card and Modal, use their own + shadow geometry and colors). `font: [style] [weight] size[/line-height] + family` sets those fields. A generic first family (`system-ui`, + `sans-serif`, `serif`) keeps the theme's typeface and `monospace` or + `ui-monospace` selects the `code` font role; a named first family is the + typeface and later families are ignored natively. Per-side borders, + multi-stop, angled and radial gradients, `transform` and outline width and + style are browser-only. - `gap` takes one value for both axes or two values in row, column order. `row-gap` and `column-gap` cascade independently. Gaps take `normal` diff --git a/src/css_parser.zi b/src/css_parser.zi index 038e6c2..3f7676e 100644 --- a/src/css_parser.zi +++ b/src/css_parser.zi @@ -596,6 +596,7 @@ CssBegin :: (p: *CssParser, source: string, path: string) { #load "parser/properties.zi"; #load "parser/flex.zi"; #load "parser/box.zi"; +#load "parser/visual.zi"; #load "parser/media.zi"; #load "parser/motion.zi"; #load "parser/statements.zi"; diff --git a/src/parser/properties.zi b/src/parser/properties.zi index 7578c1b..9aa7b8d 100644 --- a/src/parser/properties.zi +++ b/src/parser/properties.zi @@ -393,15 +393,25 @@ CssReadTypeface :: (p: *CssParser, end: s32) { CssFail(p, "expected a font family") return } - if CssCollectIssues(p) { - CssSkipSpace(p) - if CssAtByte(p, #char ",") { - CssReportIssue(p, p.pending_name_cursor, CssIssueKind.Declaration, "font-family", - "native text uses only the first font family", true) + family := p.cursor.source[start:start + length] + generic := CssGenericFamily(family) + if generic >= 0 { + // A generic family keeps the theme's typeface; monospace is the code role. + if generic == cast(s32)FontRoleCode { + role: CssValue = .{.ref = -1, .ok = true, .material = generic} + CssSetValue(p, cast(s32)CssFieldFontRole, role) } + } else { + if CssCollectIssues(p) { + CssSkipSpace(p) + if CssAtByte(p, #char ",") { + CssReportIssue(p, p.pending_name_cursor, CssIssueKind.Declaration, "font-family", + "native text uses only the first font family", true) + } + } + p.template.typeface = family + p.template.fields |= cast(u32)StyleTypeface } - p.template.typeface = p.cursor.source[start:start + length] - p.template.fields |= cast(u32)StyleTypeface p.cursor.column += end - p.cursor.pos p.cursor.pos = end } @@ -516,8 +526,8 @@ CssPassWebDeclaration :: (p: *CssParser) -> s32 { return CssScopeWeb } -css_web_properties: [10]string = .[ - "text-decoration", "text-transform", "white-space", "cursor", "box-shadow", +css_web_properties: [9]string = .[ + "text-decoration", "text-transform", "white-space", "cursor", "grid-template-rows", "grid-column", "grid-row", "z-index", "visibility"]; // Properties the web document host applies and native hosts skip: browser @@ -643,6 +653,16 @@ CssApplyProperty :: (p: *CssParser, name: CssName, end: s32) -> s32 { } else if CssNameEquals(name, "font-family") { CssReadTypeface(p, end) return ifx CssFailed(p) then -1 else CssScopeNative + } else if CssNameEquals(name, "font") || CssNameEquals(name, "box-shadow") { + status := p.status + native := ifx CssNameEquals(name, "font") then CssApplyFontShorthand(p, end) else CssApplyBoxShadow(p, end) + if !native || CssFailed(p) { + p.status = status + p.diagnostic_length = 0 + return CssPassWebDeclaration(p) + } + p.cursor = p.pending_end + return CssScopeNative } else if CssNameEquals(name, "min-height") || CssNameEquals(name, "min-block-size") { value: CssValue = CssReadField(p, cast(s32)CssFieldMinHeight) if !value.ok { return -1 } diff --git a/src/parser/visual.zi b/src/parser/visual.zi new file mode 100644 index 0000000..1be34ba --- /dev/null +++ b/src/parser/visual.zi @@ -0,0 +1,108 @@ +// Shadows and type in Kryon's own systems: box-shadow becomes an elevation +// role and a generic font family a font role. Shadow geometry and colors, +// and fallback font lists, stay with the browser. + +// none clears the elevation; a shadow whose largest blur is at most 8px is +// raised and a softer one floating. Colors, offsets and inset are checked. +CssApplyBoxShadow :: (p: *CssParser, end: s32) -> bool { + origin := p.cursor + word := CssReadName(p) + value: CssValue + value.ref = -1 + value.ok = true + if CssNameEquals(word, "none") && !CssMoreValue(p, end) { + value.material = cast(s32)ElevationRoleNone + CssSetValue(p, cast(s32)CssFieldElevation, value) + return true + } + p.cursor = origin + blur: float64 = 0.0 + while CssMoreValue(p, end) { + lengths: s32 = 0 + while CssMoreValue(p, end) && !CssAtByte(p, #char ",") { + saved := p.cursor + number := CssReadNumber(p) + if CssFailed(p) { return false } + if number.ok { + pixels: float64 = number.value + if CssNameEquals(number.unit, "rem") { pixels *= CssRemPixels } + else if CssNameEquals(number.unit, "em") { pixels *= CssEmPixels(p) } + else if !CssNameEquals(number.unit, "px") && !(number.unit.length == 0 && number.value == 0.0) { + return false + } + lengths += 1 + if lengths == 3 && pixels > blur { blur = pixels } + continue + } + p.cursor = saved + name := CssReadName(p) + if CssNameEquals(name, "inset") { continue } + p.cursor = saved + color := CssReadColor(p, true) + if !color.ok { return false } + } + if lengths < 2 || lengths > 4 { return false } + if CssAtByte(p, #char ",") { CssAdvance(p, 1) } + } + value.material = cast(s32)(ifx blur <= 8.0 then ElevationRoleRaised else ElevationRoleFloating) + CssSetValue(p, cast(s32)CssFieldElevation, value) + return true +} + +// A generic family: -1 when it is not one, else the font role it maps to +// (code for monospace, the inherited role for the others). +CssGenericFamily :: (name: string) -> s32 { + if name == "monospace" || name == "ui-monospace" { return cast(s32)FontRoleCode } + if name == "sans-serif" || name == "serif" || name == "system-ui" || name == "ui-sans-serif" || + name == "ui-serif" || name == "cursive" || name == "fantasy" || name == "-apple-system" { + return cast(s32)FontRoleInherit + } + return -1 +} + +// font: [style] [weight] size[/line-height] family, family... +CssApplyFontShorthand :: (p: *CssParser, end: s32) -> bool { + for step: 0..1 { + saved := p.cursor + word := CssReadName(p) + if word.length == 0 { p.cursor = saved; break } + if CssNameEquals(word, "italic") || CssNameEquals(word, "oblique") { + style: CssValue = .{.ref = -1, .ok = true, .material = CssFontStyleValue(word)} + CssSetValue(p, cast(s32)CssFieldFontStyle, style) + } else if CssNameEquals(word, "bold") { + weight: CssValue = .{.ref = -1, .ok = true, .number = 700.0} + CssSetValue(p, cast(s32)CssFieldFontWeight, weight) + } else if !CssNameEquals(word, "normal") { + p.cursor = saved + break + } + CssSkipSpace(p) + } + saved := p.cursor + number := CssReadNumber(p) + if number.ok && number.unit.length == 0 && !number.percent && number.value >= 100.0 && number.value <= 900.0 { + weight: CssValue = .{.ref = -1, .ok = true, .number = cast(float32)number.value} + CssSetValue(p, cast(s32)CssFieldFontWeight, weight) + } else { + p.cursor = saved + } + // One term: the '/' after it starts the line height, not a division. + term := CssReadExprTerm(p, 0) + if CssFailed(p) || !term.ok || term.value.number { return false } + size: CssValue = .{.ref = -1, .ok = true, .number = cast(float32)CssExprAt(term, 0.0, 0.0)} + if !CssExprStatic(term) { + size.number = cast(float32)CssExprAt(term, CssDefaultViewportWidth, CssDefaultViewportHeight) + size.viewport = term + } + CssSetValue(p, cast(s32)CssFieldFontSize, size) + CssSkipSpace(p) + if CssAtByte(p, #char "/") { + CssAdvance(p, 1) + line: CssValue = CssReadField(p, cast(s32)CssFieldLineHeight) + if !line.ok { return false } + CssSetValue(p, cast(s32)CssFieldLineHeight, line) + } + if !CssMoreValue(p, end) { return false } + CssReadTypeface(p, end) + return !CssFailed(p) +} diff --git a/tests/css_diagnostics.css b/tests/css_diagnostics.css index a089dec..a8a831c 100644 --- a/tests/css_diagnostics.css +++ b/tests/css_diagnostics.css @@ -2,7 +2,7 @@ Row.fixture { gap: var(--gap); gap: 13px -2px; - box-shadow: 0 1px 2px black; + text-transform: uppercase; colour: red; broken-name red; & > Text { color: blue; } @@ -14,5 +14,5 @@ Row.fixture > Text { color: red; } @supports (display: grid) { Text { color: red; } } @media (orientation: landscape) { Text { color: red; } } Text { width: 100vw; transition: transform 1s; height: 24px; } -@keyframes flash { from { opacity: 0.2; box-shadow: none; } to { opacity: 1; } } +@keyframes flash { from { opacity: 0.2; text-transform: none; } to { opacity: 1; } } Text { font-family: "Inter", sans-serif; } diff --git a/tests/css_integration_behavior.zi b/tests/css_integration_behavior.zi index e734a52..c5b9269 100644 --- a/tests/css_integration_behavior.zi +++ b/tests/css_integration_behavior.zi @@ -460,6 +460,12 @@ Text.nest { &#special { color: #333333; } } +Card.soft { box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2); } +Card.lifted { box-shadow: 0 12px 32px #0004, inset 0 0 0 1px #fff2; } +Card.flat { box-shadow: none; } +Text.body { font: italic bold 15px/1.5 system-ui, sans-serif; } +Text.mono { font-family: ui-monospace, monospace; } + :root { --inner: 7px; } Text.nested { padding: var(--outer, var(--inner, 3px)); @@ -505,9 +511,26 @@ FluidValues :: () { unused ResolveInstalledLayoutStyle(facts, null, *box) Check(box.max_width == 700.0 && box.margin_left == 8.0, "box lengths fold calc, min and em") state := cast(s32)ButtonStateNormal + normal_state := state nested := ResolveInstalledStyleRules(StyleData.{}, StyleTextFacts(0, StyleClass("nested"), StyleKindText(), state), state) Check(nested.padding_x == 7.0 && nested.gap == 5.0, "a nested var() fallback reads the inner property, then its literal") Check(nested.foreground == CurrentText(), "--kryon-text reads the installed theme's text color") + card := StyleControlRoleFacts(StyleKindCard(), 0, StyleClass("soft"), StyleAny(), 0, 0, 0, normal_state) + Check(cast(s32)ResolveInstalledStyleRules(StyleData.{}, card, normal_state).elevation == cast(s32)ElevationRoleRaised, + "a tight box-shadow is a raised elevation") + card.class_name = StyleClass("lifted") + Check(cast(s32)ResolveInstalledStyleRules(StyleData.{}, card, normal_state).elevation == cast(s32)ElevationRoleFloating, + "a soft box-shadow is a floating elevation") + card.class_name = StyleClass("flat") + flat := ResolveInstalledStyleRules(StyleData.{}, card, normal_state) + Check(StyleHasField(flat, cast(u32)StyleElevation) && cast(s32)flat.elevation == cast(s32)ElevationRoleNone, + "box-shadow: none clears the elevation") + body := ResolveInstalledStyleRules(StyleData.{}, StyleTextFacts(0, StyleClass("body"), StyleKindText(), normal_state), normal_state) + Check(body.font_size == 15.0 && body.line_height == 1.5 && body.font_weight == 700 && + body.font_style == .FontStyleItalic && !StyleHasField(body, cast(u32)StyleTypeface), + "the font shorthand sets size, line height, weight and style; a generic family keeps the theme face") + mono := ResolveInstalledStyleRules(StyleData.{}, StyleTextFacts(0, StyleClass("mono"), StyleKindText(), normal_state), normal_state) + Check(cast(s32)mono.font_role == cast(s32)FontRoleCode, "monospace selects the code font role") nest := StyleClass("nest") normal := cast(s32)ButtonStateNormal disabled := cast(s32)ButtonStateDisabled diff --git a/tests/css_sheet_behavior.zi b/tests/css_sheet_behavior.zi index d352d3d..d642ccc 100644 --- a/tests/css_sheet_behavior.zi +++ b/tests/css_sheet_behavior.zi @@ -324,7 +324,7 @@ DiagnosticIssueCount :: 14 ObserveIssue :: (context: *void, issue: CssIssue) { diagnostic_expectations: [DiagnosticIssueCount]DiagnosticExpectation = .[ .{4, 3, CssIssueKind.Declaration, "gap", false}, - .{5, 3, CssIssueKind.Declaration, "box-shadow", true}, + .{5, 3, CssIssueKind.Declaration, "text-transform", true}, .{6, 3, CssIssueKind.Declaration, "colour", false}, .{7, 3, CssIssueKind.Declaration, "broken-name", false}, .{8, 3, CssIssueKind.Selector, "& > Text", false}, @@ -335,7 +335,7 @@ ObserveIssue :: (context: *void, issue: CssIssue) { .{16, 8, CssIssueKind.Declaration, "width", true}, .{16, 22, CssIssueKind.Declaration, "transition", true}, .{16, 48, CssIssueKind.Declaration, "height", true}, - .{17, 41, CssIssueKind.Declaration, "box-shadow", true}, + .{17, 41, CssIssueKind.Declaration, "text-transform", true}, .{18, 8, CssIssueKind.Declaration, "font-family", true}] count: *s32 = cast(*s32)context index: s32 = < CssImportAnswer { - if name == "nested.css" { return .{true, "Text { box-shadow: none; }"} } + if name == "nested.css" { return .{true, "Text { text-transform: none; }"} } return .{} } @@ -359,7 +359,7 @@ ObserveImportIssue :: (context: *void, issue: CssIssue) { count: *s32 = cast(*s32)context if <