Skip to content

Commit

Permalink
refactor(runtime-vapor): extract fallback component
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Nov 15, 2024
1 parent 886440d commit d845108
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 70 deletions.
71 changes: 3 additions & 68 deletions packages/runtime-vapor/src/apiCreateComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@ import {
currentInstance,
} from './component'
import { setupComponent } from './apiRender'
import {
type NormalizedRawProps,
type RawProps,
normalizeRawProps,
walkRawProps,
} from './componentProps'
import { type RawSlots, isDynamicSlotFn } from './componentSlots'
import type { RawProps } from './componentProps'
import type { RawSlots } from './componentSlots'
import { withAttrs } from './componentAttrs'
import { isString } from '@vue/shared'
import { renderEffect } from './renderEffect'
import { setClass, setDynamicProp } from './dom/prop'
import { setStyle } from './dom/style'
import { normalizeBlock } from './block'
import { fallbackComponent } from './componentFallback'

export function createComponent(
comp: Component | string,
Expand Down Expand Up @@ -50,60 +42,3 @@ export function createComponent(

return instance
}

function fallbackComponent(
comp: string,
rawProps: RawProps | null,
slots: RawSlots | null,
instance: ComponentInternalInstance,
singleRoot: boolean = false,
): HTMLElement {
// eslint-disable-next-line no-restricted-globals
const el = document.createElement(comp)

if (rawProps || Object.keys(instance.attrs).length) {
rawProps = [() => instance.attrs, ...normalizeRawProps(rawProps)]

renderEffect(() => {
let classes: unknown[] | undefined
let styles: unknown[] | undefined

walkRawProps(
rawProps as NormalizedRawProps,
(key, valueOrGetter, getter) => {
const value = getter ? valueOrGetter() : valueOrGetter
if (key === 'class') (classes ||= []).push(value)
else if (key === 'style') (styles ||= []).push(value)
else setDynamicProp(el, key, value)
},
)

if (classes) setClass(el, classes)
if (styles) setStyle(el, styles)
})
}

if (slots) {
if (!Array.isArray(slots)) slots = [slots]
for (let i = 0; i < slots.length; i++) {
const slot = slots[i]
if (!isDynamicSlotFn(slot) && slot.default) {
const block = slot.default && slot.default()
if (block) el.append(...normalizeBlock(block))
}
}
}

if (singleRoot) {
instance.dynamicAttrs = true
for (let i = 0; i < instance.scopeIds.length; i++) {
const id = instance.scopeIds[i]
el.setAttribute(id, '')
}
}

const scopeId = instance.type.__scopeId
if (scopeId) el.setAttribute(scopeId, '')

return el
}
2 changes: 0 additions & 2 deletions packages/runtime-vapor/src/apiSetupHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import {
} from './component'
import { warn } from './warning'

// TODO: warning compiler-macros runtime usages

export function useSlots(): SetupContext['slots'] {
return getContext().slots
}
Expand Down
69 changes: 69 additions & 0 deletions packages/runtime-vapor/src/componentFallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import type { ComponentInternalInstance } from './component'
import {
type NormalizedRawProps,
type RawProps,
normalizeRawProps,
walkRawProps,
} from './componentProps'
import { type RawSlots, isDynamicSlotFn } from './componentSlots'
import { renderEffect } from './renderEffect'
import { setClass, setDynamicProp } from './dom/prop'
import { setStyle } from './dom/style'
import { normalizeBlock } from './block'

export function fallbackComponent(
comp: string,
rawProps: RawProps | null,
slots: RawSlots | null,
instance: ComponentInternalInstance,
singleRoot: boolean = false,
): HTMLElement {
// eslint-disable-next-line no-restricted-globals
const el = document.createElement(comp)

if (rawProps || Object.keys(instance.attrs).length) {
rawProps = [() => instance.attrs, ...normalizeRawProps(rawProps)]

renderEffect(() => {
let classes: unknown[] | undefined
let styles: unknown[] | undefined

walkRawProps(
rawProps as NormalizedRawProps,
(key, valueOrGetter, getter) => {
const value = getter ? valueOrGetter() : valueOrGetter
if (key === 'class') (classes ||= []).push(value)
else if (key === 'style') (styles ||= []).push(value)
else setDynamicProp(el, key, value)
},
)

if (classes) setClass(el, classes)
if (styles) setStyle(el, styles)
})
}

if (slots) {
if (!Array.isArray(slots)) slots = [slots]
for (let i = 0; i < slots.length; i++) {
const slot = slots[i]
if (!isDynamicSlotFn(slot) && slot.default) {
const block = slot.default && slot.default()
if (block) el.append(...normalizeBlock(block))
}
}
}

if (singleRoot) {
instance.dynamicAttrs = true
for (let i = 0; i < instance.scopeIds.length; i++) {
const id = instance.scopeIds[i]
el.setAttribute(id, '')
}
}

const scopeId = instance.type.__scopeId
if (scopeId) el.setAttribute(scopeId, '')

return el
}

0 comments on commit d845108

Please sign in to comment.