DEV Community

Jack Song
Jack Song

Posted on

Fixing Mobile CSS Grid Overlay Clipping, Lazy <audio> Previews & Dual-Balance Pre-Checks in Vanilla JS

Digitaler Adventskalender – 24 digitale Türchen kostenlos online erstellen

Shipping interactive holiday web tools comes with an unforgiving constraint: most visitors open shared links on narrow 375px mobile screens, tap audio samples inline, and bail immediately if clicking a button triggers a 4-second spinner that ends in a 401.

While auditing and updating Digitaler Adventskalender — a free, zero-login German advent calendar builder that pairs a 7/12/24/25-door creator (including the collaborative Mit Freunden group board) with 4 browser mini-games — three front-end bugs stood out during real mobile testing:

  1. Post-game overlay clipping on mobile inside a fixed-height game stage
  2. Simultaneous <audio loading="lazy"> playback when offering real German AI Christmas song previews alongside a custom player
  3. Wasted 401 round-trips when free earned mini-game credits (Gratis-Flocken) cannot cover paid AI endpoints (Gekaufte Flocken)

Here are the exact CSS and vanilla JS fixes we deployed — plus two new MIT-licensed helpers pushed to vanilla-js-calendar-story-snippets.


1. Replacing Fixed-Height Overlay Clipping with Single-Cell CSS Grid Stacking

Originally, our mini-game stage used position: relative; height: 320px; overflow: hidden; on .ds-board with .ds-overlay using position: absolute; inset: 0;.

During the game intro (a short title + Play button), 320px looked tidy. Once a visitor finished all 6 pairs in Memory, however, the post-game overlay rendered the score summary, reward badge, conversion pitch, WhatsApp challenge link, and replay button (~473px tall on a 375×812 viewport). Because .ds-overlay was position: absolute inside an overflow: hidden fixed-height box, both the top score and bottom CTA buttons were silently sliced off.

Refactoring .ds-board into a single-cell CSS Grid track (grid-template-areas: "stage") without overflow: hidden lets the overlay expand the stage naturally when content grows, while keeping layers stacked directly on top of one another:

.ds-board {
  position: relative;
  display: grid;
  grid-template-areas: "stage";
  border-radius: 14px;
  border: 2px solid var(--border);
  background: var(--bg);
}

.ds-canvas,
.ds-dom-area,
.ds-overlay {
  grid-area: stage;
}

.ds-canvas[hidden],
.ds-dom-area[hidden],
.ds-overlay[hidden] {
  display: none;
}

.ds-overlay {
  position: relative;
  width: 100%;
  min-block-size: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 16px;
}
🔴 Hot News: jack song bcbd/fixing mobile css grid overlay clipping lazy previews dual balance pre checks in vanilla af - ประเด็นร้อน 🔴 Hot News: jack song bcbd/fixing mobile css grid overlay clipping lazy previews dual balance pre checks in vanilla af - ประเด็นร้อน

Key detail: [hidden] elements inside CSS Grid or display: flex containers must be explicitly forced to display: none so author display rules never override the HTML hidden attribute. On a live 375×812 mobile test finishing a full Memory round on digitaleradventskalender.com/spiele, .ds-board expanded cleanly from 320px to 472.77px with 0 clipping and 0 horizontal overflow (CLS = 0).


2. Lazy <audio> Previews and Capture-Phase Single-Active Playback Guard

We added real MP3 sample previews of our personalized German AI Christmas song across the studio panel, post-creation viewer, and Preise & Pakete page so visitors can actually hear vocal quality before spending paid credits.

Two small browser quirks matter when embedding multiple <audio> players on a tool page:

  1. <audio loading="lazy" preload="none"> must keep controls: Lazy-loaded <audio> elements without controls are hidden by browsers and won't load audio. Always keep controls visible and pair it with preload="none" so the 3.8 MB MP3 isn't fetched until the visitor actually taps Play.
  2. play events on <audio> do not bubble: If you have a sample demo player, a generated track preview, and an attached calendar song player on the same page, bubbling listeners on document.addEventListener("play", ...) won't fire! Pass true (capture: true) so starting any player immediately pauses sibling players:
function isAudioElement(node) {
  return Boolean(node && typeof node.tagName === "string" && node.tagName.toUpperCase() === "AUDIO");
}

export function attachSingleActiveAudioGuard(root = typeof document !== "undefined" ? document : null) {
  if (!root || typeof root.addEventListener !== "function") {
    return () => {};
  }

  const onPlay = (event) => {
    const target = event?.target;
    if (!isAudioElement(target) || typeof root.querySelectorAll !== "function") return;
    for (const player of root.querySelectorAll("audio")) {
      if (player !== target && !player.paused && typeof player.pause === "function") {
        player.pause();
      }
    }
  };

  root.addEventListener("play", onPlay, true);
  return () => root.removeEventListener("play", onPlay, true);
}
🔴 Hot News: jack song bcbd/fixing mobile css grid overlay clipping lazy previews dual balance pre checks in vanilla af - ประเด็นร้อน 🔴 Hot News: jack song bcbd/fixing mobile css grid overlay clipping lazy previews dual balance pre checks in vanilla af - ประเด็นร้อน

3. Dual-Balance Clarity and Synchronous Pre-Checks (Zero 401 Spinners)

Free browser-earned credits (from daily door opens or mini-game rounds) are great for cosmetic skins (2 / 3 / 4 ❄️ door wraps or the 15 ❄️ Gold-Edition), while paid server-side AI tools (5 ❄️ for 24 custom German door ideas, 15 ❄️ for a custom MP3 song) require purchased credits (Gekaufte Flocken).

Showing a single combined flake number in the header confused users with 3 ❄️ free balance when AI actions returned 401. Calling the backend anyway also forced a 3–5 second spinner before failing!

Splitting the UI display into explicit free vs. paid counters and running a synchronous pre-check before starting any progress indicator eliminates wasted requests completely:

export function checkDualCreditPrecheck({
  freeBalance = 0,
  paidBalance = 0,
  requiredCost = 0,
  mode = "paid_only"
} = {}) {
  const free = Math.max(0, Math.floor(Number(freeBalance) || 0));
  const paid = Math.max(0, Math.floor(Number(paidBalance) || 0));
  const cost = Math.max(0, Math.floor(Number(requiredCost) || 0));
  const paidOnly = mode === "paid_only";
  const spendable = paidOnly ? paid : free + paid;
  const shortfall = Math.max(0, cost - spendable);

  return {
    ok: shortfall === 0,
    shortfall,
    paidOnly,
    requiredCost: cost,
    freeBalance: free,
    paidBalance: paid,
    spendableBalance: spendable,
    action: shortfall === 0 ? "proceed" : "prompt_purchase_or_fallback"
  };
}
🔴 Hot News: jack song bcbd/fixing mobile css grid overlay clipping lazy previews dual balance pre checks in vanilla af - ประเด็นร้อน 🔴 Hot News: jack song bcbd/fixing mobile css grid overlay clipping lazy previews dual balance pre checks in vanilla af - ประเด็นร้อน

When action === "prompt_purchase_or_fallback", we keep the progress bar hidden (hidden = true), reveal a clear message explaining exact free vs. paid balances, and offer both the guest checkout CTA and a 1-click free template fallback (24 digitale Türchen ready from our content ideas guide).


Try It Live or Inspect the MIT Snippets

Top comments (0)