aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/sonar-ui-common/components/theme.ts
blob: 1dabe475f62a4e626e9d640daa33959a28ce849b (plain)
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
/*
 * SonarQube
 * Copyright (C) 2009-2021 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import { css, ThemeContext as EmotionThemeContext } from '@emotion/core';
import emotionStyled, { CreateStyled } from '@emotion/styled';
import {
  ThemeProvider as EmotionThemeProvider,
  ThemeProviderProps,
  useTheme as emotionUseTheme,
  withTheme
} from 'emotion-theming';
import * as React from 'react';

export interface Theme {
  colors: T.Dict<string>;
  sizes: T.Dict<string>;
  rawSizes: T.Dict<number>;
  fonts: T.Dict<string>;
  zIndexes: T.Dict<string>;
  others: T.Dict<string>;
}

export interface ThemedProps {
  theme: Theme;
}

const ThemeContext = EmotionThemeContext as React.Context<Theme>;

export const styled = emotionStyled as CreateStyled<Theme>;
export const ThemeConsumer = ThemeContext.Consumer;
export const ThemeProvider = EmotionThemeProvider as React.ProviderExoticComponent<
  ThemeProviderProps<Theme>
>;
export const useTheme = emotionUseTheme as () => Theme;

export function themeGet(type: keyof Theme, name: string | number) {
  return function({ theme }: Partial<ThemedProps>) {
    return theme?.[type][name];
  };
}
export function themeColor(name: keyof Theme['colors']) {
  return themeGet('colors', name);
}
export function themeSize(name: keyof Theme['sizes']) {
  return themeGet('sizes', name);
}

export { css, withTheme };
export default ThemeContext;