Skip to content
Closed
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 .changeset/itchy-planets-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

feat: add possibility to decorate components
20 changes: 20 additions & 0 deletions packages/svelte/src/index-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,26 @@ export function flushSync(fn) {
flush_sync(fn);
}

/**
* Wraps a component with a decorator that is able to modify the props of the component before it is created.
* @template {import('svelte').SvelteComponent} Component
* @template {import('svelte').ComponentProps<Component>} [Props=import('svelte').ComponentProps<Component>]
* @param {Component} component
* @param {(args: { props: Props, component: (props: Props) => Component }) => Component} decorator
* @returns {Component}
*/
export function decorateComponent(component, decorator) {
// @ts-expect-error shape is different under the hood
return (target, props) => {
return decorator({
props,
component: (props) =>
// @ts-expect-error shape is different under the hood
component(target, props)
});
};
}

export { hydrate, mount, unmount } from './internal/client/render.js';

export {
Expand Down
19 changes: 19 additions & 0 deletions packages/svelte/src/index-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,23 @@ export function unmount() {

export async function tick() {}

/**
* @template {import('svelte').SvelteComponent} Component
* @template {import('svelte').ComponentProps<Component>} [Props=import('svelte').ComponentProps<Component>]
* @param {Component} component
* @param {(args: { props: Props, component: (props: Props) => Component }) => Component} decorator
* @returns {Component}
*/
export function decorateComponent(component, decorator) {
// @ts-expect-error shape is different under the hood
return (payload, props) => {
return decorator({
props,
component: (props) =>
// @ts-expect-error shape is different under the hood
component(payload, props)
});
};
}

export { getAllContexts, getContext, hasContext, setContext } from './internal/server/context.js';
7 changes: 7 additions & 0 deletions packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,13 @@ declare module 'svelte' {
* Synchronously flushes any pending state changes and those that result from it.
* */
export function flushSync(fn?: (() => void) | undefined): void;
/**
* Wraps a component with a decorator that is able to modify the props of the component before it is created.
* */
export function decorateComponent<Component extends SvelteComponent<Record<string, any>, any, any>, Props extends ComponentProps<Component> = ComponentProps<Component>>(component: Component, decorator: (args: {
props: Props;
component: (props: Props) => Component;
}) => Component): Component;
/** Anything except a function */
type NotFunction<T> = T extends Function ? never : T;
/**
Expand Down