Skip to content

Instantly share code, notes, and snippets.

@Heydon
Last active April 22, 2021 08:03
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Heydon/63c285842bf89860895fee9e4169896d to your computer and use it in GitHub Desktop.
Save Heydon/63c285842bf89860895fee9e4169896d to your computer and use it in GitHub Desktop.
/**
* @module holy-albatross
* @description
* A custom element for switching between horizontal and vertical layouts
* using Flexbox, where the switch property defines the minimum container width
* for the horizontal layout
* @property {string} switch A CSS width value
* @property {string} margin A CSS margin value
*/
export default class Albatross extends HTMLElement {
constructor() {
super();
this.observed = ['switch', 'margin'];
this.observed.forEach(attr => {
Object.defineProperty(this, attr, {
get: function () {
return this.getAttribute(attr);
},
set: function (val) {
return this.setAttribute(attr, val);
}
});
});
this.attachShadow({ mode: 'open' });
this.render = () => {
this.shadowRoot.innerHTML = `
<style>
:host {
${this.margin && `margin: calc((${this.margin} / 2) * -1);`}
}
::slotted(*) {
${this.switch && `flex-basis: calc((${this.switch} - 100%) * 999) !important;`}
${this.margin && `margin: calc(${this.margin} / 2);`}
}
</style>
<slot></slot>
`;
}
}
static get observedAttributes() {
return ['switch', 'margin'];
}
attributeChangedCallback() {
this.render();
}
}
customElements.define('holy-albatross', Albatross);
@AutoSponge
Copy link

This should work, right? this.observed = Albatross.observedAttributes;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment