Skip to content

fix(runtime-vapor): dynamic component attrs fallthrough #13466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: vapor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion packages/runtime-vapor/__tests__/componentAttrs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { type Ref, nextTick, ref } from '@vue/runtime-dom'
import {
createComponent,
createDynamicComponent,
createSlot,
defineVaporComponent,
renderEffect,
setClass,
Expand Down Expand Up @@ -277,7 +279,43 @@ describe('attribute fallthrough', () => {
expect(getCSS()).not.toContain('font-size:bold')
})

test('parent value should take priority', async () => {
it('should fallthrough attrs to dynamic component', async () => {
const Comp = defineVaporComponent({
setup() {
const n1 = createDynamicComponent(
() => 'button',
null,
{
default: () => {
const n0 = createSlot('default', null)
return n0
},
},
true,
)
return n1
},
})

const { html } = define({
setup() {
return createComponent(
Comp,
{
class: () => 'foo',
},
null,
true,
)
},
}).render()

expect(html()).toBe(
'<button class="foo"><!--slot--></button><!--dynamic-component-->',
)
})

it('parent value should take priority', async () => {
const parentVal = ref('parent')
const childVal = ref('child')

Expand Down
31 changes: 24 additions & 7 deletions packages/runtime-vapor/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
unregisterHMR,
warn,
} from '@vue/runtime-dom'
import { type Block, insert, isBlock, remove } from './block'
import { type Block, DynamicFragment, insert, isBlock, remove } from './block'
import {
type ShallowRef,
markRaw,
Expand Down Expand Up @@ -249,14 +249,16 @@ export function createComponent(
if (
instance.hasFallthrough &&
component.inheritAttrs !== false &&
instance.block instanceof Element &&
Object.keys(instance.attrs).length
) {
renderEffect(() => {
isApplyingFallthroughProps = true
setDynamicProps(instance.block as Element, [instance.attrs])
isApplyingFallthroughProps = false
})
const el = getRootElement(instance)
if (el) {
renderEffect(() => {
isApplyingFallthroughProps = true
setDynamicProps(el, [instance.attrs])
isApplyingFallthroughProps = false
})
}
}

resetTracking()
Expand Down Expand Up @@ -545,3 +547,18 @@ export function getExposed(
)
}
}

function getRootElement({
block,
}: VaporComponentInstance): Element | undefined {
if (block instanceof Element) {
return block
}

if (block instanceof DynamicFragment) {
const { nodes } = block
if (nodes instanceof Element && (nodes as any).$root) {
return nodes
}
}
}
3 changes: 2 additions & 1 deletion packages/runtime-vapor/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ export function hasAttrFromRawProps(rawProps: RawProps, key: string): boolean {
if (dynamicSources) {
let i = dynamicSources.length
while (i--) {
if (hasOwn(resolveSource(dynamicSources[i]), key)) {
const source = resolveSource(dynamicSources[i])
if (source && hasOwn(source, key)) {
return true
}
}
Expand Down