/
TabsList.tsx
108 lines (100 loc) · 2.72 KB
/
TabsList.tsx
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import type { HTMLAttributes } from "react";
import { forwardRef } from "react";
import {
bem,
useIsUserInteractionMode,
useKeyboardFocus,
} from "@react-md/utils";
import cn from "classnames";
import type { TabsConfig } from "./types";
import { useTabIndicatorStyles } from "./useTabIndicatorStyles";
export interface TabsListProps
extends HTMLAttributes<HTMLDivElement>,
TabsConfig {
/**
* The current active tab index to determine which tabs to animate in and out
* of view.
*/
activeIndex: number;
/**
* A function to call when the `activeIndex` should change due to keyboard
* movement or clicking on a tab.
*/
onActiveIndexChange(activeIndex: number): void;
/**
* Boolean if the indicator transition should be disabled while the active tab
* index changes.
*
* @defaultValue `false`
*/
disableTransition?: boolean;
}
const styles = bem("rmd-tabs");
/**
* The `TabsList` component is the container for all the individual `Tab`s that
* should be rendered. This handles adding an active indicator underneath the
* active tab and animating it to the new location when a new tab becomes
* active. It also handles the ability update which tab is selected when it has
* been clicked or updated with keyboard movement.
*
* This should probably not be used outside of this package unless a custom
* implementation is desired.
*/
export const TabsList = forwardRef<HTMLDivElement, TabsListProps>(
function TabsList(
{
style,
className,
onFocus,
onKeyDown,
children,
activeIndex,
align = "left",
automatic = false,
padded = false,
orientation = "horizontal",
onActiveIndexChange,
disableTransition = false,
...props
},
ref
) {
const horizontal = orientation === "horizontal";
const isKeyboard = useIsUserInteractionMode("keyboard");
const { focusIndex: _focusIndex, ...eventHandlers } = useKeyboardFocus({
onFocus,
onKeyDown,
onFocusChange(element, focusIndex) {
element.focus();
if (automatic) {
onActiveIndexChange(focusIndex);
}
},
});
const { refCallback, indicatorStyles } = useTabIndicatorStyles({
ref,
activeIndex,
});
return (
<div
{...props}
aria-orientation={orientation}
style={{ ...style, ...indicatorStyles }}
role="tablist"
ref={refCallback}
className={cn(
styles({
[align]: true,
padded,
vertical: !horizontal,
animate: !disableTransition && (!automatic || !isKeyboard),
}),
className
)}
{...eventHandlers}
>
{children}
</div>
);
}
);