vue3-core/packages/runtime-vapor/src/apiCreateIf.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

35 lines
889 B
TypeScript
Raw Normal View History

2025-04-25 15:08:19 +08:00
import { IF_ANCHOR_LABEL } from '@vue/shared'
import { type Block, type BlockFn, DynamicFragment, insert } from './block'
2025-04-25 15:08:19 +08:00
import { isHydrating } from './dom/hydration'
import {
insertionAnchor,
insertionParent,
resetInsertionState,
} from './insertionState'
2024-12-14 20:37:43 +08:00
import { renderEffect } from './renderEffect'
2024-12-11 11:50:17 +08:00
export function createIf(
condition: () => any,
b1: BlockFn,
b2?: BlockFn,
once?: boolean,
): Block {
const _insertionParent = insertionParent
const _insertionAnchor = insertionAnchor
2025-04-25 15:08:19 +08:00
if (!isHydrating) resetInsertionState()
let frag: Block
2024-12-14 20:37:43 +08:00
if (once) {
frag = condition() ? b1() : b2 ? b2() : []
2024-12-14 20:37:43 +08:00
} else {
2025-04-25 15:08:19 +08:00
frag = new DynamicFragment(IF_ANCHOR_LABEL)
renderEffect(() => (frag as DynamicFragment).update(condition() ? b1 : b2))
2024-12-14 20:37:43 +08:00
}
if (!isHydrating && _insertionParent) {
insert(frag, _insertionParent, _insertionAnchor)
}
return frag
2024-12-11 11:50:17 +08:00
}