圣牛...
因此,经过大量阅读和测试,我想出了如何做我想做的事情。
这个想法是将我的 SCSS 全局变量与一些 CSS 变量挂钩,并能够通过更新 CSS 变量值在整个项目中级联更改。
环境:
1. Sharepoint Online -- 无 Javascript 框架
2. Visual Studio Code -- 最新
3. Typescript -- 最新
4. Gulp -- 3.9.1
目标:
允许用户通过属性窗格更改各种 CSS 属性。更改应在整个项目中级联。
我需要能够做到这一点不搞乱 Webpack 或 gulpfile.js。
碎片:
映射值的自定义函数:
_Utility.scss
// map-value
// @access public
// @param {$key} -- Item to Map
@function map-value($key)
{
@if type-of($key) == "string"
{
@return to-string($key);
}
@else
{
@return val($key);
}
}
@function to-string($key)
{
@return inspect( val($key) );
}
@function val($key)
{
// Return value:
@return var($key);
}
-- EOF
_Variables.scss
我把这些放在文件的顶部。
$WebPartWidth: 900px;
$WebPartMinimumWidth: 800px;
$WebPartMaximumWidth: 1000px;
$WebPartHeight: 500px;
$WebPartMinimumHeight: 400px;
$WebPartMaximumHeight: 600px;
接下来我创建我的 CSS 变量并将它们链接到 SCSS 变量。
:root
{
--WebPartWidth: #{$WebPartWidth};
--WebPartMinimumWidth: #{$WebPartMinimumWidth};
--WebPartMaximumWidth: #{$WebPartMaximumWidth};
--WebPartHeight: #{$WebPartHeight};
--WebPartMinimumHeight: #{$WebPartMinimumHeight};
--WebPartMaximumHeight: #{$WebPartMaximumHeight};
}
-- EOF
现在,我创建我的全局文件。
Global.module.scss
@import './Variables';
:export
{
WebPartWidth: #{$WebPartWidth};
WebPartMinimumWidth: #{$WebPartMinimumWidth};
WebPartMaximumWidth: #{$WebPartMaximumWidth};
WebPartHeight: #{$WebPartHeight};
WebPartMinimumHeight: #{$WebPartMinimumHeight};
WebPartMaximumHeight: #{$WebPartMaximumHeight};
}
-- EOF
最后,我将它们整合到我的主 CSS 文件中。
Webpart.module.scss
// Import our functions:
@import "./Utility";
// Import our Global Variables:
@import "./Global.module.scss";
.Webpart {
width: map-value(--WebPartWidth);
min-width: map-value(--WebPartMinimumWidth);
max-width: map-value(--WebPartMaximumWidth);
height: map-value(--WebPartHeight);
min-height: map-value(--WebPartMinimumHeight);
max-height: map-value(--WebPartMaximumHeight);
}
-- EOF
在我的打字稿文件中
Webpart.ts
import * as styles from './WebPart.module.scss';
现在,我可以像这样调整值:
document.body.style.setProperty('--WebPartWidth', newValue + "px");
现在,任何引用 --WebPartWidth CSS 变量的内容都会更新并应用新值。
我真的希望这对某人有所帮助。我知道我花了很长时间试图弄清楚这一点。编码愉快!