Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit d845108

Browse files
committed
refactor(runtime-vapor): extract fallback component
1 parent 886440d commit d845108

File tree

3 files changed

+72
-70
lines changed

3 files changed

+72
-70
lines changed

packages/runtime-vapor/src/apiCreateComponent.ts

Lines changed: 3 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,11 @@ import {
55
currentInstance,
66
} from './component'
77
import { setupComponent } from './apiRender'
8-
import {
9-
type NormalizedRawProps,
10-
type RawProps,
11-
normalizeRawProps,
12-
walkRawProps,
13-
} from './componentProps'
14-
import { type RawSlots, isDynamicSlotFn } from './componentSlots'
8+
import type { RawProps } from './componentProps'
9+
import type { RawSlots } from './componentSlots'
1510
import { withAttrs } from './componentAttrs'
1611
import { isString } from '@vue/shared'
17-
import { renderEffect } from './renderEffect'
18-
import { setClass, setDynamicProp } from './dom/prop'
19-
import { setStyle } from './dom/style'
20-
import { normalizeBlock } from './block'
12+
import { fallbackComponent } from './componentFallback'
2113

2214
export function createComponent(
2315
comp: Component | string,
@@ -50,60 +42,3 @@ export function createComponent(
5042

5143
return instance
5244
}
53-
54-
function fallbackComponent(
55-
comp: string,
56-
rawProps: RawProps | null,
57-
slots: RawSlots | null,
58-
instance: ComponentInternalInstance,
59-
singleRoot: boolean = false,
60-
): HTMLElement {
61-
// eslint-disable-next-line no-restricted-globals
62-
const el = document.createElement(comp)
63-
64-
if (rawProps || Object.keys(instance.attrs).length) {
65-
rawProps = [() => instance.attrs, ...normalizeRawProps(rawProps)]
66-
67-
renderEffect(() => {
68-
let classes: unknown[] | undefined
69-
let styles: unknown[] | undefined
70-
71-
walkRawProps(
72-
rawProps as NormalizedRawProps,
73-
(key, valueOrGetter, getter) => {
74-
const value = getter ? valueOrGetter() : valueOrGetter
75-
if (key === 'class') (classes ||= []).push(value)
76-
else if (key === 'style') (styles ||= []).push(value)
77-
else setDynamicProp(el, key, value)
78-
},
79-
)
80-
81-
if (classes) setClass(el, classes)
82-
if (styles) setStyle(el, styles)
83-
})
84-
}
85-
86-
if (slots) {
87-
if (!Array.isArray(slots)) slots = [slots]
88-
for (let i = 0; i < slots.length; i++) {
89-
const slot = slots[i]
90-
if (!isDynamicSlotFn(slot) && slot.default) {
91-
const block = slot.default && slot.default()
92-
if (block) el.append(...normalizeBlock(block))
93-
}
94-
}
95-
}
96-
97-
if (singleRoot) {
98-
instance.dynamicAttrs = true
99-
for (let i = 0; i < instance.scopeIds.length; i++) {
100-
const id = instance.scopeIds[i]
101-
el.setAttribute(id, '')
102-
}
103-
}
104-
105-
const scopeId = instance.type.__scopeId
106-
if (scopeId) el.setAttribute(scopeId, '')
107-
108-
return el
109-
}

packages/runtime-vapor/src/apiSetupHelpers.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import {
55
} from './component'
66
import { warn } from './warning'
77

8-
// TODO: warning compiler-macros runtime usages
9-
108
export function useSlots(): SetupContext['slots'] {
119
return getContext().slots
1210
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import type { ComponentInternalInstance } from './component'
2+
import {
3+
type NormalizedRawProps,
4+
type RawProps,
5+
normalizeRawProps,
6+
walkRawProps,
7+
} from './componentProps'
8+
import { type RawSlots, isDynamicSlotFn } from './componentSlots'
9+
import { renderEffect } from './renderEffect'
10+
import { setClass, setDynamicProp } from './dom/prop'
11+
import { setStyle } from './dom/style'
12+
import { normalizeBlock } from './block'
13+
14+
export function fallbackComponent(
15+
comp: string,
16+
rawProps: RawProps | null,
17+
slots: RawSlots | null,
18+
instance: ComponentInternalInstance,
19+
singleRoot: boolean = false,
20+
): HTMLElement {
21+
// eslint-disable-next-line no-restricted-globals
22+
const el = document.createElement(comp)
23+
24+
if (rawProps || Object.keys(instance.attrs).length) {
25+
rawProps = [() => instance.attrs, ...normalizeRawProps(rawProps)]
26+
27+
renderEffect(() => {
28+
let classes: unknown[] | undefined
29+
let styles: unknown[] | undefined
30+
31+
walkRawProps(
32+
rawProps as NormalizedRawProps,
33+
(key, valueOrGetter, getter) => {
34+
const value = getter ? valueOrGetter() : valueOrGetter
35+
if (key === 'class') (classes ||= []).push(value)
36+
else if (key === 'style') (styles ||= []).push(value)
37+
else setDynamicProp(el, key, value)
38+
},
39+
)
40+
41+
if (classes) setClass(el, classes)
42+
if (styles) setStyle(el, styles)
43+
})
44+
}
45+
46+
if (slots) {
47+
if (!Array.isArray(slots)) slots = [slots]
48+
for (let i = 0; i < slots.length; i++) {
49+
const slot = slots[i]
50+
if (!isDynamicSlotFn(slot) && slot.default) {
51+
const block = slot.default && slot.default()
52+
if (block) el.append(...normalizeBlock(block))
53+
}
54+
}
55+
}
56+
57+
if (singleRoot) {
58+
instance.dynamicAttrs = true
59+
for (let i = 0; i < instance.scopeIds.length; i++) {
60+
const id = instance.scopeIds[i]
61+
el.setAttribute(id, '')
62+
}
63+
}
64+
65+
const scopeId = instance.type.__scopeId
66+
if (scopeId) el.setAttribute(scopeId, '')
67+
68+
return el
69+
}

0 commit comments

Comments
 (0)