For AI agents: the complete documentation index is available at https://ifhover.github.io/leaf-ui/llms.txt, the full documentation bundle is available at https://ifhover.github.io/leaf-ui/llms-full.txt, and this page is available as Markdown at https://ifhover.github.io/leaf-ui/components/layout.md.

Layout 布局

组织页面的头部、侧栏、内容与底部。

基本用法

用 Header、Sider、Content 和 Footer 组合页面,横向嵌套区域负责排列侧栏与内容。

顶部导航
正文内容
底部信息
basic.tsx
import { Layout, Text } from '@ifhover/leaf-ui';

export function LayoutBasic() {
  return (
    <Layout>
      <Layout.Header>
        <Text>顶部导航</Text>
      </Layout.Header>
      <Layout direction="horizontal">
        <Layout.Sider width={140}>侧栏</Layout.Sider>
        <Layout.Content>正文内容</Layout.Content>
      </Layout>
      <Layout.Footer>底部信息</Layout.Footer>
    </Layout>
  );
}

响应式侧栏

侧栏支持手动折叠,在窄屏上自动收起;菜单同步切换为图标导航。

概览

选择导航项查看内容。
advanced.tsx
import { Card, Layout, Menu, useBreakpoint } from '@ifhover/leaf-ui';

import { Folder, LayoutDashboard } from 'lucide-react';

import { useState } from 'react';

export function LayoutResponsive() {
  const [collapsed, setCollapsed] = useState(false);
  const { sm } = useBreakpoint();
  const compact = collapsed || !sm;
  const [selected, setSelected] = useState('overview');
  const items = [
    { key: 'overview', label: '概览', icon: <LayoutDashboard /> },
    { key: 'projects', label: '项目', icon: <Folder /> },
  ];
  return (
    <Layout direction="horizontal">
      <Layout.Sider
        width={180}
        collapsedWidth={64}
        collapsed={collapsed}
        onCollapse={setCollapsed}
        collapsible
        breakpoint={575}
      >
        <Menu
          mode="inline"
          collapsed={compact}
          selectedKey={selected}
          onSelect={setSelected}
          items={items}
        />
      </Layout.Sider>
      <Layout.Content>
        <Card title={items.find((item) => item.key === selected)?.label}>选择导航项查看内容。</Card>
      </Layout.Content>
    </Layout>
  );
}

使用说明

用 Layout.Header、Layout.Sider、Layout.Content、Layout.Footer 组合页面;横向区域使用 direction="horizontal"。侧栏可受控折叠,也可通过 breakpoint 自动收起。

类型

组装数据、定义回调和使用 ref 时,可以导入这些类型。

import type { LayoutProps, LayoutSiderProps } from '@ifhover/leaf-ui';

LayoutProps API

属性类型默认值说明
direction'horizontal' | 'vertical''vertical'排列方向或移动方向

LayoutSiderProps API

属性类型默认值说明
widthnumber | string220宽度,数字按像素计算
collapsedWidthnumber | string64收起后的宽度
collapsedboolean—受控收起状态
defaultCollapsedbooleanfalse初始收起状态
collapsiblebooleanfalse显示收起操作
breakpointnumber—自动收起的屏幕宽度阈值
onCollapse(collapsed: boolean) => void—收起状态变化
triggerReactNode—自定义触发内容