Skip to documentation

Design system / Sources & standards

Current repository source

use-member-layout.ts

src/components/members/use-member-layout.ts

This view contains the escaped source read during the deployment build. Code and policy text are unchanged. Markdown is shown as source, not rendered as a document. A new deployment refreshes the content.

"use client";

import { useEffect, useLayoutEffect, useRef, useState } from "react";

/** Observe the component's available width, not the viewport around a nested tab/panel.
 * The threshold follows the canonical medium container token. Compact SSR order is
 * summary-first; keyed React children retain their state when the order changes. */
export function useMemberLayout(enabled = true) {
  const ref = useRef<HTMLElement>(null);
  const [wide, setWide] = useState(false);
  const previousWide = useRef(false);
  const focus = useRef<{ element: HTMLElement; selection: [number, number] | null } | null>(null);
  useEffect(() => {
    if (!enabled) return;
    const root = ref.current;
    if (!root || typeof ResizeObserver === "undefined") return;
    const observer = new ResizeObserver(([entry]) => {
      const token = getComputedStyle(root).getPropertyValue("--cxh-container-md").trim();
      const rem = parseFloat(getComputedStyle(document.documentElement).fontSize) || 10;
      // 768px is the canonical fallback while stylesheet tokens are unavailable.
      const threshold = token.endsWith("rem") ? parseFloat(token) * rem : parseFloat(token) || 768;
      const next = entry.contentRect.width >= threshold;
      if (next === previousWide.current) return;
      previousWide.current = next;
      const active = document.activeElement;
      if (active instanceof HTMLElement && root.contains(active)) {
        const selection = (active instanceof HTMLInputElement || active instanceof HTMLTextAreaElement) && active.selectionStart !== null
          ? [active.selectionStart, active.selectionEnd!] as [number, number] : null;
        focus.current = { element: active, selection };
      }
      setWide(next);
    });
    observer.observe(root);
    return () => observer.disconnect();
  }, [enabled]);
  useLayoutEffect(() => {
    const saved = focus.current;
    focus.current = null;
    if (!saved?.element.isConnected) return;
    saved.element.focus({ preventScroll: true });
    if (saved.selection && (saved.element instanceof HTMLInputElement || saved.element instanceof HTMLTextAreaElement)) {
      saved.element.setSelectionRange(...saved.selection);
    }
  }, [wide]);
  return { ref, wide };
}
All sources and standards
Current components and theme tokens. Fictional example data. Source views refresh on deployment; authors maintain contract notes.