New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

open-styleable

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

open-styleable

A proof-of-concept implementation of open-styleable shadow-roots, using a combination of:

latest
Source
npmnpm
Version
0.2.0
Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

open-styleable

A proof-of-concept implementation of open-styleable shadow-roots, using a combination of:

  • A build-time HTML transform for declarative shadow DOM
  • A client-side script that overrides attachShadow

Demo

View the hosted examples:

  • basic (source)
  • nested shadow-roots (source)
  • layers (source)
  • scope (source)

Edit the code live:

Open in StackBlitz

Usage

(See standalone setup instructions below if you're trying to use this in your own project) 👀

The access for open styles is controlled at the shadow-root level. Every shadow-root needs to explicitly opt-in using one of the following ways.

For declarative shadow DOM (DSD):

  • Add the adopthoststyles attribute to the DSD template. This shadow-root will now automatically inherit all styles from the host context (i.e. the document or a parent shadow-root).

    <template shadowrootmode="open" adopthoststyles></template>
    
  • Alternatively, if you only want to adopt styles from the document and not from the parent shadow-root, then you can use adoptpagestyles.

    <template shadowrootmode="open" adoptpagestyles></template>
    

For client-rendered shadow-roots, use the adoptPageStyles and adoptHostStyles options when calling attachShadow.

  • Use adoptHostStyles to adopt all styles from the host context.
    this.attachShadow({
    	mode: "open",
    	adoptHostStyles: true,
    });
    
  • Use adoptPageStyles to adopt all styles from the page/document only.
    this.attachShadow({
    	mode: "open",
    	adoptPageStyles: true,
    });
    

[!IMPORTANT] There are some known limitations/caveats that you should be aware of.

Standalone setup

To use this "polyfill" in your own project:

  • Install open-styleable from npm (or use import maps).
    npm add open-styleable
    
  • Hook up the HTML transform into your templating system (see .eleventy.js for an example). This should ideally be executed after all bundling steps.
    import { transformHtml } from "open-styleable";
    
    // assuming `htmlContent` is the original page content
    htmlContent = transformHtml(htmlContent);
    
  • Include the /client script in your <head> before any custom elements are registered.
    <script>
    	import "open-styleable/client";
    </script>
    
    or from a CDN:
    <script src="https://esm.sh/open-styleable/client"></script>
    

[!NOTE] You do not always need to use both the build-time transform and the client-side script. They are completely independent and do different things.

Development

This demo is built with 11ty. All test pages go in the pages/ directory.

Open in GitHub Codespaces

Local setup

To run it locally, clone the repo and follow these steps:

  • Install dependencies.

    npm install
    
  • Start the dev server.

    npm run dev
    
  • Open up localhost:1174 in your browser.

FAQs

Package last updated on 01 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts