Skip to content

Commit 47e840f

Browse files
committed
Fix @Local
1 parent 59dfbba commit 47e840f

File tree

9 files changed

+100
-51
lines changed

9 files changed

+100
-51
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ Web Components with a very basic wrapper. The only magic is...
4444
- Calls `this.render()` on change.
4545
- `@Part() $foo: HTMLDivElement;` = `<div part="foo"></div>`
4646
- `this.$part.innerText = 'Hello World!';`
47-
- `@Local('default') foo;` Shorthand for localStorage.
47+
- `@Local('store') foo = Map([['key', true]]);` Shorthand for localStorage.
4848

4949
Learn More: https://github.com/Pictogrammers/Element

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"remarkable": "^2.0.1"
2727
},
2828
"devDependencies": {
29-
"@pictogrammers/element": "0.0.52",
29+
"@pictogrammers/element": "0.0.54",
3030
"@pictogrammers/element-jest": "^0.1.8",
3131
"@pictogrammers/element-webpack": "^0.1.48",
3232
"cross-env": "^7.0.3",

src/pg/buttonToggle/__examples__/basic/basic.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Part, Prop } from '@pictogrammers/element';
1+
import { Component, Part } from '@pictogrammers/element';
22
import PgButtonToggle from '../../buttonToggle';
33

44
import template from './basic.html';
@@ -18,6 +18,6 @@ export default class XPgButtonToggleBasic extends HTMLElement {
1818

1919
handleClick(e) {
2020
const { active } = e.detail;
21-
this.$value.innerText = `${active}`;
21+
this.$value.textContent = `${active}`;
2222
}
2323
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<div class="example">
2+
<p>click to toggle and persist state in LocalStorage</p>
3+
<pg-button-toggle part="button">
4+
<pg-icon slot="inactive" path="M7.41,8.58L12,13.17L16.59,8.58L18,10L12,16L6,10L7.41,8.58Z"></pg-icon>
5+
<pg-icon slot="active" path="M7.41,15.41L12,10.83L16.59,15.41L18,14L12,8L6,14L7.41,15.41Z"></pg-icon>
6+
</pg-button-toggle>
7+
<div>
8+
<code>button onclick: <code part="value"></code></code>
9+
</div>
10+
</div>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Component, Local, Part } from '@pictogrammers/element';
2+
import PgButtonToggle from '../../buttonToggle';
3+
4+
import template from './persist.html';
5+
6+
@Component({
7+
selector: 'x-pg-button-toggle-persist',
8+
template
9+
})
10+
export default class XPgButtonTogglePersist extends HTMLElement {
11+
12+
@Part() $button: PgButtonToggle;
13+
@Part() $value: HTMLSpanElement;
14+
15+
// Get shared example store
16+
@Local('example') #store = new Map<string, any>([
17+
// example:toggle defaults to false
18+
['toggle', false]
19+
]);
20+
21+
connectedCallback() {
22+
// replace with toggle event??? and throw for click events
23+
this.$button.addEventListener('click', this.#handleClick.bind(this));
24+
if (this.#store.get('toggle')) {
25+
this.$button.active = true;
26+
this.$value.textContent = 'true';
27+
}
28+
}
29+
30+
#handleClick(e: any) {
31+
const { active } = e.detail;
32+
this.$value.textContent = `${active}`;
33+
this.#store.set('toggle', active);
34+
}
35+
}

src/pg/database/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Out of all the components this is the most useful one as it controls the local cache of icon data. From a high level this component gives access to a `DatabaseService` instance.
44

55
```typescript
6-
import '@pictogrammers/components/pgAnnoy.js';
6+
import '@pictogrammers/components/pgDatabase.js';
77
```
88

99
## Usage

src/pg/menuIcon/menuIcon.ts

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ export default class PgMenuIcon extends HTMLElement {
3838
@Part() $colorPicker: any;
3939
@Part() $colorHexRgb: any;
4040

41-
@Local() cachePngColor: string = '#000000';
42-
@Local() cachePngSize: string = '24';
43-
@Local() cacheSvgColor: string = '#000000';
41+
@Local('store') store = new Map<string, any>([
42+
['cachePngColor', '#000000'],
43+
['cachePngSize', 24],
44+
['cacheSvgColor', '#000000']
45+
]);
4446

4547
color = 'svg';
4648
@Prop() currentIndex: number = 0;
@@ -50,19 +52,19 @@ export default class PgMenuIcon extends HTMLElement {
5052
// Wire Up Context Menu
5153
this.$copyIconName.addEventListener('click', this.handleCopyIconName.bind(this));
5254
this.$svgBlack.addEventListener('click', () => {
53-
this.cacheSvgColor = '#000000';
55+
this.store.set('cacheSvgColor', '#000000');
5456
this.render();
5557
});
5658
this.$svgWhite.addEventListener('click', () => {
57-
this.cacheSvgColor = '#FFFFFF';
59+
this.store.set('cacheSvgColor', '#FFFFFF');
5860
this.render();
5961
});
6062
let preventSvgColor = false;
6163
this.$svgColor.addEventListener('click', () => {
6264
if (preventSvgColor) { preventSvgColor = false; return; }
6365
this.color = 'svg';
64-
this.$colorPicker.value = this.cacheSvgColor;
65-
this.$colorHexRgb.value = this.cacheSvgColor;
66+
this.$colorPicker.value = this.store.get('cacheSvgColor');
67+
this.$colorHexRgb.value = this.store.get('cacheSvgColor');
6668
const self = this;
6769
//createPopper(this.$svgColor, this.$color, {
6870
// placement: 'bottom-start'
@@ -83,19 +85,19 @@ export default class PgMenuIcon extends HTMLElement {
8385
document.addEventListener('mousedown', handleMouseDown);
8486
});
8587
this.$pngBlack.addEventListener('click', () => {
86-
this.cachePngColor = '#000000';
88+
this.store.set('cachePngColor', '#000000');
8789
this.render();
8890
});
8991
this.$pngWhite.addEventListener('click', () => {
90-
this.cachePngColor = '#FFFFFF';
92+
this.store.set('cachePngColor', '#FFFFFF');
9193
this.render();
9294
});
9395
let preventPngColor = false;
9496
this.$pngColor.addEventListener('click', () => {
9597
if (preventPngColor) { preventPngColor = false; return; }
9698
this.color = 'png';
97-
this.$colorPicker.value = this.cachePngColor;
98-
this.$colorHexRgb.value = this.cachePngColor;
99+
this.$colorPicker.value = this.store.get('cachePngColor');
100+
this.$colorHexRgb.value = this.store.get('cachePngColor');
99101
const self = this;
100102
//createPopper(this.$pngColor, this.$color, {
101103
// placement: 'bottom-start'
@@ -116,26 +118,26 @@ export default class PgMenuIcon extends HTMLElement {
116118
document.addEventListener('mousedown', handleMouseDown);
117119
});
118120
this.$png24.addEventListener('click', () => {
119-
this.cachePngSize = '24';
121+
this.store.set('cachePngSize', 24);
120122
this.render();
121123
});
122124
this.$png36.addEventListener('click', () => {
123-
this.cachePngSize = '36';
125+
this.store.set('cachePngSize', 36);
124126
this.render();
125127
});
126128
this.$png48.addEventListener('click', () => {
127-
this.cachePngSize = '48';
129+
this.store.set('cachePngSize', 48);
128130
this.render();
129131
});
130132
this.$png96.addEventListener('click', () => {
131-
this.cachePngSize = '96';
133+
this.store.set('cachePngSize', 96);
132134
this.render();
133135
});
134136
this.$svgDownload.addEventListener('click', () => {
135-
alert(`SVG ${this.cacheSvgColor}`);
137+
alert(`SVG ${this.store.get('cacheSvgColor')}`);
136138
});
137139
this.$pngDownload.addEventListener('click', () => {
138-
alert(`SVG ${this.cachePngSize} ${this.cachePngColor}`);
140+
alert(`SVG ${this.store.get('cachePngSize')} ${this.store.get('cachePngColor')}`);
139141
});
140142
this.$copySvgInline.addEventListener('click', () => {
141143
const icon = this.icon;
@@ -214,16 +216,19 @@ export default class PgMenuIcon extends HTMLElement {
214216

215217
render() {
216218
// Context Menu
217-
this.$svgBlack.classList.toggle('active', this.cacheSvgColor === '#000000');
218-
this.$svgWhite.classList.toggle('active', this.cacheSvgColor === '#FFFFFF');
219-
this.$svgColor.classList.toggle('active', this.cacheSvgColor !== '#000000' && this.cacheSvgColor !== '#FFFFFF');
220-
this.$pngBlack.classList.toggle('active', this.cachePngColor === '#000000');
221-
this.$pngWhite.classList.toggle('active', this.cachePngColor === '#FFFFFF');
222-
this.$pngColor.classList.toggle('active', this.cachePngColor !== '#000000' && this.cachePngColor !== '#FFFFFF');
223-
this.$png24.classList.toggle('active', this.cachePngSize === '24');
224-
this.$png36.classList.toggle('active', this.cachePngSize === '36');
225-
this.$png48.classList.toggle('active', this.cachePngSize === '48');
226-
this.$png96.classList.toggle('active', this.cachePngSize === '96');
219+
const cacheSvgColor = this.store.get('cacheSvgColor');
220+
const cachePngColor = this.store.get('cachePngColor');
221+
const cachePngSize = this.store.get('cachePngSize');
222+
this.$svgBlack.classList.toggle('active', cacheSvgColor === '#000000');
223+
this.$svgWhite.classList.toggle('active', cacheSvgColor === '#FFFFFF');
224+
this.$svgColor.classList.toggle('active', cacheSvgColor !== '#000000' && cacheSvgColor !== '#FFFFFF');
225+
this.$pngBlack.classList.toggle('active', cachePngColor === '#000000');
226+
this.$pngWhite.classList.toggle('active', cachePngColor === '#FFFFFF');
227+
this.$pngColor.classList.toggle('active', cachePngColor !== '#000000' && cachePngColor !== '#FFFFFF');
228+
this.$png24.classList.toggle('active', cachePngSize === 24);
229+
this.$png36.classList.toggle('active', cachePngSize === 36);
230+
this.$png48.classList.toggle('active', cachePngSize === 48);
231+
this.$png96.classList.toggle('active', cachePngSize === 96);
227232
this.$colorPicker.addEventListener('select', this.handleColorSelect.bind(this));
228233
this.$colorHexRgb.addEventListener('change', this.handleHexRgbChange.bind(this));
229234
this.syncEyedropper();
@@ -232,10 +237,10 @@ export default class PgMenuIcon extends HTMLElement {
232237
handleColorSelect(e) {
233238
switch(this.color) {
234239
case 'svg':
235-
this.cacheSvgColor = e.detail.hex;
240+
this.store.set('cacheSvgColor', e.detail.hex);
236241
break;
237242
case 'png':
238-
this.cachePngColor = e.detail.hex;
243+
this.store.set('cachePngColor', e.detail.hex);
239244
break;
240245
}
241246
this.$colorHexRgb.value = e.detail.hex;
@@ -245,24 +250,26 @@ export default class PgMenuIcon extends HTMLElement {
245250
handleHexRgbChange(e) {
246251
switch(this.color) {
247252
case 'svg':
248-
this.cacheSvgColor = e.detail.hex;
253+
this.store.set('cacheSvgColor', e.detail.hex);
249254
break;
250255
case 'png':
251-
this.cachePngColor = e.detail.hex;
256+
this.store.set('cachePngColor', e.detail.hex);
252257
break;
253258
}
254259
this.$colorPicker.value = e.detail.hex;
255260
this.syncEyedropper();
256261
}
257262

258263
syncEyedropper() {
259-
if (this.cachePngColor !== '#000000' && this.cachePngColor !== '#FFFFFF') {
260-
this.$pngColor.style.color = this.cachePngColor;
264+
const cachePngColor = this.store.get('cachePngColor');
265+
if (cachePngColor !== '#000000' && cachePngColor !== '#FFFFFF') {
266+
this.$pngColor.style.color = cachePngColor;
261267
} else {
262268
this.$pngColor.style.color = 'transparent';
263269
}
264-
if (this.cacheSvgColor !== '#000000' && this.cacheSvgColor !== '#FFFFFF') {
265-
this.$svgColor.style.color = this.cacheSvgColor;
270+
const cacheSvgColor = this.store.get('cacheSvgColor');
271+
if (cacheSvgColor !== '#000000' && cacheSvgColor !== '#FFFFFF') {
272+
this.$svgColor.style.color = cacheSvgColor;
266273
} else {
267274
this.$svgColor.style.color = 'transparent';
268275
}

src/pg/modalAlert/modalAlert.css

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,5 @@ footer {
3636
border-top: 1px solid #ccc;
3737
background: #f1f1f1;
3838
justify-content: flex-end;
39-
}
40-
41-
[part="no"] {
42-
margin-right: 0.5rem;
39+
gap: 0.5rem;
4340
}

0 commit comments

Comments
 (0)