Skip to content

Commit 2124ede

Browse files
committed
feat: Add web component loading spinner and enhance initialization
1 parent e1133f3 commit 2124ede

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

demo-app-vite/index.html

+18
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ <h2>Custom Event</h2>
5959

6060
<!-- named slot -->
6161
<div class="customName" hidden slot="customName">I am a custom named slot </div>
62+
<div class="customName" data-web-component-loader slot="customName">
63+
<div class="spinner"></div>
64+
</div>
6265
</my-web-component>
6366

6467
<script type="module" src="./src/main.ts"></script>
@@ -117,5 +120,20 @@ <h2>Custom Event</h2>
117120
color: white;
118121
font-weight: bold;
119122
}
123+
.spinner {
124+
border: 4px solid rgba(0, 0, 0, 0.1);
125+
border-left-color: #4a90e2; /* Customize spinner color if needed */
126+
border-radius: 50%;
127+
width: 30px;
128+
height: 30px;
129+
animation: spin 1s linear infinite;
130+
margin: auto;
131+
}
132+
133+
@keyframes spin {
134+
to {
135+
transform: rotate(360deg);
136+
}
137+
}
120138
</style>
121139
</html>

demo-app-vite/src/main.ts

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ createWebComponent({
3232
h,
3333
createApp,
3434
getCurrentInstance,
35+
disableShadowDOM: false,
3536
asyncInitialization: asyncPromise
3637
})
3738

@@ -40,6 +41,7 @@ createWebComponent({
4041
elementName: 'my-child-component',
4142
plugins: pluginsWrapper,
4243
cssFrameworkStyles: style,
44+
disableShadowDOM: false,
4345
VueDefineCustomElement,
4446
h,
4547
createApp,

package/src/web-component-util.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export const defineCustomElement = ({
3939
disableShadowDOM,
4040
replaceRootWithHostInCssFramework,
4141
asyncInitialization = () => Promise.resolve(),
42+
loaderAttribute = 'data-web-component-loader'
4243
}) =>
4344
{
4445
const customElementDefiner = disableShadowDOM ? VueDefineCustomElementPatch : VueDefineCustomElement
@@ -90,6 +91,21 @@ export const defineCustomElement = ({
9091
insertStyles(comp.styles);
9192
}
9293
}
94+
95+
const host = this.$el.getRootNode()?.host || nearestElement(this.$el);
96+
if (host) {
97+
const hiddenEls = host.querySelectorAll(`[hidden]`);
98+
const loaderEls = host.querySelectorAll(`[${loaderAttribute}]`);
99+
100+
hiddenEls.forEach(el => {
101+
el.removeAttribute('hidden');
102+
});
103+
104+
loaderEls.forEach(el => {
105+
el.remove();
106+
});
107+
108+
}
93109
},
94110
unmounted() {
95111
if (!disableRemoveStylesOnUnmount) {
@@ -133,17 +149,6 @@ export const defineCustomElement = ({
133149
const namedSlots = rootComponent?.namedSlots?.reduce((acc, slotsName) => {
134150
acc[slotsName] = () => h('slot', {
135151
name: slotsName,
136-
onSlotchange: (event) => {
137-
const slotElement = event.target;
138-
const nodes = typeof slotElement.assignedElements === 'function'
139-
? slotElement.assignedElements()
140-
: (slotElement.assignedNodes ? slotElement.assignedNodes().filter(node => node.nodeType === 1) : []);
141-
nodes.forEach(node => {
142-
if (node.hasAttribute('hidden')) {
143-
node.removeAttribute('hidden');
144-
}
145-
});
146-
}
147152
});
148153
return acc;
149154
}, {});

0 commit comments

Comments
 (0)