@rogieking/figui3 1.1.5 → 1.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/fig.js +68 -73
  2. package/package.json +1 -1
package/fig.js CHANGED
@@ -1112,24 +1112,18 @@ window.customElements.define("fig-field", FigField);
1112
1112
 
1113
1113
  /* Color swatch */
1114
1114
  class FigInputColor extends HTMLElement {
1115
- #rgba;
1115
+ rgba;
1116
+ hex;
1117
+ alpha = 100;
1116
1118
  #swatch;
1117
- textInput;
1119
+ #textInput;
1118
1120
  #alphaInput;
1119
1121
  constructor() {
1120
1122
  super();
1121
1123
  }
1122
1124
  connectedCallback() {
1123
- this.#rgba = this.convertToRGBA(this.getAttribute("value"));
1124
- const alpha = (this.#rgba.a * 100).toFixed(0);
1125
- this.value = this.rgbAlphaToHex(
1126
- {
1127
- r: this.#rgba.r,
1128
- g: this.#rgba.g,
1129
- b: this.#rgba.b,
1130
- },
1131
- alpha
1132
- );
1125
+ this.#setValues(this.getAttribute("value"));
1126
+
1133
1127
  let html = ``;
1134
1128
  if (this.getAttribute("text")) {
1135
1129
  let label = `<fig-input-text type="text" placeholder="Text" value="${this.value}"></fig-input-text>`;
@@ -1140,36 +1134,34 @@ class FigInputColor extends HTMLElement {
1140
1134
  type="number"
1141
1135
  min="0"
1142
1136
  max="100"
1143
- value="${alpha}">
1137
+ value="${this.alpha}">
1144
1138
  <span slot="append">%</slot>
1145
1139
  </fig-input-text>
1146
1140
  </fig-tooltip>`;
1147
1141
  }
1148
1142
  html = `<div class="input-combo">
1149
- <fig-chit type="color" disabled="false" value="${this.value}"></fig-chit>
1143
+ <fig-chit type="color" disabled="false" value="${this.hexOpaque}"></fig-chit>
1150
1144
  ${label}
1151
1145
  </div>`;
1152
1146
  } else {
1153
- html = `<fig-chit type="color" disabled="false" value="${this.value}"></fig-chit>`;
1147
+ html = `<fig-chit type="color" disabled="false" value="${this.hexOpaque}"></fig-chit>`;
1154
1148
  }
1155
1149
  this.innerHTML = html;
1156
1150
 
1157
- this.style.setProperty("--alpha", this.#rgba.a);
1158
-
1159
1151
  requestAnimationFrame(() => {
1160
- this.#swatch = this.querySelector("input[type=color]");
1161
- this.textInput = this.querySelector("fig-input-text:not([type=number])");
1152
+ this.#swatch = this.querySelector("fig-chit[type=color]");
1153
+ this.#textInput = this.querySelector("fig-input-text:not([type=number])");
1162
1154
  this.#alphaInput = this.querySelector("fig-input-text[type=number]");
1163
1155
 
1164
1156
  this.#swatch.disabled = this.hasAttribute("disabled");
1165
- this.#swatch.addEventListener("input", this.handleInput.bind(this));
1157
+ this.#swatch.addEventListener("input", this.#handleInput.bind(this));
1166
1158
 
1167
- if (this.textInput) {
1168
- this.textInput.value = this.#swatch.value = this.rgbAlphaToHex(
1169
- this.#rgba,
1159
+ if (this.#textInput) {
1160
+ this.#textInput.value = this.#swatch.value = this.rgbAlphaToHex(
1161
+ this.rgba,
1170
1162
  1
1171
1163
  );
1172
- this.textInput.addEventListener(
1164
+ this.#textInput.addEventListener(
1173
1165
  "input",
1174
1166
  this.#handleTextInput.bind(this)
1175
1167
  );
@@ -1178,65 +1170,66 @@ class FigInputColor extends HTMLElement {
1178
1170
  if (this.#alphaInput) {
1179
1171
  this.#alphaInput.addEventListener(
1180
1172
  "input",
1181
- this.handleAlphaInput.bind(this)
1173
+ this.#handleAlphaInput.bind(this)
1182
1174
  );
1183
1175
  }
1184
1176
  });
1185
1177
  }
1178
+ #setValues(hexValue) {
1179
+ this.rgba = this.convertToRGBA(hexValue);
1180
+ this.value = this.rgbAlphaToHex(
1181
+ {
1182
+ r: this.rgba.r,
1183
+ g: this.rgba.g,
1184
+ b: this.rgba.b,
1185
+ },
1186
+ this.rgba.a
1187
+ );
1188
+ this.hexWithAlpha = this.value;
1189
+ this.hexOpaque = this.hexWithAlpha.slice(0, 7);
1190
+ if (hexValue.length > 7) {
1191
+ this.alpha = (this.rgba.a * 100).toFixed(0);
1192
+ }
1193
+ this.style.setProperty("--alpha", this.rgba.a);
1194
+ }
1195
+
1186
1196
  #handleTextInput(event) {
1187
1197
  //do not propagate to onInput handler for web component
1188
1198
  event.stopPropagation();
1189
- this.value = this.#swatch.value = this.textInput.value;
1190
- this.setAttribute("value", this.value);
1199
+ this.#setValues(event.target.value);
1200
+ if (this.#alphaInput) {
1201
+ this.#alphaInput.setAttribute("value", this.alpha);
1202
+ }
1203
+ if (this.#swatch) {
1204
+ this.#swatch.setAttribute("value", this.hexOpaque);
1205
+ }
1206
+ this.#emitInputEvent();
1191
1207
  }
1192
- handleAlphaInput(event) {
1208
+
1209
+ #handleAlphaInput(event) {
1193
1210
  //do not propagate to onInput handler for web component
1194
1211
  event.stopPropagation();
1195
- this.#rgba = this.convertToRGBA(this.#swatch.value);
1196
- this.#rgba.a = Number(this.#alphaInput.value) / 100;
1197
- this.value = this.rgbAlphaToHex(
1198
- {
1199
- r: this.#rgba.r,
1200
- g: this.#rgba.g,
1201
- b: this.#rgba.b,
1202
- },
1203
- this.#rgba.a
1204
- );
1205
- this.style.setProperty("--alpha", this.#rgba.a);
1206
- const e = new CustomEvent("input", {
1207
- bubbles: true,
1208
- cancelable: true,
1209
- });
1210
- this.dispatchEvent(e);
1212
+ const alpha = Math.round((event.target.value / 100) * 255);
1213
+ const alphaHex = alpha.toString(16).padStart(2, "0");
1214
+ this.#setValues(this.hexOpaque + alphaHex);
1215
+ this.#emitInputEvent();
1211
1216
  }
1212
1217
 
1213
1218
  focus() {
1214
1219
  this.#swatch.focus();
1215
1220
  }
1216
1221
 
1217
- handleInput(event) {
1222
+ #handleInput(event) {
1218
1223
  //do not propagate to onInput handler for web component
1219
1224
  event.stopPropagation();
1220
-
1221
- let alpha = this.#rgba.a;
1222
- this.#rgba = this.convertToRGBA(this.#swatch.value);
1223
- this.#rgba.a = alpha;
1224
- if (this.textInput) {
1225
- this.textInput.value = this.#swatch.value;
1226
- }
1227
- this.style.setProperty("--alpha", this.#rgba.a);
1228
- this.value = this.rgbAlphaToHex(
1229
- {
1230
- r: this.#rgba.r,
1231
- g: this.#rgba.g,
1232
- b: this.#rgba.b,
1233
- },
1234
- alpha
1235
- );
1236
- this.alpha = alpha;
1237
- if (this.#alphaInput) {
1238
- this.#alphaInput.value = this.#rgba.a.toFixed(0);
1225
+ this.#setValues(event.target.value);
1226
+ if (this.#textInput) {
1227
+ this.#textInput.setAttribute("value", this.value);
1239
1228
  }
1229
+ this.#emitInputEvent();
1230
+ }
1231
+
1232
+ #emitInputEvent() {
1240
1233
  const e = new CustomEvent("input", {
1241
1234
  bubbles: true,
1242
1235
  cancelable: true,
@@ -1251,15 +1244,17 @@ class FigInputColor extends HTMLElement {
1251
1244
  attributeChangedCallback(name, oldValue, newValue) {
1252
1245
  switch (name) {
1253
1246
  case "value":
1254
- if (this.textInput) {
1255
- this.value =
1256
- this.#swatch.value =
1257
- this.textInput.value =
1258
- newValue.toUpperCase();
1259
- } else {
1260
- this.value = newValue.toUpperCase();
1247
+ this.#setValues(newValue);
1248
+ if (this.#textInput) {
1249
+ this.#textInput.setAttribute("value", this.hex);
1250
+ }
1251
+ if (this.#swatch) {
1252
+ this.#swatch.setAttribute("value", this.hex);
1261
1253
  }
1262
- this.dispatchEvent(new CustomEvent("input", { bubbles: true }));
1254
+ if (this.#alphaInput) {
1255
+ this.#alphaInput.setAttribute("value", this.alpha);
1256
+ }
1257
+ this.#emitInputEvent();
1263
1258
  break;
1264
1259
  }
1265
1260
  }
@@ -1549,7 +1544,7 @@ class FigChit extends HTMLElement {
1549
1544
  connectedCallback() {
1550
1545
  this.type = this.getAttribute("type") || "color";
1551
1546
  this.src = this.getAttribute("src") || "";
1552
- this.value = this.getAttribute("value") || "";
1547
+ this.value = this.getAttribute("value") || "#000000";
1553
1548
  this.size = this.getAttribute("size") || "small";
1554
1549
  this.disabled = this.getAttribute("disabled") === "true";
1555
1550
  this.innerHTML = `<input type="color" value="${this.value}" />`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rogieking/figui3",
3
- "version": "1.1.5",
3
+ "version": "1.1.7",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "devDependencies": {