All files / src/composables useHeaderRowAlignment.ts

97.22% Statements 35/36
85.71% Branches 12/14
100% Functions 7/7
100% Lines 32/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65      9x       101x 101x 3x 3x 2x 1x   1x                       101x 101x 101x     101x 101x 101x 101x 101x 101x     101x   101x 101x 101x 101x 101x 101x     101x     101x 9x 9x     101x 101x 100x     101x    
import { computed, onMounted, onUnmounted, ref, type Ref } from 'vue'
 
// VirtualCardGrid's own padding, used outside PageLayout (which overrides it with the page gutter)
const DEFAULT_ROW_CONTAINER_PADDING = 16
 
/** The `--page-gutter` set by PageLayout (and applied to VirtualCardGrid's rows), in pixels. */
function readPageGutter(el: HTMLElement): number {
  const value = window.getComputedStyle(el).getPropertyValue('--page-gutter').trim()
  if (!value) return DEFAULT_ROW_CONTAINER_PADDING
  const amount = parseFloat(value)
  if (Number.isNaN(amount)) return DEFAULT_ROW_CONTAINER_PADDING
  if (value.endsWith('rem')) {
    return amount * parseFloat(window.getComputedStyle(document.documentElement).fontSize)
  }
  return amount
}
 
/**
 * Keeps a table-style header card's left/right edges aligned with VirtualCardGrid's rows,
 * regardless of the padding used by the header's own container-fluid, and accounting for
 * the rows' scrollbar (when present) eating into their available width.
 */
export function useHeaderRowAlignment(
  rootEl: Ref<HTMLElement | null>,
  scrollbarWidth: Ref<number>
) {
  const rowContainerPadding = ref(DEFAULT_ROW_CONTAINER_PADDING)
  const containerPaddingLeft = ref(0)
  const containerPaddingRight = ref(0)
 
  function measure() {
    const container = rootEl.value?.parentElement
    Iif (!container) return
    rowContainerPadding.value = readPageGutter(container)
    const style = window.getComputedStyle(container)
    containerPaddingLeft.value = parseFloat(style.paddingLeft) || 0
    containerPaddingRight.value = parseFloat(style.paddingRight) || 0
  }
 
  let resizeObserver: ResizeObserver | null = null
 
  onMounted(() => {
    measure()
    const container = rootEl.value?.parentElement
    Eif (container) {
      resizeObserver = new ResizeObserver(measure)
      resizeObserver.observe(container)
    }
    // Padding can change at a breakpoint without necessarily resizing the container itself.
    window.addEventListener('resize', measure)
  })
 
  onUnmounted(() => {
    resizeObserver?.disconnect()
    window.removeEventListener('resize', measure)
  })
 
  const marginLeft = computed(() => rowContainerPadding.value - containerPaddingLeft.value)
  const marginRight = computed(
    () => rowContainerPadding.value - containerPaddingRight.value + scrollbarWidth.value
  )
 
  return { marginLeft, marginRight }
}