Skip to content
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

fix: html preview with tailwind on playground #561

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
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
Loading