網站導覽

v2.1.0
資料顯示

甘特圖

在同一條時間刻度上,以長條呈現各事項的持續時間,例如專案階段、值班班表或發布排程。

本頁內容

甘特圖在同一條時間刻度上以長條呈現有持續時間的事項,例如專案階段、值班時段或定期發行排程。它以清單呈現,每列包含長條的起訖位置、項目名稱和長條上的文字。樣式表負責版面配置,將名稱與長條分成兩欄,並在每個刻度單位的起點畫一條細線。圖表本身不需要腳本或 SVG;在讀者需要規劃的地方,gantt 行為讓他們可以移動長條並調整長度。

甘特圖

<div class="card max-w(560) mx-auto">  <ol class="gantt gantt-now" style="--gantt-from: 0; --gantt-to: 8; --gantt-now: 4.4" aria-label="Launch plan, in weeks">    <li class="gantt-row gantt-scale" aria-hidden="true">      <ol class="gantt-ticks">        <li>W1</li>        <li>W2</li>        <li>W3</li>        <li>W4</li>        <li>W5</li>        <li>W6</li>        <li>W7</li>        <li>W8</li>      </ol>    </li>    <li class="gantt-row" style="--from: 0; --to: 3; --progress: 100">      <span class="gantt-label">Research</span><span class="gantt-bar">Weeks 1 to 3</span>    </li>    <li class="gantt-row" aria-current="true" style="--from: 2; --to: 6">      <span class="gantt-label">Design</span><span class="gantt-bar">Weeks 3 to 6</span>    </li>    <li class="gantt-row" style="--from: 4; --to: 7.5; --progress: 15">      <span class="gantt-label">Build</span><span class="gantt-bar">Weeks 5 to 8</span>    </li>    <li class="gantt-row" style="--from: 7.5">      <span class="gantt-label">Launch</span><span class="gantt-bar gantt-milestone">Middle of week 8</span>    </li>  </ol></div>
<div className="card max-w(560) mx-auto">  <ol className="gantt gantt-now" style={{ '--gantt-from': '0', '--gantt-to': '8', '--gantt-now': '4.4' } as React.CSSProperties} aria-label="Launch plan, in weeks">    <li className="gantt-row gantt-scale" aria-hidden="true">      <ol className="gantt-ticks">        <li>W1</li>        <li>W2</li>        <li>W3</li>        <li>W4</li>        <li>W5</li>        <li>W6</li>        <li>W7</li>        <li>W8</li>      </ol>    </li>    <li className="gantt-row" style={{ '--from': '0', '--to': '3', '--progress': '100' } as React.CSSProperties}>      <span className="gantt-label">Research</span><span className="gantt-bar">Weeks 1 to 3</span>    </li>    <li className="gantt-row" aria-current="true" style={{ '--from': '2', '--to': '6' } as React.CSSProperties}>      <span className="gantt-label">Design</span><span className="gantt-bar">Weeks 3 to 6</span>    </li>    <li className="gantt-row" style={{ '--from': '4', '--to': '7.5', '--progress': '15' } as React.CSSProperties}>      <span className="gantt-label">Build</span><span className="gantt-bar">Weeks 5 to 8</span>    </li>    <li className="gantt-row" style={{ '--from': '7.5' } as React.CSSProperties}>      <span className="gantt-label">Launch</span><span className="gantt-bar gantt-milestone">Middle of week 8</span>    </li>  </ol></div>
<div class="card max-w(560) mx-auto">  <ol class="gantt gantt-now" style="--gantt-from: 0; --gantt-to: 8; --gantt-now: 4.4" aria-label="Launch plan, in weeks">    <li class="gantt-row gantt-scale" aria-hidden="true">      <ol class="gantt-ticks">        <li>W1</li>        <li>W2</li>        <li>W3</li>        <li>W4</li>        <li>W5</li>        <li>W6</li>        <li>W7</li>        <li>W8</li>      </ol>    </li>    <li class="gantt-row" style="--from: 0; --to: 3; --progress: 100">      <span class="gantt-label">Research</span><span class="gantt-bar">Weeks 1 to 3</span>    </li>    <li class="gantt-row" aria-current="true" style="--from: 2; --to: 6">      <span class="gantt-label">Design</span><span class="gantt-bar">Weeks 3 to 6</span>    </li>    <li class="gantt-row" style="--from: 4; --to: 7.5; --progress: 15">      <span class="gantt-label">Build</span><span class="gantt-bar">Weeks 5 to 8</span>    </li>    <li class="gantt-row" style="--from: 7.5">      <span class="gantt-label">Launch</span><span class="gantt-bar gantt-milestone">Middle of week 8</span>    </li>  </ol></div>
<template>  <div class="card max-w(560) mx-auto">    <ol class="gantt gantt-now" style="--gantt-from: 0; --gantt-to: 8; --gantt-now: 4.4" aria-label="Launch plan, in weeks">      <li class="gantt-row gantt-scale" aria-hidden="true">        <ol class="gantt-ticks">          <li>W1</li>          <li>W2</li>          <li>W3</li>          <li>W4</li>          <li>W5</li>          <li>W6</li>          <li>W7</li>          <li>W8</li>        </ol>      </li>      <li class="gantt-row" style="--from: 0; --to: 3; --progress: 100">        <span class="gantt-label">Research</span><span class="gantt-bar">Weeks 1 to 3</span>      </li>      <li class="gantt-row" aria-current="true" style="--from: 2; --to: 6">        <span class="gantt-label">Design</span><span class="gantt-bar">Weeks 3 to 6</span>      </li>      <li class="gantt-row" style="--from: 4; --to: 7.5; --progress: 15">        <span class="gantt-label">Build</span><span class="gantt-bar">Weeks 5 to 8</span>      </li>      <li class="gantt-row" style="--from: 7.5">        <span class="gantt-label">Launch</span><span class="gantt-bar gantt-milestone">Middle of week 8</span>      </li>    </ol>  </div></template>

#長條定位

在清單上設定 --gantt-from 和 --gantt-to,指定刻度的起訖範圍。單位可自行選擇,例如天、週、小時或衝刺週期。每個 gantt-row 使用相同單位設定 --from 和 --to,其 gantt-bar 會依這兩個值在刻度範圍內的比例決定起訖位置。也可使用小數,讓長條在一個刻度單位中途結束。

名稱欄以最長的名稱為準,軌道占用其餘寬度。每列共用這兩欄,無須指定欄寬,長條就能對齊。長條後方的細線標出每個刻度單位。

用文字寫出長條所代表的資訊,例如日期、持續時間或負責人。即使長條太短,也請保留文字,讓螢幕閱讀器能讀出內容。放不下的文字會以省略號截斷。

#進度、目前列與現在時間

在列上設定 0 到 100 的 --progress,長條就會依該百分比填入較深的色調。以 aria-current="true" 標記的列會使用深色實心長條,表示讀者目前所處的階段,或這個畫面所聚焦的階段。

在清單上加上 gantt-now,就會在 --gantt-now 指定的位置畫出一條貫穿各列的深色線,標示今天。當指標停在某一列上時,該列的長條會加深一階,名稱也會改用高對比文字色,方便讀者在較寬的圖上沿著同一列閱讀。

#里程碑

里程碑代表單一時間點。讓該列只設定 --from,再將 gantt-milestone 加到長條上,就會在該位置畫出深色的小菱形。長條文字會保留在清單中供輔助科技讀取,但畫面上不會顯示,因此請在列名稱中寫明該時間點發生的事。

#刻度

使用 gantt-scale 的列用來標示各刻度單位。列內放一份 gantt-ticks 清單,每個刻度單位對應一個項目,各標籤會對齊該單位起點的細線。刻度將長條文字已有的資訊以視覺方式呈現,因此請加上 aria-hidden。

一天中的各時段

<div class="card max-w(560) mx-auto">  <ol class="gantt" style="--gantt-from: 0; --gantt-to: 24" aria-label="On call today">    <li class="gantt-row gantt-scale" aria-hidden="true">      <ol class="gantt-ticks">        <li>00</li>        <li></li>        <li></li>        <li></li>        <li></li>        <li></li>        <li>06</li>        <li></li>        <li></li>        <li></li>        <li></li>        <li></li>        <li>12</li>        <li></li>        <li></li>        <li></li>        <li></li>        <li></li>        <li>18</li>        <li></li>        <li></li>        <li></li>        <li></li>        <li></li>      </ol>    </li>    <li class="gantt-row" style="--from: 0; --to: 8">      <span class="gantt-label">Grace</span><span class="gantt-bar">00:00 to 08:00</span>    </li>    <li class="gantt-row" aria-current="true" style="--from: 8; --to: 16">      <span class="gantt-label">Ada</span><span class="gantt-bar">08:00 to 16:00</span>    </li>    <li class="gantt-row" style="--from: 16; --to: 24">      <span class="gantt-label">Alan</span><span class="gantt-bar">16:00 to 24:00</span>    </li>  </ol></div>
<div className="card max-w(560) mx-auto">  <ol className="gantt" style={{ '--gantt-from': '0', '--gantt-to': '24' } as React.CSSProperties} aria-label="On call today">    <li className="gantt-row gantt-scale" aria-hidden="true">      <ol className="gantt-ticks">        <li>00</li>        <li></li>        <li></li>        <li></li>        <li></li>        <li></li>        <li>06</li>        <li></li>        <li></li>        <li></li>        <li></li>        <li></li>        <li>12</li>        <li></li>        <li></li>        <li></li>        <li></li>        <li></li>        <li>18</li>        <li></li>        <li></li>        <li></li>        <li></li>        <li></li>      </ol>    </li>    <li className="gantt-row" style={{ '--from': '0', '--to': '8' } as React.CSSProperties}>      <span className="gantt-label">Grace</span><span className="gantt-bar">00:00 to 08:00</span>    </li>    <li className="gantt-row" aria-current="true" style={{ '--from': '8', '--to': '16' } as React.CSSProperties}>      <span className="gantt-label">Ada</span><span className="gantt-bar">08:00 to 16:00</span>    </li>    <li className="gantt-row" style={{ '--from': '16', '--to': '24' } as React.CSSProperties}>      <span className="gantt-label">Alan</span><span className="gantt-bar">16:00 to 24:00</span>    </li>  </ol></div>
<div class="card max-w(560) mx-auto">  <ol class="gantt" style="--gantt-from: 0; --gantt-to: 24" aria-label="On call today">    <li class="gantt-row gantt-scale" aria-hidden="true">      <ol class="gantt-ticks">        <li>00</li>        <li></li>        <li></li>        <li></li>        <li></li>        <li></li>        <li>06</li>        <li></li>        <li></li>        <li></li>        <li></li>        <li></li>        <li>12</li>        <li></li>        <li></li>        <li></li>        <li></li>        <li></li>        <li>18</li>        <li></li>        <li></li>        <li></li>        <li></li>        <li></li>      </ol>    </li>    <li class="gantt-row" style="--from: 0; --to: 8">      <span class="gantt-label">Grace</span><span class="gantt-bar">00:00 to 08:00</span>    </li>    <li class="gantt-row" aria-current="true" style="--from: 8; --to: 16">      <span class="gantt-label">Ada</span><span class="gantt-bar">08:00 to 16:00</span>    </li>    <li class="gantt-row" style="--from: 16; --to: 24">      <span class="gantt-label">Alan</span><span class="gantt-bar">16:00 to 24:00</span>    </li>  </ol></div>
<template>  <div class="card max-w(560) mx-auto">    <ol class="gantt" style="--gantt-from: 0; --gantt-to: 24" aria-label="On call today">      <li class="gantt-row gantt-scale" aria-hidden="true">        <ol class="gantt-ticks">          <li>00</li>          <li></li>          <li></li>          <li></li>          <li></li>          <li></li>          <li>06</li>          <li></li>          <li></li>          <li></li>          <li></li>          <li></li>          <li>12</li>          <li></li>          <li></li>          <li></li>          <li></li>          <li></li>          <li>18</li>          <li></li>          <li></li>          <li></li>          <li></li>          <li></li>        </ol>      </li>      <li class="gantt-row" style="--from: 0; --to: 8">        <span class="gantt-label">Grace</span><span class="gantt-bar">00:00 to 08:00</span>      </li>      <li class="gantt-row" aria-current="true" style="--from: 8; --to: 16">        <span class="gantt-label">Ada</span><span class="gantt-bar">08:00 to 16:00</span>      </li>      <li class="gantt-row" style="--from: 16; --to: 24">        <span class="gantt-label">Alan</span><span class="gantt-bar">16:00 to 24:00</span>      </li>    </ol>  </div></template>

#窄容器中的甘特圖

甘特圖本身是尺寸容器。當自身寬度低於 26rem 時,各列名稱會移到長條上方,讓軌道使用整個寬度。在手機上請縮短刻度範圍,八個單位容易閱讀,三十個就太擠。

#移動長條

將長條標記為 role="slider" 並掛載 gantt 行為,讀者就能移動它:拖曳長條本體可沿著刻度移動,拖曳任一端則可改變開始或結束的時間。長條會貼齊刻度的整數單位(這裡是天),按住期間,長條文字會顯示它將取得的範圍。Escape 會讓它回到原位。指標能做的事,按鍵都能做到。

移動長條

<div class="card max-w(640) mx-auto">  <ol class="gantt gantt-now" id="sprint" style="--gantt-from: 0; --gantt-to: 12; --gantt-now: 4.5" aria-label="Sprint plan, November 2 to 13">    <li class="gantt-row gantt-scale" aria-hidden="true">      <ol class="gantt-ticks">        <li>2</li>        <li>3</li>        <li>4</li>        <li>5</li>        <li>6</li>        <li>7</li>        <li>8</li>        <li>9</li>        <li>10</li>        <li>11</li>        <li>12</li>        <li>13</li>      </ol>    </li>    <li class="gantt-row" data-value="research" style="--from: 0; --to: 3; --progress: 100">      <span class="gantt-label">Research</span><span class="gantt-bar" role="slider">Nov 2 – 4 · 3 days</span>    </li>    <li class="gantt-row" data-value="design" aria-current="true" style="--from: 2; --to: 6">      <span class="gantt-label">Design</span><span class="gantt-bar" role="slider">Nov 4 – 7 · 4 days</span>    </li>    <li class="gantt-row" data-value="build" style="--from: 5; --to: 10; --progress: 20">      <span class="gantt-label">Build</span><span class="gantt-bar" role="slider">Nov 7 – 11 · 5 days</span>    </li>    <li class="gantt-row" data-value="review" style="--from: 9; --to: 11">      <span class="gantt-label">Review</span><span class="gantt-bar" role="slider">Nov 11 – 12 · 2 days</span>    </li>    <li class="gantt-row" data-value="launch" style="--from: 11">      <span class="gantt-label">Launch</span><span class="gantt-bar gantt-milestone" role="slider">Nov 13</span>    </li>  </ol>  <p class="gantt-status" role="status"></p></div><script>addEventListener('DOMContentLoaded', function () {var day = new Intl.DateTimeFormat('en', { month: 'short', day: 'numeric' })var date = function (offset = 0) { return new Date(2026, 10, 2 + offset) }MasterUI.gantt(document.getElementById('sprint'), {  format: function (start, end) {    if (start === end) return day.format(date(start))    var length = end - start    return day.formatRange(date(start), date(end - 1)) + ' · ' + length + (length === 1 ? ' day' : ' days')  }})})</script><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
import { useEffect, useRef } from 'react'import { attach, gantt } from '@master/ui/behaviors'var day = new Intl.DateTimeFormat('en', { month: 'short', day: 'numeric' })var date = function (offset = 0) { return new Date(2026, 10, 2 + offset) }export function Example() {  const root = useRef<HTMLDivElement>(null)  useEffect(() => {    const detach = [      attach(root.current!),      gantt(document.getElementById('sprint')!, {        format: function (start, end) {          if (start === end) return day.format(date(start))          var length = end - start          return day.formatRange(date(start), date(end - 1)) + ' · ' + length + (length === 1 ? ' day' : ' days')        }      }),    ]    return () => detach.forEach(stop => stop())  }, [])  return (    <div ref={root} className="card max-w(640) mx-auto">      <ol className="gantt gantt-now" id="sprint" style={{ '--gantt-from': '0', '--gantt-to': '12', '--gantt-now': '4.5' } as React.CSSProperties} aria-label="Sprint plan, November 2 to 13">        <li className="gantt-row gantt-scale" aria-hidden="true">          <ol className="gantt-ticks">            <li>2</li>            <li>3</li>            <li>4</li>            <li>5</li>            <li>6</li>            <li>7</li>            <li>8</li>            <li>9</li>            <li>10</li>            <li>11</li>            <li>12</li>            <li>13</li>          </ol>        </li>        <li className="gantt-row" data-value="research" style={{ '--from': '0', '--to': '3', '--progress': '100' } as React.CSSProperties}>          <span className="gantt-label">Research</span><span className="gantt-bar" role="slider">Nov 2 – 4 · 3 days</span>        </li>        <li className="gantt-row" data-value="design" aria-current="true" style={{ '--from': '2', '--to': '6' } as React.CSSProperties}>          <span className="gantt-label">Design</span><span className="gantt-bar" role="slider">Nov 4 – 7 · 4 days</span>        </li>        <li className="gantt-row" data-value="build" style={{ '--from': '5', '--to': '10', '--progress': '20' } as React.CSSProperties}>          <span className="gantt-label">Build</span><span className="gantt-bar" role="slider">Nov 7 – 11 · 5 days</span>        </li>        <li className="gantt-row" data-value="review" style={{ '--from': '9', '--to': '11' } as React.CSSProperties}>          <span className="gantt-label">Review</span><span className="gantt-bar" role="slider">Nov 11 – 12 · 2 days</span>        </li>        <li className="gantt-row" data-value="launch" style={{ '--from': '11' } as React.CSSProperties}>          <span className="gantt-label">Launch</span><span className="gantt-bar gantt-milestone" role="slider">Nov 13</span>        </li>      </ol>      <p className="gantt-status" role="status"></p>    </div>  )}
<script>  import { attach, gantt } from '@master/ui/behaviors'  var day = new Intl.DateTimeFormat('en', { month: 'short', day: 'numeric' })  var date = function (offset = 0) { return new Date(2026, 10, 2 + offset) }  function mounted(root) {    const detach = [      attach(root),      gantt(document.getElementById('sprint'), {        format: function (start, end) {          if (start === end) return day.format(date(start))          var length = end - start          return day.formatRange(date(start), date(end - 1)) + ' · ' + length + (length === 1 ? ' day' : ' days')        }      }),    ]    return () => detach.forEach(stop => stop())  }</script><div class="card max-w(640) mx-auto" {@attach mounted}>  <ol class="gantt gantt-now" id="sprint" style="--gantt-from: 0; --gantt-to: 12; --gantt-now: 4.5" aria-label="Sprint plan, November 2 to 13">    <li class="gantt-row gantt-scale" aria-hidden="true">      <ol class="gantt-ticks">        <li>2</li>        <li>3</li>        <li>4</li>        <li>5</li>        <li>6</li>        <li>7</li>        <li>8</li>        <li>9</li>        <li>10</li>        <li>11</li>        <li>12</li>        <li>13</li>      </ol>    </li>    <li class="gantt-row" data-value="research" style="--from: 0; --to: 3; --progress: 100">      <span class="gantt-label">Research</span><span class="gantt-bar" role="slider">Nov 2 – 4 · 3 days</span>    </li>    <li class="gantt-row" data-value="design" aria-current="true" style="--from: 2; --to: 6">      <span class="gantt-label">Design</span><span class="gantt-bar" role="slider">Nov 4 – 7 · 4 days</span>    </li>    <li class="gantt-row" data-value="build" style="--from: 5; --to: 10; --progress: 20">      <span class="gantt-label">Build</span><span class="gantt-bar" role="slider">Nov 7 – 11 · 5 days</span>    </li>    <li class="gantt-row" data-value="review" style="--from: 9; --to: 11">      <span class="gantt-label">Review</span><span class="gantt-bar" role="slider">Nov 11 – 12 · 2 days</span>    </li>    <li class="gantt-row" data-value="launch" style="--from: 11">      <span class="gantt-label">Launch</span><span class="gantt-bar gantt-milestone" role="slider">Nov 13</span>    </li>  </ol>  <p class="gantt-status" role="status"></p></div>
<script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach, gantt } from '@master/ui/behaviors'var day = new Intl.DateTimeFormat('en', { month: 'short', day: 'numeric' })var date = function (offset = 0) { return new Date(2026, 10, 2 + offset) }const root = ref(null)let detach = []onMounted(() => {  detach = [    attach(root.value),    gantt(document.getElementById('sprint'), {      format: function (start, end) {        if (start === end) return day.format(date(start))        var length = end - start        return day.formatRange(date(start), date(end - 1)) + ' · ' + length + (length === 1 ? ' day' : ' days')      }    }),  ]})onUnmounted(() => detach.forEach(stop => stop()))</script><template>  <div ref="root" class="card max-w(640) mx-auto">    <ol class="gantt gantt-now" id="sprint" style="--gantt-from: 0; --gantt-to: 12; --gantt-now: 4.5" aria-label="Sprint plan, November 2 to 13">      <li class="gantt-row gantt-scale" aria-hidden="true">        <ol class="gantt-ticks">          <li>2</li>          <li>3</li>          <li>4</li>          <li>5</li>          <li>6</li>          <li>7</li>          <li>8</li>          <li>9</li>          <li>10</li>          <li>11</li>          <li>12</li>          <li>13</li>        </ol>      </li>      <li class="gantt-row" data-value="research" style="--from: 0; --to: 3; --progress: 100">        <span class="gantt-label">Research</span><span class="gantt-bar" role="slider">Nov 2 – 4 · 3 days</span>      </li>      <li class="gantt-row" data-value="design" aria-current="true" style="--from: 2; --to: 6">        <span class="gantt-label">Design</span><span class="gantt-bar" role="slider">Nov 4 – 7 · 4 days</span>      </li>      <li class="gantt-row" data-value="build" style="--from: 5; --to: 10; --progress: 20">        <span class="gantt-label">Build</span><span class="gantt-bar" role="slider">Nov 7 – 11 · 5 days</span>      </li>      <li class="gantt-row" data-value="review" style="--from: 9; --to: 11">        <span class="gantt-label">Review</span><span class="gantt-bar" role="slider">Nov 11 – 12 · 2 days</span>      </li>      <li class="gantt-row" data-value="launch" style="--from: 11">        <span class="gantt-label">Launch</span><span class="gantt-bar gantt-milestone" role="slider">Nov 13</span>      </li>    </ol>    <p class="gantt-status" role="status"></p>  </div></template>
拖曳長條或它的任一端。也可以按 Tab 移到圖表,用 ArrowUp 和 ArrowDown 選擇長條,用 ArrowLeft 和 ArrowRight 移動它;搭配 Shift 時移動結束端,搭配 Alt(Option)時移動開始端。

樣式表為可移動的長條繪製的內容:

  • 靜止時外觀與一般長條相同,游標為抓取手勢。指標停在上方或取得焦點時,兩端會出現短短的直立握把,游標移到握把上會變成調整大小的箭頭;觸控螢幕沒有懸停狀態,因此握把會一直顯示並加寬,長條則維持 28px 高。
  • 取得焦點時會顯示焦點外框,畫在周圍列的上方。
  • 按住時會帶有 aria-busy="true":長條以抬起的陰影和較深的色調浮起,無緩動地跟隨指標,無論指標移到哪裡,游標都維持握緊的手,按住一端時則維持調整大小的箭頭(gantt-resize-start、gantt-resize-end)。按鍵或從外部設定的值會讓它滑到定位;啟用減少動態效果時則直接跳到定位。
  • 唯讀(aria-readonly="true")時,可以取得焦點並被讀出,但不會移動。停用(aria-disabled="true")時不會移動,按鍵操作會略過它,文字也會套用停用狀態的文字色。

里程碑以單一點的方式移動,沒有可調整的兩端。

#行為

gantt 接收根元素(帶有 gantt 類別的元素),並回傳可卸載行為的函式。只有標記為 role="slider" 的長條可以移動,其他長條仍只是圖像。行為會在每個可移動的長條上寫入滑桿所需、而頁面未提供的資訊:取自該列 gantt-label 的名稱;取自刻度的 aria-valuemin 和 aria-valuemax,也就是最早的開始與最晚的結束;長條開始處的 aria-valuenow;以及 aria-valuetext,也就是 format 對其範圍的描述。若要讓長條限制在較窄的區間內,請自行在長條上寫入 aria-valuemin 和 aria-valuemax。

app.js
import { gantt } from '@master/ui/behaviors/gantt.js'const plan = gantt(document.querySelector('.gantt'), {  // What a bar says for a range, as its text and to assistive technology.  format: (start, end) => `Days ${start + 1} to ${end}`,})plan.value // { research: [0, 3], design: [2, 6], … }plan.value = { design: [3, 7] }
  • 值是每個長條的範圍,以刻度單位表示為 [start, end],以該列的 data-value 為鍵,沒有時則以標籤文字為鍵。里程碑的範圍起訖都在它所在的點。設定它可從外部移動長條,不會派送事件。
  • 事件。 每當長條的範圍改變(包括拖曳期間),根元素都會派送 input;放開或按鍵改變範圍後,則派送一次 change。
  • 指標。 按下後移動超過 4px 就是拖曳;未超過則只會讓長條取得焦點。按在握把上(樣式表在兩端繪製的寬度)會移動該端,按在其他位置則移動整個長條。刻度單位是從長條繪製的寬度讀出的,因此無論版面如何,拖曳都會貼齊 step 個單位,預設為 1。手指往頁面下方移動時仍會捲動頁面(touch-action: pan-y)。
  • 限制。 長條會維持在它的 aria-valuemin 和 aria-valuemax 之間,長度也絕不會短於一個步進,或短於它目前的長度。
  • 播報內容。 放開或按鍵套用新範圍後,長條會像滑桿播報其值一樣,透過 aria-valuetext 播報新範圍。值無法表達的內容,例如變更遭拒或拖曳已取消,會由 gantt-status 區域播報,它是緊接在根元素之後的 role="status"。傳入 messages 即可用頁面的語言播報。
  • 頁面由右至左排列時,刻度、指標和方向鍵也都由右至左:ArrowLeft 代表較晚。

沒有行為時,圖表仍是一份可閱讀的事項及其日期清單。

#頁面的規則

相依關係、資源和工作日都由頁面掌握。長條取得某個範圍之前,會派送可取消的 beforechange 事件,其中包含長條的 id、將取得的 range,以及原本的 previous。呼叫 preventDefault() 即可拒絕:按鍵不會有任何作用,放開的長條會回到拖曳開始的位置,狀態區域也會播報長條維持原狀。這個範例中,Build 不能在 Design 完成前開始,而 Freeze 屬於發行團隊,因此設為唯讀。

頁面的規則

<div class="card max-w(560) mx-auto">  <ol class="gantt" id="release" style="--gantt-from: 0; --gantt-to: 12" aria-label="Release plan, November 2 to 13">    <li class="gantt-row" data-value="design" style="--from: 0; --to: 4">      <span class="gantt-label">Design</span><span class="gantt-bar" role="slider">Nov 2 – 5 · 4 days</span>    </li>    <li class="gantt-row" data-value="build" style="--from: 4; --to: 9">      <span class="gantt-label">Build</span><span class="gantt-bar" role="slider">Nov 6 – 10 · 5 days</span>    </li>    <li class="gantt-row" data-value="freeze" style="--from: 9; --to: 11">      <span class="gantt-label">Freeze</span><span class="gantt-bar" role="slider" aria-readonly="true">Nov 11 – 12 · 2 days</span>    </li>  </ol>  <p class="gantt-status" role="status"></p>  <p class="mb(0) mt-sm text-sm fg-text-muted" id="release-note">    Build starts once Design is done.  </p></div><script>addEventListener('DOMContentLoaded', function () {var day = new Intl.DateTimeFormat('en', { month: 'short', day: 'numeric' })var date = function (offset = 0) { return new Date(2026, 10, 2 + offset) }var root = document.getElementById('release')var note = document.getElementById('release-note')var plan = MasterUI.gantt(root, {  format: function (start, end) {    var length = end - start    return day.formatRange(date(start), date(end - 1)) + ' · ' + length + (length === 1 ? ' day' : ' days')  }})root.addEventListener('beforechange', function (event) {  var ranges = plan.value  ranges[event.detail.id] = event.detail.range  if (ranges.build[0] < ranges.design[1]) {    event.preventDefault()    note.textContent = 'Build starts once Design is done, on ' + day.format(date(ranges.design[1])) + ' at the earliest.'  } else note.textContent = 'Build starts once Design is done.'})})</script><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
把 Build 拖到 Design 結束之前,或把 Design 拖過 Build 的開始:長條會回到原位。
app.js
root.addEventListener('beforechange', (event) => {  const ranges = { ...plan.value, [event.detail.id]: event.detail.range }  if (ranges.build[0] < ranges.design[1]) event.preventDefault()})

這個事件會向上冒泡,因此在根元素上用一個監聽器就能收到每個長條的事件。只有在按鍵將移動長條,以及拖曳放開時才會詢問,拖曳的每一步都不會詢問,因此讀者可以把長條拖過不能停留的位置。

#鍵盤操作

按鍵動作
Tab進入圖表時焦點落在其中一個長條,再按即離開:整個圖表只占一個停駐點
ArrowUp, ArrowDown移到上方或下方的長條,略過已停用的長條
ArrowLeft, ArrowRight將長條提前或延後一步,長度不變;文字從右向左排列時,ArrowLeft 為延後
Shift+ArrowLeft, Shift+ArrowRight將長條的終點移動一步,與起點的距離不會小於一步
Alt+ArrowLeft, Alt+ArrowRight將長條的起點移動一步(Mac 上為 Option)
Home, End將長條移到允許的最早起點或最晚終點;按住 Shift 時移動終點,按住 Alt 時移動起點
Escape拖曳長條期間按下時,將長條放回拖曳開始時的位置

和工具列或樹狀檢視一樣,整個圖表在 Tab 順序中只占一個停駐點,因此冗長的計畫也不會有幾十個停駐點。ArrowUp 和 ArrowDown 在長條之間移動,因為各列是上下堆疊的;ArrowLeft 和 ArrowRight 移動長條,因為時間是左右延伸的:方向鍵與畫面一致。圖表維持為清單而非格線,因為每列只有一個可操作的對象,也就是長條;否則長條的方向鍵就得等切換模式後才能使用。

#甘特圖、時間軸或圖表

甘特圖適合呈現有開始和結束時間的事項。若要依發生順序呈現各個時間點的事件,請用時間軸。若要比較與時間無關的數值,請用圖表。

#無障礙

  • 使用 <ol>,並以 aria-label 或標題為清單命名。每列都是一個清單項目,會依序讀出名稱和長條文字,例如「設計,第 3 到第 6 週」。
  • 長條、刻度細線和現在時間線由樣式表繪製。請在文字中寫出它們傳達的資訊,例如長條的起訖日期,並在需要時說明哪一列是目前的階段。
  • 辨識目前列無須依賴色相。它以深色長條和其餘列的淺色長條形成明暗對比。在強制色彩模式下,目前列為實心長條,其餘列只有外框。
  • 可移動的長條是以所在列命名的滑桿,其值是以頁面用語描述的範圍,例如「設計,滑桿,11 月 4 日至 7 日,共 4 天」。請讓 format 使用與長條文字相同的措辭,讓兩者說法一致。
  • 從一開始就在頁面中保留空白的 gantt-status 區域:在要播報時才加入的區域常常不會被讀出。它只播報滑桿本身的值無法表達的內容,因此不會重複播報。
  • 指標能做的事,按鍵都能做到。對於使用指標但無法拖曳的讀者,也請為該列提供不需拖曳就能設定日期的方式,例如從該列開啟的對話框中的欄位。
  • 在圖表附近說明如何用按鍵移動長條,就像上方的說明文字一樣:以 Shift 和 Alt 移動兩端的滑桿,不是讀者猜得到的操作。

#React、Svelte 和 Vue

gantt 元件會根據資料寫出各列,每列包含 id、label、start 和 end(沒有 end 的列是里程碑),並依值 { [id]: [start, end] } 寫出每個長條的位置與文字。它以 from 和 to 接收刻度的起訖值,以 now 接收目前時間的位置,並以 ticks 接收刻度單位的名稱。長條移動時回報 onValueChange,放開後回報 onValueCommit,並在長條取得範圍前詢問 onBeforeChange(Vue 中為發出的 beforeChange 事件)。使用 npx master-ui add gantt 將元件複製到專案中;元件的實作方式與用法請見 React、Svelte 和 Vue。

ui/gantt.tsx
'use client'import { useId, useRef, useState, type ComponentProps, type CSSProperties } from 'react'import { gantt, type GanttChange, type GanttMessages, type GanttRange, type GanttValue } from '@master/ui/behaviors/gantt.js'import { useBehavior } from './use-behavior'export interface GanttRow {  /** What the bar stands for in the value. */  id: string  /** What the row is called; the bar is named by it. */  label: string  /** Where the bar begins, in the scale's unit. */  start: number  /** Where it ends. A row without an end is a milestone, a point at its start. */  end?: number  /** How much of the bar is done, from 0 to 100. */  progress?: number  /** The row that matters now: its bar is ink. */  current?: boolean  /** The bar takes focus and is read, and does not move. */  readOnly?: boolean  /** The bar does not move and the keys pass over it. */  disabled?: boolean  /** The earliest start and the latest end the bar may take. The scale's by default. */  min?: number  max?: number}export interface GanttProps extends Omit<ComponentProps<'ol'>, 'children' | 'defaultValue' | 'onChange' | 'ref'> {  rows: GanttRow[]  /** Where the scale begins and ends, in any unit: days, weeks, hours. */  from: number  to: number  /** Where the line of now stands on the scale; no line without it. */  now?: number  /** The names of the scale's units, one to a unit, drawn over the bars. */  ticks?: string[]  /** Each bar's range, `[start, end]`, keyed by its row's id. The rows' own by default. */  value?: GanttValue  defaultValue?: GanttValue  /** Called as a bar moves, while it is dragged too. */  onValueChange?: (value: GanttValue) => void  /** Called once a drop or a key has changed a bar. */  onValueCommit?: (value: GanttValue) => void  /** Asked before a bar takes a range: call `event.preventDefault()` to refuse it. */  onBeforeChange?: (event: CustomEvent<GanttChange>) => void  /** What a bar says for a range, as its text and to assistive technology. */  format?: (start: number, end: number) => string  /** How far a key moves a bar, and what the pointer snaps to, in the scale's unit. */  step?: number  /** What the status region says, in the page's language. */  messages?: GanttMessages}const plain = (start: number, end: number) => (start === end ? `${start}` : `${start} to ${end}`)/** * A gantt whose bars the reader moves. React writes the rows from its data and each bar's place and text * from the value; the behavior drags and resizes the bars, moves them with the keys and asks * `onBeforeChange` before a bar takes a range. */export function Gantt({ rows, from, to, now, ticks, value, defaultValue, onValueChange, onValueCommit, onBeforeChange, format = plain, step, messages, className, style, ...root }: GanttProps) {  const id = useId()  const [kept, setKept] = useState<GanttValue>(() => value ?? defaultValue ?? {})  const ranges = value ?? kept  const rangeOf = (row: GanttRow): GanttRange => ranges[row.id] ?? [row.start, row.end ?? row.start]  const latest = useRef<GanttValue>({})  latest.current = Object.fromEntries(rows.map(row => [row.id, rangeOf(row)]))  const ref = useBehavior(gantt, { format, step, messages, text: false }, {    value,    defaultValue,    onValueChange: (next: GanttValue) => {      latest.current = next      setKept(next)      onValueChange?.(next)    },    on: {      beforechange: event => onBeforeChange?.(event as CustomEvent<GanttChange>),      change: (event) => { if (event.target === event.currentTarget) onValueCommit?.(latest.current) },    },  })  const stop = rows.find(row => !row.disabled)?.id  const classes = now === undefined ? 'gantt' : 'gantt gantt-now'  return (    <>      <ol className={className ? `${classes} ${className}` : classes} style={{ '--gantt-from': from, '--gantt-to': to, '--gantt-now': now, ...style } as CSSProperties} ref={ref} {...root}>        {ticks && (          <li className="gantt-row gantt-scale" aria-hidden="true">            <ol className="gantt-ticks">{ticks.map((tick, index) => <li key={index}>{tick}</li>)}</ol>          </li>        )}        {rows.map((row, index) => {          const [start, end] = rangeOf(row)          const milestone = row.end === undefined          const words = format(start, end)          return (            <li className="gantt-row" data-value={row.id} key={row.id} aria-current={row.current || undefined} style={{ '--from': start, '--to': milestone ? undefined : end, '--progress': row.progress } as CSSProperties}>              <span className="gantt-label" id={`${id}label${index}`}>{row.label}</span>              <span                className={milestone ? 'gantt-bar gantt-milestone' : 'gantt-bar'}                role="slider"                tabIndex={row.id === stop ? 0 : -1}                aria-labelledby={`${id}label${index}`}                aria-valuemin={row.min ?? from}                aria-valuemax={row.max ?? to}                aria-valuenow={start}                aria-valuetext={words}                aria-readonly={row.readOnly || undefined}                aria-disabled={row.disabled || undefined}              >                {words}              </span>            </li>          )        })}      </ol>      <p className="gantt-status" role="status" />    </>  )}
ui/Gantt.svelte
<script lang="ts" module>  import type { HTMLOlAttributes } from 'svelte/elements'  import type { GanttChange, GanttMessages, GanttValue } from '@master/ui/behaviors/gantt.js'  export interface GanttRow {    /** What the bar stands for in the value. */    id: string    /** What the row is called; the bar is named by it. */    label: string    /** Where the bar begins, in the scale's unit. */    start: number    /** Where it ends. A row without an end is a milestone, a point at its start. */    end?: number    /** How much of the bar is done, from 0 to 100. */    progress?: number    /** The row that matters now: its bar is ink. */    current?: boolean    /** The bar takes focus and is read, and does not move. */    readOnly?: boolean    /** The bar does not move and the keys pass over it. */    disabled?: boolean    /** The earliest start and the latest end the bar may take. The scale's by default. */    min?: number    max?: number  }  export interface GanttProps extends Omit<HTMLOlAttributes, 'children'> {    rows: GanttRow[]    /** Where the scale begins and ends, in any unit: days, weeks, hours. */    from: number    to: number    /** Where the line of now stands on the scale; no line without it. */    now?: number    /** The names of the scale's units, one to a unit, drawn over the bars. */    ticks?: string[]    /** Each bar's range, `[start, end]`, keyed by its row's id. Bind it, or leave it out: the rows' own. */    value?: GanttValue    /** Called as a bar moves, while it is dragged too. */    onValueChange?: (value: GanttValue) => void    /** Called once a drop or a key has changed a bar. */    onValueCommit?: (value: GanttValue) => void    /** Asked before a bar takes a range: call `event.preventDefault()` to refuse it. */    onBeforeChange?: (event: CustomEvent<GanttChange>) => void    /** What a bar says for a range, as its text and to assistive technology. */    format?: (start: number, end: number) => string    /** How far a key moves a bar, and what the pointer snaps to, in the scale's unit. */    step?: number    /** What the status region says, in the page's language. */    messages?: GanttMessages  }</script><script lang="ts">  import { untrack } from 'svelte'  import { gantt } from '@master/ui/behaviors/gantt.js'  import { behavior } from './behavior.svelte.js'  let { rows, from, to, now, ticks, value = $bindable(), onValueChange, onValueCommit, onBeforeChange, format = (start: number, end: number) => (start === end ? `${start}` : `${start} to ${end}`), step, messages, class: className, ...root }: GanttProps = $props()  const id = $props.id()  // The ranges the bars are drawn at: the value given, or the one the reader left them at.  let kept = $state<GanttValue>(untrack(() => value) ?? {})  const ranges = $derived(value ?? kept)  const rangeOf = (row: GanttRow): [number, number] => ranges[row.id] ?? [row.start, row.end ?? row.start]  const stop = $derived(rows.find(row => !row.disabled)?.id)  const attach = behavior(gantt, () => ({ format, step, messages, text: false }), {    value: () => value,    change: (next) => { kept = next; value = next; onValueChange?.(next) },    settled: (held) => { kept = held },    on: {      beforechange: (event) => onBeforeChange?.(event as CustomEvent<GanttChange>),      change: (event) => { if (event.target === event.currentTarget) onValueCommit?.(Object.fromEntries(rows.map(row => [row.id, rangeOf(row)]))) },    },  })</script><!--  @component  A gantt whose bars the reader moves. Svelte writes the rows from its data and each bar's place and text  from the value; the behavior drags and resizes the bars, moves them with the keys and asks  `onBeforeChange` before a bar takes a range.--><ol class={['gantt', now !== undefined && 'gantt-now', className]} style:--gantt-from={from} style:--gantt-to={to} style:--gantt-now={now} {@attach attach} {...root}>  {#if ticks}    <li class="gantt-row gantt-scale" aria-hidden="true">      <ol class="gantt-ticks">        {#each ticks as tick, index (index)}<li>{tick}</li>{/each}      </ol>    </li>  {/if}  {#each rows as row, index (row.id)}    {@const [start, end] = rangeOf(row)}    {@const words = format(start, end)}    <li class="gantt-row" data-value={row.id} aria-current={row.current || undefined} style:--from={start} style:--to={row.end === undefined ? undefined : end} style:--progress={row.progress}>      <span class="gantt-label" id="{id}-label-{index}">{row.label}</span>      <span        class={['gantt-bar', row.end === undefined && 'gantt-milestone']}        role="slider"        tabindex={row.id === stop ? 0 : -1}        aria-labelledby="{id}-label-{index}"        aria-valuemin={row.min ?? from}        aria-valuemax={row.max ?? to}        aria-valuenow={start}        aria-valuetext={words}        aria-readonly={row.readOnly || undefined}        aria-disabled={row.disabled || undefined}      >{words}</span>    </li>  {/each}</ol><p class="gantt-status" role="status"></p>
ui/Gantt.vue
<script setup lang="ts">import { computed, ref, useId, useTemplateRef } from 'vue'import { gantt, type GanttChange, type GanttMessages, type GanttRange, type GanttValue } from '@master/ui/behaviors/gantt.js'import { useBehavior } from './use-behavior'defineOptions({ inheritAttrs: false })export interface GanttRow {  /** What the bar stands for in the value. */  id: string  /** What the row is called; the bar is named by it. */  label: string  /** Where the bar begins, in the scale's unit. */  start: number  /** Where it ends. A row without an end is a milestone, a point at its start. */  end?: number  /** How much of the bar is done, from 0 to 100. */  progress?: number  /** The row that matters now: its bar is ink. */  current?: boolean  /** The bar takes focus and is read, and does not move. */  readOnly?: boolean  /** The bar does not move and the keys pass over it. */  disabled?: boolean  /** The earliest start and the latest end the bar may take. The scale's by default. */  min?: number  max?: number}const { rows, from, to, now, ticks, format = (start: number, end: number) => (start === end ? `${start}` : `${start} to ${end}`), step, messages } = defineProps<{  rows: GanttRow[]  /** Where the scale begins and ends, in any unit: days, weeks, hours. */  from: number  to: number  /** Where the line of now stands on the scale; no line without it. */  now?: number  /** The names of the scale's units, one to a unit, drawn over the bars. */  ticks?: string[]  /** What a bar says for a range, as its text and to assistive technology. */  format?: (start: number, end: number) => string  /** How far a key moves a bar, and what the pointer snaps to, in the scale's unit. */  step?: number  /** What the status region says, in the page's language. */  messages?: GanttMessages}>()/** Each bar's range, `[start, end]`, keyed by its row's id: `v-model`. The rows' own by default. */const model = defineModel<GanttValue>()const emit = defineEmits<{  /** The ranges, once a drop or a key has changed a bar. */  valueCommit: [value: GanttValue]  /** Asked before a bar takes a range: call `event.preventDefault()` to refuse it. */  beforeChange: [event: CustomEvent<GanttChange>]}>()/* * A gantt whose bars the reader moves. Vue writes the rows from its data and each bar's place and text from * the value; the behavior drags and resizes the bars, moves them with the keys and asks `beforeChange` * before a bar takes a range. */const id = useId()const kept = ref<GanttValue>(model.value ?? {})const ranges = computed(() => model.value ?? kept.value)const rangeOf = (row: GanttRow): GanttRange => ranges.value[row.id] ?? [row.start, row.end ?? row.start]const stop = computed(() => rows.find(row => !row.disabled)?.id)// The ranges the reader leaves: what the model is told, kept here too for an owner that binds no model.const left = computed({  get: () => model.value,  set: (next) => {    kept.value = next ?? {}    model.value = next  },})const root = useTemplateRef<HTMLOListElement>('root')useBehavior(root, gantt, () => ({ format, step, messages, text: false }), left, {  beforechange: event => emit('beforeChange', event as CustomEvent<GanttChange>),  change: (event) => { if (event.target === event.currentTarget) emit('valueCommit', Object.fromEntries(rows.map(row => [row.id, rangeOf(row)]))) },})</script><template>  <ol class="gantt" :class="{ 'gantt-now': now !== undefined }" :style="{ '--gantt-from': from, '--gantt-to': to, '--gantt-now': now }" ref="root" v-bind="$attrs">    <li v-if="ticks" class="gantt-row gantt-scale" aria-hidden="true">      <ol class="gantt-ticks">        <li v-for="(tick, index) in ticks" :key="index">{{ tick }}</li>      </ol>    </li>    <li v-for="(row, index) in rows" :key="row.id" class="gantt-row" :data-value="row.id" :aria-current="row.current || undefined" :style="{ '--from': rangeOf(row)[0], '--to': row.end === undefined ? undefined : rangeOf(row)[1], '--progress': row.progress }">      <span :id="`${id}-label-${index}`" class="gantt-label">{{ row.label }}</span>      <span        class="gantt-bar"        :class="{ 'gantt-milestone': row.end === undefined }"        role="slider"        :tabindex="row.id === stop ? 0 : -1"        :aria-labelledby="`${id}-label-${index}`"        :aria-valuemin="row.min ?? from"        :aria-valuemax="row.max ?? to"        :aria-valuenow="rangeOf(row)[0]"        :aria-valuetext="format(...rangeOf(row))"        :aria-readonly="row.readOnly || undefined"        :aria-disabled="row.disabled || undefined"      >{{ format(...rangeOf(row)) }}</span>    </li>  </ol>  <p class="gantt-status" role="status" /></template>

#類別

類別說明
gantt清單分為名稱與長條兩欄。刻度範圍從 --gantt-from 到 --gantt-to,每個單位以細線標示
gantt-now在 --gantt-now 的位置繪製一條貫穿各列的前景色直線,標示今天搭配 gantt 使用
gantt-row每列的行內樣式用 --from 和 --to 設定起訖位置,用 --progress 設定進度。指標移到列上時,長條的顏色會加深一階。容器寬度小於 26rem 時,名稱會移到長條上方。
gantt-scale此列標示刻度單位,包含 gantt-ticks,沒有軌道。若長條的文字已標明日期,請將此列對輔助技術隱藏搭配 gantt-row 使用
gantt-label列首的主要文字,限一行,超出時以省略號結尾
gantt-bar長條高 28px,從 --from 到 --to 使用淡化的前景色,再以較深的同色填至 --progress 所指定的百分比。長條的文字說明其所代表的資訊,例如起訖日期。設為 role="slider" 時可以移動:指標可拖曳長條本體,或拖曳兩端的握把調整長度;觸控螢幕上握把會一直顯示並加寬,長條仍維持 28px
gantt-milestone在 --from 的位置以一個前景色小菱形標示單一時間點,並保留文字供輔助技術讀取搭配 gantt-bar 使用
gantt-resize-start指標按住長條起點時由行為設定:握把以實色顯示,無論指標移到哪裡,游標都維持調整大小的樣式搭配 gantt-bar 使用
gantt-resize-end指標按住長條終點時由行為設定搭配 gantt-bar 使用
gantt-ticksgantt-scale 內的刻度單位名稱,每個單位各一項。
gantt-status緊接在圖表後方的區域,說明長條的值無法表達的資訊,例如變更遭拒絕或拖曳已取消。不占版面空間

#自訂屬性

屬性預設值說明
--gantt-from0刻度起點
--gantt-to12刻度終點
--gantt-now0gantt-now 標記線在刻度上的位置
--from該列長條的起點,以刻度單位表示。長條移動時由行為寫入設定於 gantt-row
--to該列長條的終點。長條移動或調整長度時由行為寫入設定於 gantt-row
--progress長條的完成百分比,範圍為 0 到 100設定於 gantt-row
--gantt-gap.375rem列與列之間的間距
--gantt-colorvar(--color-surface-inverse)長條淡色填色所用的基準色

#原始碼

@master/ui/components/gantt.css
/* * Gantt: things that last, as bars on one scale of time. * It is a list. The scale runs from --gantt-from to --gantt-to, in any unit: days, weeks, hours. Each row * carries where its bar begins and ends as --from and --to, its name in gantt-label and what the bar says as * the text of gantt-bar, so the list reads as data to anyone who is not looking at it. The stylesheet does the * placing: names in one column, bars in the other, a hairline at every unit of the scale. * A bar is a tint of ink, 28px tall, and fills with a firmer tint as far as --progress percent. The row * marked aria-current has an ink bar. A milestone is a point, not a span: a small ink diamond at --from. * gantt-now draws an ink line down the rows at --gantt-now. * In a narrow container the name stands over its bar instead of beside it. * A bar the reader moves is one the page marks role="slider"; the behavior then writes --from and --to as it * is dragged or moved with the keys. Such a bar shows a grab cursor, a grip at each end on hover and focus * (always on a touch screen, where they are wider too), and the focus ring. Held, it says aria-busy: it lifts * with the lifted shadow and a firmer tint, and follows the pointer at once, while a key or a value set from * outside glides it to its place. gantt-resize-start and gantt-resize-end mark the end the pointer holds, so * the cursor resizes wherever the pointer goes. A read-only or disabled bar has none of this. */@layer components.ui {  .gantt {    --gantt-from: 0;    --gantt-to: 12;    --gantt-now: 0;    --gantt-gap: .375rem;    --gantt-color: var(--color-surface-inverse);    --gantt-span: calc(var(--gantt-to) - var(--gantt-from));    container-type: inline-size;    display: grid;    grid-template-columns: auto minmax(0, 1fr);    gap: var(--gantt-gap) 1rem;    min-width: 0;    margin: 0;    padding: 0;    color: var(--color-text-body);    font: 400 .8125rem/1.25rem var(--font-family-sans);    font-feature-settings: inherit;    font-variant-numeric: tabular-nums;    list-style: none;    /* The bars, the line of now and a held bar stack inside the chart, never over a sticky header beside it. */    isolation: isolate;    /*     * The line of now: drawn in every row, so it runs the height of the chart. It passes under the bars, as a     * schedule's passes under its events, so it never strikes through their words.     */    &.gantt-now .gantt-row:not(.gantt-scale)::after {      content: '';      z-index: 0;      grid-area: 1 / 2;      align-self: stretch;      justify-self: start;      width: 1px;      margin-block: calc(var(--gantt-gap) / -2);      margin-inline-start: calc((var(--gantt-now) - var(--gantt-from)) / var(--gantt-span) * 100% - .5px);      background: var(--color-text-strong);      pointer-events: none;    }    /* While a bar is held, the cursor stays what it was at the press, wherever the pointer goes. */    &:has(.gantt-bar[aria-busy="true"]) {      cursor: grabbing;      user-select: none;      -webkit-user-select: none;    }    &:has(.gantt-resize-start, .gantt-resize-end) {      cursor: ew-resize;    }    /* Under the pointer a row's bar is a step firmer and its name takes the strong ink, at once. */    @media (hover: hover) {      .gantt-row:not([aria-current="true"]):hover {        .gantt-bar {          --gantt-tint: color-mix(in oklab, var(--gantt-color) 16%, transparent);          --gantt-done: color-mix(in oklab, var(--gantt-color) 30%, transparent);        }        .gantt-label {          color: var(--color-text-strong);        }      }    }    @container (width < 26rem) {      .gantt-row {        grid-template-columns: minmax(0, 1fr);        row-gap: .125rem;      }      .gantt-label {        grid-area: 1 / 1;        max-width: none;      }      .gantt-row::before, .gantt-bar, .gantt-ticks {        grid-area: 2 / 1;      }      &.gantt-now .gantt-row:not(.gantt-scale)::after {        grid-area: 2 / 1;      }      .gantt-scale .gantt-ticks {        grid-area: 1 / 1;      }    }    @media (forced-colors: active) {      --gantt-color: CanvasText;      /* The line of now is a fill, which forced colors would otherwise paint as the canvas. */      &.gantt-now .gantt-row:not(.gantt-scale)::after {        forced-color-adjust: none;        background: CanvasText;      }    }  }  /* One row: the name in the first column, and the track with its bar in the second. */  .gantt-row {    display: grid;    grid-column: 1 / -1;    grid-template-columns: subgrid;    align-items: center;    min-width: 0;    min-height: 1.75rem;    margin: 0;    padding: 0;    /* A row the page filters out is hidden, as the behavior expects: the grid above would otherwise beat [hidden]. */    &[hidden] {      display: none;    }    /* The scale behind the bar: a hairline at each unit, joined to the row above and below. */    &::before {      content: '';      grid-area: 1 / 2;      align-self: stretch;      margin-block: calc(var(--gantt-gap) / -2);      background: repeating-linear-gradient(90deg, var(--color-line-hair) 0 1px, transparent 1px calc(100% / var(--gantt-span)));      box-shadow: 1px 0 0 var(--color-line-hair);      pointer-events: none;    }    /* Where the scale runs from the right, so do its hairlines, and the closing one is at the left. */    &:dir(rtl)::before {      background: repeating-linear-gradient(270deg, var(--color-line-hair) 0 1px, transparent 1px calc(100% / var(--gantt-span)));      box-shadow: -1px 0 0 var(--color-line-hair);    }    /* The hairlines are a gradient and a shadow, which forced colors drop: kept, in the system's gray. */    @media (forced-colors: active) {      &::before {        forced-color-adjust: none;        background: repeating-linear-gradient(90deg, GrayText 0 1px, transparent 1px calc(100% / var(--gantt-span)));        box-shadow: 1px 0 0 GrayText;      }      &:dir(rtl)::before {        background: repeating-linear-gradient(270deg, GrayText 0 1px, transparent 1px calc(100% / var(--gantt-span)));        box-shadow: -1px 0 0 GrayText;      }    }    /* The row that matters now: its bar is ink. */    &[aria-current="true"] {      .gantt-bar {        background: var(--gantt-color);        color: var(--color-text-inverse);        box-shadow: var(--shadow-ink-top);      }      .gantt-label {        color: var(--color-text-strong);      }    }    /* The row of the scale itself: its names, and no track of its own. */    &.gantt-scale {      min-height: 1.25rem;      &::before {        content: none;      }    }  }  /* What the row is. */  .gantt-label {    grid-area: 1 / 1;    max-width: 10rem;    overflow: hidden;    text-overflow: ellipsis;    white-space: nowrap;  }  /* The bar: from --from to --to of the scale, filled as far as --progress percent. Its text is what it says. */  .gantt-bar {    --gantt-tint: color-mix(in oklab, var(--gantt-color) 10%, transparent);    --gantt-done: color-mix(in oklab, var(--gantt-color) 22%, transparent);    z-index: 1;    grid-area: 1 / 2;    box-sizing: border-box;    width: calc((var(--to, 1) - var(--from, 0)) / var(--gantt-span) * 100%);    min-width: .25rem;    height: 1.75rem;    margin-inline-start: calc((var(--from, 0) - var(--gantt-from)) / var(--gantt-span) * 100%);    padding: .25rem .5rem;    border-radius: calc(var(--radius-chip) * var(--corner-scale));    corner-shape: var(--corner-shape);    overflow: hidden;    background: linear-gradient(90deg, var(--gantt-done) calc(var(--progress, 0) * 1%), var(--gantt-tint) 0);    color: var(--color-text-strong);    font-weight: var(--font-weight-control);    font-size: .78125rem;    text-overflow: ellipsis;    white-space: nowrap;    /* A bar answers the pointer and is not a line to select. */    cursor: default;    &:dir(rtl) {      background-image: linear-gradient(270deg, var(--gantt-done) calc(var(--progress, 0) * 1%), var(--gantt-tint) 0);    }    /* A bar the reader moves: one stop in the tab order for the whole chart, with the focus ring above the rows. */    &[role="slider"] {      position: relative;      &:focus-visible {        z-index: 3;        @apply --focus-ring;      }    }    &[role="slider"]:not([aria-readonly="true"], [aria-disabled="true"]) {      cursor: grab;      /* A finger that moves along the bar drags it; one that moves down the page still scrolls it. */      touch-action: pan-y;      user-select: none;      -webkit-user-select: none;      -webkit-tap-highlight-color: transparent;      /* A key or a value set from outside glides the bar to its place; the pointer moves it at once. */      transition-property: margin-inline-start, width;      transition-duration: var(--duration-quick);      transition-timing-function: var(--easing-glide);      /* The grips: an end of the bar is where the pointer resizes it, marked with a short upright line. */      &::before, &::after {        content: '';        position: absolute;        inset-block: 0;        width: min(.5rem, 30%);        background: linear-gradient(currentColor 0 0) center / 2px .75rem no-repeat;        opacity: 0;        cursor: ew-resize;        transition: opacity var(--duration-quick) var(--easing-glide);      }      &::before {        inset-inline-start: 0;      }      &::after {        inset-inline-end: 0;      }      @media (hover: hover) {        &:hover::before, &:hover::after {          opacity: .45;        }      }      &:focus-visible::before, &:focus-visible::after, &[aria-busy="true"]::before, &[aria-busy="true"]::after {        opacity: .45;      }      /* A milestone is a point: it moves, and has no ends to resize. */      &.gantt-milestone {        &::before, &::after {          content: none;        }      }      /* Held: it lifts above the rows on a firmer tint, and follows the pointer with no easing. */      &[aria-busy="true"] {        --gantt-tint: color-mix(in oklab, var(--gantt-color) 16%, transparent);        --gantt-done: color-mix(in oklab, var(--gantt-color) 30%, transparent);        z-index: 3;        box-shadow: var(--shadow-lifted);        cursor: grabbing;        transition-property: none;      }      /* The end the pointer holds: its grip is firm and the cursor resizes. */      &.gantt-resize-start, &.gantt-resize-end {        cursor: ew-resize;      }      &.gantt-resize-start::before, &.gantt-resize-end::after {        opacity: 1;      }      /*       * A touch screen has no hover to show the grips, so they stay, and wider for a finger. The bar keeps its       * 28px: the rows stand too close for a target to reach past it, and 28px is more than a finger is owed.       */      @media (pointer: coarse) {        &::before, &::after {          width: min(.75rem, 30%);          opacity: .45;        }      }      @media (prefers-reduced-motion: reduce) {        transition-property: none;        &::before, &::after {          transition: none;        }      }    }    /* A disabled bar is drawn in the disabled ink; a read-only one looks as any bar does. */    &[aria-disabled="true"] {      color: var(--color-text-disabled);    }    /* A milestone: a point in time, drawn as a diamond on it. Its text is kept for assistive technology. */    &.gantt-milestone {      width: .75rem;      min-width: 0;      height: .75rem;      margin-inline-start: calc((var(--from, 0) - var(--gantt-from)) / var(--gantt-span) * 100% - .375rem);      padding: 0;      border-radius: 2px;      corner-shape: round;      background: var(--gantt-color);      color: transparent;      font-size: 0;      rotate: 45deg;    }    /* The canvas goes under the fill, so the part already done still shows, in either direction. */    @media (forced-colors: active) {      outline: 1px solid CanvasText;      outline-offset: -1px;      forced-color-adjust: none;      background-color: Canvas;      color: CanvasText;      &.gantt-milestone {        background: CanvasText;      }      /* A disabled bar takes the system's disabled ink, which the adjustment above would otherwise lose. */      &[aria-disabled="true"] {        outline-color: GrayText;        color: GrayText;      }      /* The current row's bar is the system's highlight, as a schedule's current event is, with its text for the words. */      .gantt-row[aria-current="true"] & {        background: Highlight;        color: HighlightText;      }      &[role="slider"]:focus-visible, &[aria-busy="true"] {        outline: 2px solid Highlight;        outline-offset: 2px;      }    }  }  /* The names of the scale's units, one to a unit, each over the hairline that begins it. */  .gantt-ticks {    display: grid;    grid-area: 1 / 2;    grid-auto-columns: minmax(0, 1fr);    grid-auto-flow: column;    min-width: 0;    margin: 0;    padding: 0;    color: var(--color-text-muted);    font-size: .71875rem;    line-height: 1.25rem;    list-style: none;    white-space: nowrap;  }  /* What the status region says is for assistive technology: it takes no room. */  .gantt-status {    position: absolute;    width: 1px;    height: 1px;    margin: -1px;    padding: 0;    overflow: hidden;    clip-path: inset(50%);    white-space: nowrap;  }}