Skip to content

Commit 5a03fff

Browse files
committed
fix(button): button theme values using backup if theme not available
1 parent 5467f80 commit 5a03fff

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/components/Button.tsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from "react";
22
import "./css/main.css";
33
import styled from "styled-components";
4+
import { getThemeValue } from "./ThemeProvider";
45

56
export type ButtonProps = React.HTMLAttributes<
67
HTMLButtonElement | HTMLAnchorElement
@@ -58,41 +59,51 @@ export const Button = (props: ButtonProps) => {
5859
}};
5960
border: ${(props) => {
6061
if (variant !== "outlined") return "none";
61-
return `1px solid ${props.theme[accent || "info"]}`;
62+
return `1px solid ${
63+
props.theme[accent || "info"] ?? getThemeValue(accent || "info")
64+
}`;
6265
}};
6366
background-color: ${(props) => {
64-
if (variant === "contained") return props.theme[accent || "info"];
67+
if (variant === "contained")
68+
return props.theme[accent || "info"] ?? getThemeValue(accent || "info");
6569
return "transparent";
6670
}};
6771
color: ${(props) => {
6872
if (variant === "contained") return "#fff";
69-
return props.theme[accent || "info"];
73+
return props.theme[accent || "info"] ?? getThemeValue(accent || "info");
7074
}};
7175
7276
&:disabled {
7377
border: ${(props) => {
7478
if (variant !== "outlined") return "none";
75-
return `1px solid ${props.theme.disabled}`;
79+
return `1px solid ${props.theme.disabled ?? getThemeValue("disabled")}`;
7680
}};
7781
background-color: ${(props) => {
78-
if (variant === "contained") return props.theme.disabled;
82+
if (variant === "contained")
83+
return props.theme.disabled ?? getThemeValue("disabled");
7984
return "transparent";
8085
}};
8186
color: ${(props) => {
8287
if (variant === "contained") return "#fff";
83-
return props.theme.disabled;
88+
return props.theme.disabled ?? getThemeValue("disabled");
8489
}};
8590
}
8691
8792
&:hover:not(:disabled) {
8893
filter: drop-shadow(
89-
0em 0.125em 0.625em ${(props) => props.theme[accent || "info"]}80
94+
0em 0.125em 0.625em
95+
${(props) =>
96+
props.theme[accent || "info"] ??
97+
getThemeValue(accent || "info")}80
9098
)
9199
brightness(110%);
92100
}
93101
&:active:not(:disabled) {
94102
filter: drop-shadow(
95-
0em 0.0625em 0.3125em ${(props) => props.theme[accent || "info"]}80
103+
0em 0.0625em 0.3125em
104+
${(props) =>
105+
props.theme[accent || "info"] ??
106+
getThemeValue(accent || "info")}80
96107
)
97108
brightness(80%);
98109
}

src/components/ThemeProvider.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,10 @@ export const ThemeProvider = (props: {
6363
</StyledThemeProvider>
6464
</ThemeContext.Provider>
6565
);
66+
}
67+
68+
export const getThemeValue = (key: keyof Palette) => {
69+
const theme = React.useContext(ThemeContext);
70+
const darkMode = useDarkMode(false);
71+
return darkMode.value ? theme.dark[key] : theme.light[key];
6672
}

0 commit comments

Comments
 (0)