导航

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 按对应比例在轨道上起止。支持小数值,因此条形可以在一个单位的中途结束。

名称列的宽度由最长的名称决定,轨道占据剩余宽度。所有行共用这两列,无需显式指定宽度即可对齐条形。条形后方的细线标出标尺的每个单位。

用条形文本写明条形表达的信息,例如起止日期、时长和负责人。即使条形太短也要写这段文本,屏幕阅读器会朗读它。超出可用空间的文本会以省略号结尾。

#进度、当前行和当前时间

在行上将 --progress 设为 0 到 100 之间的值,条形就会按相应比例填入更深的色调。用 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 时,名称移到对应条形的上方,轨道占满整行。在手机上应减少标尺单位数:8 个单位仍清晰可读,30 个就难以辨认。

#移动条形

将条形标记为 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 的结束时间,冻结期归发布团队管理,因此是只读的。

页面的规则

<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进入图表时聚焦一个条形,再按 Tab 键离开:整个图表在 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 或标题为其命名。每行是一个列表项,先朗读名称,再朗读条形文本,例如“Design,第 3 周至第 6 周”。
  • 条形、细线和当前时间线均由样式表绘制。用文本写明图形表达的信息,包括条形的起止日期,并在有必要时注明哪一行是当前行。
  • 区分当前行无需辨认色相。常规模式下,当前行用墨色条形,其他行用浅色条形。强制颜色模式下,当前行用实心条形,其他行只显示轮廓。
  • 可移动的条形是以所在行命名的滑块,其值是用页面语言表达的范围,例如“Design,滑块,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用于单行。在该行的 style 中设置 --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;  }}