@arraypress/waveform-player 1.19.0 → 1.19.3

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.
@@ -986,12 +986,12 @@ var WaveformPlayer = class _WaveformPlayer {
986
986
  ` : ""}
987
987
  ${this.options.showPlaybackSpeed ? `
988
988
  <div class="waveform-speed">
989
- <button class="speed-btn" aria-label="Playback speed" aria-haspopup="true" aria-expanded="false">
989
+ <button class="speed-btn" aria-label="Playback speed" aria-haspopup="menu" aria-expanded="false">
990
990
  <span class="speed-value">1x</span>
991
991
  </button>
992
- <div class="speed-menu" style="display: none;">
992
+ <div class="speed-menu" role="menu" aria-label="Playback speed" style="display: none;">
993
993
  ${this.options.playbackRates.map(
994
- (rate) => `<button class="speed-option" data-rate="${rate}">${rate}x</button>`
994
+ (rate) => `<button class="speed-option" role="menuitemradio" tabindex="-1" aria-checked="false" data-rate="${rate}">${rate}x</button>`
995
995
  ).join("")}
996
996
  </div>
997
997
  </div>
@@ -1096,27 +1096,70 @@ var WaveformPlayer = class _WaveformPlayer {
1096
1096
  const speedBtn = this.container.querySelector(".speed-btn");
1097
1097
  const speedMenu = this.container.querySelector(".speed-menu");
1098
1098
  if (!speedBtn || !speedMenu) return;
1099
+ const options = () => Array.from(speedMenu.querySelectorAll(".speed-option"));
1100
+ const isOpen = () => speedMenu.style.display !== "none";
1099
1101
  const setSpeedMenu = (open) => {
1100
1102
  speedMenu.style.display = open ? "block" : "none";
1101
1103
  speedBtn.setAttribute("aria-expanded", open ? "true" : "false");
1104
+ if (open) {
1105
+ const opts = options();
1106
+ (opts.find((o) => o.getAttribute("aria-checked") === "true") || opts[0])?.focus();
1107
+ }
1108
+ };
1109
+ const focusOption = (i) => {
1110
+ const opts = options();
1111
+ if (opts.length) opts[(i + opts.length) % opts.length].focus();
1112
+ };
1113
+ const selectOption = (opt) => {
1114
+ this.setPlaybackRate(parseFloat(opt.dataset.rate));
1115
+ setSpeedMenu(false);
1116
+ speedBtn.focus();
1102
1117
  };
1103
1118
  speedBtn.addEventListener("click", (e) => {
1104
1119
  e.stopPropagation();
1105
- setSpeedMenu(speedMenu.style.display === "none");
1120
+ setSpeedMenu(!isOpen());
1106
1121
  }, { signal: this._ac.signal });
1107
1122
  document.addEventListener("click", () => setSpeedMenu(false), { signal: this._ac.signal });
1108
1123
  speedMenu.addEventListener("click", (e) => {
1109
1124
  e.stopPropagation();
1110
- if (e.target.classList.contains("speed-option")) {
1111
- this.setPlaybackRate(parseFloat(e.target.dataset.rate));
1112
- setSpeedMenu(false);
1113
- speedBtn.focus();
1114
- }
1125
+ const opt = e.target.closest(".speed-option");
1126
+ if (opt) selectOption(opt);
1115
1127
  }, { signal: this._ac.signal });
1116
1128
  speedBtn.closest(".waveform-speed")?.addEventListener("keydown", (e) => {
1117
- if (e.key === "Escape" && speedMenu.style.display !== "none") {
1118
- setSpeedMenu(false);
1119
- speedBtn.focus();
1129
+ const opts = options();
1130
+ const i = opts.indexOf(document.activeElement);
1131
+ if (!isOpen()) {
1132
+ if ((e.key === "ArrowDown" || e.key === "ArrowUp") && document.activeElement === speedBtn) {
1133
+ e.preventDefault();
1134
+ setSpeedMenu(true);
1135
+ }
1136
+ return;
1137
+ }
1138
+ switch (e.key) {
1139
+ case "ArrowDown":
1140
+ e.preventDefault();
1141
+ focusOption(i < 0 ? 0 : i + 1);
1142
+ break;
1143
+ case "ArrowUp":
1144
+ e.preventDefault();
1145
+ focusOption(i < 0 ? opts.length - 1 : i - 1);
1146
+ break;
1147
+ case "Home":
1148
+ e.preventDefault();
1149
+ focusOption(0);
1150
+ break;
1151
+ case "End":
1152
+ e.preventDefault();
1153
+ focusOption(opts.length - 1);
1154
+ break;
1155
+ case "Escape":
1156
+ e.preventDefault();
1157
+ setSpeedMenu(false);
1158
+ speedBtn.focus();
1159
+ break;
1160
+ case "Tab":
1161
+ setSpeedMenu(false);
1162
+ break;
1120
1163
  }
1121
1164
  }, { signal: this._ac.signal });
1122
1165
  this.updateSpeedUI();
@@ -1357,7 +1400,7 @@ var WaveformPlayer = class _WaveformPlayer {
1357
1400
  _updateMediaSession(state) {
1358
1401
  if (!("mediaSession" in navigator) || !this.options.enableMediaSession || !this.audio) return;
1359
1402
  try {
1360
- if (state === "playing") this._applyMediaMetadata();
1403
+ if (state === "playing") this.initMediaSession();
1361
1404
  navigator.mediaSession.playbackState = state;
1362
1405
  const d = this.audio.duration;
1363
1406
  if (navigator.mediaSession.setPositionState && d && isFinite(d)) {
@@ -1526,7 +1569,6 @@ var WaveformPlayer = class _WaveformPlayer {
1526
1569
  }
1527
1570
  this.drawWaveform();
1528
1571
  this.renderMarkers();
1529
- this.initMediaSession();
1530
1572
  if (this.options.onLoad) {
1531
1573
  this.options.onLoad(this);
1532
1574
  }
@@ -1829,10 +1871,12 @@ var WaveformPlayer = class _WaveformPlayer {
1829
1871
  this.progress = clamp((clientX - rect.left) / rect.width);
1830
1872
  this.drawWaveform();
1831
1873
  this._updateSeekHandle();
1832
- this._updateHoverTip(clientX);
1833
1874
  const dur = this.getSeekDuration();
1834
1875
  if (dur && this.currentTimeEl) {
1835
1876
  this.currentTimeEl.textContent = formatTime(this.progress * dur);
1877
+ this._hideHoverTip();
1878
+ } else {
1879
+ this._updateHoverTip(clientX);
1836
1880
  }
1837
1881
  }
1838
1882
  /**
@@ -2188,7 +2232,9 @@ var WaveformPlayer = class _WaveformPlayer {
2188
2232
  speedValue.textContent = rate === 1 ? "1x" : `${rate}x`;
2189
2233
  }
2190
2234
  this.container.querySelectorAll(".speed-option").forEach((btn) => {
2191
- btn.classList.toggle("active", parseFloat(btn.dataset.rate) === this.audio.playbackRate);
2235
+ const on = parseFloat(btn.dataset.rate) === this.audio.playbackRate;
2236
+ btn.classList.toggle("active", on);
2237
+ btn.setAttribute("aria-checked", on ? "true" : "false");
2192
2238
  });
2193
2239
  }
2194
2240
  // ============================================