Skip to content
Merged
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
5 changes: 5 additions & 0 deletions docs/src/routes/plugins/component/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@

<Code code={data.codeBlocks.extension} />

<p>
Attributes of the matched element are passed to the component as props, using hast property names:
for example, <code>data-item-id="1"</code> is available as the <code>dataItemId</code> prop.
</p>

<h3>Custom logic</h3>

<p>You can use custom logic when selecting which nodes to map:</p>
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-component/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
],
"dependencies": {
"esm-env": "^1.0.0",
"property-information": "^7.0.0",
"rehype-parse": "^9.0.0",
"unist-util-visit": "^5.0.0"
}
Expand Down
14 changes: 8 additions & 6 deletions packages/plugin-component/src/lib/svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { mount, type Component } from 'svelte';
import { render } from 'svelte/server';
import type * as hast from 'hast';
import { BROWSER } from 'esm-env';
import { find, html as htmlSchema } from 'property-information';
import { unified } from 'unified';
import rehypeParse from 'rehype-parse';
import { EXIT, visit } from 'unist-util-visit';
Expand Down Expand Up @@ -114,12 +115,13 @@ export const initializeComponents = (
continue;
}

const props = Array.from(placeholder.attributes)
.filter((attr) => attr.name !== 'data-component')
.map((attr) => ({
[attr.name]: attr.value
}))
.reduce((acc, curr) => ({ ...acc, ...curr }), {});
// Use hast property names, so that components receive the same props
// on the server and on the client (e.g. `data-item-id` -> `dataItemId`).
const props: Record<string, string> = {};
for (const attr of Array.from(placeholder.attributes)) {
if (attr.name === 'data-component') continue;
props[find(htmlSchema, attr.name).property] = attr.value;
}

const wrapper = document.createElement('div');
wrapper.setAttribute('data-mounted-component', id);
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.