This polyfill lets you use CSS ::part()
selectors in querySelector
and querySelectorAll
on ShadowRoot
.
The script is 442 bytes minified, 300 bytes gzipped.
Include this polyfill somewhere in your document.
<script src="https://gist.githack.com/jonathantneal/f88e2a0f162a712bebbc95be8f00cbc5/raw/c1db6fdcc9d3ee4c2e2a0d080a695142ba4a30c1/2.ShadowRootQSAPartPolyfill.js"></script>
That’s it! Start using ::part()
!
const host = document.createElement('div')
const root = host.attachShadow({ mode: 'open' })
root.innerHTML = '<p part="super fun">'
root.querySelector('::part(super)') // <p part="super fun">
root.querySelector(':host::part(fun)') // <p part="super fun">
root.querySelector('::part(super fun)') // <p part="super fun">