]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor(runtime-vapor): extract fallback component
author三咲智子 Kevin Deng <sxzz@sxzz.moe>
Fri, 15 Nov 2024 18:43:56 +0000 (02:43 +0800)
committer三咲智子 Kevin Deng <sxzz@sxzz.moe>
Fri, 15 Nov 2024 18:43:56 +0000 (02:43 +0800)
packages/runtime-vapor/src/apiCreateComponent.ts
packages/runtime-vapor/src/apiSetupHelpers.ts
packages/runtime-vapor/src/componentFallback.ts [new file with mode: 0644]

index 8f19e6b3286302f48677dc0ee706790ec89db884..62681c9058ef29f17bba551f466fe70a9b1ef1bf 100644 (file)
@@ -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,
@@ -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
-}
index 4afd4304685fea8593ee581f3ceb5ad57b26848a..1737f6ec4cba73ebba88cef2d6a94d1659a45a89 100644 (file)
@@ -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
 }
diff --git a/packages/runtime-vapor/src/componentFallback.ts b/packages/runtime-vapor/src/componentFallback.ts
new file mode 100644 (file)
index 0000000..adceb54
--- /dev/null
@@ -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
+}