#112

Zoom: A drawing taller than its padded element loses the padding when enlarged

Openronicayuopened 1mo ago

What happens

When a zoomable drawing is taller than its element and the element has padding, enlarging it sizes the view to the drawing's own height. The view is then pulled over the padding by the negative margins, so:

  • the element's scroll height shrinks by the vertical padding, and
  • the top of the drawing moves from inside the padding to the element's top edge.

Pressing reset puts both back.

Reproduce

Tested against main (36e584d), in Chromium.

<style>
  #d { width: 360px; height: 260px; padding: 24px; overflow: auto; }
</style>
<div id="d"></div>
<script type="module">
  import { mount } from 'pele';
  mount(document.getElementById('d'), `flowchart TB
    A[A fairly long first label] --> B[Another rather long label here] --> C[And a third long label] --> D[Fourth] --> E[Fifth]
    A --> F[Side branch with a long label] --> G[Another side node] --> E`);
</script>

The drawing is shrunk to fit (scale 0.8) and is taller than the element, so the zoom controls appear. Press Zoom in:

At restZoomed inAfter reset
#d.scrollHeight386334386
Drawing's top, measured from the element's padding box24024
Height of the SVG334334334

The scroll range shrinks by 52px: the 48px of padding, plus the room for descenders that an inline drawing has at rest.

Cause

measure() in src/zoom.ts:

box = { width: Math.max(rest.width, element.clientWidth), height: Math.max(rest.height, element.clientHeight) };

clientHeight includes the padding, but rest.height doesn't. When the drawing is taller than the element, box.height becomes rest.height. bleed then takes the padding back off that height, so the view is the drawing's height minus the padding.

Suggested fix

Count the padding in the dimension where the drawing overflows:

const style = view().getComputedStyle(element);
const pad = (side: string): number => parseFloat(style.getPropertyValue(`padding-${side}`)) || 0;
box = {
  width: Math.max(rest.width + pad('left') + pad('right'), element.clientWidth),
  height: Math.max(rest.height + pad('top') + pad('bottom'), element.clientHeight),
};
roomy = box.width > rest.width + 1 || box.height > rest.height + 1;
if (!roomy) box = rest;
bleed = ['top', 'right', 'bottom', 'left'].map((side) => `${-pad(side)}px`).join(' ');

With this change, the same page measures 382 for both the scroll height and the SVG while zoomed in. The view covers the drawing and its padding, and the scroll range stays the same apart from the 4px of descender room. The existing tests pass.

A test against the mock in tests/zoom.test.ts. On main it fails, because the view is 297 tall rather than about 440. It passes with the fix:

it('keeps the padding around a drawing that overflows its element', () => {
  globals.getComputedStyle = () => ({ position: 'relative', getPropertyValue: () => '24px' });
  // A drawing as tall as the element's content, so taller than the room inside its padding.
  const drawing = drawingIn(400);
  Object.assign(drawing.element, { clientWidth: 448 });
  const zoom = enableZoom(drawing.element, { controls: false });
  zoom.zoomTo(zoom.fit * 1.01);
  expect(Number(drawing.svg.getAttribute('viewBox')?.split(' ')[3])).toBeCloseTo(444 / 1.01);
});

Activity

0

No activity yet.