src / mixin / update.js

Copyright (c) 2018 Florian Klampfer

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see

const hasCSSOM = "attributeStyleMap" in Element.prototype && "CSS" in window && CSS.number;

export const updateMixin = C =>
  class extends C {
    histId() {
      return || this.constructor.componentName;

Prepare and cleanup interaction

prepareInteraction causes various side effects before sliding the drawer.

Note that the drawer receives the hy-drawer-opened CSS class when it is opened. This class makes the drawer appear open by setting the CSS left (right) property, instead of an absoulte transform value. This way, the drawer’s width can change while it is open without having to recalculate translateX on every resize. However, it has to be removed before we move the drawer via translateX again.

    prepareInteraction() {
      if (hasCSSOM) {
        this.contentEl.attributeStyleMap.set("will-change", "transform");
        this.scrimEl.attributeStyleMap.set("will-change", "opacity");
      } else { = "transform"; = "opacity";

Cleanup code after a completed interaction. Will add/remove the beforementioned hy-drawer-opened class.

    cleanupInteraction(opened) {
      if (hasCSSOM) {

        if (opened) {
          this.scrimEl.attributeStyleMap.set("pointer-events", new CSSKeywordValue("all"));
        } else {
      } else { = ""; = "";

        if (opened) {
 = "all";
        } else {
 = "";

If the experimental back button feature is enabled we hack the history API, so that it matches the state of the drawer…

      if (this._backButton) {
        const id =;
        const hash = `#${id}--opened`;

        if (opened && window.location.hash !== hash) {
          window.history.pushState({ [id]: true }, document.title, hash);

        if (!opened
            && (window.history.state && window.history.state[])
            && window.location.hash !== '') {

Once we’re finished cleaning up, we fire the transitioned event.

      this.fireEvent("transitioned", { detail: opened });

Update DOM

In the end, we only modify two properties: The x-coordinate of the drawer, and the opacity of the scrim, which is handled by updateDOM.

    updateDOM(translateX) {
      this.translateX = translateX;

      const inv = this.align === "left" ? 1 : -1;
      const opacity = (this.opacity = (translateX / this.drawerWidth) * inv);

      if (this.moveCallback) this.moveCallback({ translateX, opacity });
      /* this.fireEvent("move", { detail: { translateX, opacity } }); */

      if (hasCSSOM) {
          new CSSTransformValue([new CSSTranslate(CSS.px(translateX), CSS.px(0))])
        this.scrimEl.attributeStyleMap.set("opacity", this.opacity);
      } else { = `translateX(${translateX}px)`; = this.opacity;

© 2018 Florian Klampfer

Powered by Hydejack v8.0.0