@jetbrains/ring-ui 6.0.51 → 6.0.52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,9 @@ declare enum Theme {
4
4
  LIGHT = "light",
5
5
  DARK = "dark"
6
6
  }
7
+ export declare const ThemeContext: import("react").Context<{
8
+ theme: Theme.LIGHT | Theme.DARK;
9
+ }>;
7
10
  export declare function useTheme(): Theme.LIGHT | Theme.DARK;
8
11
  export declare function useThemeClasses(theme: Theme): string;
9
12
  export interface WithThemeClassesProps {
@@ -1,4 +1,4 @@
1
- import { useMemo, useState, useEffect, forwardRef, useContext } from 'react';
1
+ import { useMemo, useState, useEffect, forwardRef, useContext, createContext } from 'react';
2
2
  import classNames from 'classnames';
3
3
  import { createPortal } from 'react-dom';
4
4
  import { PopupTarget, PopupTargetContext } from '../popup/popup.target';
@@ -12,6 +12,8 @@ var Theme;
12
12
  Theme["LIGHT"] = "light";
13
13
  Theme["DARK"] = "dark";
14
14
  })(Theme || (Theme = {}));
15
+ export const ThemeContext = createContext({ theme: Theme.LIGHT });
16
+ const GLOBAL_DARK_CLASS_NAME = 'ring-ui-theme-dark';
15
17
  const darkMatcher = window.matchMedia('(prefers-color-scheme: dark)');
16
18
  export function useTheme() {
17
19
  const [dark, setDark] = useState(darkMatcher.matches);
@@ -27,6 +29,7 @@ export function useThemeClasses(theme) {
27
29
  const resolvedTheme = theme === Theme.AUTO ? systemTheme : theme;
28
30
  return classNames({
29
31
  [styles.dark]: resolvedTheme === Theme.DARK,
32
+ [GLOBAL_DARK_CLASS_NAME]: resolvedTheme === Theme.DARK,
30
33
  [defaultStyles.light]: resolvedTheme === Theme.LIGHT
31
34
  });
32
35
  }
@@ -38,11 +41,11 @@ export function applyTheme(theme, container) {
38
41
  if (theme === Theme.DARK) {
39
42
  container.classList.remove(defaultStyles.light);
40
43
  container.classList.add(styles.dark);
41
- container.classList.add('ring-ui-theme-dark');
44
+ container.classList.add(GLOBAL_DARK_CLASS_NAME);
42
45
  }
43
46
  else {
44
47
  container.classList.remove(styles.dark);
45
- container.classList.remove('ring-ui-theme-dark');
48
+ container.classList.remove(GLOBAL_DARK_CLASS_NAME);
46
49
  container.classList.add(defaultStyles.light);
47
50
  }
48
51
  }
@@ -50,6 +53,7 @@ export const ThemeProvider = forwardRef(function ThemeProvider({ theme = Theme.A
50
53
  const systemTheme = useTheme();
51
54
  const resolvedTheme = theme === Theme.AUTO ? systemTheme : theme;
52
55
  const id = useMemo(() => getUID('popups-with-theme-'), []);
56
+ const themeValue = useMemo(() => ({ theme: resolvedTheme }), [resolvedTheme]);
53
57
  useEffect(() => {
54
58
  if (target != null) {
55
59
  applyTheme(resolvedTheme, target);
@@ -57,14 +61,16 @@ export const ThemeProvider = forwardRef(function ThemeProvider({ theme = Theme.A
57
61
  }, [resolvedTheme, target]);
58
62
  const themeClasses = useThemeClasses(theme);
59
63
  const parentTarget = useContext(PopupTargetContext);
60
- return (<div ref={ref} className={target != null ? undefined : classNames(className, themeClasses)} {...restProps}>{passToPopups
64
+ return (<ThemeContext.Provider value={themeValue}>
65
+ <div ref={ref} className={target != null ? undefined : classNames(className, themeClasses)} {...restProps}>{passToPopups
61
66
  ? (<PopupTarget id={id}>
62
- {popupTarget => (<>
63
- {children}
64
- {createPortal(<div className={themeClasses}>{popupTarget}</div>, parentTarget && getPopupContainer(parentTarget) || document.body)}
65
- </>)}
66
- </PopupTarget>)
67
+ {popupTarget => (<>
68
+ {children}
69
+ {createPortal(<div className={themeClasses}>{popupTarget}</div>, parentTarget && getPopupContainer(parentTarget) || document.body)}
70
+ </>)}
71
+ </PopupTarget>)
67
72
  : children}
68
- </div>);
73
+ </div>
74
+ </ThemeContext.Provider>);
69
75
  });
70
76
  export default Theme;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jetbrains/ring-ui",
3
- "version": "6.0.51",
3
+ "version": "6.0.52",
4
4
  "description": "JetBrains UI library",
5
5
  "author": "JetBrains",
6
6
  "license": "Apache-2.0",