我正在构建一个小部件,因此希望在影子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应用程序中的方法吗?
发布于 2022-03-01 17:45:03
shadowDOM的全部目的是不采用全局/外部CSS的样式。
ShadowDOM的样式如下:
<style> https://lamplightdev.com/blog/2019/03/26/why-is-my-web-component-inheriting-styles/
https://meowni.ca/posts/part-theme-explainer/
<slot>是反射的,它们不是由shadowDOM设计的,而是由它的容器设计的。https://caniuse.com/mdn-api_cssstylesheet_cssstylesheet
https://stackoverflow.com/questions/71306680
复制相似问题