Skip to content

Commit d9b690f

Browse files
committed
refactor: manage fallthrough attributes for dynamic fragments using a dedicated attrs property instead of a root flag.
1 parent 1ae7da0 commit d9b690f

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

packages/runtime-vapor/src/component.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,12 @@ export function setupComponent(
407407
component.inheritAttrs !== false &&
408408
Object.keys(instance.attrs).length
409409
) {
410-
const root = getRootElement(instance.block, false)
410+
const root = getRootElement(
411+
instance.block,
412+
// attach attrs to dynamic fragments for applying during each update
413+
frag => (frag.attrs = instance.attrs),
414+
false,
415+
)
411416
if (root) {
412417
renderEffect(() => applyFallthroughProps(root, instance.attrs))
413418
} else if (
@@ -865,23 +870,26 @@ export function getExposed(
865870

866871
export function getRootElement(
867872
block: Block,
873+
onDynamicFragment?: (frag: DynamicFragment) => void,
868874
recurse: boolean = true,
869875
): Element | undefined {
870876
if (block instanceof Element) {
871877
return block
872878
}
873879

874880
if (recurse && isVaporComponent(block)) {
875-
return getRootElement(block.block, recurse)
881+
return getRootElement(block.block, onDynamicFragment, recurse)
876882
}
877883

878884
if (isFragment(block) && !(block instanceof TeleportFragment)) {
879-
if (block instanceof DynamicFragment) block.root = true
885+
if (block instanceof DynamicFragment && onDynamicFragment) {
886+
onDynamicFragment(block)
887+
}
880888
const { nodes } = block
881889
if (nodes instanceof Element && (nodes as any).$root) {
882890
return nodes
883891
}
884-
return getRootElement(nodes, recurse)
892+
return getRootElement(nodes, onDynamicFragment, recurse)
885893
}
886894

887895
// The root node contains comments. It is necessary to filter out
@@ -895,7 +903,7 @@ export function getRootElement(
895903
hasComment = true
896904
continue
897905
}
898-
const thisRoot = getRootElement(b, recurse)
906+
const thisRoot = getRootElement(b, onDynamicFragment, recurse)
899907
// only return root if there is exactly one eligible root in the array
900908
if (!thisRoot || singleRoot) {
901909
return

packages/runtime-vapor/src/fragment.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ export class DynamicFragment extends VaporFragment {
7777
current?: BlockFn
7878
fallback?: BlockFn
7979
anchorLabel?: string
80-
root?: boolean
80+
81+
// fallthrough attrs
82+
attrs?: Record<string, any>
8183

8284
// get the kept-alive scope when used in keep-alive
8385
getScope?: (key: any) => EffectScope | undefined
@@ -204,21 +206,16 @@ export class DynamicFragment extends VaporFragment {
204206

205207
if (parent) {
206208
// fallthrough attrs
207-
if (
208-
this.root &&
209-
this.parentComponent &&
210-
(this.parentComponent as VaporComponentInstance)!.hasFallthrough &&
211-
Object.keys(this.parentComponent!.attrs).length
212-
) {
209+
if (this.attrs) {
213210
if (this.nodes instanceof Element) {
214-
applyFallthroughProps(this.nodes, this.parentComponent!.attrs)
211+
applyFallthroughProps(this.nodes, this.attrs)
215212
} else if (
216213
__DEV__ &&
217214
// preventing attrs fallthrough on slots
218215
// consistent with VDOM slots behavior
219216
this.anchorLabel === 'slot'
220217
) {
221-
warnExtraneousAttributes(this.parentComponent!.attrs)
218+
warnExtraneousAttributes(this.attrs)
222219
}
223220
}
224221

0 commit comments

Comments
 (0)