Skip to content

Commit

Permalink
add slots management
Browse files Browse the repository at this point in the history
  • Loading branch information
thipages committed Dec 25, 2024
1 parent c456fb6 commit 3b97345
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- `originalText(remove)` for getting the original `textContent`,
- `originalFragment(remove)` for getting the original `childNodes` appended to a document fragment,
- `remove` argument to delete from memory the text or fragment (default : true),
- `getSlotByName(name)` to get all mamed slots present as custom-element children but removed and stored for later use,
- events
- `onload` triggers when async init finishes (sync init also)
- attributes
Expand Down
14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
{
"name": "@titsoft/m-element",
"version": "0.7.0",
"description": "HTMLParsedElement extension",
"version": "0.8.0",
"description": "custom-element class with (a)sync loading, slots, error handling and level-up",
"main": "src/index.js",
"type": "module",
"keywords": [
"custom-elements",
"slots",
"asynchronous",
"synchronous",
"error handling",
"load event handling",
"life-cycle",
"level-up"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const isAsyncFunction = fn => fn.constructor.name === 'AsyncFunction'
export default class MElement extends HTMLParsedElement {
#config
#fragment
#slots
constructor(config) {
super()
this.#config = config || {}
Expand Down Expand Up @@ -48,6 +49,9 @@ export default class MElement extends HTMLParsedElement {
parsedCallback() {
const that = this
const end = (e) => that.#finish(e)
// slots removal and storage
this.#slots = this.querySelectorAll('slot')
this.#slots.forEach(e => e.remove())
// move childNodes to a fragment
this.#fragment = document.createDocumentFragment()
this.#fragment.append(...this.childNodes)
Expand All @@ -69,4 +73,7 @@ export default class MElement extends HTMLParsedElement {
end()
}
}
getSlotByName(name) {
return [...this.#slots].filter(e => name && e.name === name) [0]
}
}
6 changes: 4 additions & 2 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ <h1>output</h1>
<!-- Retrieve content with orginalContent method -->
<test-sync id="A5">a content</test-sync>
<error-async id="A6" delay="500">a content</error-async>

<!-- Retrieve content with originalFragment method -->
<test-sync2 id="A7">
<span>original fragment</span>
<div></div>
</test-sync2>

<!-- slots tests -->
<test-sync id="A8">
<slot name="slot-test">slot content</slot>
</test-sync>
</html>
7 changes: 5 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ customElements.define('error-async', class extends MElement {
)
}
})
const num = 6
const num = 7
const el = (id) => document.getElementById(id)
Array(num).fill('').map((v, i)=>'A' + (i+1)).forEach (id => el(id).addEventListener('load', loaded))
let loadedCount = 0
Expand All @@ -71,7 +71,10 @@ function runTests() {
!document.getElementById('A4'),
A5.originalText() === 'a content',
A6.onError = true,
A7.children.length === 2
A7.children.length === 2,
A8.getSlotByName('slot-test').textContent === 'slot content'
&& A8.querySelectorAll('slot').length === 0
&& A8.getSlotByName('slot-foo') === undefined
]
addResults(tests)
}
Expand Down

0 comments on commit 3b97345

Please sign in to comment.