Skip to content

Commit

Permalink
fix: use Shadow DOM to avoid CSS conflicts (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
motea927 authored Oct 15, 2024
1 parent dc9a4fe commit b0de5c6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
1 change: 1 addition & 0 deletions playground/vite/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
style="text-decoration: none; margin-top: 10px; display: block"
>You can use right panel to control your layout image.</a
>
<button>style test</button>
<script type="module" src="./main.ts"></script>
</body>
</html>
1 change: 0 additions & 1 deletion popup/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,3 @@ const handleClickOperation = (offset: number) => {
}
}
</script>
./utils
17 changes: 15 additions & 2 deletions popup/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,34 @@ import type { Component } from 'vue'

import App from './App.vue'

import './index.css'
import styles from './index.css?inline'

function createDevToolsContainer(App: Component) {
// Plugin root div
const CONTAINER_ID = '__vue-overlay-layout__'
const el = document.createElement('div')
el.setAttribute('id', CONTAINER_ID)
el.setAttribute('data-v-inspector-ignore', 'true')

// Use Shadow DOM to avoid CSS conflicts
const shadowRoot = el.attachShadow({ mode: 'open' })
const styleEl = document.createElement('style')
styleEl.textContent = styles
shadowRoot.appendChild(styleEl)

const contentEl = document.createElement('div')
contentEl.setAttribute('id', 'shadow-root-content')
shadowRoot.appendChild(contentEl)

document.getElementsByTagName('body')[0].appendChild(el)

const app = createApp({
render: () => h(App),
devtools: {
hide: true
}
})
app.mount(el)
app.mount(contentEl)
}

createDevToolsContainer(App)
8 changes: 0 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ export const unpluginFactory: UnpluginFactory<Options | undefined> = (
type: 'module',
src: '/__OVERLAY_LAYOUT__/overlay-layout.js'
}
},
{
tag: 'link',
injectTo: 'head',
attrs: {
rel: 'stylesheet',
href: '/__OVERLAY_LAYOUT__/overlay-layout.css'
}
}
]
}
Expand Down
4 changes: 0 additions & 4 deletions src/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,5 @@ export default defineNuxtModule<ModuleOptions>({
}
]
)

nuxt.options.app.head.link.push(
...[{ rel: 'stylesheet', href: '/__OVERLAY_LAYOUT__/overlay-layout.css' }]
)
}
})

0 comments on commit b0de5c6

Please sign in to comment.