应用栏

应用栏就是一个内含一行内容的 <header>。各个部件是分开的,方便你自行排布:appBarNav() 放链接,appBarSpacer() 把后面的内容顶到另一端,appBarActions() 放末尾的按钮。

基础应用栏

sitelo
appBar({ brand: 'sitelo' },
  appBarSpacer(),
  appBarActions(
    button({ size: 'sm', variant: 'soft' }, '登录'),
  ),
)

带导航

navLink() 是应用栏里的链接样式;current 除了配色,还会用 aria-current 标出当前页面。

sitelo
appBar({ brand: 'sitelo' },
  appBarNav(
    navLink({ href: '#docs', current: true }, '文档'),
    navLink({ href: '#ui' }, 'UI'),
    navLink({ href: '#examples' }, '示例'),
  ),
  appBarSpacer(),
  appBarActions(
    button({ size: 'sm', variant: 'outline', color: 'neutral' }, 'GitHub'),
    button({ size: 'sm' }, '开始使用'),
  ),
)

带标识的品牌

品牌位可以放任意标记,默认链接到 /,除非 href 另有指定。

Ssitelo
v2.6.3
appBar({
  href: '#home',
  brand: stack({ direction: 'row', gap: 'sm', inline: true, align: 'center' },
    avatar({ name: 'S', size: 'sm', square: true, color: 'primary' }),
    'sitelo',
  ),
},
  appBarSpacer(),
  appBarActions(chip({ size: 'sm', color: 'neutral' }, 'v2.6.3')),
)

吸顶与毛玻璃

sticky 把栏钉在滚动容器顶部;blur 让它半透明,内容从下面穿过。这里两者都放在一个可滚动的方框里演示,而不是用在页面本身。

sitelo
sticky

滚动看看——第 1 段。

滚动看看——第 2 段。

滚动看看——第 3 段。

滚动看看——第 4 段。

滚动看看——第 5 段。

滚动看看——第 6 段。

div({ style: 'height: 12rem; overflow: auto; border: 1px solid var(--su-border); border-radius: 0.6rem' },
  appBar({ brand: 'sitelo', sticky: true, blur: true },
    appBarSpacer(),
    appBarActions(chip({ size: 'sm', color: 'primary' }, 'sticky')),
  ),
  container({ size: 'sm', style: 'padding-block: 1rem' },
    stack({ gap: 'md' },
      ...Array.from({ length: 6 }, (unused, index) =>
        text({ variant: 'small', tone: 'muted' }, '滚动看看——第 ' + (index + 1) + ' 段。'),
      ),
    ),
  ),
)

滚动时让开

sticky: 'auto' 同样把栏钉在顶部,但页面向下滚动时把它滑走,一旦向上滚动或回到顶部就立刻滑回来。键盘焦点在栏内、或栏里的菜单打开时,它会一直保持可见。

在 Chromium 中这完全由 CSS 实现(@container scroll-state(scrolled)),不加载任何东西。其他浏览器会在页面加载时获取一个小模块,改由它来监听滚动。栏跟随页面滚动;放在你自己的滚动容器里时(比如这里),还要给容器加上 container-type: scroll-state。

sitelo
auto

向下滚,再滚回来——第 1 段。

向下滚,再滚回来——第 2 段。

向下滚,再滚回来——第 3 段。

向下滚,再滚回来——第 4 段。

向下滚,再滚回来——第 5 段。

向下滚,再滚回来——第 6 段。

向下滚,再滚回来——第 7 段。

向下滚,再滚回来——第 8 段。

向下滚,再滚回来——第 9 段。

向下滚,再滚回来——第 10 段。

向下滚,再滚回来——第 11 段。

向下滚,再滚回来——第 12 段。

div({ style: 'height: 12rem; overflow: auto; container-type: scroll-state; border: 1px solid var(--su-border); border-radius: 0.6rem' },
  appBar({ brand: 'sitelo', sticky: 'auto', blur: true },
    appBarSpacer(),
    appBarActions(chip({ size: 'sm', color: 'primary' }, 'auto')),
  ),
  container({ size: 'sm', style: 'padding-block: 1rem' },
    stack({ gap: 'md' },
      ...Array.from({ length: 12 }, (unused, index) =>
        text({ variant: 'small', tone: 'muted' }, '向下滚,再滚回来——第 ' + (index + 1) + ' 段。'),
      ),
    ),
  ),
)

小屏配抽屉

常见做法:桌面端把链接放在栏里,手机端用一个按钮打开 drawer()。抽屉是一个 <dialog>,按钮通过 command 打开它,所以不需要脚本。

fragment(
  appBar({ brand: 'sitelo' },
    appBarSpacer(),
    appBarActions(
      themeToggle(),
      iconButton({
        label: '打开导航',
        variant: 'ghost',
        color: 'neutral',
        commandfor: 'app-bar-drawer', command: 'show-modal',
        icon: icon('menu'),
      }),
    ),
  ),
  drawer({ id: 'app-bar-drawer', title: '导航' },
    navLink({ href: '#docs' }, '文档'),
    navLink({ href: '#ui' }, 'UI'),
    navLink({ href: '#examples' }, '示例'),
  ),
)

属性

appBar():

属性类型默认值说明
brandChild—开头那个品牌链接的内容。
hrefstring'/'品牌链接指向哪里。
stickyboolean | 'auto'false滚动时把栏钉在顶部。'auto' 还会在向下滚动时把它藏起来。
blurbooleanfalse半透明背景,并对其后内容做模糊。
asstring'header'渲染成哪个元素。

各个部件:

部件属性默认值说明
appBarNav—承载链接的 nav 元素。
appBarSpacer—弹性空隙;它之后的一切都会靠到另一端。
appBarActions—末尾的按钮组。
navLinkhref, current, color—应用栏样式的链接;current 标出当前页面。