-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuseTimeTicks.ts
More file actions
51 lines (46 loc) · 1.46 KB
/
useTimeTicks.ts
File metadata and controls
51 lines (46 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import type { ScaleTime } from 'd3-scale';
import type { RefObject } from 'react';
import { useState } from 'react';
import type { Tick } from './useTicks.js';
import { useTicks } from './useTicks.js';
type Directions = 'horizontal' | 'vertical';
export interface UseTimeTicksResult {
/**
* The scale used to generate the ticks' position.
* This is not necessarily the same as the one passed to the hook, and may be
* the scale passed to a previous call to the hook.
* If you need to do additional processing with the ticks, for example to
* compute the position of secondary ticks, you should use this scale, and not
* the one you passed to the hook.
*/
scale: ScaleTime<number, number>;
/**
* The ticks generated by the hook.
* The number of ticks account for the space available based on the dom
* reference passed to the hook, as well as the space required to render the
* formatted tick labels.
*/
ticks: Array<Tick<Date>>;
}
interface Options {
tickFormat?: (d: Date) => string;
minSpace?: number;
}
export function useTimeTicks(
scale: ScaleTime<number, number>,
direction: Directions,
ref: RefObject<SVGGElement | null>,
options: Options,
): UseTimeTicksResult {
const { tickFormat = scale.tickFormat() } = options;
const [ticks, setTicks] = useState<UseTimeTicksResult>(() => ({
ticks: [],
scale,
}));
useTicks<Date>(scale, direction, ref, {
...options,
setTicks,
tickFormat,
});
return ticks;
}