@rogieking/figui3 1.1.6 → 1.1.8

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 +34 -45
  2. package/package.json +1 -1
package/fig.js CHANGED
@@ -1114,7 +1114,7 @@ window.customElements.define("fig-field", FigField);
1114
1114
  class FigInputColor extends HTMLElement {
1115
1115
  rgba;
1116
1116
  hex;
1117
- alpha;
1117
+ alpha = 100;
1118
1118
  #swatch;
1119
1119
  #textInput;
1120
1120
  #alphaInput;
@@ -1122,12 +1122,11 @@ class FigInputColor extends HTMLElement {
1122
1122
  super();
1123
1123
  }
1124
1124
  connectedCallback() {
1125
- this.#syncHex(this.getAttribute("value"));
1126
- this.value = this.hex;
1125
+ this.#setValues(this.getAttribute("value"));
1127
1126
 
1128
1127
  let html = ``;
1129
1128
  if (this.getAttribute("text")) {
1130
- let label = `<fig-input-text type="text" placeholder="Text" value="${this.hex}"></fig-input-text>`;
1129
+ let label = `<fig-input-text type="text" placeholder="Text" value="${this.value}"></fig-input-text>`;
1131
1130
  if (this.getAttribute("alpha") === "true") {
1132
1131
  label += `<fig-tooltip text="Opacity">
1133
1132
  <fig-input-text
@@ -1141,14 +1140,13 @@ class FigInputColor extends HTMLElement {
1141
1140
  </fig-tooltip>`;
1142
1141
  }
1143
1142
  html = `<div class="input-combo">
1144
- <fig-chit type="color" disabled="false" value="${this.hex}"></fig-chit>
1143
+ <fig-chit type="color" disabled="false" value="${this.hexOpaque}"></fig-chit>
1145
1144
  ${label}
1146
1145
  </div>`;
1147
1146
  } else {
1148
- html = `<fig-chit type="color" disabled="false" value="${this.hex}"></fig-chit>`;
1147
+ html = `<fig-chit type="color" disabled="false" value="${this.hexOpaque}"></fig-chit>`;
1149
1148
  }
1150
1149
  this.innerHTML = html;
1151
- this.style.setProperty("--alpha", this.rgba.a);
1152
1150
 
1153
1151
  requestAnimationFrame(() => {
1154
1152
  this.#swatch = this.querySelector("fig-chit[type=color]");
@@ -1156,7 +1154,7 @@ class FigInputColor extends HTMLElement {
1156
1154
  this.#alphaInput = this.querySelector("fig-input-text[type=number]");
1157
1155
 
1158
1156
  this.#swatch.disabled = this.hasAttribute("disabled");
1159
- this.#swatch.addEventListener("input", this.handleInput.bind(this));
1157
+ this.#swatch.addEventListener("input", this.#handleInput.bind(this));
1160
1158
 
1161
1159
  if (this.#textInput) {
1162
1160
  this.#textInput.value = this.#swatch.value = this.rgbAlphaToHex(
@@ -1172,73 +1170,66 @@ class FigInputColor extends HTMLElement {
1172
1170
  if (this.#alphaInput) {
1173
1171
  this.#alphaInput.addEventListener(
1174
1172
  "input",
1175
- this.handleAlphaInput.bind(this)
1173
+ this.#handleAlphaInput.bind(this)
1176
1174
  );
1177
1175
  }
1178
1176
  });
1179
1177
  }
1180
- #syncHex(hexValue) {
1178
+ #setValues(hexValue) {
1181
1179
  this.rgba = this.convertToRGBA(hexValue);
1182
- this.hex = this.rgbAlphaToHex(
1180
+ this.value = this.rgbAlphaToHex(
1183
1181
  {
1184
1182
  r: this.rgba.r,
1185
1183
  g: this.rgba.g,
1186
1184
  b: this.rgba.b,
1187
1185
  },
1188
- 1
1186
+ this.rgba.a
1189
1187
  );
1190
- this.alpha = (this.rgba.a * 100).toFixed(0);
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);
1191
1194
  }
1192
1195
 
1193
1196
  #handleTextInput(event) {
1194
1197
  //do not propagate to onInput handler for web component
1195
1198
  event.stopPropagation();
1196
- this.#syncHex(event.target.value);
1197
- this.value = this.hex;
1199
+ this.#setValues(event.target.value);
1198
1200
  if (this.#alphaInput) {
1199
1201
  this.#alphaInput.setAttribute("value", this.alpha);
1200
1202
  }
1201
1203
  if (this.#swatch) {
1202
- this.#swatch.setAttribute("value", this.hex);
1204
+ this.#swatch.setAttribute("value", this.hexOpaque);
1203
1205
  }
1204
- this.style.setProperty("--alpha", this.rgba.a);
1206
+ this.#emitInputEvent();
1205
1207
  }
1206
1208
 
1207
- handleAlphaInput(event) {
1209
+ #handleAlphaInput(event) {
1208
1210
  //do not propagate to onInput handler for web component
1209
1211
  event.stopPropagation();
1210
- this.rgba = this.convertToRGBA(this.#swatch.value);
1211
- this.rgba.a = Number(this.#alphaInput.value) / 100;
1212
- this.value = this.rgbAlphaToHex(
1213
- {
1214
- r: this.rgba.r,
1215
- g: this.rgba.g,
1216
- b: this.rgba.b,
1217
- },
1218
- 1
1219
- );
1220
- this.style.setProperty("--alpha", this.rgba.a);
1221
- const e = new CustomEvent("input", {
1222
- bubbles: true,
1223
- cancelable: true,
1224
- });
1225
- 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();
1226
1216
  }
1227
1217
 
1228
1218
  focus() {
1229
1219
  this.#swatch.focus();
1230
1220
  }
1231
1221
 
1232
- handleInput(event) {
1222
+ #handleInput(event) {
1233
1223
  //do not propagate to onInput handler for web component
1234
1224
  event.stopPropagation();
1235
- let alpha = this.alpha;
1236
- this.#syncHex(event.target.value);
1237
- this.alpha = alpha;
1238
- this.value = this.hex;
1225
+ this.#setValues(event.target.value);
1239
1226
  if (this.#textInput) {
1240
1227
  this.#textInput.setAttribute("value", this.value);
1241
1228
  }
1229
+ this.#emitInputEvent();
1230
+ }
1231
+
1232
+ #emitInputEvent() {
1242
1233
  const e = new CustomEvent("input", {
1243
1234
  bubbles: true,
1244
1235
  cancelable: true,
@@ -1253,19 +1244,17 @@ class FigInputColor extends HTMLElement {
1253
1244
  attributeChangedCallback(name, oldValue, newValue) {
1254
1245
  switch (name) {
1255
1246
  case "value":
1256
- this.#syncHex(newValue);
1247
+ this.#setValues(newValue);
1257
1248
  if (this.#textInput) {
1258
- this.#textInput.setAttribute("value", this.hex);
1249
+ this.#textInput.setAttribute("value", this.value);
1259
1250
  }
1260
1251
  if (this.#swatch) {
1261
- this.#swatch.setAttribute("value", this.hex);
1252
+ this.#swatch.setAttribute("value", this.hexOpaque);
1262
1253
  }
1263
1254
  if (this.#alphaInput) {
1264
1255
  this.#alphaInput.setAttribute("value", this.alpha);
1265
1256
  }
1266
- this.value = this.hex;
1267
- this.style.setProperty("--alpha", this.rgba.a);
1268
- this.dispatchEvent(new CustomEvent("input", { bubbles: true }));
1257
+ this.#emitInputEvent();
1269
1258
  break;
1270
1259
  }
1271
1260
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rogieking/figui3",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "devDependencies": {