|
| 1 | +import { createTheme, type PaletteColor, type PaletteColorOptions } from '@mui/material'; |
| 2 | +import type { ThemeTokens } from './getTokensByMode'; |
| 3 | +import isDarkMode from './isDarkMode'; |
| 4 | + |
| 5 | +// Extend theme options |
| 6 | +declare module '@mui/material/styles' { |
| 7 | + interface TypeText { |
| 8 | + tertiary: string; |
| 9 | + main: string; |
| 10 | + } |
| 11 | + |
| 12 | + interface Palette { |
| 13 | + tertiary: PaletteColor; |
| 14 | + hover: PaletteColor; |
| 15 | + disabled: PaletteColor; |
| 16 | + } |
| 17 | + |
| 18 | + interface PaletteOptions { |
| 19 | + tertiary?: PaletteColorOptions; |
| 20 | + hover?: PaletteColorOptions; |
| 21 | + disabled?: PaletteColorOptions; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +const getMuiCustomTheme = ({ tokens }: { tokens: ThemeTokens }) => { |
| 26 | + const buttonSx = { |
| 27 | + height: 40, // 40px |
| 28 | + textTransform: 'none', |
| 29 | + borderRadius: tokens.shapes.borderRadiusMedium, |
| 30 | + } as const; |
| 31 | + |
| 32 | + const { colors } = tokens; |
| 33 | + |
| 34 | + return createTheme({ |
| 35 | + palette: { |
| 36 | + mode: isDarkMode() ? 'dark' : 'light', |
| 37 | + primary: { |
| 38 | + main: colors.primary, |
| 39 | + contrastText: colors.onPrimary, |
| 40 | + dark: colors.primary, |
| 41 | + light: colors.background, |
| 42 | + }, |
| 43 | + secondary: { |
| 44 | + main: colors.secondary, |
| 45 | + contrastText: colors.onSecondary, |
| 46 | + dark: colors.secondary, |
| 47 | + light: colors.background, |
| 48 | + }, |
| 49 | + tertiary: { |
| 50 | + main: colors.tertiary, |
| 51 | + contrastText: colors.onTertiary, |
| 52 | + dark: colors.tertiary, |
| 53 | + light: colors.background, |
| 54 | + }, |
| 55 | + success: { |
| 56 | + main: colors.success, |
| 57 | + contrastText: colors.onSuccess, |
| 58 | + dark: colors.successHover, |
| 59 | + light: colors.background, |
| 60 | + }, |
| 61 | + warning: { |
| 62 | + main: colors.warning, |
| 63 | + contrastText: colors.onWarning, |
| 64 | + dark: colors.warningHover, |
| 65 | + light: colors.background, |
| 66 | + }, |
| 67 | + error: { |
| 68 | + main: colors.error, |
| 69 | + contrastText: colors.onError, |
| 70 | + dark: colors.errorHover, |
| 71 | + light: colors.background, |
| 72 | + }, |
| 73 | + background: { |
| 74 | + default: colors.background, |
| 75 | + paper: colors.surface, |
| 76 | + }, |
| 77 | + text: { |
| 78 | + primary: colors.textSecondary, // This is the default text color |
| 79 | + main: colors.textPrimary, // This is primary color for specific uses |
| 80 | + secondary: colors.textSecondary, |
| 81 | + tertiary: colors.textTertiary, |
| 82 | + }, |
| 83 | + divider: colors.border, |
| 84 | + hover: { |
| 85 | + main: colors.primaryHover, |
| 86 | + }, |
| 87 | + disabled: { |
| 88 | + main: colors.disabled, |
| 89 | + }, |
| 90 | + }, |
| 91 | + components: { |
| 92 | + MuiButton: { |
| 93 | + styleOverrides: { |
| 94 | + root: { |
| 95 | + ...buttonSx, |
| 96 | + fontSize: tokens.typography.typeScale.desktop['body-base'].fontSize.value, |
| 97 | + lineHeight: tokens.typography.typeScale.desktop['body-base'].lineHeight.value, |
| 98 | + fontWeight: tokens.typography.weight['caption-semibold'].value, |
| 99 | + }, |
| 100 | + outlined: { |
| 101 | + borderColor: colors.primary, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + MuiToolbar: { |
| 106 | + styleOverrides: { |
| 107 | + root: { |
| 108 | + paddingLeft: '0', |
| 109 | + paddingRight: '0', |
| 110 | + '@media (min-width: 600px)': { |
| 111 | + paddingLeft: 0, |
| 112 | + paddingRight: 0, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + MuiAppBar: { |
| 118 | + styleOverrides: { |
| 119 | + root: { |
| 120 | + backgroundColor: colors.surface, |
| 121 | + color: colors.onSurface, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + MuiPaper: { |
| 126 | + styleOverrides: { |
| 127 | + root: { |
| 128 | + backgroundColor: colors.background, |
| 129 | + color: colors.onBackground, |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + MuiOutlinedInput: { |
| 134 | + styleOverrides: { |
| 135 | + root: { |
| 136 | + backgroundColor: colors.surface, |
| 137 | + borderRadius: tokens.shapes.borderRadiusMedium, |
| 138 | + backgroundClip: 'padding-box', |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + MuiInputLabel: { |
| 143 | + styleOverrides: { |
| 144 | + sizeSmall: { |
| 145 | + fontSize: tokens.typography.typeScale.desktop['body-base'].fontSize.value, |
| 146 | + lineHeight: tokens.typography.typeScale.desktop['body-base'].lineHeight.value, |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + MuiFormHelperText: { |
| 151 | + styleOverrides: { |
| 152 | + root: { |
| 153 | + color: colors.onSurface, |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + MuiTypography: { |
| 158 | + styleOverrides: { |
| 159 | + h1: createResponsiveTypography(tokens, 'headline', 'headline'), |
| 160 | + h2: createResponsiveTypography(tokens, 'subtitle', 'subtitle'), |
| 161 | + h3: createResponsiveTypography(tokens, 'heading-1', 'heading-1'), |
| 162 | + h4: createResponsiveTypography(tokens, 'heading-2', 'heading-2'), |
| 163 | + h5: createResponsiveTypography(tokens, 'heading-3', 'heading-3'), |
| 164 | + h6: createResponsiveTypography(tokens, 'heading-4', 'heading-4'), |
| 165 | + subtitle1: createResponsiveTypography( |
| 166 | + tokens, |
| 167 | + 'body-extended-semibold', |
| 168 | + 'body-extended-semibold' |
| 169 | + ), |
| 170 | + subtitle2: createResponsiveTypography(tokens, 'body-base-semibold', 'body-base-semibold'), |
| 171 | + body1: createResponsiveTypography(tokens, 'body-extended', 'body-extended'), |
| 172 | + body2: createResponsiveTypography(tokens, 'body-base', 'body-base'), |
| 173 | + caption: createResponsiveTypography(tokens, 'caption', 'caption'), |
| 174 | + }, |
| 175 | + }, |
| 176 | + }, |
| 177 | + typography: { |
| 178 | + fontFamily: tokens.typography.typeface.plain.value, |
| 179 | + h1: { |
| 180 | + fontSize: tokens.typography.typeScale.desktop.headline.fontSize.value, |
| 181 | + lineHeight: tokens.typography.typeScale.desktop.headline.lineHeight.value, |
| 182 | + fontWeight: tokens.typography.typeScale.desktop.headline.fontWeight.value, |
| 183 | + }, |
| 184 | + h2: { |
| 185 | + fontSize: tokens.typography.typeScale.desktop.subtitle.fontSize.value, |
| 186 | + lineHeight: tokens.typography.typeScale.desktop.subtitle.lineHeight.value, |
| 187 | + fontWeight: tokens.typography.typeScale.desktop.subtitle.fontWeight.value, |
| 188 | + }, |
| 189 | + h3: { |
| 190 | + fontSize: tokens.typography.typeScale.desktop['heading-1'].fontSize.value, |
| 191 | + lineHeight: tokens.typography.typeScale.desktop['heading-1'].lineHeight.value, |
| 192 | + fontWeight: tokens.typography.typeScale.desktop['heading-1'].fontWeight.value, |
| 193 | + }, |
| 194 | + h4: { |
| 195 | + fontSize: tokens.typography.typeScale.desktop['heading-2'].fontSize.value, |
| 196 | + lineHeight: tokens.typography.typeScale.desktop['heading-2'].lineHeight.value, |
| 197 | + fontWeight: tokens.typography.typeScale.desktop['heading-2'].fontWeight.value, |
| 198 | + }, |
| 199 | + h5: { |
| 200 | + fontSize: tokens.typography.typeScale.desktop['heading-3'].fontSize.value, |
| 201 | + lineHeight: tokens.typography.typeScale.desktop['heading-3'].lineHeight.value, |
| 202 | + fontWeight: tokens.typography.typeScale.desktop['heading-3'].fontWeight.value, |
| 203 | + }, |
| 204 | + h6: { |
| 205 | + fontSize: tokens.typography.typeScale.desktop['heading-4'].fontSize.value, |
| 206 | + lineHeight: tokens.typography.typeScale.desktop['heading-4'].lineHeight.value, |
| 207 | + fontWeight: tokens.typography.typeScale.desktop['heading-4'].fontWeight.value, |
| 208 | + }, |
| 209 | + subtitle1: { |
| 210 | + fontSize: tokens.typography.typeScale.desktop['body-extended-semibold'].fontSize.value, |
| 211 | + lineHeight: tokens.typography.typeScale.desktop['body-extended-semibold'].lineHeight.value, |
| 212 | + fontWeight: tokens.typography.typeScale.desktop['body-extended-semibold'].fontWeight.value, |
| 213 | + }, |
| 214 | + subtitle2: { |
| 215 | + fontSize: tokens.typography.typeScale.desktop['body-base-semibold'].fontSize.value, |
| 216 | + lineHeight: tokens.typography.typeScale.desktop['body-base-semibold'].lineHeight.value, |
| 217 | + fontWeight: tokens.typography.typeScale.desktop['body-base-semibold'].fontWeight.value, |
| 218 | + }, |
| 219 | + body1: { |
| 220 | + fontSize: tokens.typography.typeScale.desktop['body-extended'].fontSize.value, |
| 221 | + lineHeight: tokens.typography.typeScale.desktop['body-extended'].lineHeight.value, |
| 222 | + fontWeight: tokens.typography.typeScale.desktop['body-extended'].fontWeight.value, |
| 223 | + }, |
| 224 | + body2: { |
| 225 | + fontSize: tokens.typography.typeScale.desktop['body-base'].fontSize.value, |
| 226 | + lineHeight: tokens.typography.typeScale.desktop['body-base'].lineHeight.value, |
| 227 | + fontWeight: tokens.typography.typeScale.desktop['body-base'].fontWeight.value, |
| 228 | + }, |
| 229 | + caption: { |
| 230 | + fontSize: tokens.typography.typeScale.desktop.caption.fontSize.value, |
| 231 | + lineHeight: tokens.typography.typeScale.desktop.caption.lineHeight.value, |
| 232 | + fontWeight: tokens.typography.typeScale.desktop.caption.fontWeight.value, |
| 233 | + }, |
| 234 | + }, |
| 235 | + }); |
| 236 | +}; |
| 237 | + |
| 238 | +// Helper function to generate responsive typography |
| 239 | +function createResponsiveTypography( |
| 240 | + tokens: ThemeTokens, |
| 241 | + desktopVariant: keyof typeof tokens.typography.typeScale.desktop, |
| 242 | + mobileVariant: keyof typeof tokens.typography.typeScale.mobile |
| 243 | +) { |
| 244 | + return { |
| 245 | + '@media (max-width:1199px)': { |
| 246 | + fontSize: `calc(${tokens.typography.typeScale.mobile[mobileVariant].fontSize.value} * ${desktopVariant === 'headline' ? 1.5 : 1.15})`, |
| 247 | + lineHeight: `calc(${tokens.typography.typeScale.mobile[mobileVariant].lineHeight.value} * ${desktopVariant === 'headline' ? 1.5 : 1.15})`, |
| 248 | + fontWeight: tokens.typography.typeScale.mobile[mobileVariant].fontWeight.value, |
| 249 | + }, |
| 250 | + '@media (max-width:899px)': { |
| 251 | + fontSize: tokens.typography.typeScale.mobile[mobileVariant].fontSize.value, |
| 252 | + lineHeight: tokens.typography.typeScale.mobile[mobileVariant].lineHeight.value, |
| 253 | + fontWeight: tokens.typography.typeScale.mobile[mobileVariant].fontWeight.value, |
| 254 | + }, |
| 255 | + }; |
| 256 | +} |
| 257 | + |
| 258 | +export default getMuiCustomTheme; |
0 commit comments