[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 ...
随机推荐
- Oracle的查询-多行查询
多行函数[聚合函数],作用于多行,返回一个值 ) from emp;--查询总数量 select count(empno) from emp;--查询总数量 select count(*) from ...
- redis单机连接池
一.配置文件 1. db.properties配置文件#IP地址 redis.ip = 127.0.0.1 #端口号 redis.port= #最大连接数 redis.max.total= #最大空闲 ...
- axios拦截器的介绍
interceptors 拦截器 拦截器一般做什么? 1. 修改请求头的一些配置项 2. 给请求的过程添加一些请求的图标 3. 给请求添加参数 拦截器的基本语法: 拦截器分为全局拦截器和局部拦截器 全 ...
- WPF入门(2)——数据绑定与INotifyPropertyChanged(INPC)
接上篇,我们在MainViewModel类中创建个属性: public string Name { get; set; } 然后去UI的xaml文件中binding一下: 此时运行程序是不会得到我们想 ...
- getContextPath、getServletPath、getRequestURI、getRealPath、getRequestURL、getPathInfo();的区别
<% out.println("getContextPath: "+request.getContextPath()+"<br/>"); ou ...
- ReLU、LReLU、PReLU、CReLU、ELU、SELU
ReLU.LReLU.PReLU.CReLU.ELU.SELU 2018年01月22日 22:25:34 luxiaohai的学习专栏 阅读数 28218更多 分类专栏: 深度学习 版权声明:本文 ...
- .net core 根据数据库生成实体类
微软最近几年在跨平台上不断发力,很多.net程序员也摩拳擦掌,对微软寄以厚望.就在最近,微软还推出了asp .net core2.0预览版. 通过对.net core的简单尝试,我发现以往我们开发MV ...
- 设计模式风格<二>;消息总线
以前开发的动车模拟驾驶系统,有好几个软件(不在一台机器上),他们互相之间通信,因此每个软件要配置每个模块的IP和端口,就是每个模块都要知道别的模块的端口和IP. 这样有个重复的地方,B模块和C模块都要 ...
- 微信公众号支付备忘及填坑之路-java
一.背景 最近公司给第三方开发了一个公众号,其中最重要的功能是支付,由于是第一次开发,遇到的坑特别的多,截止我写博客时,支付已经完成,在这里我把遇到的坑记录一下(不涉及退款).不得不吐槽一下,腾讯这么 ...
- JavaScript Basics_Fundamentals Part 1_Variables
JavaScript Variables JavaScript 变量(Variables)是用于存储数据值的容器. 创建一个 JavaScript 变量,可以使用关键字 let. Example le ...