Context

Import

Signature

contextProvided(__namedParameters): (protoOrDescriptor: TODO , name?: K) => void | any

Parameters

__namedParameters
{context: {__context__: ValueType}, subscribe: boolean}

Import

Signature

contextProvider(__namedParameters): (protoOrDescriptor: TODO , name?: K) => void | any

Parameters

__namedParameters
{context: {__context__: ValueType}}

A ContextRoot can be used to gather unsatisfied context requests and re-dispatch them when new providers which satisfy matching context keys are available.

Import

Details

This allows providers to be added to a DOM tree, or upgraded, after the consumers.

Methods and properties

Attach the ContextRoot to a given element to intercept context-request and context-provider events.

Parameters
element
MDN HTMLElement

an element to add event listeners to

Removes the ContextRoot event listeners from a given element.

Parameters
element
MDN HTMLElement

an element from which to remove event listeners

An event fired by a context requester to signal it desires a specified context with the given key.

Import

Details

A provider should inspect the context property of the event to determine if it has a value that can satisfy the request, calling the callback with the requested value if so. If the requested context event contains a truthy subscribe value, then a provider can call the callback multiple times if the value is changed, if this is the case the provider should pass an unsubscribe method to the callback which consumers can invoke to indicate they no longer wish to receive these updates. If no subscribe value is present in the event, then the provider can assume that this is a 'one time' request for the context and can therefore not track the consumer.

Methods and properties

new ContextRequestEvent(context, callback, subscribe?): ContextRequestEvent<C>

Permalink to constructor View source
Parameters
context
C

the context key to request

callback
ContextCallback<ContextType<C>>

the callback that should be invoked when the context with the specified key is available

subscribe?
boolean

an optional argument, if true indicates we want to subscribe to future updates

the callback that should be invoked when the context with the specified key is available

the context key to request

readonly subscribe?: boolean

Permalink to subscribe View source

an optional argument, if true indicates we want to subscribe to future updates

A callback which is provided by a context requester and is called with the value satisfying the request. This callback can be called multiple times by context providers as the requested value is changed.

Import

Type

(value: ValueType, unsubscribe?: () => void) => void

Interface definition for a ContextRequest

Import

Methods and properties

readonly subscribe?: boolean

Permalink to subscribe View source

A ReactiveController which adds context consuming behavior to a custom element by dispatching context-request events.

Import

Details

When the host element is connected to the document it will emit a context-request event with its context key. When the context request is satisfied the controller will invoke the callback, if present, and trigger a host update so it can respond to the new value. It will also call the dispose method given by the provider when the host element is disconnected.

Methods and properties

new ContextConsumer(host, options): ContextConsumer<C, HostElement>

Permalink to constructor View source
Parameters
host
HostElement
options
Options<C>

Import

Methods and properties

callback?: (value: ContextType<C>, dispose?: () => void) => void

Permalink to callback View source

A ReactiveController which adds context provider behavior to a custom element.

Import

Details

This controller simply listens to the context-request event when the host is connected to the DOM and registers the received callbacks against its observable Context implementation.

Methods and properties

new ContextProvider(host, options): ContextProvider<T>

Permalink to constructor View source
Parameters
host
ReactiveElement
options
Options<T>

host: ReactiveElement

Permalink to host View source

addCallback(callback, subscribe?): void

Permalink to addCallback View source
Parameters
callback
ContextCallback<ContextType<T>>
subscribe?
boolean
Parameters
ev
ContextRequestEvent<{__context__: unknown}>

setValue(v, force): void

Permalink to setValue View source
Parameters
v
ContextType<T>
force
boolean

Import

Methods and properties

Parameters
context
C

the context which this provider can provide

the context which this provider can provide

Import

Creates a typed Context.

Import

Signature

createContext(key): Context<K, ValueType>

Parameters

key
K

a context key value

Details

Contexts are compared with with strict equality. If you want two separate createContext() calls to referer to the same context, then use a key that will by equal under strict equality like a string for Symbol.for():

If you want a context to be unique so that it's guaranteed to not collide with other contexts, use a key that's unique under strict equality, like a Symbol() or object.:

The Context type defines a type brand to associate a key value with the context value type

Import

Type

TODO

Import

Type

Context<KeyType, ValueType>

A helper type which can extract a Context value type from a Context type

Import

Type

Key ? ValueType : never

A property decorator that adds a ContextConsumer controller to the component which will try and retrieve a value for the property via the Context API.

Import

Signature

consume(__namedParameters): (protoOrDescriptor: TODO , name?: K) => void | any

Parameters

__namedParameters
{context: {__context__: ValueType}, subscribe: boolean}

A property decorator that adds a ContextConsumer controller to the component which will try and retrieve a value for the property via the Context API.

Import

Signature

provide(__namedParameters): (protoOrDescriptor: TODO , name?: K) => void | any

Parameters

__namedParameters
{context: {__context__: ValueType}}