:context Directive

Set component properties or use an object as the context for an element and its children.

On a component, :context updates its properties from an object, including properties that are not declared in props.

On other tags, it uses the object as the context for that element and its children.

r-context is the default alias and works the same way.

:context / r-contextComposition

Set component properties or use an object as the context for an element and its children.

Try it live

Set component propertiesLive Regor
CHILD CONTEXT / OBJECT INPUTRegor release

Editing is available.

import { defineComponent, html, ref } from 'regor'

export function createContext(interactive = false) {
  const ReleaseCard = defineComponent(
    html`<article class="directive-card directive-context-card">
      <span class="guide-kicker">CHILD CONTEXT / OBJECT INPUT</span
      ><strong>{{ title || 'Untitled release' }}</strong>
      <p>
        {{ editable ? 'Editing is available.' : 'This release is read-only.' }}
      </p>
    </article>`,
    {
      context: () => ({ title: ref(''), editable: ref(false) }),
    },
  )
  return {
    interactive,
    title: ref('Regor release'),
    editable: ref(true),
    components: { ReleaseCard },
  }
}

export const contextTemplate = html` <div class="guide-demo guide-demo--split">
  <div class="guide-controls">
    <label for="context-title">Parent title</label
    ><input
      id="context-title"
      type="text"
      maxlength="50"
      r-model="title"
      :value="title"
      :disabled="!interactive"
    /><label class="guide-check"
      ><input
        type="checkbox"
        r-model="editable"
        :checked="editable"
        :disabled="!interactive"
      />
      Allow editing</label
    >
  </div>
  <ReleaseCard :context="{ title: title, editable: editable }" />
</div>`
import { createApp, useScope } from 'regor'
import { directiveExamples } from './examples'

const element = document.getElementById('directive-demo')
const name = element?.dataset.example
if (element && name && Object.hasOwn(directiveExamples, name)) {
  const example = directiveExamples[name as keyof typeof directiveExamples]
  createApp<object>(
    useScope<object>(() => example.create(true)),
    { element, template: example.template },
  )
}

Edit the title and permissions to update the component.

Using :context on components

<MyComponent :context="{ title: pageTitle, theme: activeTheme }"></MyComponent>
<MyComponent r-context="{ title: pageTitle, theme: activeTheme }"></MyComponent>

The component can use title and theme. When pageTitle or activeTheme changes, the corresponding component properties update.

Using :context on normal tags

Use :context="user" to write name instead of user.name on that element and inside it. r-context works the same way.

<div :context="user">
  <span>{{ name }}</span>
</div>

Here, name means user.name. Elements outside the div keep their own context.

The object is chosen once when the element is bound. Its ref properties remain reactive.

Choosing a binding

Use :context to set several component properties at once or to avoid repeating an object name in child bindings.

Use :prop-name="value" or r-bind:prop-name="value" to bind a single property declared in the component's props list.

Use r-bind="{...}" or :="{...}" to set element attributes.

Combining :context with declared props

<UserCard
  :user-id="selectedUserId"
  :context="{ badge: userBadge, canEdit: isAdmin }"
></UserCard>
const UserCard = defineComponent('<section>...</section>', {
  props: ['userId'],
  context: (head) => ({
    userId: head.props.userId,
    badge: head.props.badge,
    canEdit: head.props.canEdit,
  }),
})

In this example:

  • userId comes from declared prop binding (:user-id)
  • badge and canEdit come from :context

Back to the directives

Context and attributes

Use :context for component properties or to choose the context for an element. Use r-bind to set attributes.

Keep exploring Select components dynamically.