Skip to content

High performance self-hosted photo and video management solution.

1.1 KB
Raw Blame
<script lang="ts">
  interface Props {
    class?: string;
    itemCount?: number;
    children?: import('svelte').Snippet<[{ itemCount: number }]>;
  }

  let { class: className, itemCount = $bindable(1), children }: Props = $props();

  let container: HTMLElement | undefined = $state();
  let contentRect: DOMRectReadOnly | undefined = $state();

  const getColumnGap = (element: Element) => {
    const style = getComputedStyle(element);
    return Number.parseInt(style.columnGap) || 0;
  };

  const getItemCount = (container: HTMLElement, containerWidth: number) => {
    if (!container.firstElementChild) {
      return 1;
    }

    const childContentRect = container.firstElementChild.getBoundingClientRect();
    const childWidth = Math.floor(childContentRect.width || Infinity);
    const columnGap = getColumnGap(container);

    return Math.floor((containerWidth + columnGap) / (childWidth + columnGap)) || 1;
  };

  $effect(() => {
    if (container && contentRect) {
      itemCount = getItemCount(container, contentRect.width);
    }
  });
</script>

<div class={className} bind:this={container} bind:contentRect>
  {@render children?.({ itemCount })}
</div>