From db251a3cb4c67c4d0a5b4b6408e186d072861017 Mon Sep 17 00:00:00 2001 From: sunhao Date: Mon, 5 Aug 2024 10:53:15 +0800 Subject: [PATCH] * [misc] add style::var helper to set css var. --- lib/zin/core/style.class.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/zin/core/style.class.php b/lib/zin/core/style.class.php index 6f4b432e92..5253a60c01 100644 --- a/lib/zin/core/style.class.php +++ b/lib/zin/core/style.class.php @@ -27,7 +27,7 @@ class style extends \zin\utils\dataset implements iDirective public function __call(string $name, array $args): style { $value = empty($args) ? true : (count($args) === 1 ? $args[0] : $args); - return $this->setVal($name, $value);; + return $this->setVal($name, $value); } /** @@ -68,6 +68,16 @@ class style extends \zin\utils\dataset implements iDirective if($class) $node->setProp('class', $class); } + public function var(string|array $nameOrMap, mixed $value = null): style + { + if(is_array($nameOrMap)) + { + foreach($nameOrMap as $name => $val) $this->setVal('--' . $name, $val); + return $this; + } + return $this->setVal('--' . $nameOrMap, $value); + } + /** * Magic static method for style property value. * @@ -108,6 +118,10 @@ class style extends \zin\utils\dataset implements iDirective { $value .= 'px'; } + elseif(is_string($value) && str_starts_with($value, '--')) + { + $value = 'var(' . $value . ')'; + } return is_string($value) ? $value : (string)$value; }