fitContentJs constant

String const fitContentJs

JavaScript code to determine the tightest possible dimensions (width and height) that encompass all rendered HTML elements. This computes the maximum right and bottom coordinates from the getBoundingClientRect() of all elements.

Returns a JavaScript array: [width, height]

Implementation

static const fitContentJs = """
(function() {
 let maxRight = 0;
 let maxBottom = 0;

  document.body.querySelectorAll('*').forEach(el => {
  const rect = el.getBoundingClientRect();
  maxRight = Math.max(maxRight, rect.right);
  maxBottom = Math.max(maxBottom, rect.bottom);
  });

  return [maxRight, maxBottom];
  })();
""";