@melodicdev/components 3.0.0 → 3.1.0

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.
@@ -321,6 +321,10 @@ function disposePart(part) {
321
321
  for (const item of part.arrayState.items.values()) disposeContainerParts(item.container);
322
322
  part.arrayState = void 0;
323
323
  }
324
+ if (part.positionalArrayState) {
325
+ for (const item of part.positionalArrayState.items) disposeContainerParts(item.container);
326
+ part.positionalArrayState = void 0;
327
+ }
324
328
  if (part.directiveState !== void 0) {
325
329
  disposeDirectiveState(part.directiveState);
326
330
  part.directiveState = void 0;
@@ -3140,6 +3144,21 @@ function warnUnsafePropertyBinding(name) {
3140
3144
  function isDevMode() {
3141
3145
  return !(typeof import.meta !== "undefined" && true);
3142
3146
  }
3147
+ function warnUnkeyedArrayChurn(state, recreated, total) {
3148
+ if (state.warnedChurn || recreated < 2 || total < 2) return;
3149
+ if (!isDevMode()) return;
3150
+ state.warnedChurn = true;
3151
+ console.warn(`[melodic] An interpolated array rebuilt ${recreated} of ${total} items on one update. Unkeyed arrays are reused by index, so entries that change position lose their DOM nodes (and with them focus, scroll position, and in-flight clicks). Use repeat(items, keyFn, template) to track items by identity instead.`);
3152
+ }
3153
+ var warnedPartiallyKeyedParts = /* @__PURE__ */ new WeakSet();
3154
+ function warnPartiallyKeyedArray(part, values) {
3155
+ if (!isDevMode() || warnedPartiallyKeyedParts.has(part)) return;
3156
+ let keyed = 0;
3157
+ for (const value of values) if (value && typeof value === "object" && value.__keyed === true) keyed++;
3158
+ if (keyed === 0 || keyed === values.length) return;
3159
+ warnedPartiallyKeyedParts.add(part);
3160
+ console.warn(`[melodic] An interpolated array mixes keyed and unkeyed items (${keyed} of ${values.length} keyed). Keyed diffing requires every item to carry a key, so this array falls back to index-based reuse. Key every item, or none.`);
3161
+ }
3143
3162
  var ANY_MARKER_REGEX = /* @__PURE__ */ new RegExp(`${COMMENT_NODE_MARKER}|${ATTRIBUTE_MARKER_PREFIX}\\d+__|__(?:event|prop|action|bool)-\\d+__`);
3144
3163
  function describeSnippet(html$1, index) {
3145
3164
  const start = Math.max(0, index - 40);
@@ -3151,12 +3170,17 @@ function warnUnsupportedBinding(position, html$1, index) {
3151
3170
  console.warn(`[melodic] Template contains a binding in an unsupported position (${position}). The parser cannot track bindings here, so the value will not render or update. Offending template: ${describeSnippet(html$1, index)}`);
3152
3171
  }
3153
3172
  function warnUnsupportedBindingPositions(html$1) {
3154
- if (!isDevMode()) return;
3173
+ if (!isDevMode()) return false;
3174
+ let warned$1 = false;
3175
+ const report = (position, index) => {
3176
+ warned$1 = true;
3177
+ warnUnsupportedBinding(position, html$1, index);
3178
+ };
3155
3179
  const rawTextRegex = /<(textarea|title)(?:\s[^>]*)?>([\s\S]*?)<\/\1\s*>/gi;
3156
3180
  let rawTextMatch;
3157
- while ((rawTextMatch = rawTextRegex.exec(html$1)) !== null) if (ANY_MARKER_REGEX.test(rawTextMatch[2])) warnUnsupportedBinding(`inside <${rawTextMatch[1].toLowerCase()}> content`, html$1, rawTextMatch.index);
3181
+ while ((rawTextMatch = rawTextRegex.exec(html$1)) !== null) if (ANY_MARKER_REGEX.test(rawTextMatch[2])) report(`inside <${rawTextMatch[1].toLowerCase()}> content`, rawTextMatch.index);
3158
3182
  const tagNameIndex = html$1.search(/* @__PURE__ */ new RegExp(`</?${COMMENT_NODE_MARKER}`));
3159
- if (tagNameIndex !== -1) warnUnsupportedBinding("tag-name position", html$1, tagNameIndex);
3183
+ if (tagNameIndex !== -1) report("tag-name position", tagNameIndex);
3160
3184
  let searchFrom = 0;
3161
3185
  for (;;) {
3162
3186
  const open = html$1.indexOf("<!--", searchFrom);
@@ -3167,9 +3191,21 @@ function warnUnsupportedBindingPositions(html$1) {
3167
3191
  }
3168
3192
  const close = html$1.indexOf("-->", open + 4);
3169
3193
  const content = close === -1 ? html$1.slice(open + 4) : html$1.slice(open + 4, close);
3170
- if (content.includes(MARKER) || /__(?:event|prop|action|bool)-\d+__/.test(content)) warnUnsupportedBinding("inside an HTML comment", html$1, open);
3194
+ if (content.includes(MARKER) || /__(?:event|prop|action|bool)-\d+__/.test(content)) report("inside an HTML comment", open);
3171
3195
  searchFrom = close === -1 ? html$1.length : close + 3;
3172
3196
  }
3197
+ return warned$1;
3198
+ }
3199
+ function warnLeakedBindings(partPaths, expressionCount, html$1) {
3200
+ if (!isDevMode()) return;
3201
+ const anchored = /* @__PURE__ */ new Set();
3202
+ for (const partPath of partPaths) if (partPath.attributeIndices) for (const index of partPath.attributeIndices) anchored.add(index);
3203
+ else if (partPath.index >= 0) anchored.add(partPath.index);
3204
+ const lost = [];
3205
+ for (let index = 0; index < expressionCount; index++) if (!anchored.has(index)) lost.push(index);
3206
+ if (lost.length === 0) return;
3207
+ const markerIndex = html$1.indexOf(createAttributeMarker(lost[0]));
3208
+ console.warn(`[melodic] Template part marker leaked: ${lost.length} binding${lost.length === 1 ? "" : "s"} (value index ${lost.join(", ")}) could not be anchored to the parsed template and will never render or update. The usual cause is an unbalanced quote in an attribute value, which swallows the markup that follows it. Offending template: ${describeSnippet(html$1, markerIndex === -1 ? 0 : markerIndex)}`);
3173
3209
  }
3174
3210
  function extractListenerOptions(value) {
3175
3211
  const { capture, once, passive } = value;
@@ -3250,7 +3286,9 @@ var TemplateResult = class TemplateResult {
3250
3286
  const s = this.strings[i];
3251
3287
  const valueIndex = i - 1;
3252
3288
  const match = /([@.:?]?[\w:-]+)\s*=\s*["']?$/.exec(html$1);
3253
- const quotedAttrMatch = /([@.:?]?[\w:-]+)\s*=\s*(["'])([^"']*)$/.exec(html$1);
3289
+ const doubleQuotedAttrMatch = /([@.:?]?[\w:-]+)\s*=\s*(")([^"]*)$/.exec(html$1);
3290
+ const singleQuotedAttrMatch = /([@.:?]?[\w:-]+)\s*=\s*(')([^']*)$/.exec(html$1);
3291
+ const quotedAttrMatch = doubleQuotedAttrMatch && singleQuotedAttrMatch ? doubleQuotedAttrMatch.index >= singleQuotedAttrMatch.index ? doubleQuotedAttrMatch : singleQuotedAttrMatch : doubleQuotedAttrMatch ?? singleQuotedAttrMatch;
3254
3292
  let attrKey = "___";
3255
3293
  if (activeAttributeName) html$1 += createAttributeMarker(valueIndex);
3256
3294
  else {
@@ -3287,7 +3325,7 @@ var TemplateResult = class TemplateResult {
3287
3325
  }
3288
3326
  }
3289
3327
  }
3290
- warnUnsupportedBindingPositions(html$1);
3328
+ const hasUnsupportedBinding = warnUnsupportedBindingPositions(html$1);
3291
3329
  const element = document.createElement("template");
3292
3330
  element.innerHTML = html$1;
3293
3331
  const partPaths = [];
@@ -3396,6 +3434,7 @@ var TemplateResult = class TemplateResult {
3396
3434
  }
3397
3435
  };
3398
3436
  walkTemplate(element.content, []);
3437
+ if (!hasUnsupportedBinding) warnLeakedBindings(partPaths, this.strings.length - 1, html$1);
3399
3438
  cached = {
3400
3439
  element,
3401
3440
  parts,
@@ -3563,6 +3602,10 @@ var TemplateResult = class TemplateResult {
3563
3602
  for (const item of part.arrayState.items.values()) disposeContainerParts(item.container);
3564
3603
  part.arrayState = void 0;
3565
3604
  }
3605
+ if (part.positionalArrayState) {
3606
+ for (const item of part.positionalArrayState.items) disposeContainerParts(item.container);
3607
+ part.positionalArrayState = void 0;
3608
+ }
3566
3609
  if (part.renderedNodes && part.renderedNodes.length > 0) for (const node of part.renderedNodes) node.parentNode?.removeChild(node);
3567
3610
  part.renderedNodes = [];
3568
3611
  }
@@ -3620,6 +3663,7 @@ var TemplateResult = class TemplateResult {
3620
3663
  const parent = part.endMarker.parentNode;
3621
3664
  const keyedValues = this.getKeyedValues(values);
3622
3665
  if (keyedValues) {
3666
+ if (part.positionalArrayState) this.clearRenderedNodes(part);
3623
3667
  const state = part.arrayState ?? {
3624
3668
  items: /* @__PURE__ */ new Map(),
3625
3669
  keys: []
@@ -3664,26 +3708,42 @@ var TemplateResult = class TemplateResult {
3664
3708
  part.renderedNodes = newKeys.flatMap((key) => newItems.get(key).nodes);
3665
3709
  return;
3666
3710
  }
3667
- this.clearRenderedNodes(part);
3668
- const renderedNodes = [];
3669
- const renderedContainers = [];
3670
- for (const value of values) if (value instanceof TemplateResult) {
3671
- const fragment = document.createDocumentFragment();
3672
- value.renderInto(fragment);
3673
- const nodes = Array.from(fragment.childNodes);
3674
- renderedNodes.push(...nodes);
3675
- renderedContainers.push(fragment);
3676
- parent.insertBefore(fragment, part.endMarker);
3677
- } else if (value instanceof Node) {
3678
- renderedNodes.push(value);
3679
- parent.insertBefore(value, part.endMarker);
3680
- } else if (value !== null && value !== void 0) {
3681
- const textNode = document.createTextNode(String(value));
3682
- renderedNodes.push(textNode);
3683
- parent.insertBefore(textNode, part.endMarker);
3711
+ warnPartiallyKeyedArray(part, values);
3712
+ this.renderPositionalArray(part, values, parent);
3713
+ }
3714
+ renderPositionalArray(part, values, parent) {
3715
+ const endMarker = part.endMarker;
3716
+ const isUpdate = part.positionalArrayState !== void 0;
3717
+ if (!isUpdate) this.clearRenderedNodes(part);
3718
+ const state = part.positionalArrayState ?? { items: [] };
3719
+ const items = state.items;
3720
+ let recreated = 0;
3721
+ for (let index = 0; index < values.length; index++) {
3722
+ const value = values[index];
3723
+ const existing = items[index];
3724
+ if (existing) {
3725
+ const anchor = this.findPositionalAnchor(items, index + 1, endMarker);
3726
+ if (this.updateArrayItem(existing, value, parent, anchor)) recreated++;
3727
+ } else {
3728
+ const created = this.createArrayItem(value, parent, endMarker);
3729
+ items[index] = {
3730
+ value,
3731
+ container: created.container,
3732
+ nodes: created.nodes
3733
+ };
3734
+ }
3684
3735
  }
3685
- part.renderedNodes = renderedNodes;
3686
- part.renderedContainers = renderedContainers.length > 0 ? renderedContainers : void 0;
3736
+ if (items.length > values.length) for (const removed of items.splice(values.length)) {
3737
+ disposeContainerParts(removed.container);
3738
+ for (const node of removed.nodes) node.parentNode?.removeChild(node);
3739
+ }
3740
+ part.positionalArrayState = state;
3741
+ part.renderedNodes = items.flatMap((item) => item.nodes);
3742
+ if (isUpdate) warnUnkeyedArrayChurn(state, recreated, values.length);
3743
+ }
3744
+ findPositionalAnchor(items, from, endMarker) {
3745
+ for (let index = from; index < items.length; index++) for (const node of items[index].nodes) if (node.parentNode) return node;
3746
+ return endMarker;
3687
3747
  }
3688
3748
  getKeyedValues(values) {
3689
3749
  if (values.length === 0) return null;
@@ -3709,21 +3769,30 @@ var TemplateResult = class TemplateResult {
3709
3769
  nodes
3710
3770
  };
3711
3771
  }
3712
- updateArrayItem(item, value, parent, endMarker) {
3713
- if (value instanceof TemplateResult) {
3714
- item.nodes = renderDetachedItem(value, item.container, item.nodes, endMarker);
3772
+ updateArrayItem(item, value, parent, anchor) {
3773
+ const hasPartTree = item.container.__parts !== void 0;
3774
+ if (value instanceof TemplateResult && hasPartTree) {
3775
+ const previousNodes = item.nodes;
3776
+ item.nodes = renderDetachedItem(value, item.container, item.nodes, anchor);
3715
3777
  item.value = value;
3716
- return;
3778
+ return item.nodes !== previousNodes;
3779
+ }
3780
+ if (!(value instanceof TemplateResult) && value === item.value) return false;
3781
+ if (!(value instanceof TemplateResult) && !(value instanceof Node) && value !== null && value !== void 0 && !hasPartTree && item.nodes.length === 1 && item.nodes[0].nodeType === Node.TEXT_NODE) {
3782
+ item.nodes[0].nodeValue = String(value);
3783
+ item.value = value;
3784
+ return false;
3717
3785
  }
3718
- if (value === item.value) return;
3719
3786
  disposeContainerParts(item.container);
3720
3787
  for (const node of item.nodes) node.parentNode?.removeChild(node);
3721
3788
  item.container = document.createDocumentFragment();
3722
- if (value instanceof Node) item.container.appendChild(value);
3789
+ if (value instanceof TemplateResult) value.renderInto(item.container);
3790
+ else if (value instanceof Node) item.container.appendChild(value);
3723
3791
  else if (value !== null && value !== void 0) item.container.appendChild(document.createTextNode(String(value)));
3724
3792
  item.nodes = Array.from(item.container.childNodes);
3725
- parent.insertBefore(item.container, endMarker);
3793
+ parent.insertBefore(item.container, anchor);
3726
3794
  item.value = value;
3795
+ return true;
3727
3796
  }
3728
3797
  commitEventPart(part, value) {
3729
3798
  const element = part.node;
@@ -9925,6 +9994,7 @@ registerAdapter((el) => el.tagName === "ML-SELECT", {
9925
9994
  el.disabled = disabled;
9926
9995
  }
9927
9996
  });
9997
+ var TYPEAHEAD_RESET_MS = 700;
9928
9998
  var SelectComponent = class SelectComponent$1 {
9929
9999
  constructor() {
9930
10000
  this.label = "";
@@ -9951,6 +10021,8 @@ var SelectComponent = class SelectComponent$1 {
9951
10021
  }));
9952
10022
  this._lastCloseTime = 0;
9953
10023
  this._syncingValues = false;
10024
+ this._typeaheadBuffer = "";
10025
+ this._typeaheadLastTime = 0;
9954
10026
  this.optionId = (index) => {
9955
10027
  return `${this.listboxId}-option-${index}`;
9956
10028
  };
@@ -10110,6 +10182,7 @@ var SelectComponent = class SelectComponent$1 {
10110
10182
  this.isOpen = false;
10111
10183
  this.focusedIndex = -1;
10112
10184
  this.search = "";
10185
+ this._typeaheadBuffer = "";
10113
10186
  this._lastCloseTime = Date.now();
10114
10187
  this.stopPositioning();
10115
10188
  this.elementRef.dispatchEvent(new CustomEvent("ml:close", {
@@ -10158,17 +10231,58 @@ var SelectComponent = class SelectComponent$1 {
10158
10231
  break;
10159
10232
  case "Home":
10160
10233
  event.preventDefault();
10161
- if (this.isOpen) this.focusedIndex = this.findFirstEnabledIndex();
10234
+ if (this.isOpen) {
10235
+ this.focusedIndex = this.findFirstEnabledIndex();
10236
+ this.scrollFocusedOptionIntoView();
10237
+ }
10162
10238
  break;
10163
10239
  case "End":
10164
10240
  event.preventDefault();
10165
- if (this.isOpen) this.focusedIndex = this.findLastEnabledIndex();
10241
+ if (this.isOpen) {
10242
+ this.focusedIndex = this.findLastEnabledIndex();
10243
+ this.scrollFocusedOptionIntoView();
10244
+ }
10166
10245
  break;
10167
10246
  case "Tab":
10168
10247
  this.close();
10169
10248
  break;
10170
- default: break;
10249
+ default:
10250
+ if (!isSearchInput) this.handleTypeahead(event);
10251
+ break;
10252
+ }
10253
+ }
10254
+ handleTypeahead(event) {
10255
+ if (event.key.length !== 1 || event.ctrlKey || event.metaKey || event.altKey) return;
10256
+ event.preventDefault();
10257
+ const now = Date.now();
10258
+ if (now - this._typeaheadLastTime > TYPEAHEAD_RESET_MS) this._typeaheadBuffer = "";
10259
+ this._typeaheadLastTime = now;
10260
+ this._typeaheadBuffer += event.key.toLowerCase();
10261
+ const query = this._typeaheadBuffer.length > 1 && this._typeaheadBuffer.split("").every((char) => char === this._typeaheadBuffer[0]) ? this._typeaheadBuffer[0] : this._typeaheadBuffer;
10262
+ const options = this.getActiveOptions();
10263
+ const startIndex = this.isOpen ? this.focusedIndex : options.findIndex((opt) => opt.value === this.value);
10264
+ const matchIndex = this.findTypeaheadMatch(query, startIndex);
10265
+ if (matchIndex < 0) return;
10266
+ if (this.isOpen) {
10267
+ this.focusedIndex = matchIndex;
10268
+ this.scrollFocusedOptionIntoView();
10269
+ return;
10270
+ }
10271
+ if (this.multiple) return;
10272
+ this.selectOption(options[matchIndex]);
10273
+ }
10274
+ findTypeaheadMatch(query, startIndex) {
10275
+ const options = this.getActiveOptions();
10276
+ for (let offset$1 = 1; offset$1 <= options.length; offset$1++) {
10277
+ const index = (startIndex + offset$1) % options.length;
10278
+ const option = options[index];
10279
+ if (!option.disabled && option.label.toLowerCase().startsWith(query)) return index;
10171
10280
  }
10281
+ return -1;
10282
+ }
10283
+ scrollFocusedOptionIntoView() {
10284
+ if (this.focusedIndex < 0) return;
10285
+ (this.elementRef.shadowRoot?.getElementById(this.optionId(this.focusedIndex)))?.scrollIntoView({ block: "nearest" });
10172
10286
  }
10173
10287
  focusNextOption() {
10174
10288
  let index = this.focusedIndex + 1;
@@ -10176,6 +10290,7 @@ var SelectComponent = class SelectComponent$1 {
10176
10290
  while (index < options.length) {
10177
10291
  if (!options[index].disabled) {
10178
10292
  this.focusedIndex = index;
10293
+ this.scrollFocusedOptionIntoView();
10179
10294
  return;
10180
10295
  }
10181
10296
  index++;
@@ -10187,6 +10302,7 @@ var SelectComponent = class SelectComponent$1 {
10187
10302
  while (index >= 0) {
10188
10303
  if (!options[index].disabled) {
10189
10304
  this.focusedIndex = index;
10305
+ this.scrollFocusedOptionIntoView();
10190
10306
  return;
10191
10307
  }
10192
10308
  index--;