<link rel="stylesheet" href="/_merged_assets/_static/search/noscript.css">
Apollo Elements Apollo Elements Guides API Blog Toggle darkmode

Class Mixins: ControllerHostMixin

Mixin which adds the ReactiveController interface to your element. You can use this to render updates from your controllers by implementing an update method that calls super.update().

import { ControllerHostMixin } from '@apollo-elements/mixins/controller-host-mixin';
import { MouseController } from './mouse-controller.js';
const template = document.createElement('template');
      template.innerHTML = `
        %3Clink rel="stylesheet" href="color-picker.css">
        <div id="loupe"><div id="cursor">⊹</div></div>
      `;

class ColorPicker extends ControllerHostMixin(HTMLElement) {
  mouse = new MouseController(this);

  constructor() {
    super();
    this
      .attachShadow({ mode: 'open' })
      .append(template.content.cloneNode(true));
    this.loupe = this.shadowRoot.getElementById('loupe');
    this.cursor = this.shadowRoot.getElementById('cursor');
    this.addEventListener('click', () => {
      this.pick();
      this.cursor.animate({ scale: ['100%', '120%', '100%'], easing: 'ease-in-out' }, 300);
    });
  }

  update() {
    const [x, y] = this.mouse.pos;
    const { clientWidth, clientHeight } = document.documentElement;
    const hue = Math.floor((x / clientWidth) * 360);
    const saturation = 100 - Math.floor((y / clientHeight) * 100);
    this.style.setProperty('--x', `${x}px`);
    this.style.setProperty('--y', `${y}px`);
    this.style.setProperty('--hue', hue);
    this.style.setProperty('--saturation', `${saturation}%`);
    if (this.mouse.down)
      this.pick();
    super.update();
  }

  async pick() {
    await this.updateComplete;
    this.dispatchEvent(new CustomEvent('pick', {
      bubbles: true,
      detail: getComputedStyle(this.loupe).getPropertyValue('background-color')
    }));
  }
};

customElements.define('color-picker', ColorPicker);

Signature

ControllerHostMixin

Parameters

superclass

T

Properties

updateComplete

Promise<boolean>

Methods

updated

Parameters

args

any[]

update

Parameters

args

any[]

requestUpdate

Returns

void

removeController

Parameters

controller

ReactiveController

Returns

void

addController

Parameters

controller

ReactiveController

Returns

void
Private API

Private Properties

private

#updatePending

boolean
private

#updateComplete


private

#resolve

(v: boolean) => void
private

#controllers


Private Methods

private

doUpdate

Exports

import { ControllerHostMixin } from '@apollo-elements/mixins/controller-host-mixin';