[Web Component] Allow External Styling of a Web Component's Shadow DOM
The Shadow DOM protects your components from style conflicts. The same protection also makes it hard for users to modify the inner style for their own needs. In this lesson we go over 3 ways to define API for a controlled manipulation of encapsulated styles.
<style>
custom-element {
--text-color: purple; // Define a variable for the color
}
div.text {
color: red !important;
text-decoration: underline;
font-size: 36px;
}
</style> <template>
<style>
.text {
color: var(--text-color, blue); // set 'blue' as default value
text-decoration: overline;
font-size: 28px;
}
</style>
<div class="text">
In the Shadow
</div>
</template> <script>
const template = document.querySelector('template');
class CustomElement extends HTMLElement {
constructor() {
super();
this.attachShadow({mode: "open"});
this.shadowRoot.appendChild(template.content.cloneNode(true));
} // allow to use javascript to change the style
changeStyles(styles) {
const textElement = this.shadowRoot.querySelector('.text');
Object.keys(styles).forEach(styleKey => {
textElement.style[styleKey] = styles[styleKey];
})
} // listen for the attributes changes, here only listening 'color', 'text-decoration'
static get observedAttributes() {
return ['color', 'text-decoration'];
} // Once the attribute changes, apply the style
attributeChangedCallback(attribute, oldValue, newValue) {
this.changeStyles({[attribute]: newValue});
}
}
window.customElements.define('custom-element', CustomElement);
</script> <custom-element></custom-element>
<div class="text">Outside the shadow</div>
[Web Component] Allow External Styling of a Web Component's Shadow DOM的更多相关文章
- ASP.NET Web API 2 external logins with Facebook and Google in AngularJS app
转载:http://bitoftech.net/2014/08/11/asp-net-web-api-2-external-logins-social-logins-facebook-google-a ...
- Unable to connect to web server 'IIS Express'(无法连接到Web服务器“IIS Express”)的解决方式-Jexus Manager
在运行微软示例工程eShopOnWeb时候, 在经过一段时间再运行启动报Error "Unable to connect to web server 'IIS Express'" ...
- 使用shadow dom封装web组件
什么是shadow dom? 首先我们先来看看它长什么样子.在HTML5中,我们只用写如下简单的两行代码,就可以通过 <video> 标签来创建一个浏览器自带的视频播放器控件. <v ...
- 【Web技术】401- 在 React 中使用 Shadow DOM
本文作者:houfeng 1. Shadow DOM 是什么 Shadow DOM 是什么?我们先来打开 Chrome 的 DevTool,并在 'Settings -> Preferences ...
- 【Web技术】400- 浅谈Shadow DOM
编者按:本文作者:刘观宇,360 奇舞团高级前端工程师.技术经理,W3C CSS 工作组成员. 为什么会有Shadow DOM 你在实际的开发中很可能遇到过这样的需求:实现一个可以拖拽的滑块,以实现范 ...
- 在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用
由于ASP.NET Web API具有与ASP.NET MVC类似的编程方式,再加上目前市面上专门介绍ASP.NET Web API 的书籍少之又少(我们看到的相关内容往往是某本介绍ASP.NET M ...
- HTML5权威指南--Web Storage,本地数据库,本地缓存API,Web Sockets API,Geolocation API(简要学习笔记二)
1.Web Storage HTML5除了Canvas元素之外,还有一个非常重要的功能那就是客户端本地保存数据的Web Storage功能. 以前都是用cookies保存用户名等简单信息. 但是c ...
- 【shadow dom入UI】web components思想如何应用于实际项目
回顾 经过昨天的优化处理([前端优化之拆分CSS]前端三剑客的分分合合),我们在UI一块做了几个关键动作: ① CSS入UI ② CSS作为组件的一个节点而存在,并且会被“格式化”,即选择器带id前缀 ...
- web前端基础知识-(六)web框架
一.web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. #!/usr/bin/env python #coding:ut ...
随机推荐
- Codeforces 718A Efim and Strange Grade 程序分析
Codeforces 718A Efim and Strange Grade 程序分析 jerry的程序 using namespace std; typedef long long ll; stri ...
- Scrapy里Selectors 四种基础的方法
在Scrapy里面,Selectors 有四种基础的方法xpath():返回一系列的selectors,每一个select表示一个xpath参数表达式选择的节点css():返回一系列的selector ...
- Redis学习存档(1)——安装
以虚拟机中的Linux系统(CentOS 6.5)中安装Redis为例 一.下载Redis 使用wget进行下载,可能部分系统不带wget命令,则yum下载即可 yum -y install wget ...
- shell习题第17题:检测磁盘
[题目要求] 写一个shell脚本,检测所有磁盘分区使用率和inode使用率并记录到以当天日期命名的日志文件里,当发现某个分区容量或者inode使用量大于85%时候,发邮件提醒 [核心要点] df d ...
- volatile 关键字(修饰变量)
目录 volatile 关键字(修饰变量) 1. 含义 2. 作用 3. 如何保证可见性 4. 如何禁止指令重排序优化 5. volatile 是不安全的 6. volatile 不适用场景 vola ...
- intellij IDE 破解 简单 License server 法
http://idea.iteblog.com/key.php
- 垃圾分类,javascript和python
首先,实现的步骤,首先在微信applet中设计一个简单的界面,开始映射到python服务器.有关具体界面,请参阅微信小程序设计指南.以下主要讨论后台服务器交互和处理点. 1.使用js将图像上传到pyt ...
- TVM图优化(以Op Fusion为例)
首先给出一个TVM 相关的介绍,这个是Tianqi Chen演讲在OSDI18上用的PPThttps://files.cnblogs.com/files/jourluohua/Tianqi-Chen- ...
- android适配知识总结
一.http适配 背景:API升级到28以后,不再支持明文的网络请求,只支持https请求.运行所报错误:java.net.UnknownServiceException: CLEARTEXT com ...
- CI/CD持续集成小结
一.概念 什么是devops,基于Gitlab从零开始搭建自己的持续集成流水线(Pipeline) https://blog.csdn.net/chengzi_comm/article/details ...