在 Shadow DOM 中进行 Preact 但丢失了所有样式
我正在构建一个小部件,因此希望在影子 DOM 中加载小部件 Preact 应用程序以避免 CSS 冲突。
我能够成功地做到这一点。
const host = document.querySelector('#widget-_hw');
const shadow = host?.attachShadow({ mode: 'open' });
const renderIn = document.createElement('div');
shadow?.appendChild(renderIn);
render(h(App, { ...config, element: el }), renderIn)
但失去了所有的风格。
我在一个名为 main.css 的文件中拥有 Widget 所需的所有样式。我在哪里定义了这样的所有样式
reset {
composes: scope;
background: var(--color-bg);
color: var(--color-text);
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
margin: 0;
}
.reset h1,
.reset h2,
.reset h3,
.reset h4,
.reset h5,
.reset h6 {
line-height: var(--line-height);
}
//and many other like button, input fields, containers etc
有人可以建议我一种将样式元素导入回 preact 应用程序的方法吗?
I'm building a widget and hence wanted to load the widget Preact app in the shadow DOM to avoid CSS conflicts.
I am succesfully able to do so like this.
const host = document.querySelector('#widget-_hw');
const shadow = host?.attachShadow({ mode: 'open' });
const renderIn = document.createElement('div');
shadow?.appendChild(renderIn);
render(h(App, { ...config, element: el }), renderIn)
But lost all styles.
I have all styles required for Widget inside a file called main.css. Where I have all styles defined like this
reset {
composes: scope;
background: var(--color-bg);
color: var(--color-text);
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
margin: 0;
}
.reset h1,
.reset h2,
.reset h3,
.reset h4,
.reset h5,
.reset h6 {
line-height: var(--line-height);
}
//and many other like button, input fields, containers etc
Could someone suggest me a way to import style elements back into the preact app?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ShadowDOM 的重点是不通过全局/外部 CSS 进行样式化
ShadowDOM 的样式化如下:
可继承样式
https://lamplightdev.com /blog/2019/03/26/why-is-my-web-component-inheriting-styles/
(级联)CSS 属性
shadowParts(和主题)
https://meowni.ca/posts/part-theme-explainer/< /p>
被反映,它们不是由 ShadowDOM 设计样式,而是由其容器设计。请参阅:::分槽内容
(2022 年 2 月)可构造样式表仍然是仅限 Chromium 的一方
https://caniuse.com/mdn-api_cssstylesheet_cssstylesheet
您的自定义指令
The whole point of shadowDOM is to NOT be styled by global/outside CSS
ShadowDOM is styled by:
<style>
within shadowDOMInheritable styles
https://lamplightdev.com/blog/2019/03/26/why-is-my-web-component-inheriting-styles/
(cascading) CSS properties
shadowParts (and Themes)
https://meowni.ca/posts/part-theme-explainer/
<slot>
are reflected, they are NOT styled by the shadowDOM, but by its container.See: ::slotted content
(feb 2022) Constructible StyleSheets is still a Chromium only party
https://caniuse.com/mdn-api_cssstylesheet_cssstylesheet
your custom directive