Concorde documentation (crawl) · interactive version

Concorde decorator — @ancestorAttribute. The @ancestorAttribute decorator automatically injects the value of an ancestor's attribute into a class property at the time of connectedCallback. Doc ID: docs/_decorators/ancestor-attribute. Keywords: Concorde, supersoniks, docs/_decorators/ancestor-attribute, ancestor-attribute, @ancestorAttribute, decorator, ancestorAttribute, connectedCallback, HTML.getAncestorAttributeValue, dataProvider, testAttribute, dynamic: false, formDataProvider, wordingProvider. URL: https://concorde.supersoniks.org/crawl/docs/_decorators/ancestor-attribute.html.

@ancestorAttribute

The @ancestorAttribute decorator automatically injects the value of an ancestor's attribute into a class property at the time of connectedCallback.

Principle

This decorator uses HTML.getAncestorAttributeValue to traverse up the DOM tree from the current element and find the first ancestor that has the specified attribute. The value of this attribute is then assigned to the decorated property.

Usage

Import

import { ancestorAttribute } from "@supersoniks/concorde/decorators";

Basic example

The component reads dataProvider and testAttribute from its ancestor wrapper. By default (dynamic: false), values are read once at connect.

import { html, LitElement } from "lit";
import { customElement } from "lit/decorators.js";
import { ancestorAttribute } from "@supersoniks/concorde/decorators";

@customElement("demo-ancestor-attribute")
export class DemoAncestorAttribute extends LitElement {
  @ancestorAttribute("dataProvider")
  dataProvider: string | null = null;

  @ancestorAttribute("testAttribute")
  testAttribute: string | null = null;

  render() {
    return html`
      <p>dataProvider: <strong>${this.dataProvider ?? "null"}</strong></p>
      <p>testAttribute: <strong>${this.testAttribute ?? "null"}</strong></p>
    `;
  }
}
<div dataProvider="demoDataProvider" testAttribute="test-value-123">
  <docs-demo-sources for="demo-ancestor-attribute"></docs-demo-sources>
  <demo-ancestor-attribute></demo-ancestor-attribute>
</div>

Use cases

This decorator is particularly useful for:

Behavior

Dynamic mode

Use { dynamic: true } when the decorated property must follow the DOM. With Lit, add @state() on the same property so template updates are visible when the observer assigns a new value.

The live demo below shows only the resolved attribute value — no store, no reparenting.

@customElement("demo-ancestor-attribute-dynamic")
export class DemoAncestorAttributeDynamic extends LitElement {
  @ancestorAttribute("dataProvider", { dynamic: true })
  @state()
  dataProvider: string | null = null;

  render() {
    return html`
      &lt;p&gt;Nearest ancestor: &lt;strong&gt;${this.dataProvider ?? "null"}&lt;/strong&gt;&lt;/p&gt;
    `;
  }
}

Try in the demo:

<docs-demo-sources for="demo-ancestor-attribute-dynamic-section"></docs-demo-sources>
    <demo-ancestor-attribute-dynamic-section></demo-ancestor-attribute-dynamic-section>

Static mode (default) — read once at connect, no observer:

@ancestorAttribute("dataProvider")
dataProvider: string | null = null;

Notes