From 146f9d628b5acfd8df81dc59fc43868b18fbb4d5 Mon Sep 17 00:00:00 2001 From: Nathan Walker Date: Fri, 14 Mar 2025 13:50:15 -0700 Subject: [PATCH 1/5] feat(css): color-mix --- apps/automated/src/ui/styling/style-tests.ts | 35 +++++ apps/toolbox/src/app.css | 19 +++ apps/toolbox/src/main-page.xml | 2 +- package-lock.json | 145 ++++++++++++++---- package.json | 7 +- packages/core/package.json | 5 +- packages/core/ui/core/properties/index.ts | 141 +++++++++++++++-- .../core/ui/styling/css-color-mix.spec.ts | 18 +++ packages/core/ui/styling/style-scope.ts | 12 +- 9 files changed, 341 insertions(+), 43 deletions(-) create mode 100644 packages/core/ui/styling/css-color-mix.spec.ts diff --git a/apps/automated/src/ui/styling/style-tests.ts b/apps/automated/src/ui/styling/style-tests.ts index 0cc5a7c2aa..032639bc96 100644 --- a/apps/automated/src/ui/styling/style-tests.ts +++ b/apps/automated/src/ui/styling/style-tests.ts @@ -2,6 +2,7 @@ import * as TKUnit from '../../tk-unit'; import { Application, Button, Label, Page, StackLayout, WrapLayout, TabView, TabViewItem, View, Utils, Color, resolveFileNameFromUrl, removeTaggedAdditionalCSS, addTaggedAdditionalCSS, unsetValue, knownFolders, Screen } from '@nativescript/core'; import * as helper from '../../ui-helper'; import { _evaluateCssCalcExpression } from '@nativescript/core/ui/core/properties'; +import { _evaluateCssColorMixExpression } from 'packages/core/ui/core/properties'; export function test_css_dataURI_is_applied_to_backgroundImageSource() { const stack = new StackLayout(); @@ -1826,6 +1827,40 @@ export function test_nested_css_calc() { TKUnit.assertDeepEqual(stack.width, { unit: '%', value: 0.5 }, 'Stack - width === 50%'); } +export function test_evaluateCssColorMixExpression() { + TKUnit.assertEqual(_evaluateCssColorMixExpression('color-mix(in oklab, var(--color-black) 50%, transparent)'), '2px', 'Single percentage'); + TKUnit.assertEqual(_evaluateCssColorMixExpression('color-mix(in lab, plum 60%, #f00 50%)'), '60px', 'Double percentage'); +} + +export function test_nested_css_color_mix() { + const page = helper.getClearCurrentPage(); + + const stack = new StackLayout(); + stack.css = ` + StackLayout.gray { + background-color: color-mix(in oklab, var(--color-black) 50%, transparent); + } + + StackLayout.coral { + background-color: color-mix(in hsl, hsl(200 50 80), coral 80%); + } + `; + + const label = new Label(); + page.content = stack; + stack.addChild(label); + + stack.className = 'gray'; + TKUnit.assertEqual(stack.backgroundColor, 'rgba(0, 0, 0, 0.5)', 'Stack - backgroundColor === rgba(0, 0, 0, 0.5)'); + + stack.className = 'coral'; + TKUnit.assertEqual(stack.backgroundColor, 'rgba(0, 0, 0, 0.5)', 'Stack - backgroundColor === rgba(0, 0, 0, 0.5)'); + + (stack as any).style = `background-color: calc(100% * calc(1 / 2))`; + + TKUnit.assertDeepEqual(stack.backgroundColor, 'rgba(0, 0, 0, 0.5)', 'Stack - backgroundColor === rgba(0, 0, 0, 0.5)'); +} + export function test_css_variables() { const blackColor = '#000000'; const redColor = '#FF0000'; diff --git a/apps/toolbox/src/app.css b/apps/toolbox/src/app.css index cc9cdcc3f2..de93b655ca 100644 --- a/apps/toolbox/src/app.css +++ b/apps/toolbox/src/app.css @@ -8,6 +8,25 @@ components that have the btn class name. Button { text-transform: none; } +* { + --x: red; + --color-black: black; +} + +.calc-padding { + padding: calc(4 * 6) + +} + +.colormix { + background-color: color-mix(in oklab, var(--color-black) 50%, transparent); +} + +/* TODO: test these */ +.x {color: var(--x) } +.x.y { --x: red } +.x.z { --x: yellow } + .btn-view-demo { /* background-color: #65ADF1; */ border-radius: 8; diff --git a/apps/toolbox/src/main-page.xml b/apps/toolbox/src/main-page.xml index d166017292..a69a7f4c49 100644 --- a/apps/toolbox/src/main-page.xml +++ b/apps/toolbox/src/main-page.xml @@ -5,7 +5,7 @@ - +