feat(core): named slots (#21)
This commit is contained in:
parent
51be11da89
commit
17c5b810f9
7 changed files with 53 additions and 31 deletions
14
package-lock.json
generated
14
package-lock.json
generated
|
@ -3991,9 +3991,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"packages/config/node_modules/@mcflyjs/core": {
|
"packages/config/node_modules/@mcflyjs/core": {
|
||||||
"version": "0.2.14",
|
"version": "0.2.15",
|
||||||
"resolved": "https://registry.npmjs.org/@mcflyjs/core/-/core-0.2.14.tgz",
|
"resolved": "https://registry.npmjs.org/@mcflyjs/core/-/core-0.2.15.tgz",
|
||||||
"integrity": "sha512-3R9aVJ2Wf5XaTWWSgMvwBnls9D4czmRKgHoleMZ+1jNCgZn8ZoFM0mZuYbHCtw3gEtZsQALJDtkV1piktyWY6w==",
|
"integrity": "sha512-BWG04g8+CIP86KTYNupVAETIFczH4oq46aVVcOnm8AZYalspixDjl+tPAwlk05VTPssh6+d5oIxz7oGKedRcfA==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"esprima": "^4.0.1",
|
"esprima": "^4.0.1",
|
||||||
"h3": "^1.8.2",
|
"h3": "^1.8.2",
|
||||||
|
@ -4002,7 +4002,7 @@
|
||||||
},
|
},
|
||||||
"packages/core": {
|
"packages/core": {
|
||||||
"name": "@mcflyjs/core",
|
"name": "@mcflyjs/core",
|
||||||
"version": "0.2.15",
|
"version": "0.3.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"esprima": "^4.0.1",
|
"esprima": "^4.0.1",
|
||||||
|
@ -4066,9 +4066,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"templates/basic/node_modules/@mcflyjs/core": {
|
"templates/basic/node_modules/@mcflyjs/core": {
|
||||||
"version": "0.2.14",
|
"version": "0.2.15",
|
||||||
"resolved": "https://registry.npmjs.org/@mcflyjs/core/-/core-0.2.14.tgz",
|
"resolved": "https://registry.npmjs.org/@mcflyjs/core/-/core-0.2.15.tgz",
|
||||||
"integrity": "sha512-3R9aVJ2Wf5XaTWWSgMvwBnls9D4czmRKgHoleMZ+1jNCgZn8ZoFM0mZuYbHCtw3gEtZsQALJDtkV1piktyWY6w==",
|
"integrity": "sha512-BWG04g8+CIP86KTYNupVAETIFczH4oq46aVVcOnm8AZYalspixDjl+tPAwlk05VTPssh6+d5oIxz7oGKedRcfA==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"esprima": "^4.0.1",
|
"esprima": "^4.0.1",
|
||||||
"h3": "^1.8.2",
|
"h3": "^1.8.2",
|
||||||
|
|
|
@ -142,7 +142,7 @@ async function buildRegistry(usedCustomElements, type, storage) {
|
||||||
!isBaseClassImported &&
|
!isBaseClassImported &&
|
||||||
content.toString().includes("extends WebComponent")
|
content.toString().includes("extends WebComponent")
|
||||||
) {
|
) {
|
||||||
const baseClassImport = `import { WebComponent } from "https://unpkg.com/web-component-base@1.11.1/WebComponent.js";`;
|
const baseClassImport = `import { WebComponent } from "https://unpkg.com/web-component-base@1.11.2/WebComponent.js";`;
|
||||||
|
|
||||||
registryScript += baseClassImport;
|
registryScript += baseClassImport;
|
||||||
isBaseClassImported = true;
|
isBaseClassImported = true;
|
||||||
|
@ -335,10 +335,28 @@ async function useFragments(html, storage) {
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
function replaceSlots(fragmentNode, node) {
|
function replaceSlots(fragmentNode, node) {
|
||||||
|
let slotted = [];
|
||||||
|
const containsAll = (arr, target) => target.every(v => arr.includes(v));
|
||||||
walkSync(fragmentNode, (n) => {
|
walkSync(fragmentNode, (n) => {
|
||||||
if (n.type === ELEMENT_NODE && n.name === "slot") {
|
if (n.type === ELEMENT_NODE && n.name === "slot") {
|
||||||
|
// find node child with same name attribute
|
||||||
|
const currentSlotName = n.attributes?.['name'] ?? null;
|
||||||
|
let nodeChildren = [];
|
||||||
|
|
||||||
|
if (currentSlotName === null) {
|
||||||
|
nodeChildren = node.children.filter(child => !child.attributes?.['slot'] && child.type === ELEMENT_NODE);
|
||||||
|
} else {
|
||||||
|
nodeChildren = node.children.filter(child => {
|
||||||
|
const childSlotName = child.attributes?.['slot'];
|
||||||
|
return childSlotName === currentSlotName;
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nodeChildren.length > 0 && !containsAll(slotted, nodeChildren)) {
|
||||||
|
slotted = [...slotted, ...nodeChildren]
|
||||||
const index = n.parent.children.indexOf(n);
|
const index = n.parent.children.indexOf(n);
|
||||||
n.parent.children.splice(index, 1, ...node.children);
|
n.parent.children.splice(index, 1, ...nodeChildren);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@mcflyjs/core",
|
"name": "@mcflyjs/core",
|
||||||
"version": "0.2.15",
|
"version": "0.3.0",
|
||||||
"description": "McFly core package",
|
"description": "McFly core package",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
import McFly from "@mcflyjs/config";
|
import McFly from "@mcflyjs/config";
|
||||||
export default defineNitroConfig({
|
export default defineNitroConfig({
|
||||||
...McFly(),
|
...McFly(),
|
||||||
|
devServer: {
|
||||||
|
watch: ["../packages"],
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
"
|
"
|
||||||
>
|
>
|
||||||
<a style="color: white" href="/">
|
<a style="color: white" href="/">
|
||||||
<h1>McFly</h1>
|
<h1>McFly <slot name="title-postfix" /> </h1>
|
||||||
</a>
|
</a>
|
||||||
<span>Back to the Basics. Into the Future.</span>
|
<slot />
|
||||||
</header>
|
</header>
|
||||||
|
|
|
@ -16,7 +16,10 @@
|
||||||
</script>
|
</script>
|
||||||
</my-head>
|
</my-head>
|
||||||
<body>
|
<body>
|
||||||
<awesome-header><span>heyyy</span></awesome-header>
|
<awesome-header>
|
||||||
|
<span>Back to the Basics. Into the Future.</span>
|
||||||
|
<span slot="title-postfix">- This is strong!</span>
|
||||||
|
</awesome-header>
|
||||||
<main>
|
<main>
|
||||||
<section id="intro">
|
<section id="intro">
|
||||||
<h1>McFly Demo</h1>
|
<h1>McFly Demo</h1>
|
||||||
|
@ -75,8 +78,6 @@
|
||||||
</script>
|
</script>
|
||||||
</my-head>
|
</my-head>
|
||||||
<body>
|
<body>
|
||||||
<awesome-header><span>heyyy</span></awesome-header>
|
|
||||||
<main>
|
|
||||||
<a href="/demo/about">{{ count }}</a>
|
<a href="/demo/about">{{ count }}</a>
|
||||||
<div>
|
<div>
|
||||||
<my-hello-world id="my-hello" data-name="{{name }}"></my-hello-world>
|
<my-hello-world id="my-hello" data-name="{{name }}"></my-hello-world>
|
||||||
|
@ -89,8 +90,6 @@
|
||||||
<p>some text: {{greeting}}</p>
|
<p>some text: {{greeting}}</p>
|
||||||
{{greeting}} hey<br />
|
{{greeting}} hey<br />
|
||||||
<clickable-text></clickable-text>
|
<clickable-text></clickable-text>
|
||||||
</main>
|
|
||||||
<my-footer></my-footer>
|
|
||||||
</body>
|
</body>
|
||||||
</html></pre
|
</html></pre
|
||||||
>
|
>
|
||||||
|
|
|
@ -20,7 +20,9 @@
|
||||||
</style>
|
</style>
|
||||||
</my-head>
|
</my-head>
|
||||||
<body>
|
<body>
|
||||||
<awesome-header></awesome-header>
|
<awesome-header>
|
||||||
|
<span>Back to the Basics. Into the Future.</span>
|
||||||
|
</awesome-header>
|
||||||
<main>
|
<main>
|
||||||
<section>
|
<section>
|
||||||
<p>
|
<p>
|
||||||
|
|
Loading…
Reference in a new issue