/** * A table wrapper is a scroll container so a table wider than the page * scrolls inside it instead of pushing the page sideways. A scroll container * clips everything that sticks out of it, though, such as a ribbon moved past * the edge of its cell. The renderer marks the wrappers that only clip for * the scrolling (`data-tableberg-overflow="auto"`; rounded corners always * need the clip), and here they scroll only while the table is wider than * them. */ function tableIsWiderThan(wrapper: HTMLElement): boolean { const table = wrapper.querySelector("table"); if (!table) { return false; } // A pixel of slack for sub-pixel widths. return table.getBoundingClientRect().width > wrapper.clientWidth + 1; } function updateOverflow(wrapper: HTMLElement) { wrapper.style.overflow = tableIsWiderThan(wrapper) ? "auto" : "visible"; } export function initializeOverflow() { const wrappers = Array.from( document.querySelectorAll( ".tableberg-table-wrapper[data-tableberg-overflow='auto']" ) ); if (wrappers.length === 0) { return; } wrappers.forEach(updateOverflow); if (typeof ResizeObserver === "undefined") { return; } // Both sizes matter: the wrapper follows the page, the table its content // and the responsive modes (which also change it at a breakpoint). const observer = new ResizeObserver(entries => { const changed = new Set(); entries.forEach(entry => { const wrapper = (entry.target as HTMLElement).closest( ".tableberg-table-wrapper[data-tableberg-overflow='auto']" ); if (wrapper) { changed.add(wrapper); } }); changed.forEach(updateOverflow); }); wrappers.forEach(wrapper => { observer.observe(wrapper); const table = wrapper.querySelector("table"); if (table) { observer.observe(table); } }); }