Date brush
A date brush should only land on days that have data, and say so when it can’t.
The problem
Data doesn’t exist for every day. A brush that lets you start a range on an empty day leaves two bad options: the chart shows a misleading gap at the edge, or it quietly shifts your selection somewhere you didn’t pick.
Clicks have it worse. Click in a long empty stretch and a typical brush either does nothing, which looks broken, or jumps to some far-off day you never meant.
What I concluded
The brush knows which days have data and treats them as the only places it can stand.
Ranges snap outward. The start moves back to the nearest data day at or before it, and the end moves forward. Outward means the range still covers everything you dragged across.
const start = DATA_DAYS.filter((d) => d <= lo).at(-1) ?? DATA_DAYS[0];
const end = DATA_DAYS.find((d) => d >= hi) ?? DATA_DAYS.at(-1);
The fallbacks handle a drag that starts before the first data day or ends after the last. There’s nothing further out, so it clamps.
A click asks for one day. If a data day is within a small reach of the pointer, it picks that one. If not, it refuses visibly with a short “No data here” note and leaves your current selection alone.
Rules
- Snap outward, never inward. The range always contains what you dragged over.
- Clamp at the ends. A drag past the data snaps to the first or last real day.
- Clicks have a reach. Near a data day picks it, far from one is refused.
- Refuse out loud. A silent no-op looks like a bug. Put the note in a polite live region so screen readers hear it too.
- Keyboard follows the same rule. The handles should act as sliders whose arrow keys step to the previous or next data day, never an empty one.
What you get
A selection that always lines up with real data, so the chart never opens on an invented edge. The demo shows the outward snap and the refusal in the empty stretch. It doesn’t include the keyboard handles.
Related: Trend windows, Dates and locale