@snack-uikit/code-editor 0.2.1-preview-03f6df7e.0 → 0.2.1-preview-41bee0a4.0

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.
package/README.md CHANGED
@@ -12,7 +12,7 @@
12
12
  ### Props
13
13
  | name | type | default value | description |
14
14
  |------|------|---------------|-------------|
15
- | themeClassName | `string` | - | Класснейм подключенной темы (из хука useThemeConfig() или ThemeProvider) <br> По дефолту берется значение из useTheme внутри |
15
+ | themeName | `string` | - | Название текущей темы. Значение не важно, важно что смена значения запускает пересчет стилей. |
16
16
  | hasBackground | `boolean` | true | Включение/отключение псевдобекграунда |
17
17
  | defaultValue | `string` | - | Default value of the current model |
18
18
  | defaultLanguage | `string` | - | Default language of the current model |
@@ -1,11 +1,13 @@
1
1
  import { EditorProps } from '@monaco-editor/react';
2
2
  import { WithSupportProps } from '@snack-uikit/utils';
3
3
  export type CodeEditorProps = WithSupportProps<{
4
- /** Класснейм подключенной темы (из хука useThemeConfig() или ThemeProvider)
5
- * <br> По дефолту берется значение из useTheme внутри
4
+ /**
5
+ * Название текущей темы. Значение не важно, важно что смена значения запускает пересчет стилей.
6
+ */
7
+ themeName?: string;
8
+ /**
9
+ * Включение/отключение псевдобекграунда
6
10
  */
7
- themeClassName?: string;
8
- /** Включение/отключение псевдобекграунда*/
9
11
  hasBackground?: boolean;
10
12
  }> & EditorProps;
11
- export declare function CodeEditor({ themeClassName, className, theme, options, loading, hasBackground, onMount, ...props }: CodeEditorProps): import("react/jsx-runtime").JSX.Element;
13
+ export declare function CodeEditor({ themeName, className, theme, options, loading, hasBackground, onMount, ...props }: CodeEditorProps): import("react/jsx-runtime").JSX.Element;
@@ -12,7 +12,7 @@ var __rest = (this && this.__rest) || function (s, e) {
12
12
  import { jsx as _jsx } from "react/jsx-runtime";
13
13
  import { Editor, useMonaco } from '@monaco-editor/react';
14
14
  import cn from 'classnames';
15
- import { useCallback, useMemo } from 'react';
15
+ import { useCallback, useMemo, useRef } from 'react';
16
16
  import { Spinner } from '@snack-uikit/loaders';
17
17
  import { extractSupportProps } from '@snack-uikit/utils';
18
18
  import { CODE_EDITOR_OPTIONS, DEFAULT_THEME_OPTIONS, DEFAULT_THEME_VALUES } from './constants';
@@ -21,14 +21,15 @@ import styles from './styles.module.css';
21
21
  import { initLoaderConfig, isDark } from './utils';
22
22
  initLoaderConfig();
23
23
  export function CodeEditor(_a) {
24
- var { themeClassName, className, theme, options, loading, hasBackground = true, onMount } = _a, props = __rest(_a, ["themeClassName", "className", "theme", "options", "loading", "hasBackground", "onMount"]);
24
+ var { themeName, className, theme, options, loading, hasBackground = true, onMount } = _a, props = __rest(_a, ["themeName", "className", "theme", "options", "loading", "hasBackground", "onMount"]);
25
25
  const monaco = useMonaco();
26
+ const wrapperRef = useRef(null);
26
27
  const onEditorMount = useCallback((editor, monaco) => {
27
28
  var _a, _b;
28
29
  monaco && ((_b = (_a = window === null || window === void 0 ? void 0 : window.document) === null || _a === void 0 ? void 0 : _a.fonts) === null || _b === void 0 ? void 0 : _b.ready.finally(monaco.editor.remeasureFonts));
29
30
  onMount === null || onMount === void 0 ? void 0 : onMount(editor, monaco);
30
31
  }, [onMount]);
31
- const themeValues = useCalculatedThemeValues(themeClassName);
32
+ const themeValues = useCalculatedThemeValues({ themeName, stylesOriginNode: wrapperRef.current });
32
33
  const themeDataWithoutBase = useMemo(() => (Object.assign({ inherit: true }, ((themeValues === null || themeValues === void 0 ? void 0 : themeValues.sys)
33
34
  ? {
34
35
  semanticTokenColors: {
@@ -85,5 +86,5 @@ export function CodeEditor(_a) {
85
86
  fontFamily: themeValues.mono.body.s['font-family'],
86
87
  }
87
88
  : DEFAULT_THEME_OPTIONS.mono.s)), options)), [options, themeValues === null || themeValues === void 0 ? void 0 : themeValues.mono]);
88
- return (_jsx("div", Object.assign({ className: className }, extractSupportProps(props), { children: _jsx(Editor, Object.assign({}, props, { theme: (theme !== null && theme !== void 0 ? theme : dark) ? 'snackDark' : 'snack', className: cn({ [styles.editor]: hasBackground }), loading: loading !== null && loading !== void 0 ? loading : _jsx(Spinner, {}), options: mergedOptions, onMount: onEditorMount })) })));
89
+ return (_jsx("div", Object.assign({ className: className }, extractSupportProps(props), { ref: wrapperRef, children: _jsx(Editor, Object.assign({}, props, { theme: (theme !== null && theme !== void 0 ? theme : dark) ? 'snackDark' : 'snack', className: cn({ [styles.editor]: hasBackground }), loading: loading !== null && loading !== void 0 ? loading : _jsx(Spinner, {}), options: mergedOptions, onMount: onEditorMount })) })));
89
90
  }
@@ -1,4 +1,8 @@
1
- export declare function useCalculatedThemeValues(themeClassNameProps?: string): {
1
+ type UseCalculatedThemeValuesProps = {
2
+ stylesOriginNode: HTMLDivElement | null;
3
+ themeName?: string;
4
+ };
5
+ export declare function useCalculatedThemeValues({ stylesOriginNode, themeName }: UseCalculatedThemeValuesProps): {
2
6
  sys: {
3
7
  neutral: {
4
8
  decorDefault: string;
@@ -37,3 +41,4 @@ export declare function useCalculatedThemeValues(themeClassNameProps?: string):
37
41
  };
38
42
  };
39
43
  } | undefined;
44
+ export {};
@@ -1,4 +1,4 @@
1
- import { useEffect, useMemo, useState } from 'react';
1
+ import { useEffect, useState } from 'react';
2
2
  import { useThemeContext } from '@snack-uikit/utils';
3
3
  import { THEME_VARS } from './constants';
4
4
  function getThemeVars(styles) {
@@ -14,16 +14,15 @@ function getThemeVars(styles) {
14
14
  return res;
15
15
  };
16
16
  }
17
- export function useCalculatedThemeValues(themeClassNameProps) {
17
+ export function useCalculatedThemeValues({ stylesOriginNode, themeName }) {
18
18
  const [values, setValues] = useState(undefined);
19
- const { themeClassName: themeClassNameContext } = useThemeContext();
20
- const themeClassName = useMemo(() => themeClassNameProps !== null && themeClassNameProps !== void 0 ? themeClassNameProps : themeClassNameContext, [themeClassNameContext, themeClassNameProps]);
19
+ const { themeClassName } = useThemeContext();
20
+ const trigger = themeName !== null && themeName !== void 0 ? themeName : themeClassName;
21
21
  useEffect(() => {
22
- const elem = document.querySelector(themeClassName ? '.' + themeClassName : 'body');
23
- if (elem) {
24
- const styles = getComputedStyle(elem);
22
+ if (stylesOriginNode) {
23
+ const styles = getComputedStyle(stylesOriginNode);
25
24
  setValues(getThemeVars(styles)(THEME_VARS));
26
25
  }
27
- }, [themeClassName]);
26
+ }, [stylesOriginNode, trigger]);
28
27
  return values;
29
28
  }
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.2.1-preview-03f6df7e.0",
7
+ "version": "0.2.1-preview-41bee0a4.0",
8
8
  "sideEffects": [
9
9
  "*.css",
10
10
  "*.woff",
@@ -37,5 +37,5 @@
37
37
  "@snack-uikit/utils": "3.3.0",
38
38
  "classnames": "2.5.1"
39
39
  },
40
- "gitHead": "6901ea13eae02eca6b24ac88fc2f8e8e8f9c3dba"
40
+ "gitHead": "74a281ab089c84e90be8d8a6c363c277b53bd802"
41
41
  }
@@ -1,6 +1,6 @@
1
1
  import { Editor, EditorProps, OnMount, useMonaco } from '@monaco-editor/react';
2
2
  import cn from 'classnames';
3
- import { useCallback, useMemo } from 'react';
3
+ import { useCallback, useMemo, useRef } from 'react';
4
4
 
5
5
  import { Spinner } from '@snack-uikit/loaders';
6
6
  import { extractSupportProps, WithSupportProps } from '@snack-uikit/utils';
@@ -13,17 +13,19 @@ import { initLoaderConfig, isDark } from './utils';
13
13
  initLoaderConfig();
14
14
 
15
15
  export type CodeEditorProps = WithSupportProps<{
16
- /** Класснейм подключенной темы (из хука useThemeConfig() или ThemeProvider)
17
- * <br> По дефолту берется значение из useTheme внутри
16
+ /**
17
+ * Название текущей темы. Значение не важно, важно что смена значения запускает пересчет стилей.
18
+ */
19
+ themeName?: string;
20
+ /**
21
+ * Включение/отключение псевдобекграунда
18
22
  */
19
- themeClassName?: string;
20
- /** Включение/отключение псевдобекграунда*/
21
23
  hasBackground?: boolean;
22
24
  }> &
23
25
  EditorProps;
24
26
 
25
27
  export function CodeEditor({
26
- themeClassName,
28
+ themeName,
27
29
  className,
28
30
  theme,
29
31
  options,
@@ -33,6 +35,7 @@ export function CodeEditor({
33
35
  ...props
34
36
  }: CodeEditorProps) {
35
37
  const monaco = useMonaco();
38
+ const wrapperRef = useRef<HTMLDivElement>(null);
36
39
 
37
40
  const onEditorMount = useCallback<OnMount>(
38
41
  (editor, monaco) => {
@@ -42,7 +45,7 @@ export function CodeEditor({
42
45
  [onMount],
43
46
  );
44
47
 
45
- const themeValues = useCalculatedThemeValues(themeClassName);
48
+ const themeValues = useCalculatedThemeValues({ themeName, stylesOriginNode: wrapperRef.current });
46
49
 
47
50
  const themeDataWithoutBase = useMemo(
48
51
  () => ({
@@ -121,7 +124,7 @@ export function CodeEditor({
121
124
  );
122
125
 
123
126
  return (
124
- <div className={className} {...extractSupportProps(props)}>
127
+ <div className={className} {...extractSupportProps(props)} ref={wrapperRef}>
125
128
  <Editor
126
129
  {...props}
127
130
  theme={theme ?? dark ? 'snackDark' : 'snack'}
@@ -1,4 +1,4 @@
1
- import { useEffect, useMemo, useState } from 'react';
1
+ import { useEffect, useState } from 'react';
2
2
 
3
3
  import { useThemeContext } from '@snack-uikit/utils';
4
4
 
@@ -21,25 +21,24 @@ function getThemeVars(styles: CSSStyleDeclaration) {
21
21
  };
22
22
  }
23
23
 
24
- export function useCalculatedThemeValues(themeClassNameProps?: string) {
24
+ type UseCalculatedThemeValuesProps = {
25
+ stylesOriginNode: HTMLDivElement | null;
26
+ themeName?: string;
27
+ };
28
+
29
+ export function useCalculatedThemeValues({ stylesOriginNode, themeName }: UseCalculatedThemeValuesProps) {
25
30
  const [values, setValues] = useState<typeof THEME_VARS | undefined>(undefined);
26
31
 
27
- const { themeClassName: themeClassNameContext } = useThemeContext();
32
+ const { themeClassName } = useThemeContext();
28
33
 
29
- const themeClassName = useMemo(
30
- () => themeClassNameProps ?? themeClassNameContext,
31
- [themeClassNameContext, themeClassNameProps],
32
- );
34
+ const trigger = themeName ?? themeClassName;
33
35
 
34
36
  useEffect(() => {
35
- const elem = document.querySelector(themeClassName ? '.' + themeClassName : 'body');
36
-
37
- if (elem) {
38
- const styles = getComputedStyle(elem);
39
-
37
+ if (stylesOriginNode) {
38
+ const styles = getComputedStyle(stylesOriginNode);
40
39
  setValues(getThemeVars(styles)(THEME_VARS) as typeof THEME_VARS);
41
40
  }
42
- }, [themeClassName]);
41
+ }, [stylesOriginNode, trigger]);
43
42
 
44
43
  return values;
45
44
  }