首页 > 其他分享 >[Web Component] using `part` to allow applying styling from outside the shadow DOM

[Web Component] using `part` to allow applying styling from outside the shadow DOM

时间:2024-08-20 14:28:30浏览次数:7  
标签:Web product applying template DOM part querySelector const root

Let's say we have a web component: 

import { getProductById } from "../services/Menu.js";
import { addToCart } from "../services/Order.js";

export default class DetailsPage extends HTMLElement {
  constructor() {
    super();

    this.root = this.attachShadow({ mode: "open" });

    const template = document.getElementById("details-page-template");
    const content = template.content.cloneNode(true);
    const styles = document.createElement("style");
    this.root.appendChild(content);
    this.root.appendChild(styles);

    async function loadCSS() {
      const request = await fetch("/components/DetailsPage.css");
      styles.textContent = await request.text();
    }
    loadCSS();
  }

  async renderData() {
    if (this.dataset.productId) {
      this.product = await getProductById(this.dataset.productId);
      this.root.querySelector("h2").textContent = this.product.name;
      this.root.querySelector("img").src = `/data/images/${this.product.image}`;
      this.root.querySelector(".description").textContent =
        this.product.description;
      this.root.querySelector(
        ".price"
      ).textContent = `$ ${this.product.price.toFixed(2)} ea`;
      this.root.querySelector("button").addEventListener("click", () => {
        addToCart(this.product.id);
        app.router.go("/order");
      });
    } else {
      alert("Invalid Product ID");
    }
  }

  connectedCallback() {
    this.renderData();
  }
}

customElements.define("details-page", DetailsPage);

 

The template for the web component:

<template id="details-page-template">
      <header>
        <a href="#" onclick="app.router.go('/'); event.preventDefault()"
          >&lt; Back</a
        >
        <h2></h2>
        <a></a>
      </header>
      <img src="" />
      <p class="description"></p>
      <p class="price"></p>
      <button>Add to cart</button>
    </template>

 

Let's say we want to apply styling from outside to the imgof the Web component. We cannot do it directly due to web compoennt has shadow DOM.

 

But we can add part attribute to enable this change

    <template id="details-page-template">
      <header>
        <a href="#" onclick="app.router.go('/'); event.preventDefault()"
          >&lt; Back</a
        >
        <h2></h2>
        <a></a>
      </header>
      <!--add part to open shadow DOM to outside, so css can access it-->
      <img src="" part="image" />
      <p class="description"></p>
      <p class="price"></p>
      <button>Add to cart</button>
    </template>

 

Now we can target the img by using css:

details-page::part(image) {
  margin-left: -10%;
  width: 120%;
}

 

标签:Web,product,applying,template,DOM,part,querySelector,const,root
From: https://www.cnblogs.com/Answer1215/p/18369372

相关文章

  • 踩过的坑(一)——web容器升级
    背景:国产化web容器(宝兰德)升级。 踩坑过程:web容器升级后,web系统正常访问,看似正常。晚上批量跑批的时候,中断了。中断原因:发现某路径下的文件,的确存在,但是程序报NoSuchFileException。调查得知,现在该文件的权限是640(linux),想定应该是644。其他用户组的read权限缺失,导致文件不......
  • 第一次项目搭建笔记&路由导航守卫&web前后端会话跟踪
    1.重新搭建后端项目在IDEA中重新创建一个JavaEE项目,记得勾选Webprofile之后在java文件中重新搭建分级的文件夹按照标准创建com.xxxx.dorm文件夹并创建dao(数据处理),filter(过滤器),model(模型),util(工具),web(服务端)等文件夹进行不同功能部分的分类搭建完基本的框......
  • ctfshow-web入门-sql注入(web224-web230)文件类型注入、routines存储过程与函数状态、ha
    目录1、web2242、web2253、web2264、web2275、web2286、web2297、web2301、web224登录页面测了下没发现注入点存在robots.txt访问/pwdreset.php  ,是管理员密码重置的页面直接重置密码,这里以123456为例使用admin/123456登录 来到一个文件生成界......
  • WEB渗透免杀篇-免杀工具全集
     往期文章 WEB渗透免杀篇-加载器免杀-CSDN博客 WEB渗透免杀篇-分块免杀-CSDN博客WEB渗透免杀篇-Powershell免杀-CSDN博客WEB渗透免杀篇-Python源码免杀-CSDN博客WEB渗透免杀篇-C#源码免杀-CSDN博客WEB渗透免杀篇-MSF+shellcode免杀-CSDN博客WEB渗透免杀篇-Bypass-AMS......
  • Vulnhub靶机:AI-WEB-1.0
    一.网站查询二.扫描当前网站目录 扫出有http://172.16.1.88/robots.txt继续扫这个刚扫出来的,然后得出 我们再访问m3dinfo/info.php和 /se3reTdir777/这俩目录 得到如上图所示三.进行sql注入1.判断是否有sql注入 /有结果,有报错 2.利用burp抓包进行注入......
  • [ABC367D] Pedometer-xzy巨佬简洁做法
    [ABC367D]Pedometer-xzy巨佬简洁做法https://www.luogu.com/article/n64n78cs对照巨佬的代码进一步理解//徐知鱼#include<bits/stdc++.h>usingnamespacestd;inlineintread(){ intx=0,f=1; charch=getchar(); while(!isdigit(ch)){ if(ch=='-')f=......
  • Java轻松实现跨平台(Windows、Linux)多协议(Twain、Sane)的Web扫描
     由于项目需要,开发在Windows下与Linux下扫描功能,Linux主要是信创的两个系统(UOS、麒麟),研究了一下发现,Windows使用Twain协议与扫描仪通讯,Linux使用的是Sane协议与扫描仪通讯,找到Twain协议和Sane协议的标准文档,英文的,都有大几百页,项目一个月内要求上线,明显没时间慢慢研究,于......
  • WebGIS产品分析
    WebGIS(网络地理信息系统)是指基于网络平台,客户端应用软件采用网络协议,运行在网络上的地理信息系统,即将GIS所能提供的功能通过网络展现给用户。顾名思义,WebGIS就是展现在网络上的GIS,是GIS与Web融合的产物。GIS通过Web功能得以扩展,使得GIS冲破专业圈子,真正成为大众化的GIS。如今,网......
  • 界面控件DevExpress ASP.NET Web Forms v24.1最新版本系统环境配置要求
    本文档包含有关安装和使用 DevExpressASP.NETWebForms控件的系统要求的信息。点击获取DevExpressv24.1正式版.NETFrameworkDevExpressASP.NETWebForms控件支持以下.NET框架版本。如果您需要DevExpress产品的早期版本,请咨询“在线客服”获取。IDEDevExpressASP.......
  • 秒开WebView?Android性能优化全攻略
    在如今的移动应用时代,用户体验的好坏直接关系到应用的成功与否。而在众多的用户体验因素中,应用的加载速度尤其重要。特别是对于使用 WebView 加载网页的应用,如果加载速度过慢,用户往往会产生不满,从而流失。因此,实现“秒开”WebView成为了开发者必须面对的一项挑战。本文将深......