mayank99 / open-styleable

experimental server+client "polyfill" for openly styling shadow-roots
https://open-styleable.mynk.app/
MIT License
2 stars 1 forks source link

open-styleable

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

Demo

View the hosted examples:

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):

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

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

Standalone setup

To use this "polyfill" in your own project:

  1. Install open-styleable from npm (or use import maps).
    npm add open-styleable
  2. 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);
  3. 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: 1. Install dependencies. ``` npm install ``` 2. Start the dev server. ``` npm run dev ``` 3. Open up `localhost:1174` in your browser.