pull/17515/merge
Paolo Ricciuti 1 day ago committed by GitHub
commit cedf8c25da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': minor
---
feat: add `beforeMount` callback to `MountOptions`

@ -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

@ -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>} */
@ -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;
@ -241,7 +248,7 @@ function _mount(Component, { target, anchor, props = {}, events, context, intro
}
}
if (context) {
if (context || beforeMount) {
pop();
}
}

@ -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

Loading…
Cancel
Save