Skip to content

Commit

Permalink
Fixes JSX usage of slots with dashes in their name (#5080)
Browse files Browse the repository at this point in the history
* Fixes JSX usage of slots with dashes in their name

* Adding a changeset
  • Loading branch information
matthewp authored Oct 13, 2022
1 parent 5fb7c93 commit 90b715d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/large-cougars-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix Astro-in-MDX dashes in slot attr
2 changes: 1 addition & 1 deletion packages/astro/src/jsx-runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface AstroVNode {
props: Record<string, any>;
}

const toSlotName = (str: string) => str.trim().replace(/[-_]([a-z])/g, (_, w) => w.toUpperCase());
const toSlotName = (slotAttr: string) => slotAttr;

export function isVNode(vnode: any): vnode is AstroVNode {
return vnode && typeof vnode === 'object' && vnode[AstroJSX];
Expand Down
39 changes: 39 additions & 0 deletions packages/astro/test/units/render/jsx.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,44 @@ describe('core/render', () => {
const html = await response.text();
expect(html).to.include('<div><p class="n">works</p></div>');
});

it('Can render slots with a dash in the name', async () => {
const Wrapper = createComponent((result, _props, slots = {}) => {
return render`<div>${renderSlot(result, slots['my-slot'])}</div>`;
});

const Page = createAstroJSXComponent(() => {
return jsx('main', {
children: [
jsx(Wrapper, {
// Children as an array
children: [
jsx('p', {
slot: 'my-slot',
className: 'n',
children: 'works'
})
]
}),
jsx(Wrapper, {
// Children as a VNode
children: jsx('p', {
slot: 'my-slot',
className: 'p',
children: 'works'
})
})
]
})
});

const ctx = createRenderContext({ request: new Request('http://example.com/' )});
const response = await renderPage(createAstroModule(Page), ctx, env);

expect(response.status).to.equal(200);

const html = await response.text();
expect(html).to.include('<main><div><p class="n">works</p></div><div><p class="p">works</p></div></main>');
});
});
});

0 comments on commit 90b715d

Please sign in to comment.