Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Next] shadow root style injection #1850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
Loading
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add the actual support for injecting styles into a shadowRoot
  • Loading branch information
tcnijmeijer committed Jul 7, 2021
commit 9deb088fdf9ea90d4439792609c4736b0f87bdaf
11 changes: 7 additions & 4 deletions 11 src/styleInjection.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
interface ComponentOptions {
beforeMount?(): void
__cssBlocks: Record<string, CSSBlock>
shadowRoot?: HTMLElement
}

interface ComponentInstance {
$options: ComponentOptions
$root: ComponentInstance
}

interface CSSBlock {
id: string
}

function getStyleElement(id: string) {
var existing = document.querySelector("[data-style-id='" + id + "']")
function getStyleElement(id: string, parent: HTMLElement) {
var existing = parent.querySelector("[data-style-id='" + id + "']")
if (existing) return existing

var styleElement = document.createElement('style')
styleElement.setAttribute('data-style-id', id)
styleElement.setAttribute('type', 'text/css')
document.head.appendChild(styleElement)
parent.appendChild(styleElement)
return styleElement
}

function injectStyles(component: ComponentInstance) {
const parent = component.$root.$options.shadowRoot || document.head
Object.values(component.$options.__cssBlocks).forEach(function (cssBlock) {
var styleElement = getStyleElement(cssBlock.id)
var styleElement = getStyleElement(cssBlock.id, parent)
styleElement.innerHTML = cssBlock.toString()
})
}
Expand Down
15 changes: 15 additions & 0 deletions 15 test/fixtures/shadow-root-injection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createApp } from 'vue'

import Component from './basic.vue'

if (typeof window !== 'undefined') {
const container = window.document.getElementById('#app')
const shadowRoot = container.attachShadow({ mode: 'open' })

Component.shadowRoot = shadowRoot

const app = createApp(Component)
window.instance = app.mount(shadowRoot)
}

export default Component
14 changes: 14 additions & 0 deletions 14 test/style.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,18 @@ test('CSS Modules Extend', async () => {
expect(style).toContain(`.${instance.$style.red} {\n color: #FF0000;\n}`)
})

test('shadow root injection', async () => {
const { window, instance } = await mockBundleAndRun({
entry: './test/fixtures/shadow-root-injection.js',
})

const headStyles = window.document.head.querySelectorAll('style')
expect(headStyles.length).toBe(0)

const shadowStyles = instance.$options.shadowRoot.querySelectorAll('style')
expect(shadowStyles.length).toBe(1)

expect(shadowStyles[0].innerHTML).toContain('comp-a h2 {\n color: #f00;\n}')
})

test.todo('experimental <style vars>')
Morty Proxy This is a proxified and sanitized view of the page, visit original site.