Skip to content

Commit

Permalink
Remove DOM hooks from document
Browse files Browse the repository at this point in the history
  • Loading branch information
barneycarroll committed Apr 18, 2024
1 parent 60e9e4a commit 6eddc2e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 21 deletions.
27 changes: 12 additions & 15 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const reorderArgs = (args: any[]) => {
// if attrs isn't the first arg
let i = 1;
for( let a of args.slice(1) as any[] ) {

if (Parser.nested.has(a)) {
args.splice(i, 1)
args.push(a)
Expand Down Expand Up @@ -100,33 +100,25 @@ export { pretty }
export default function Setup(m: Static, options?: Options) {
let styleEl: [HTMLStyleElement] | [] = []

const base = pretty(`
css-node {
display: none;
}
`)

if (!options?.server) {
styleEl = [document.createElement('style')]

for (let el of styleEl) {
el.type = 'text/css'
document.head.appendChild(el)

el.textContent += '\n' + base
el.textContent += '\n'
}
} else {
sheets.set('', base)
}

const h: Static =
const h: Static =

Object.assign((...args: Parameters<Static>) => {
// has to happen before the native mithril hyperscript
// because it does the key check there, and it will fail
// if attrs isn't the first arg
hyperscriptPlugin.before(args, options)

const vnode = m(...args)

hyperscriptPlugin.after(vnode, options)
Expand Down Expand Up @@ -161,11 +153,16 @@ export default function Setup(m: Static, options?: Options) {
// rest happens in hyperscript plugin
},
oncreate(vnode) {
const {parentNode} = vnode.dom;

// Leave DOM structure intact
(parentNode as HTMLElement).removeChild(vnode.dom)

if (options?.server) {
return
}

(vnode.dom as any).parentNode.classList.add(parsed.hash)
(parentNode as HTMLElement).classList.add(parsed.hash)

if (!sheets.has(parsed.hash)) {
for (let el of styleEl) {
Expand All @@ -177,7 +174,7 @@ export default function Setup(m: Static, options?: Options) {
if (isStream(v.value)) {
onremoves.push(
v.value.observe((latest) => {
;(vnode.dom.parentNode as HTMLElement).style.setProperty(
(parentNode as HTMLElement).style.setProperty(
v.varName(),
latest,
)
Expand Down
3 changes: 0 additions & 3 deletions test/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ test('browser', async () => {
assert.equal(
pretty(document.head.children[0].innerHTML),
pretty(`
css-node {
display: none;
}
.${className} {
color: var(--${className}-1);
@media(min-width: 1000px) {
Expand Down
3 changes: 0 additions & 3 deletions test/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ test('server', async () => {
assertStringEq(
pretty([...sheets.values()].join('')),
pretty(`
css-node {
display: none;
}
.css-3wjkw3 {
color: var(--css-3wjkw3-1);
@media(min-width: 1000px) {
Expand Down

0 comments on commit 6eddc2e

Please sign in to comment.