diff --git a/site/content/docs/03-run-time.md b/site/content/docs/03-run-time.md index b35af93838..386c18d5fd 100644 --- a/site/content/docs/03-run-time.md +++ b/site/content/docs/03-run-time.md @@ -178,6 +178,26 @@ Retrieves the context that belongs to the closest parent component with the spec ``` +#### `hasContext` + +```js +hasContext: boolean = hasContext(key: any) +``` + +--- + +Checks whether a given `key` has been set in the context of a parent component. Must be called during component initialisation. + +```sv + +``` + #### `createEventDispatcher` ```js diff --git a/src/runtime/internal/lifecycle.ts b/src/runtime/internal/lifecycle.ts index 4a7616bdea..002bd78d24 100644 --- a/src/runtime/internal/lifecycle.ts +++ b/src/runtime/internal/lifecycle.ts @@ -54,6 +54,10 @@ export function getContext(key): T { return get_current_component().$$.context.get(key); } +export function hasContext(key): boolean { + return get_current_component().$$.context.has(key); +} + // TODO figure out if we still want to support // shorthand events, or if we want to implement // a real bubbling mechanism