Class

Component

component~Component()

// Unminified
import Component from 'https://fancy-pants.js.org/component.js';

// Minified
import Component from 'https://fancy-pants.js.org/min/component.js';

This is the main class for using the FancyPants system. When extended it will provide easy means for tracking dirty changes and rendering dynamic parts of the custom element's DOM. You have access to both a ShadowDOM and the HTMLElement itself.

You don't need to instantiate it yourself as this is done by the insertion of the custom element into the DOM.

Use the .register() method to define the custom element and associate a template to the custom element.

Constructor

# new Component()

Perform any setup in the constructor. This is a good place to assign tracked() properties when targeting Mobile Safari browsers.

import Component from './fancy-pants/component.js';

class MyComponent extends Component {
  constructor() {
    super();
    this.foobar = tracked();
  }
}

MyComponent.register();

If you do not need to support Mobile Safari then you can use class field syntax instead.

class MyComponent extends Component {
  foobar = tracked();
}

View Source component.js, line 90

Members

string

# static shadow

Optional string version of the Shadow DOM template. Defaults to null.

class MyComponent extends Component {
  static get shadow() {
    return `<p>lorem ipsum</p>`;
  }
}

View Source component.js, line 355

string

# static tagName

Optional tag name. Defaults to a dasherized version of the class name

class MyComponent extends Component {
  static get tagName() {
    return 'my-alternative-component-tag-name';
  }
}

View Source component.js, line 337

string

# static template

Optional string version of the innerHTML template. Defaults to null. Return null to disable appendChild on construction.

class MyComponent extends Component {
  static get template() {
    return `<p>lorem ipsum</p>`;
  }
}

View Source component.js, line 374

Methods

# static register(templatableopt, queryableopt)

Call this static method to define the component as a custom element with the browser.

class MyComponent extends Component {
  …
}

MyComponent.register();
MyComponent.register('#selector');
MyComponent.register(myTemplateNode);

When defining a template, register will look for a <template> element that matches the kabob-case of the component's class name. The shadow DOM can be used instead by adding the data-shadow attribute to the template element.

Parameters:
Name Type Attributes Default Description
templatable HTMLElement | string | undefined <optional>

optional template element or selector

queryable Queryable <optional>
document

optional scope for the template selector

View Source component.js, line 310

# attributeChangedCallback()

This will auto-track any attributes listed in observedAttributes.

class MyComponent extends Component {
  static get observedAttributes() {
    return ['foo', 'bar'];
  }
}

View Source component.js, line 195

# connectedCallback()

Here is a good place to add event listeners to the DOM. Activates tracking and registers this component instance with the renderer. First render is scheduled after this callback.

class MyComponent extends Component {
  connectedCallback() {
    super.connectedCallback();
    this.clickHandler = () => this.doSomething();
    this.shadow.querySelector('button')
      .addEventListener('click', this.clickHandler);
  }
}

View Source component.js, line 148

# disconnectedCallback()

Here is a good place to remove event listeners to the DOM. Removes this component instance from the renderer.

class MyComponent extends Component {
  disconnectedCallback() {
    super.disconnectedCallback();
    this.shadow.querySelector('button')
      .removeEventListener('click', this.clickHandler);
  }
}

View Source component.js, line 180

# render(attrs)

When this component needs to render this function will be called.

class MyComponent extends Component {
  render() {
    this.shadow.querySelector('.foo').textContent = this.foobar;
  }
}
Parameters:
Name Type Description
attrs object

an object with all values which have been yielded by parent components index by their camelized name or id of the respective components

View Source component.js, line 243

# track() → {this}

Activate tracking on this component. Called automatically during .connectedCallback() Use this if you want to assign to a tracked property prior to being appended to the DOM.

let myComponent = document.createElement('my-component').track();
myComponent.foobar = 'This is now tracked';
document.body.appendChild(myComponent);

View Source component.js, line 223

this

# yields()

Called during child components' rendering to ask for any yielded values. This is a method to pass tracked values to other components in it. Most cases this will be a component that has null for the shadow and template but it isn't required. The render() and the yields() are two separate ways to react to tracked changes but they can be used together. Anything returned from this hook will be passed to any child component's render() hook as named argument.

<my-provider>
  <my-presenter></my-presenter>
</my-provider>
class MyProvider extends Component {
  trackedValue = tracked();
  static get shadow() { return null; }
  yields() {
    return { yieldedValue: this.trackedValue };
  }
}
MyProvider.register();

class MyPresenter extends Component {
  render(attrs) {
    let { yieldedValue } = attrs.myProvider;
    this.shadow.textContent = yieldedValue;
  }
}
MyPresenter.register();
Tutorials:

View Source component.js, line 282

any