Skip to content

Commit db75e72

Browse files
authored
fix typing on styled components v6 theme and props (#649)
1 parent e1f9ca7 commit db75e72

File tree

6 files changed

+2284
-1777
lines changed

6 files changed

+2284
-1777
lines changed

package-lock.json

Lines changed: 2264 additions & 1764 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
"@types/react-plotly.js": "^2.2.4",
4242
"@types/react-router-dom": "^5.1.5",
4343
"@types/sinon": "7.0.6",
44-
"@types/styled-components": "^5.1.34",
4544
"@types/uuid": "^9.0.7",
4645
"@typescript-eslint/eslint-plugin": "^5.42.1",
4746
"@typescript-eslint/parser": "^5.42.1",

src/components/CustomButton/index.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
TOOLTIP_DELAY,
99
} from "../../constants";
1010
import { ButtonClass, TooltipPlacement } from "../../constants/interfaces";
11+
import { ThemedProps } from "../../styles/theme/theme";
1112

1213
interface CustomButtonProps extends Omit<AntButtonProps, "type" | "variant"> {
1314
variant?: ButtonClass;
@@ -26,7 +27,7 @@ interface TooltipButtonProps extends CustomButtonProps {
2627
tooltipPlacement?: TooltipPlacement;
2728
}
2829

29-
const baseStyles = css`
30+
const baseStyles = css<ThemedProps>`
3031
font-family: ${(props) => props.theme.typography};
3132
border-radius: 3px;
3233
height: 32px;
@@ -46,7 +47,7 @@ const baseStyles = css`
4647
const generateButtonStyles = (
4748
variant: "primary" | "secondary",
4849
theme: "light" | "dark"
49-
) => css`
50+
) => css<ThemedProps>`
5051
${({
5152
theme: {
5253
colors: { button },
@@ -84,7 +85,7 @@ const generateButtonStyles = (
8485
}}
8586
`;
8687

87-
const actionStyles = css`
88+
const actionStyles = css<ThemedProps>`
8889
${({
8990
theme: {
9091
colors: {
@@ -115,15 +116,15 @@ const actionStyles = css`
115116
`}
116117
`;
117118

118-
const variantStyles: Record<ButtonClass, RuleSet<object>> = {
119+
const variantStyles: Record<ButtonClass, RuleSet<ThemedProps>> = {
119120
[ButtonClass.LightPrimary]: generateButtonStyles("primary", "light"),
120121
[ButtonClass.LightSecondary]: generateButtonStyles("secondary", "light"),
121122
[ButtonClass.DarkPrimary]: generateButtonStyles("primary", "dark"),
122123
[ButtonClass.DarkSecondary]: generateButtonStyles("secondary", "dark"),
123124
[ButtonClass.Action]: actionStyles,
124125
};
125126

126-
const StyledButton = styled(AntButton)<CustomButtonProps>`
127+
const StyledButton = styled(AntButton)<CustomButtonProps & ThemedProps>`
127128
${baseStyles}
128129
${({ variant = ButtonClass.LightPrimary }) => variantStyles[variant]}
129130
`;

src/components/CustomDropdown/DropdownMenuItems.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from "react";
22
import { Link, LinkProps } from "react-router-dom";
33
import styled, { css } from "styled-components";
44
import { ArrowRight } from "../Icons";
5+
import { ThemedProps, ThemeType } from "../../styles/theme/theme";
56

67
// Dropdown items can be of a few component
78
// varieties, including buttons, router links, and
@@ -10,7 +11,7 @@ import { ArrowRight } from "../Icons";
1011
// for semantically explicit usage in dropdowns.
1112

1213
// Common styles
13-
const baseStyles = css`
14+
const baseStyles = css<{ theme: ThemeType }>`
1415
font-family: ${(props) => props.theme.typography};
1516
background: none;
1617
border: 2px solid ${({ theme }) => theme.colors.dropdown.background};
@@ -62,17 +63,17 @@ const contentStyles = css`
6263
`;
6364

6465
// Styled components
65-
const StyledDropdownButton = styled.button`
66+
const StyledDropdownButton = styled.button<ThemedProps>`
6667
${baseStyles}
6768
${contentStyles}
6869
`;
6970

70-
const StyledRouterLink = styled(Link)`
71+
const StyledRouterLink = styled(Link)<ThemedProps>`
7172
${baseStyles}
7273
${contentStyles}
7374
`;
7475

75-
const StyledExternalLink = styled.a`
76+
const StyledExternalLink = styled.a<ThemedProps>`
7677
${baseStyles}
7778
${contentStyles}
7879
`;

src/styles/theme/colors.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export const componentColors = {
9292
light: {
9393
background: baseColors.dark.five,
9494
text: baseColors.gray.six,
95+
border: baseColors.dark.five,
9596
hover: {
9697
background: semanticColors.darkActiveBg,
9798
text: semanticColors.primaryPurple,
@@ -113,6 +114,7 @@ export const componentColors = {
113114
dark: {
114115
background: semanticColors.primaryPurple,
115116
text: baseColors.dark.three,
117+
border: semanticColors.primaryPurple,
116118
hover: {
117119
background: semanticColors.activePurple,
118120
text: semanticColors.lightBgActiveText,
@@ -139,6 +141,7 @@ export const componentColors = {
139141
light: {
140142
background: semanticColors.transparentBg,
141143
text: baseColors.dark.five,
144+
border: semanticColors.transparentBg,
142145
hover: {
143146
background: baseColors.dark.one,
144147
text: semanticColors.primaryPurple,
@@ -188,6 +191,7 @@ export const componentColors = {
188191
text: semanticColors.primaryPurple,
189192
hover: {
190193
text: semanticColors.activePurple,
194+
background: semanticColors.lightPurpleBg,
191195
},
192196
active: {
193197
text: semanticColors.activePurple,

src/styles/theme/theme.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { ThemeConfig } from "antd";
2-
import { DefaultTheme } from "styled-components";
32
import { themeColors } from "./colors";
43

54
const typographyToken = {
@@ -10,10 +9,13 @@ const typographyToken = {
109
// make variables available as props in
1110
// .styled components by passing this into
1211
// the ThemeProvider in the StyleProvider
13-
export const styledTheme: DefaultTheme = {
12+
export const styledTheme = {
1413
colors: themeColors,
1514
typography: typographyToken.fontFamily,
16-
};
15+
} as const;
16+
17+
export type ThemeType = typeof styledTheme;
18+
export type ThemedProps<T = unknown> = T & { theme: ThemeType };
1719

1820
export const antdTheme: ThemeConfig = {
1921
token: {

0 commit comments

Comments
 (0)