網站導覽
分頁標籤
用一列文字標籤切換頁面或面板,並以深色底線標示目前選取的項目。
本頁內容
分頁標籤沿著細線排列,用來切換同層的頁面或面板。目前選取的標籤會加上深色底線。如果要在有限空間內選擇數值或選項,請使用分段控制項。
瀏覽器支援 CSS 錨點定位時,所有分頁標籤共用一條底線。aria-selected 變更時,底線會滑到目前的分頁標籤,頂部的圓角讓線條收尾更柔和。其他瀏覽器則由各分頁標籤各自繪製底線。
分頁標籤
<nav class="tabs" aria-label="Project"> <a class="tabs-item" href="#overview" aria-current="page">Overview</a> <a class="tabs-item" href="#activity">Activity</a> <a class="tabs-item" href="#settings">Settings</a></nav><nav className="tabs" aria-label="Project"> <a className="tabs-item" href="#overview" aria-current="page">Overview</a> <a className="tabs-item" href="#activity">Activity</a> <a className="tabs-item" href="#settings">Settings</a></nav><nav class="tabs" aria-label="Project"> <a class="tabs-item" href="#overview" aria-current="page">Overview</a> <a class="tabs-item" href="#activity">Activity</a> <a class="tabs-item" href="#settings">Settings</a></nav><template> <nav class="tabs" aria-label="Project"> <a class="tabs-item" href="#overview" aria-current="page">Overview</a> <a class="tabs-item" href="#activity">Activity</a> <a class="tabs-item" href="#settings">Settings</a> </nav></template>#顯示數量
顯示數量
<nav class="tabs" aria-label="Repository"> <a class="tabs-item" href="#code" aria-current="page">Code</a> <a class="tabs-item" href="#issues">Issues <span class="badge badge-sm badge-pill">24</span></a> <a class="tabs-item" href="#pulls"> Pull requests <span class="badge badge-sm badge-pill">3</span> </a></nav><nav className="tabs" aria-label="Repository"> <a className="tabs-item" href="#code" aria-current="page">Code</a> <a className="tabs-item" href="#issues"> Issues <span className="badge badge-sm badge-pill">24</span> </a> <a className="tabs-item" href="#pulls"> Pull requests <span className="badge badge-sm badge-pill">3</span> </a></nav><nav class="tabs" aria-label="Repository"> <a class="tabs-item" href="#code" aria-current="page">Code</a> <a class="tabs-item" href="#issues">Issues <span class="badge badge-sm badge-pill">24</span></a> <a class="tabs-item" href="#pulls"> Pull requests <span class="badge badge-sm badge-pill">3</span> </a></nav><template> <nav class="tabs" aria-label="Repository"> <a class="tabs-item" href="#code" aria-current="page">Code</a> <a class="tabs-item" href="#issues">Issues <span class="badge badge-sm badge-pill">24</span></a> <a class="tabs-item" href="#pulls"> Pull requests <span class="badge badge-sm badge-pill">3</span> </a> </nav></template>#小尺寸
小尺寸
<nav class="tabs tabs-sm" aria-label="Period"> <a class="tabs-item" href="#day">Day</a> <a class="tabs-item" href="#week" aria-current="page">Week</a> <a class="tabs-item" href="#month">Month</a></nav><nav className="tabs tabs-sm" aria-label="Period"> <a className="tabs-item" href="#day">Day</a> <a className="tabs-item" href="#week" aria-current="page">Week</a> <a className="tabs-item" href="#month">Month</a></nav><nav class="tabs tabs-sm" aria-label="Period"> <a class="tabs-item" href="#day">Day</a> <a class="tabs-item" href="#week" aria-current="page">Week</a> <a class="tabs-item" href="#month">Month</a></nav><template> <nav class="tabs tabs-sm" aria-label="Period"> <a class="tabs-item" href="#day">Day</a> <a class="tabs-item" href="#week" aria-current="page">Week</a> <a class="tabs-item" href="#month">Month</a> </nav></template>#分頁面板
頁面內的面板請依循 WAI-ARIA 分頁標籤模式,使用帶有 role="tablist" 的容器、帶有 role="tab"、aria-selected 和 aria-controls 的按鈕,以及帶有 role="tabpanel" 的區域。樣式表負責呈現各種狀態,tabs 行為負責切換面板,並讓分頁標籤清單支援鍵盤操作。
分頁標籤清單
<div class="tabs" role="tablist" aria-label="Account"> <button type="button" class="tabs-item" role="tab" id="tab-profile" aria-selected="true" aria-controls="panel-profile"> Profile </button> <button type="button" class="tabs-item" role="tab" id="tab-billing" aria-selected="false" aria-controls="panel-billing" tabindex="-1"> Billing </button> <button type="button" class="tabs-item" role="tab" id="tab-members" aria-selected="false" aria-controls="panel-members" tabindex="-1"> Members </button></div><div role="tabpanel" tabindex="0" id="panel-profile" aria-labelledby="tab-profile" class="pt-md text-sm fg-text-body"> Profile settings</div><div role="tabpanel" tabindex="0" id="panel-billing" aria-labelledby="tab-billing" class="pt-md text-sm fg-text-body" hidden> Billing settings</div><div role="tabpanel" tabindex="0" id="panel-members" aria-labelledby="tab-members" class="pt-md text-sm fg-text-body" hidden> Members of the workspace</div><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root}> <div className="tabs" role="tablist" aria-label="Account"> <button type="button" className="tabs-item" role="tab" id="tab-profile" aria-selected="true" aria-controls="panel-profile"> Profile </button> <button type="button" className="tabs-item" role="tab" id="tab-billing" aria-selected="false" aria-controls="panel-billing" tabIndex={-1}> Billing </button> <button type="button" className="tabs-item" role="tab" id="tab-members" aria-selected="false" aria-controls="panel-members" tabIndex={-1}> Members </button> </div> <div role="tabpanel" tabIndex={0} id="panel-profile" aria-labelledby="tab-profile" className="pt-md text-sm fg-text-body"> Profile settings </div> <div role="tabpanel" tabIndex={0} id="panel-billing" aria-labelledby="tab-billing" className="pt-md text-sm fg-text-body" hidden> Billing settings </div> <div role="tabpanel" tabIndex={0} id="panel-members" aria-labelledby="tab-members" className="pt-md text-sm fg-text-body" hidden> Members of the workspace </div> </div> )}<script> import { attach } from '@master/ui/behaviors'</script><div {@attach attach}> <div class="tabs" role="tablist" aria-label="Account"> <button type="button" class="tabs-item" role="tab" id="tab-profile" aria-selected="true" aria-controls="panel-profile"> Profile </button> <button type="button" class="tabs-item" role="tab" id="tab-billing" aria-selected="false" aria-controls="panel-billing" tabindex="-1"> Billing </button> <button type="button" class="tabs-item" role="tab" id="tab-members" aria-selected="false" aria-controls="panel-members" tabindex="-1"> Members </button> </div> <div role="tabpanel" tabindex="0" id="panel-profile" aria-labelledby="tab-profile" class="pt-md text-sm fg-text-body"> Profile settings </div> <div role="tabpanel" tabindex="0" id="panel-billing" aria-labelledby="tab-billing" class="pt-md text-sm fg-text-body" hidden> Billing settings </div> <div role="tabpanel" tabindex="0" id="panel-members" aria-labelledby="tab-members" class="pt-md text-sm fg-text-body" hidden> Members of the workspace </div></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root"> <div class="tabs" role="tablist" aria-label="Account"> <button type="button" class="tabs-item" role="tab" id="tab-profile" aria-selected="true" aria-controls="panel-profile"> Profile </button> <button type="button" class="tabs-item" role="tab" id="tab-billing" aria-selected="false" aria-controls="panel-billing" tabindex="-1"> Billing </button> <button type="button" class="tabs-item" role="tab" id="tab-members" aria-selected="false" aria-controls="panel-members" tabindex="-1"> Members </button> </div> <div role="tabpanel" tabindex="0" id="panel-profile" aria-labelledby="tab-profile" class="pt-md text-sm fg-text-body"> Profile settings </div> <div role="tabpanel" tabindex="0" id="panel-billing" aria-labelledby="tab-billing" class="pt-md text-sm fg-text-body" hidden> Billing settings </div> <div role="tabpanel" tabindex="0" id="panel-members" aria-labelledby="tab-members" class="pt-md text-sm fg-text-body" hidden> Members of the workspace </div> </div></template>將焦點移到分頁標籤後,可使用方向鍵、Home 或 End。也可以點按分頁標籤來選取。
tabs 接收分頁標籤列,並回傳卸載行為的函式。
import { tabs } from '@master/ui/behaviors/tabs.js'const detach = tabs(document.querySelector('.tabs[role="tablist"]'))- 單一 Tab 停駐點。 只有選取的分頁標籤會進入 Tab 巡覽順序。方向鍵會移到上一個或下一個分頁標籤並選取它,略過停用的分頁標籤,到了兩端則循環切換。Home 和 End 分別移到第一個與最後一個分頁標籤。帶有
aria-orientation="vertical"的分頁標籤清單改用上下方向鍵。 - 面板。 每個分頁標籤以
aria-controls指向自己的面板。選取的分頁標籤會顯示對應面板,其餘面板則加上hidden。 - 值。 選取另一個分頁標籤時,分頁標籤清單會依序發出可冒泡的
input和change事件,兩者的detail.value都是分頁標籤的data-value,若未設定則取分頁標籤文字。行為回傳的函式也有相同的value,可從外部設定它來選取分頁標籤,這不會發出任何事件。 - 沒有腳本時,只有已選取分頁標籤的面板未加上
hidden,也無法切換面板。如果每個分頁標籤都能做成獨立頁面,請在nav中使用連結。
#可搜尋的面板
在 tabs() 的第二個引數傳入 { searchable: true },或設定框架元件的 searchable 屬性,可透過頁內搜尋與片段連結找到文字較多的未選取面板。原生 beforematch 會選取該面板對應的分頁標籤,並派送與點擊相同的事件。請把裝飾和間距放在內層元素,讓隱藏的面板不占空間。不支援 beforematch 的瀏覽器沿用一般的隱藏行為。
#React、Svelte 和 Vue
tabs 元件根據資料呈現分頁標籤清單及面板,以 ID 將各分頁標籤連到對應面板,並掛載行為,讓外部可傳入選取值並取得變更。若分頁標籤是 nav 內的連結,直接使用標記即可。執行 npx master-ui add tabs 將元件複製到專案中。React、Svelte 和 Vue說明元件的實作與用法。
#類別
| 類別 | 說明 |
|---|---|
tabs | 帶有細線的分頁列,套用在有標籤的 nav 或設有 role="tablist" 的元素上。分頁放不下時可水平捲動 |
tabs-item | 分頁可用帶有 aria-current="page" 的連結,或帶有 role="tab" 與 aria-selected 的按鈕 |
tabs-sm | 緊湊分頁高 36px,標籤字級為 13px。預設高度為 44px,標籤字級為 14px搭配 tabs 使用 |