[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 ...
随机推荐
- flannel vxlan工作基本原理及常见排障方法
写在前面 最近用kubeadm鼓捣了几个cluster集群测试用,网络用的flannel.因为这些机器都不是纯净的环境(以前部署过其他的k8s或者有一些特别的设置),所以部署起来遇到了很多问题.看了下 ...
- Flask Bug记录之JinJa2.exceptions.UndefinedError: 'sqlite3.Row object' has no attribute 'get'
源码 py文件定义db的工厂函数如下 def get_db(): if "db" not in g: g.db = sqlite3.connect( current_app.con ...
- 剑指offer20:定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
1 题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). 2. 思路和方法 利用辅助栈来存储现有栈的最小值.在入栈和出栈的时候将现有栈和最小 ...
- ~ android与ios的区别
Oracle与Mysql的区别 项目类别 android ios 应用上 可以使用常用的android模拟器,来模拟各种android设备 只能直接使用iphone或ipad进行测试 开发语言 基于L ...
- golang数据类型
整数类型 Golang各整数类型分:有符号和无符号,int uint 的大小和系统有关. Golang查看一个变量的数据类型: package main import "fmt" ...
- gitlab安装指南(gitlab-ce-9.4.3-ce.0.el7.x86_64 centos7)
1,安装gitlab wget https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-9.4.3-ce.0.el7. ...
- WPF入门(1)——DataContext
在WPF中,应用程序有两层:UI层和Data层.这里新建一个项目说明哪些是UI层,哪些是数据层. UI层很明显,就是用户看到的界面.但是数据层并不是下图所示: 上图中是UI层view的后台代码.当然, ...
- 【kmp】似乎在梦中见过的样子
参考博客: BZOJ 3620: 似乎在梦中见过的样子 [KMP]似乎在梦中见过的样子 题目描述 「Madoka,不要相信QB!」伴随着Homura的失望地喊叫,Madoka与QB签订了契约. 这是M ...
- AlertManager 部署及使用
熟悉了 Grafana 的报警功能,但是 Grafana 的报警功能目前还比较弱,只支持 Graph 的图表的报警.今天来给大家介绍一个功能更加强大的报警工具:AlertManager. 简介 之前我 ...
- Python 异常处理与反射机制
Python 的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承.Py ...