aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/theme/ThemeProvider.tsx
blob: a996cde84f3ef8f396bec1699e4de46358ccfbdd (plain) (blame)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
 * SPDX-FileCopyrightText: 2021-2024 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */

import {
  alpha,
  createTheme,
  responsiveFontSizes,
  type Theme,
  type ThemeOptions,
  ThemeProvider as MaterialUiThemeProvider,
  type TypographyStyle,
  type CSSObject,
} from '@mui/material/styles';
import { observer } from 'mobx-react-lite';
import { type ReactNode, createContext, useContext } from 'react';

import { useRootStore } from '../RootStoreProvider';

interface OuterPalette {
  background: string;
  border: string;
}

interface TypeHashPalette {
  text: string;
  box: string;
}

interface HighlightPalette {
  number: string;
  parameter: string;
  comment: string;
  activeLine: string;
  selection: string;
  foldPlaceholder: string;
  activeLintRange: string;
  occurences: {
    read: string;
    write: string;
  };
  search: {
    match: string;
    selected: string;
    contrastText: string;
  };
  typeHash: TypeHashPalette[];
}

declare module '@mui/material/styles' {
  interface TypographyVariants {
    fontWeightEditorNormal: number;
    fontWeightEditorTypeHash: number;
    fontWeightEditorBold: number;
    editor: TypographyStyle;
  }

  interface TypographyVariantsOptions {
    fontWeightEditorNormal?: number;
    fontWeightEditorTypeHash?: number;
    fontWeightEditorBold?: number;
    editor?: TypographyStyle;
  }

  interface Palette {
    outer: OuterPalette;
    highlight: HighlightPalette;
  }

  interface PaletteOptions {
    outer?: Partial<OuterPalette>;
    highlight?: Partial<HighlightPalette>;
  }
}

function createResponsiveTheme(
  options: ThemeOptions,
  overrides: ThemeOptions = {},
): Theme {
  const theme = createTheme({
    ...options,
    typography: {
      fontFamily:
        '"Open Sans Variable", "Open Sans", "Roboto", "Helvetica", "Arial", sans-serif',
      fontWeightMedium: 500,
      fontWeightEditorNormal: 400,
      fontWeightEditorTypeHash: 500,
      fontWeightEditorBold: 700,
      button: {
        fontWeight: 600,
        fontVariationSettings: '"wdth" 87.5',
        fontSize: '1rem',
        lineHeight: 1.5,
      },
      editor: {
        fontFamily:
          '"JetBrains Mono Variable", "JetBrains Mono", "Cascadia Code", "Fira Code", monospace',
        fontFeatureSettings: '"liga", "calt"',
        // `rem` for JetBrains MonoVariable make the text too large in Safari.
        fontSize: '16px',
        fontWeight: 400,
        lineHeight: 1.5,
        letterSpacing: 0,
        textRendering: 'optimizeLegibility',
      },
      ...(options.typography ?? {}),
    },
  });

  function shadedButtonStyle(color: string): CSSObject {
    const opacity = theme.palette.action.focusOpacity;
    return {
      background: alpha(color, opacity),
      ':hover': {
        background: alpha(color, opacity + theme.palette.action.hoverOpacity),
        '@media(hover: none)': {
          background: alpha(color, opacity),
        },
      },
      '&.Mui-disabled': {
        background: alpha(theme.palette.text.disabled, opacity),
      },
    };
  }

  const themeWithComponents = createTheme(theme, {
    components: {
      MuiCssBaseline: {
        styleOverrides: {
          body: {
            overscrollBehavior: 'contain',
          },
        },
      },
      MuiButton: {
        styleOverrides: {
          root: {
            '&.rounded': { borderRadius: '50em' },
            '.MuiButton-startIcon': { marginRight: 6 },
            '.MuiButton-endIcon': { marginLeft: 6 },
            '&.shaded': {
              ...shadedButtonStyle(theme.palette.text.primary),
              ...(
                [
                  'primary',
                  'secondary',
                  'error',
                  'warning',
                  'success',
                  'info',
                ] as const
              ).reduce((accumulator: CSSObject, color) => {
                const colorCapitalized =
                  (color[0] ?? '').toUpperCase() + color.substring(1);
                return {
                  ...accumulator,
                  [`&.MuiButton-text${colorCapitalized}, &.MuiButton-outlined${colorCapitalized}`]:
                    shadedButtonStyle(theme.palette[color].main),
                };
              }, {}),
            },
          },
          sizeSmall: { fontSize: '0.875rem', lineHeight: '1.75' },
          sizeLarge: { fontSize: '1rem' },
          text: { '&.rounded': { padding: '6px 14px' } },
          textSizeSmall: { '&.rounded': { padding: '4px 8px' } },
          textSizeLarge: { '&.rounded': { padding: '8px 20px' } },
          outlined: { '&.rounded': { padding: '5px 13px' } },
          outlinedSizeSmall: { '&.rounded': { padding: '3px 9px' } },
          outlinedSizeLarge: { '&.rounded': { padding: '7px 19px' } },
        },
      },
      MuiToggleButton: {
        styleOverrides: {
          root: { '&.iconOnly': { borderRadius: '100%' } },
        },
      },
      MuiToggleButtonGroup: {
        styleOverrides: {
          root: {
            '&.rounded .MuiToggleButtonGroup-groupedHorizontal': {
              ':first-of-type': {
                paddingLeft: 15,
                borderRadius: '50em 0 0 50em',
              },
              ':last-of-type': {
                paddingRight: 15,
                borderRadius: '0 50em 50em 0',
              },
              '&.MuiToggleButton-sizeSmall': {
                ':first-of-type': { paddingLeft: 9 },
                ':last-of-type': { paddingRight: 9 },
              },
              '&.MuiToggleButton-sizeLarge': {
                ':first-of-type': { paddingLeft: 21 },
                ':last-of-type': { paddingRight: 21 },
              },
            },
          },
        },
      },
      MuiTooltip: {
        styleOverrides: {
          tooltip: {
            background: alpha('#212121', 0.93),
            color: '#fff',
          },
          arrow: {
            color: alpha('#212121', 0.93),
          },
        },
      },
    },
  });

  const themeWithOverrides = createTheme(themeWithComponents, overrides);

  return responsiveFontSizes(themeWithOverrides);
}

const lightTheme = (() => {
  const primaryText = '#19202b';
  const disabledText = '#a0a1a7';
  const darkBackground = '#f5f5f5';

  return createResponsiveTheme({
    palette: {
      mode: 'light',
      primary: { main: '#038a99' },
      secondary: { main: '#61afef' },
      error: { main: '#ca1243' },
      warning: { main: '#c18401' },
      success: { main: '#50a14f' },
      info: { main: '#4078f2' },
      background: {
        default: '#fff',
        paper: '#fff',
      },
      text: {
        primary: primaryText,
        secondary: '#696c77',
        disabled: disabledText,
      },
      divider: alpha(primaryText, 0.16),
      outer: {
        background: darkBackground,
        border: '#c8c8c8',
      },
      highlight: {
        number: '#0084bc',
        parameter: '#6a3e3e',
        comment: disabledText,
        activeLine: darkBackground,
        selection: '#c8e4fb',
        foldPlaceholder: alpha(primaryText, 0.08),
        activeLintRange: alpha('#f2a60d', 0.28),
        occurences: {
          read: alpha(primaryText, 0.16),
          write: alpha(primaryText, 0.16),
        },
        search: {
          match: '#00bcd4',
          selected: '#d500f9',
          contrastText: '#fff',
        },
        typeHash: [
          { text: '#986801', box: '#e5c07b' },
          { text: '#d6493e', box: '#e06c75' },
          { text: '#50a14f', box: '#98c379' },
          { text: '#a626a4', box: '#c678dd' },
          { text: '#4078f2', box: '#80a7f4' },
          { text: '#827662', box: '#e3d1b2' },
          { text: '#904f53', box: '#e78b8f' },
          { text: '#637855', box: '#abcc94' },
          { text: '#805f89', box: '#dbb2e8' },
          { text: '#5987ae', box: '#92c0e9' },
        ],
      },
    },
  });
})();

const darkTheme = (() => {
  const primaryText = '#ebebff';
  const secondaryText = '#abb2bf';
  const darkBackground = '#21252b';

  return createResponsiveTheme(
    {
      typography: {
        fontWeightEditorNormal: 350,
        fontWeightEditorTypeHash: 350,
        fontWeightEditorBold: 650,
      },
      palette: {
        mode: 'dark',
        primary: { main: '#56b6c2' },
        secondary: { main: '#be5046' },
        error: { main: '#e06c75' },
        warning: { main: '#d19a66' },
        success: { main: '#98c379' },
        info: { main: '#61afef' },
        background: {
          default: '#282c34',
          paper: darkBackground,
        },
        text: {
          primary: primaryText,
          secondary: secondaryText,
          disabled: '#5c6370',
        },
        divider: alpha(primaryText, 0.24),
        outer: {
          background: darkBackground,
          border: '#181a1f',
        },
        highlight: {
          number: '#6188a6',
          parameter: '#c8ae9d',
          comment: '#7f848e',
          activeLine: '#2c313c',
          selection: '#404859',
          foldPlaceholder: alpha(primaryText, 0.12),
          activeLintRange: alpha('#fbc346', 0.28),
          occurences: {
            read: alpha(primaryText, 0.14),
            write: alpha(primaryText, 0.14),
          },
          search: {
            match: '#33eaff',
            selected: '#dd33fa',
            contrastText: darkBackground,
          },
          typeHash: [
            { text: '#e5c07b', box: '#ae8003' },
            { text: '#e06c75', box: '#a23b47' },
            { text: '#98c379', box: '#428141' },
            { text: '#c678dd', box: '#854797' },
            { text: '#61afef', box: '#3982bb' },
            { text: '#e3d1b2', box: '#827662' },
            { text: '#e78b8f', box: '#904f53' },
            { text: '#abcc94', box: '#647e63' },
            { text: '#dbb2e8', box: '#805f89' },
            { text: '#92c0e9', box: '#4f7799' },
          ],
        },
      },
    },
    {
      components: {
        MuiTooltip: {
          styleOverrides: {
            tooltip: {
              color: primaryText,
            },
          },
        },
      },
    },
  );
})();

const ContrastThemeContext = createContext<Theme | undefined>(undefined);

function ThemeAndContrastThemeProvider({
  theme,
  contrastTheme,
  children,
}: {
  theme: Theme;
  contrastTheme: Theme;
  children?: ReactNode;
}): JSX.Element {
  return (
    <MaterialUiThemeProvider theme={theme}>
      <ContrastThemeContext.Provider value={contrastTheme}>
        {children}
      </ContrastThemeContext.Provider>
    </MaterialUiThemeProvider>
  );
}

ThemeAndContrastThemeProvider.defaultProps = {
  children: undefined,
};

export function ContrastThemeProvider({
  children,
}: {
  children?: ReactNode;
}): JSX.Element {
  const contrastTheme = useContext(ContrastThemeContext);
  if (!contrastTheme) {
    throw new Error('ContrastThemeProvider must be used within ThemeProvider');
  }
  return (
    <MaterialUiThemeProvider theme={contrastTheme}>
      {children}
    </MaterialUiThemeProvider>
  );
}

ContrastThemeProvider.defaultProps = {
  children: undefined,
};

const ThemeProvider = observer(function ThemeProvider({
  children,
}: {
  children?: ReactNode;
}): JSX.Element {
  const {
    themeStore: { darkMode },
  } = useRootStore();

  return (
    <ThemeAndContrastThemeProvider
      theme={darkMode ? darkTheme : lightTheme}
      contrastTheme={darkTheme}
    >
      {children}
    </ThemeAndContrastThemeProvider>
  );
});

ThemeProvider.defaultProps = {
  children: undefined,
};

export default ThemeProvider;