Skip to content

Commit

Permalink
Changed order in which setup will be called
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianFun123 committed Mar 10, 2024
1 parent 2da298c commit e2f2192
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 21 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# JDOM `3.1.9`
# JDOM `3.1.10`
## A wrapper for query selector and html elements + templating & reactivity framework

- [Installation or embedding](#install)
Expand Down Expand Up @@ -28,12 +28,12 @@ npm install jdomjs

### Module
```js
import { $, $n, $c, $r, $h, JDOM } from 'https://cdn.jsdelivr.net/npm/[email protected].9'
import { $, $n, $c, $r, $h, JDOM } from 'https://cdn.jsdelivr.net/npm/[email protected].10'
```

### HTML import
```js
<script src="https://cdn.jsdelivr.net/npm/[email protected].9/dist/jdom.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected].10/dist/jdom.js"></script>
```

## DOM Manipulation
Expand Down
2 changes: 1 addition & 1 deletion dist/jdom.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion examples/js-components/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { JDOMComponent, $, html, css, computed } from '../../index.js';
import Hook from "../../src/Hook.js";

class ToDoApp extends JDOMComponent {
class ToDoApp extends JDOMComponent.unshadowed {
tasks = new Hook([]);
newTaskText = new Hook('');

Expand Down Expand Up @@ -64,5 +64,7 @@ class ToDoApp extends JDOMComponent {
}
}

console.log(ToDoApp.unshadowed)

// Register custom element
window.customElements.define('todo-app', ToDoApp);
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jdomjs",
"version": "3.1.9",
"version": "3.1.10",
"description": "A wrapper for query selector and html elements",
"main": "./index.js",
"types": "./types/index.d.ts",
Expand Down
9 changes: 9 additions & 0 deletions src/JDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,13 @@ export default class JDOMComponent extends HTMLElement {
styles() {
return undefined
}

/**
* @type {typeof JDOMComponent}
*/
static unshadowed = class JDOMUnshadowedComponent extends JDOMComponent {
constructor(options = {}) {
super({ shadowed: false, ...options })
}
}
}
42 changes: 28 additions & 14 deletions src/template/TemplateDOMAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ export default class TemplateDOMAdapter {
Object.entries(value).forEach(([key, value]) => {
if (value instanceof Hook) {
const hook = value
const listener = v => el.style[key] = v
el.addEventListener(':detached', () => hook.removeListener(listener))
el.addEventListener(':attached', () => hook.addListener(listener))
const listener = v => elem.style[key] = v
elem.addEventListener(':detached', () => hook.removeListener(listener))
elem.addEventListener(':attached', () => {
hook.addListener(listener)
listener(hook.value)
})

value = hook.value
}
Expand All @@ -111,15 +114,21 @@ export default class TemplateDOMAdapter {
Object.entries(value).forEach(([key, value]) => {
if (value instanceof Hook) {
const hook = value

const listener = v => {
if (v && !el.classList.contains(key)) {
el.classList.add(key)
} else if (!v && el.classList.contains(key)) {
el.classList.remove(key)
if (v && !elem.classList.contains(key)) {
elem.classList.add(key)
} else if (!v && elem.classList.contains(key)) {
elem.classList.remove(key)
}
}
el.addEventListener(':detached', () => hook.removeListener(listener))
el.addEventListener(':attached', () => hook.addListener(listener))
elem.addEventListener(':detached', () => {
hook.removeListener(listener)
})
elem.addEventListener(':attached', () => {
hook.addListener(listener)
listener(hook.value)
})
value = hook.value
}
if (value) {
Expand Down Expand Up @@ -311,12 +320,12 @@ export default class TemplateDOMAdapter {
lastValue = value

if (value) {
toRepl = this.replaceElement(toRepl, savedElement)
el = savedElement
if (!addedChildren) {
setup(el)
setup(savedElement)
addedChildren = true
}
toRepl = this.replaceElement(toRepl, savedElement)
el = savedElement
} else {
toRepl = this.replaceElement(toRepl, commentElement)
el = commentElement
Expand Down Expand Up @@ -504,14 +513,19 @@ export default class TemplateDOMAdapter {

replElements.forEach(e => this.removeElement(e))

firstEl.dispatchEvent(new CustomEvent(':detached'))
firstEndEl.dispatchEvent(new CustomEvent(':child_attached'))

let lastEl = firstEndEl
firstEndEl.dispatchEvent(new CustomEvent(':attach'))

endElements.forEach(e => {
lastEl = this.afterElement(lastEl, e)
})

firstEndEl.dispatchEvent(new CustomEvent(':attached'))

firstEl.dispatchEvent(new CustomEvent(':replaced_with', { detail: { to: finalEndElements } }))
firstEl.dispatchEvent(new CustomEvent(':detached'))
firstEndEl.dispatchEvent(new CustomEvent(':child_attached'))

return finalEnd
}
Expand Down
4 changes: 4 additions & 0 deletions types/src/JDOMComponent.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
* @property {String|null|undefined} name
*/
export default class JDOMComponent extends HTMLElement {
/**
* @type {typeof JDOMComponent}
*/
static unshadowed: typeof JDOMComponent;
/** @param options */
constructor(options?: {});
/** @type {ShadowRoot|Node} */
Expand Down

0 comments on commit e2f2192

Please sign in to comment.