Skip to content

Commit

Permalink
Fix html preview with tailwind
Browse files Browse the repository at this point in the history
  • Loading branch information
2hu12 committed Oct 14, 2023
1 parent 461b0ef commit 3500755
Showing 1 changed file with 18 additions and 26 deletions.
44 changes: 18 additions & 26 deletions playground/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -432,38 +432,31 @@ const LiveSatori = withLive(function ({
(node: HTMLIFrameElement) => {
if (node) {
if (node.contentWindow?.document) {
/* Force tailwindcss to create stylesheets on first render */
const forceUpdate = () => {
return setTimeout(() => {
const div = doc.createElement('div')
div.classList.add('hidden')
doc.body.appendChild(div)
setTimeout(() => {
doc.body.removeChild(div)
}, 300)
}, 200)
}
const doc = node.contentWindow.document
const script = doc.createElement('script')
script.src = 'https://cdn.tailwindcss.com'
doc.head.appendChild(script)
script.addEventListener('load', () => {
const configScript = doc.createElement('script')
configScript.text = `
tailwind.config = {
plugins: [{
handler({ addBase }) {
addBase({
'html': {
'line-height': 1.2,
}
})
}
}]
;(node.contentWindow as any).tailwind.config = {
plugins: [
{
handler({ addBase }: { addBase: (style: any) => void }) {
addBase(
Object.create(null, {
html: {
value: {
'line-height': 1.2,
},
enumerable: true,
},
})
)
},
},
],
}
`
doc.head.appendChild(configScript)
})

const updateClass = () => {
Array.from(doc.querySelectorAll('[tw]')).forEach((v) => {
const tw = v.getAttribute('tw')
Expand All @@ -473,7 +466,6 @@ const LiveSatori = withLive(function ({
}
})
}
forceUpdate()
const observer = new MutationObserver(updateClass)
observer.observe(doc.body, { childList: true, subtree: true })
setIframeNode(doc.body)
Expand Down

0 comments on commit 3500755

Please sign in to comment.