覆疊元件
工具提示
在指標停留、取得焦點或長按時顯示的深色短標籤。
本頁內容
工具提示是簡短的深色標籤,用來顯示控制項的名稱或快速鍵。它使用提示型彈出層。請在標籤上設定 popover="hint" 和 .tooltip,並在控制項上設定 interestfor。指標停留或控制項取得鍵盤焦點後,瀏覽器會稍作延遲再顯示提示,在觸控螢幕上則可長按顯示。按 Esc 可隱藏提示。顯示提示時,已開啟的選單會保持開啟。CSS 錨點定位會將提示置中在控制項上方。
工具提示
<button type="button" class="btn btn-square" interestfor="tip-search" aria-label="Search"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <circle cx="7" cy="7" r="4.5" /><path d="M10.182 10.182L14 14" /> </svg></button><button type="button" class="btn btn-square" interestfor="tip-filter" aria-label="Filter"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4.5365 3L11.4635 3A1 1 0 0 1 12.2548 4.6114L9.4587 8.2299A1 1 0 0 0 9.25 8.8413L9.25 11.323A1 1 0 0 1 8.6214 12.2514L7.875 12.55A0.8203 0.8203 0 0 1 6.75 11.7883L6.75 8.8413A1 1 0 0 0 6.5413 8.2299L3.7452 4.6114A1 1 0 0 1 4.5365 3Z" /> </svg></button><div id="tip-search" popover="hint" class="tooltip">Search <kbd class="kbd">/</kbd></div><div id="tip-filter" popover="hint" class="tooltip">Filter <kbd class="kbd">F</kbd></div>import SearchIcon from '@master/icons-react/native/16/soft/search'import FilterIcon from '@master/icons-react/native/16/soft/filter'<> <button type="button" className="btn btn-square" {...{ interestfor: 'tip-search' }} aria-label="Search"> <SearchIcon className="icon" /> </button> <button type="button" className="btn btn-square" {...{ interestfor: 'tip-filter' }} aria-label="Filter"> <FilterIcon className="icon" /> </button> <div id="tip-search" popover="hint" className="tooltip">Search <kbd className="kbd">/</kbd></div> <div id="tip-filter" popover="hint" className="tooltip">Filter <kbd className="kbd">F</kbd></div></><script> import SearchIcon from '@master/icons-svelte/native/16/soft/search' import FilterIcon from '@master/icons-svelte/native/16/soft/filter'</script><button type="button" class="btn btn-square" interestfor="tip-search" aria-label="Search"> <SearchIcon class="icon" /></button><button type="button" class="btn btn-square" interestfor="tip-filter" aria-label="Filter"> <FilterIcon class="icon" /></button><div id="tip-search" popover="hint" class="tooltip">Search <kbd class="kbd">/</kbd></div><div id="tip-filter" popover="hint" class="tooltip">Filter <kbd class="kbd">F</kbd></div><script setup>import SearchIcon from '@master/icons-vue/native/16/soft/search'import FilterIcon from '@master/icons-vue/native/16/soft/filter'</script><template> <button type="button" class="btn btn-square" interestfor="tip-search" aria-label="Search"> <SearchIcon class="icon" /> </button> <button type="button" class="btn btn-square" interestfor="tip-filter" aria-label="Filter"> <FilterIcon class="icon" /> </button> <div id="tip-search" popover="hint" class="tooltip">Search <kbd class="kbd">/</kbd></div> <div id="tip-filter" popover="hint" class="tooltip">Filter <kbd class="kbd">F</kbd></div></template>將指標移到按鈕上,或按 Tab 將焦點移到按鈕。
#位置
工具提示位於控制項上方,間隔 6px。上方空間不足時,會改到下方。頂部工具列的控制項可用 tooltip-bottom 優先將提示放在下方。
下方
<button type="button" class="btn" interestfor="tip-deploy">Deploy</button><div id="tip-deploy" popover="hint" class="tooltip tooltip-bottom"> Ships the main branch to production</div><> <button type="button" className="btn" {...{ interestfor: 'tip-deploy' }}>Deploy</button> <div id="tip-deploy" popover="hint" className="tooltip tooltip-bottom"> Ships the main branch to production </div></><button type="button" class="btn" interestfor="tip-deploy">Deploy</button><div id="tip-deploy" popover="hint" class="tooltip tooltip-bottom"> Ships the main branch to production</div><template> <button type="button" class="btn" interestfor="tip-deploy">Deploy</button> <div id="tip-deploy" popover="hint" class="tooltip tooltip-bottom"> Ships the main branch to production </div></template>#內容
工具提示用幾個字就好。文字到 16rem 寬時會換行,並平衡各行的長度。工具提示會調整按鍵標示使用的 token,因此內部的 kbd 可直接在深色表面上清楚呈現,無須指定變體。連結、表單等需要使用者操作的內容,請用彈出面板呈現。
#無障礙
- 工具提示用來補充控制項的名稱。只有圖示的按鈕仍須保留
aria-label,因為觸控螢幕上只有長按才會顯示提示。 - 瀏覽器會將工具提示與控制項關聯起來,讓輔助技術將提示朗讀為控制項的說明。
- Chrome 和 Edge 支援 interestfor 觸發機制。瀏覽器缺少這項支援時,工具提示不會顯示,但控制項仍保有無障礙名稱。若也要在這些瀏覽器顯示提示,可在指標與焦點事件的處理函式中呼叫
showPopover()和hidePopover(),並沿用同一份樣式表。
#React、Svelte 和 Vue
tooltip 元件透過 ID 將控制項與工具提示關聯起來。控制項沿用頁面本身的標記,工具提示由瀏覽器顯示。使用 npx master-ui add tooltip 將元件複製到專案中,並參閱 React、Svelte 和 Vue 瞭解元件的建構方式和用法。
#類別
| 類別 | 說明 |
|---|---|
tooltip | 將標籤放在設有 popover="hint" 的元素上,缺少 popover 屬性時便不會顯示。標籤置中於控制項上方,相距 --anchored-gap(6px)。上方空間不足時會移到下方,文字達 16rem 寬時換行。內部的 kbd 沿用預設樣式,在以前景色為底的工具提示中仍清楚可讀。 |
tooltip-bottom | 優先使用控制項下方的空間,適合頂端列中的控制項搭配 tooltip 使用 |
#自訂屬性
| 屬性 | 預設值 | 說明 |
|---|---|---|
--anchored-gap | 浮層與觸發控制項之間的間距,未設定時為 6px。設定於 tooltip |