เลย์เอาต์หัวข้อ

ควบคุม header container ด้วย useFlexLayoutControl โดยพับ header ตามทิศทางการเลื่อน และรักษาสัดส่วนการแสดงผลปัจจุบันเมื่อเปลี่ยนเส้นทาง


โครงสร้าง root/head

เพื่อควบคุม header ต้องมี header ที่ FlexLayoutContainerประกอบด้วย head container ภายใน root layout และส่วนที่เหลือของหน้าจอจะถูกจัดเรียงใน body container

tsx
<FlexLayout layoutName="root" direction="column">
  <FlexLayoutContainer containerName="head">
    <Header />
  </FlexLayoutContainer>

  <FlexLayoutContainer containerName="body">
    {children}
  </FlexLayoutContainer>
</FlexLayout>

ควบคุม Header

ส่งมอบเงื่อนไขการแสดงและซ่อนจาก Header component ปัจจุบัน pathnameและ contentRefเชื่อมต่อกับ wrapper เนื้อหา header ที่แท้จริง

tsx
"use client";

import { useFlexLayoutControl } from "@byeolnaerim/flex-layout";
import { usePathname } from "next/navigation";

export default function HeaderShell() {
  const pathname = usePathname();

  const hideHeaderByRoute =
    pathname.startsWith("/editor") || pathname.startsWith("/viewer");

  const { contentRef } = useFlexLayoutControl({
    layoutName: "root",
    containerName: "head",
    pathname,
    hideContainer: hideHeaderByRoute ? true : null,
    enableScrollHideOnScroll: true,
    containerSizeKeyName: "__root_headSize",
  });

  return (
    <header ref={contentRef}>
      <GlobalNavigation />
    </header>
  );
}

พับด้วย scroll

enableScrollHideOnScrollไปยัง trueเมื่อเปิดใช้งาน จะทำให้ header container หดเมื่อเลื่อนลง และจะขยายเมื่อเลื่อนขึ้นหรือกลับไปที่ด้านบนของหน้า

ซ่อน/แสดงตามเงื่อนไข route

หากต้องการซ่อนหรือแสดง header อย่างชัดเจนใน route เฉพาะ ให้ส่งเงื่อนไขนั้นไปที่ hideContainerค่าคือ trueจะทำให้ header container หด และค่าคือ falseจะทำให้ header container ขยาย ในทั้งสองกรณี การหด/ขยายอัตโนมัติตาม scroll จะถูกปิดใช้งาน

tsx
const pathname = usePathname();

const shouldForceHideHeader =
  pathname.startsWith("/editor") || pathname.startsWith("/viewer");

const { contentRef } = useFlexLayoutControl({
  layoutName: "root",
  containerName: "head",
  pathname,
  hideContainer: shouldForceHideHeader ? true : null,
  enableScrollHideOnScroll: true,
  containerSizeKeyName: "__root_headSize",
});

ใช้ขนาด Header

หากต้องการขนาดที่แสดงอยู่ของ header ให้สร้าง state ด้วยค่าเดียวกัน paramscontainerSizeKeyName

tsx
import { createContainerSizeState } from "@byeolnaerim/flex-layout";

const [, , useHeaderSize] = createContainerSizeState({
  containerSizeKeyName: "__root_headSize",
});

function ContentLayout() {
  const headerSize = useHeaderSize();

  return (
    <main style={{ paddingTop: headerSize }}>
      Content
    </main>
  );
}

return value

layoutName

제어할 container가 들어 있는 decomposition layout 이름입니다. header 예시에서는 root를 사용합니다.

containerName

flex-grow로 제어할 decomposition container 이름입니다. header 예시에서는 head를 사용합니다.

pathname

route 변경을 감지하고 현재 main-axis 노출 비율을 새 route에 동기화하는 데 사용합니다.

hideContainer

대상 container의 표시 상태를 명시적으로 제어하는 tri-state 값입니다. true이면 강제 숨김, false이면 강제 표시, null/undefined이면 명시적 제어 없이 enableScrollHideOnScroll 설정을 따릅니다.

enableScrollHideOnScroll

hideContainer가 null/undefined일 때 scroll 방향에 따라 대상 container를 자동으로 접거나 복원할지 여부입니다. hideContainer가 true 또는 false이면 이 설정은 적용되지 않습니다.

containerSizeKeyName

측정된 container main-axis size를 저장할 global-rx-state key입니다.

containerSizeStorageOptions

container size state에 적용할 global-rx-state storage 옵션입니다.

Open

contentRef

실제 content size를 측정할 wrapper에 연결하는 ref입니다.