completefrontend

Write the codeMedium

Write a function that finds the element behind the largest layout shift.

When CLS is poor, the first thing to learn is what moved. The web-vitals attribution build answers that with largestShiftSource and largestShiftTarget. Write largestShift(entries), which finds that element the same way. entries are the layout-shift entries CLS was made of, the shifts of its worst session window, each { startTime, value, sources }. Each source is { node, previousRect, currentRect }, and the browser lists up to five, the one whose old and new places covered the most of the screen first. Return { node, target, value, time }, or null when there are no entries.

The largest shift is the entry with the largest value; when two tie, the later one. Its culprit is its first source whose node is an element. A source's node can be a text node, or null when its element has left the page since; if no source is an element, the culprit is the first source. node is the culprit's node, and value and time are the shift's value and startTime.

target is a CSS selector for the node, built the way web-vitals builds one, or null when node is null. Start at the node and walk up through its parents until you reach the document. Each adds a part in front, joined with >: # and its id when it has one, and otherwise its tag name in lower case followed by its classes, sorted, each after a .. A text node's part is TEXT. Stop after a part made from an id. The first part always goes in, but stop before adding a part that would make the selector longer than 100 characters.

largestShift([
  { startTime: 640, value: 0.03, sources: [{ node: addButton, previousRect, currentRect }] },
  { startTime: 1210, value: 0.09, sources: [{ node: mintCard, previousRect, currentRect }, { node: addButton, previousRect, currentRect }] },
]);
// { node: mintCard, target: "#teas>article.card.featured.tea", value: 0.09, time: 1210 }
Write this: solution.js
function largestShift(entries) {
  // Today it blames the first thing that moved in the first shift, and names it by its tag.
  const [shift] = entries;
  const { node } = shift.sources[0];
  return { node, target: node.nodeName.toLowerCase(), value: shift.value, time: shift.startTime };
}
The page every test starts from
<header class="site-header">
  <a class="logo" href="/">Leaf and Kettle</a>
</header>
<div class="flex items-center justify-between rounded-lg bg-amber-100 px-4 py-3 text-sm">
  <p class="note"><span class="uppercase tracking-wide text-amber-900 font-semibold">Free delivery over £30</span></p>
</div>
<main>
  <section id="teas">
    <article class="tea card featured">
      <h2>Mint tea</h2>
      <p class="price">£4.50</p>
    </article>
    <article class="card tea">
      <h2>Oolong</h2>
      <p class="price">£6.00</p>
    </article>
  </section>
  <section class="reviews">
    <p class="review">Fresh and bright, like a garden after rain.</p>
  </section>
  <button id="add" class="button primary">Add to basket</button>
</main>
The tests your code must pass
  • names the element that moved most in the largest shift
  • a tie goes to the later shift
  • skips text that moved to name the first element, by its id alone
  • when only text moved, names the text
  • skips a source whose element has left the page, and gives null when nothing else is left
  • stops a selector before it passes 100 characters
  • no shifts, no culprit

The answer

Write it yourself first. Then compare it with a reference solution and the checks it passes.

Checking your sign-in…