feat: add `beforeMount` callback to `MountOptions`

beforemount-callback
paoloricciuti 6 months ago
parent c1fab8caae
commit fd023fbff2

@ -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>} */
@ -214,6 +217,8 @@ function _mount(Component, { target, anchor, props = {}, events, context, intro
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;

Loading…
Cancel
Save