Skip to content

Commit

Permalink
feat: refactor + convert pl-toggle-theme to lit-element
Browse files Browse the repository at this point in the history
  • Loading branch information
sghoweri committed Oct 20, 2019
1 parent 46009d9 commit 95a3b21
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* eslint-disable no-unused-vars, no-param-reassign */
import { LitElement, html, customElement } from 'lit-element';
import { store } from '../../store.js'; // connect to the Redux store.
import { updateThemeMode } from '../../actions/app.js'; // redux actions needed
import './pl-toggle-theme.scss?external';

@customElement('pl-toggle-theme')
class ThemeToggle extends LitElement {
constructor(self) {
self = super(self);
self.targetOrigin =
window.location.protocol === 'file:'
? '*'
: window.location.protocol + '//' + window.location.host;
return self;
}

static get properties() {
return {
themeMode: {
attribute: true,
type: String,
},
};
}

createRenderRoot() {
return this;
}

connectedCallback() {
if (super.connectedCallback) {
super.connectedCallback();
}

const state = store.getState();
this.themeMode = state.app.themeMode || 'dark';

this.__storeUnsubscribe = store.subscribe(() =>
this._stateChanged(store.getState())
);
this._stateChanged(store.getState());

store.dispatch(updateThemeMode(this.themeMode));
}

disconnectedCallback() {
this.__storeUnsubscribe && this.__storeUnsubscribe();

if (super.disconnectedCallback) {
super.disconnectedCallback();
}
}

_stateChanged(state) {
this.themeMode = state.app.themeMode;
this.iframeElement = document.querySelector('.pl-js-iframe');

if (this.iframeElement) {
const obj = JSON.stringify({
event: 'patternLab.stateChange',
state,
});
this.iframeElement.contentWindow.postMessage(obj, this.targetOrigin);
}
}

render() {
const toggleThemeMode = this.themeMode !== 'dark' ? 'dark' : 'light';
return html`
<pl-button
class="pl-c-tools__action pl-c-toggle-theme__action"
title="Switch Theme"
@click="${_ => store.dispatch(updateThemeMode(toggleThemeMode))}"
>
Switch Theme
<pl-icon slot="after" name="theme-${this.themeMode}"></pl-icon>
</pl-button>
`;
}
}

export { ThemeToggle };
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@import '../../../sass/scss/core.scss';

pl-toggle-theme {
display: flex;
align-self: center;
justify-content: center;
align-items: center;
z-index: 10;
width: 100%;
cursor: pointer;
}

.pl-c-toggle-theme,
.pl-c-toggle-theme__action {
width: 100%;
}

0 comments on commit 95a3b21

Please sign in to comment.