Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/cyan-kings-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': minor
---

feat: add `beforeMount` callback to `MountOptions`
4 changes: 4 additions & 0 deletions packages/svelte/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ export type MountOptions<Props extends Record<string, any> = Record<string, any>
* Can be accessed via `getContext()` at the component level.
*/
context?: Map<any, any>;
/**
* A function invoked before the component is mounted but after the component context has been initialized.
*/
beforeMount?: () => void;
/**
* Whether or not to play transitions on initial render.
* @default true
Expand Down
17 changes: 12 additions & 5 deletions packages/svelte/src/internal/client/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ const document_listeners = new Map();
* @param {MountOptions} options
* @returns {Exports}
*/
function _mount(Component, { target, anchor, props = {}, events, context, intro = true }) {
function _mount(
Component,
{ target, anchor, props = {}, beforeMount, events, context, intro = true }
) {
init_operations();

/** @type {Set<string>} */
Expand Down Expand Up @@ -208,12 +211,16 @@ function _mount(Component, { target, anchor, props = {}, events, context, intro
pending: () => {}
},
(anchor_node) => {
if (context) {
if (context || beforeMount) {
push({});
var ctx = /** @type {ComponentContext} */ (component_context);
ctx.c = context;
if (context) {
var ctx = /** @type {ComponentContext} */ (component_context);
ctx.c = context;
}
}

beforeMount?.();

if (events) {
// We can't spread the object or else we'd lose the state proxy stuff, if it is one
/** @type {any} */ (props).$$events = events;
Expand Down Expand Up @@ -241,7 +248,7 @@ function _mount(Component, { target, anchor, props = {}, events, context, intro
}
}

if (context) {
if (context || beforeMount) {
pop();
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/svelte/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ declare module 'svelte' {
* Can be accessed via `getContext()` at the component level.
*/
context?: Map<any, any>;
/**
* A function invoked before the component is mounted but after the component context has been initialized.
*/
beforeMount?: () => void;
/**
* Whether or not to play transitions on initial render.
* @default true
Expand Down
Loading