【发布时间】:2019-11-02 08:38:57
【问题描述】:
我已经使用 Create Guten Block (https://github.com/ahmadawais/create-guten-block) 创建了一个有效的 Gutenberg Block。 目前它仅适用于内联样式,但作为一项要求,我必须避免使用它们。
因此,我想在保存帖子时创建一个帖子/页面样式表,包括我的块的样式设置(例如背景颜色、颜色、字体大小...)
我的区块当前保存功能(block.js)
save: function( props ) {
const { attributes: { typetext, infotext, linktext, background_color, background_button_color, text_color, text_color_button }} = props;
return (
<div id="cgb-infoblock" className="cgb-infoblock">
<div className="cgb-infoblock-body" style={{
backgroundColor: background_color,
color: text_color,
}}>
<div className="cgb-infoblock-type">
<p>
<span className="cgb-infoblock-icon"><i>i</i></span>
{ typetext && !! typetext.length && (
<RichText.Content
tagName="span"
className={ classnames(
'cgb-infoblock-type-text'
) }
style={ {
color: text_color
} }
value={ typetext }
/>
)}
</p>
</div>
<div className="cgb-infoblock-text">
{ infotext && !! infotext.length && (
<RichText.Content
tagName="p"
style={ {
color: text_color
} }
value={ infotext }
/>
)}
</div>
</div>
<div className="cgb-infoblock-button" style={{
backgroundColor: background_button_color,
color: text_color_button,
}}>
{ linktext && !! linktext.length && (
<RichText.Content
tagName="p"
style={ {
color: text_color_button
} }
value={ linktext }
/>
)}
</div>
</div>
);
},
最好的解决方案是为整个页面/帖子生成某种样式表,其中包含来自所有块的所有设置。
如果样式表生成发生在页面保存时,最好的方法是,但如果它发生在页面加载时也可以。由于这些帖子不会很大,因此性能应该不是那么大的问题。
【问题讨论】:
-
你真的需要创建一个样式表,比如“创建一个filename.css”文件吗?或者如果组件具有某种样式就足够了,这取决于块的属性?
-
@niklas 是的,我想要一个 filename.css ;如果每次有人访问页面时都生成它,那将是可以的。 (我知道,对性能不好)
-
“避免内联样式”的要求从何而来。它可能仍然比加载新样式表更高效。
标签: javascript css wordpress reactjs create-guten-block