Skip to content

Commit 9ade41c

Browse files
committedMay 6, 2023
Colors work properly
1 parent fbc088f commit 9ade41c

File tree

3 files changed

+64
-28
lines changed

3 files changed

+64
-28
lines changed
 

‎package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{
2-
"name": "newsightwords",
2+
"name": "colourgrid",
33
"version": "1.0.0",
4-
"description": "Sight Words",
4+
"description": "Colour Grid",
55
"main": "index.js",
66
"repository": {
77
"type": "git",
8-
"url": "git+https://github.com/tapted/newsightwords.git"
8+
"url": "git+https://github.com/tapted/colourgrid.git"
99
},
1010
"author": "",
1111
"license": "ISC",
1212
"bugs": {
13-
"url": "https://github.com/tapted/newsightwords/issues"
13+
"url": "https://github.com/tapted/colourgrid/issues"
1414
},
15-
"homepage": "https://github.com/tapted/newsightwords#readme",
15+
"homepage": "https://github.com/tapted/colourgrid#readme",
1616
"dependencies": {
1717
"lit": "^2.7.3",
1818
"lit-element": "^3.3.2",

‎src/elements/board.ts

+18-7
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ export class EBoard extends LitElement {
2222
}
2323
fieldset {
2424
border: none;
25-
display: flex;
26-
width: 90vw;
25+
display: grid;
26+
min-width: 80%;
27+
max-width: 100%;
28+
grid-template-columns: repeat(6, minmax(0, 1fr));
2729
}
2830
e-slider {
2931
width: 80vw;
@@ -40,15 +42,16 @@ export class EBoard extends LitElement {
4042
display: inline-block;
4143
background: #ddd;
4244
border-radius: 1em;
43-
border: 1px solid grey;
45+
border: 1px solid #bbb;
4446
border-bottom: solid 4px #bbb;
4547
padding: 10px;
4648
margin-top: 0;
4749
vertical-align: bottom;
4850
cursor: pointer;
4951
font-family: sans-serif;
5052
text-overflow: ellipsis;
51-
font-size: 12px;
53+
font-size: 14px;
54+
font-weight: 800;
5255
overflow: hidden;
5356
whitespace: nowrap;
5457
margin: 4px;
@@ -68,13 +71,21 @@ export class EBoard extends LitElement {
6871
this.grid.size = this.slider.value;
6972
}
7073

71-
private renderColorButton(color: string) {
74+
private onColorChange(color: string) {
75+
this.grid.color = color;
76+
}
77+
78+
private renderColorButton(color: string, textColor = 'black') {
7279
const style = {
7380
backgroundColor: color,
81+
color: textColor,
7482
};
7583
return html`
7684
<label class="e-button">
77-
<input type="radio" name="color-button">
85+
<input
86+
@change=${() => this.onColorChange(color)}
87+
type="radio"
88+
name="color-button">
7889
<span class="button-label" style=${styleMap(style)}>
7990
${color}
8091
</span>
@@ -86,7 +97,7 @@ export class EBoard extends LitElement {
8697
<e-slider min=1 max=30 value=10 @slider-input=${this.onSizeChange}>
8798
</e-slider>
8899
<fieldset>
89-
${COLORS.map((c) => this.renderColorButton(c))}
100+
${COLORS.map((c) => this.renderColorButton(...c))}
90101
</fieldset>
91102
<e-grid></e-grid>
92103
`;

‎src/elements/grid.ts

+41-16
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@ import {customElement, property} from 'lit/decorators.js';
33
import {html} from 'lit-html';
44
import {styleMap} from 'lit-html/directives/style-map.js';
55

6-
export const COLORS = [
7-
'white',
8-
'red',
9-
'orange',
10-
'yellow',
11-
'green',
12-
'blue',
13-
'indigo',
14-
'violet',
15-
'grey',
16-
'black',
17-
'lightgrey',
6+
export const COLORS: Array<[string, string]> = [
7+
['white', 'black'],
8+
['red', 'black'],
9+
['orange', 'black'],
10+
['yellow', 'black'],
11+
['green', 'black'],
12+
['blue', 'black'],
13+
['indigo', 'white'],
14+
['violet', 'black'],
15+
['grey', 'black'],
16+
['black', 'white'],
17+
['lightgrey', 'black'],
18+
['cyan', 'black'],
1819
];
1920

2021
@customElement('e-grid')
2122
export class EGrid extends LitElement {
23+
@property({attribute: false}) color = '';
2224
@property({attribute: false})
2325
get size(): number {
2426
return Number(this.style.getPropertyValue('--size')) || 8;
@@ -31,6 +33,8 @@ export class EGrid extends LitElement {
3133

3234
colors: number[] = [];
3335

36+
private lastMoveIndex = -1;
37+
3438
constructor() {
3539
super();
3640
this.size = 5;
@@ -43,6 +47,7 @@ export class EGrid extends LitElement {
4347
column-count: var(--size, 8);
4448
column-gap: 0;
4549
border: 1px solid black;
50+
user-select: none;
4651
}
4752
div {
4853
border: 1px solid black;
@@ -53,20 +58,40 @@ export class EGrid extends LitElement {
5358
`;
5459
}
5560

61+
private getColorIndex(index: number): number {
62+
const colorIndex = COLORS.findIndex(([c,]) => this.color == c);
63+
if (colorIndex >= 0) {
64+
return colorIndex;
65+
}
66+
return ((this.colors[index] ?? 0) + 1) % COLORS.length;
67+
}
68+
69+
private onCellMove(event: PointerEvent, index: number) {
70+
if (!event.isPrimary || index === this.lastMoveIndex) {
71+
return;
72+
}
73+
if (event.pointerType === 'mouse' && event.buttons === 0) {
74+
return;
75+
}
76+
this.lastMoveIndex = index;
77+
this.onCellClick(index);
78+
}
79+
5680
private onCellClick(index: number) {
57-
const value = ((this.colors[index] ?? 0) + 1) % COLORS.length;
58-
this.colors[index] = value;
81+
this.colors[index] = this.getColorIndex(index);
5982
this.requestUpdate();
6083
}
6184

6285
private renderCell(index: number, colorIndex: number) {
6386
const style = {
64-
backgroundColor: COLORS[colorIndex],
87+
backgroundColor: COLORS[colorIndex][0],
6588
};
6689
return html`
6790
<div
91+
@pointermove=${(e: PointerEvent) => this.onCellMove(e, index)}
6892
@click=${() => this.onCellClick(index)}
69-
style=${styleMap(style)}></div>`;
93+
style=${styleMap(style)}
94+
draggable="false"></div>`;
7095
}
7196

7297
override render() {

0 commit comments

Comments
 (0)