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的更多相关文章

  1. 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 ...

  2. Unable to connect to web server 'IIS Express'(无法连接到Web服务器“IIS Express”)的解决方式-Jexus Manager

    在运行微软示例工程eShopOnWeb时候, 在经过一段时间再运行启动报Error "Unable to connect to web server 'IIS Express'"  ...

  3. 使用shadow dom封装web组件

    什么是shadow dom? 首先我们先来看看它长什么样子.在HTML5中,我们只用写如下简单的两行代码,就可以通过 <video> 标签来创建一个浏览器自带的视频播放器控件. <v ...

  4. 【Web技术】401- 在 React 中使用 Shadow DOM

    本文作者:houfeng 1. Shadow DOM 是什么 Shadow DOM 是什么?我们先来打开 Chrome 的 DevTool,并在 'Settings -> Preferences ...

  5. 【Web技术】400- 浅谈Shadow DOM

    编者按:本文作者:刘观宇,360 奇舞团高级前端工程师.技术经理,W3C CSS 工作组成员. 为什么会有Shadow DOM 你在实际的开发中很可能遇到过这样的需求:实现一个可以拖拽的滑块,以实现范 ...

  6. 在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用

    由于ASP.NET Web API具有与ASP.NET MVC类似的编程方式,再加上目前市面上专门介绍ASP.NET Web API 的书籍少之又少(我们看到的相关内容往往是某本介绍ASP.NET M ...

  7. HTML5权威指南--Web Storage,本地数据库,本地缓存API,Web Sockets API,Geolocation API(简要学习笔记二)

    1.Web Storage HTML5除了Canvas元素之外,还有一个非常重要的功能那就是客户端本地保存数据的Web Storage功能. 以前都是用cookies保存用户名等简单信息.   但是c ...

  8. 【shadow dom入UI】web components思想如何应用于实际项目

    回顾 经过昨天的优化处理([前端优化之拆分CSS]前端三剑客的分分合合),我们在UI一块做了几个关键动作: ① CSS入UI ② CSS作为组件的一个节点而存在,并且会被“格式化”,即选择器带id前缀 ...

  9. web前端基础知识-(六)web框架

    一.web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. #!/usr/bin/env python #coding:ut ...

随机推荐

  1. redis主从同步错误处理

    背景: 之前resis只有一台服务器(redis  master),现需要为其增加一台slave服务器(slave),做成一主一从 要求,不影响 master的正常情况下完成 . 1.配置slave: ...

  2. HTTP报文学习

    HTTP报文用于HTTP协议的信息交互,分为请求报文和响应报文.报文由首部和主体两部分组成,中间使用空行(CR+LF)分隔 1. 报文结构 报文由首部.空行和实体组成: 报文中首先是请求行或者状态行, ...

  3. 用JavaScript写一个简单的倒计时,可以应用在发送短信验证码的“59秒后重新发送验证短信”

    倒计时——从10倒数到0,点击按钮会还原倒计时 <body> <!-- 将textvalue值设为10,从10倒数 --> <input type="text& ...

  4. dede按照权重排序dede:list和得的:arclist

    dede:list 的方法 1.找到"根目录\include\arc.listview.class.php"文件. 2.修改代码:在文件第727行处添加按weight排序判断代码( ...

  5. 怎样初始化XMLHttpRequest实例对象xhr

    xhr.open() 接收5个参数, 用于初始化一个http请求, 它接收5个参数: 1. method: 请求类型; 2. url: 请求的url; 3. async: 是否为异步, 默认为true ...

  6. 怎样安装并编译TypeScript?

    1. 使用: npm -v 查看是否安装了 npm ,  如果没有安装, 请前往 Nodejs 官网 下载安装, 下图表示已经安装 npm , 版本为: 6.9.0 . PS C:\Users\Adm ...

  7. 浅谈Promise原理与应用

    在JavaScript中,所有代码都是单线程.由于该“缺陷”,JavaScript在处理网络操作.事件操作时都是需要进行异步执行的.AJAX就是一个典型的异步操作 对于异步操作,有传统的利用回调函数和 ...

  8. php 限制标题长度,将一个中文转换成一个字符

    点击链接加入群[php/web 学习课堂]:https://jq.qq.com/?_wv=1027&k=5UJ9vEa 欢迎大家加入,一起讨论学习 玩这个功能的时候,我们要注意一点,我们是用中 ...

  9. mysql authentication plugin 'caching_sha2_password'

    解决办法: ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root1';

  10. go语言入门(8)异常处理

    1,error接口 Go语言引入了一个关于错误处理的标准模式,即error接口,它是Go语言内建的接口类型,该接口的定义如下: type error interface { Error() strin ...