Skip to content

Commit 46009d9

Browse files
committed
feat: refactor + convert pl-toggle-layout to lit-element
1 parent aff1777 commit 46009d9

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { LitElement, html, customElement } from 'lit-element';
2+
import { store } from '../../store.js'; // connect to the Redux store.
3+
import { updateLayoutMode } from '../../actions/app.js'; // redux actions
4+
import './pl-toggle-layout.scss?external';
5+
6+
@customElement('pl-toggle-layout')
7+
class LayoutToggle extends LitElement {
8+
constructor(self) {
9+
self = super(self);
10+
self.handleClick = self.handleClick.bind(self);
11+
return self;
12+
}
13+
14+
createRenderRoot() {
15+
return this;
16+
}
17+
18+
connectedCallback() {
19+
if (super.connectedCallback) {
20+
super.connectedCallback();
21+
}
22+
23+
const state = store.getState();
24+
this.layoutMode = state.app.layoutMode || 'vertical';
25+
26+
this.__storeUnsubscribe = store.subscribe(() =>
27+
this._stateChanged(store.getState())
28+
);
29+
this._stateChanged(store.getState());
30+
31+
this.size = this.size || 'medium';
32+
}
33+
34+
disconnectedCallback() {
35+
this.__storeUnsubscribe && this.__storeUnsubscribe();
36+
37+
if (super.disconnectedCallback) {
38+
super.disconnectedCallback();
39+
}
40+
}
41+
42+
static get properties() {
43+
return {
44+
layoutMode: {
45+
attribute: true,
46+
type: String,
47+
},
48+
text: {
49+
attribute: true,
50+
type: String,
51+
},
52+
size: {
53+
attribute: true,
54+
type: String,
55+
},
56+
iconOnly: {
57+
attribute: 'icon-only',
58+
type: Boolean,
59+
reflect: true,
60+
},
61+
};
62+
}
63+
64+
_stateChanged(state) {
65+
if (this.layoutMode !== state.app.layoutMode) {
66+
this.layoutMode = state.app.layoutMode;
67+
}
68+
}
69+
70+
handleClick() {
71+
const getLayoutMode =
72+
this.layoutMode !== 'vertical' ? 'vertical' : 'horizontal';
73+
74+
this.layoutMode = getLayoutMode;
75+
store.dispatch(updateLayoutMode(this.layoutMode));
76+
}
77+
78+
render() {
79+
return html`
80+
<pl-button
81+
title="Switch Layout"
82+
@click="${this.handleClick}"
83+
size="${this.size}"
84+
?icon-only="${this.iconOnly || false}"
85+
>
86+
${this.text}
87+
<pl-icon
88+
slot="after"
89+
name="${this.layoutMode === 'horizontal' ? 'layout-h' : 'layout-v'}"
90+
></pl-icon>
91+
</pl-button>
92+
`;
93+
}
94+
}
95+
96+
export { LayoutToggle };
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@import '../../../sass/scss/core.scss';
2+
3+
pl-toggle-layout {
4+
display: none;
5+
align-self: center;
6+
justify-content: center;
7+
align-items: center;
8+
z-index: 10;
9+
width: 100%;
10+
cursor: pointer;
11+
12+
@media all and (min-width: $pl-bp-med) {
13+
display: flex;
14+
}
15+
}
16+
17+
.pl-c-toggle-layout,
18+
.pl-c-toggle-layout__action {
19+
width: 100%;
20+
}

0 commit comments

Comments
 (0)