-
Notifications
You must be signed in to change notification settings - Fork 14
/
Tab.tsx
177 lines (152 loc) · 4.89 KB
/
Tab.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import React, { useLayoutEffect, createRef, forwardRef, Ref, useContext } from "react";
import styled from "styled-components";
import DxcBadge from "../badge/Badge";
import { NavTabsContext } from "./NavTabs";
import { TabProps } from "./types";
import DxcTypography from "../typography/Typography";
import useTheme from '../useTheme';
const DxcTab = forwardRef(
(
{ href, active = false, icon, disabled = false, notificationNumber = false, children, ...otherProps }: TabProps,
ref: Ref<HTMLAnchorElement>
): JSX.Element => {
const tabRef: React.MutableRefObject<HTMLAnchorElement> = createRef();
const { iconPosition, tabIndex, hasIcons, focusedLabel } = useContext(NavTabsContext);
const colorsTheme = useTheme();
useLayoutEffect(() => {
focusedLabel === children.toString() && tabRef?.current?.focus();
}, [focusedLabel]);
const handleOnKeyDown = (event: React.KeyboardEvent<HTMLAnchorElement>) => {
switch (event.key) {
case " ":
case "Enter":
tabRef?.current?.click();
event.preventDefault();
break;
}
};
return (
<TabContainer active={active} role="tab" aria-selected={active} aria-disabled={disabled}>
<Tab
href={!disabled && href ? href : undefined}
disabled={disabled}
iconPosition={iconPosition}
hasIcon={hasIcons}
ref={(anchorRef) => {
tabRef.current = anchorRef;
if (ref) {
if (typeof ref === "function") ref(anchorRef);
else {
(ref as React.MutableRefObject<HTMLAnchorElement | null>).current = anchorRef;
}
}
}}
onKeyDown={handleOnKeyDown}
tabIndex={active ? tabIndex : -1}
{...otherProps}
>
{icon && (
<TabIconContainer iconPosition={iconPosition}>
{typeof icon === "string" ? <img src={icon} /> : icon}
</TabIconContainer>
)}
<LabelContainer>
<DxcTypography
color={
disabled
? colorsTheme.navTabs.disabledFontColor
: active
? colorsTheme.navTabs.selectedFontColor
: colorsTheme.navTabs.unselectedFontColor
}
fontFamily={colorsTheme.navTabs.fontFamily}
fontSize={colorsTheme.navTabs.fontSize}
fontStyle={colorsTheme.navTabs.fontStyle}
fontWeight={colorsTheme.navTabs.fontWeight}
textAlign="center"
letterSpacing="0.025em"
lineHeight="1.715em"
> {children}
</DxcTypography>
{notificationNumber && (
<BadgeContainer>
<DxcBadge notificationText={notificationNumber > 99 ? "+99" : notificationNumber} disabled={disabled} />
</BadgeContainer>
)}
</LabelContainer>
</Tab>
</TabContainer>
);
}
);
type TabContainerProps = {
active?: boolean;
};
const TabContainer = styled.div<TabContainerProps>`
border-bottom: 2px solid ${(props) => (props.active ? props.theme.selectedUnderlineColor : props.theme.dividerColor)};
svg {
color: ${(props) => props.theme.unselectedIconColor};
}
&[aria-selected="true"] {
svg {
color: ${(props) => props.theme.selectedIconColor};
}
}
&[aria-disabled="true"] {
svg {
color: ${(props) => props.theme.disabledIconColor};
}
}
`;
type TabStyleProps = {
disabled: boolean;
hasIcon: boolean;
iconPosition: "top" | "left";
};
const Tab = styled.a<TabStyleProps>`
display: flex;
flex-direction: ${(props) => (props.hasIcon && props.iconPosition === "top" ? "column" : "row")};
justify-content: center;
align-items: center;
text-decoration-color: transparent;
cursor: ${(props) => (props.disabled ? "not-allowed" : "pointer")};
background: ${(props) => (props.theme.selectedBackgroundColor)};
height: ${(props) => (props.hasIcon && props.iconPosition === "top" ? "66px" : "32px")};
min-width: 164px;
margin: 0.5rem;
padding: 0.375rem;
border-radius: 4px;
${(props) =>
!props.disabled &&
`:hover {
background: ${props.theme.hoverBackgroundColor};
}
:focus {
outline: 2px solid ${props.theme.focusOutline};
}
:active {
background: ${props.theme.pressedBackgroundColor};
outline: 2px solid #33aaff};
}`}
`;
type TabIconContainerProps = {
iconPosition?: "top" | "left";
};
const TabIconContainer = styled.div<TabIconContainerProps>`
display: flex;
margin-bottom: ${(props) => props.iconPosition === "top" && "0.375rem"};
margin-right: ${(props) => props.iconPosition === "left" && "0.625rem"};
img,
svg {
height: 24px;
width: 24px;
}
`;
const LabelContainer = styled.div`
display: flex;
align-items: center;
`;
const BadgeContainer = styled.div`
margin-left: 0.5rem;
`;
export default DxcTab;