@dr.pogodin/react-utils 1.46.1 → 1.47.0-alpha.1
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/babel.config.js +1 -0
- package/babel.libweb.config.js +16 -0
- package/bin/build.js +24 -15
- package/bin/setup.js +11 -8
- package/build/development/client/getInj.js.map +1 -1
- package/build/development/client/index.js.map +1 -1
- package/build/development/client/init.js.map +1 -1
- package/build/development/index.js +7 -1
- package/build/development/index.js.map +1 -1
- package/build/development/server/Cache.js.map +1 -1
- package/build/development/server/index.js +13 -8
- package/build/development/server/index.js.map +1 -1
- package/build/development/server/renderer.js +15 -8
- package/build/development/server/renderer.js.map +1 -1
- package/build/development/server/server.js +7 -5
- package/build/development/server/server.js.map +1 -1
- package/build/development/shared/components/Button/index.js +6 -6
- package/build/development/shared/components/Button/index.js.map +1 -1
- package/build/development/shared/components/Checkbox/index.js +8 -8
- package/build/development/shared/components/Checkbox/index.js.map +1 -1
- package/build/development/shared/components/GenericLink/index.js +5 -1
- package/build/development/shared/components/GenericLink/index.js.map +1 -1
- package/build/development/shared/components/Input/index.js +10 -9
- package/build/development/shared/components/Input/index.js.map +1 -1
- package/build/development/shared/components/Link.js.map +1 -1
- package/build/development/shared/components/Modal/index.js +8 -6
- package/build/development/shared/components/Modal/index.js.map +1 -1
- package/build/development/shared/components/NavLink.js.map +1 -1
- package/build/development/shared/components/PageLayout/index.js +6 -6
- package/build/development/shared/components/PageLayout/index.js.map +1 -1
- package/build/development/shared/components/TextArea/index.js +13 -9
- package/build/development/shared/components/TextArea/index.js.map +1 -1
- package/build/development/shared/components/Throbber/index.js +6 -6
- package/build/development/shared/components/Throbber/index.js.map +1 -1
- package/build/development/shared/components/WithTooltip/Tooltip.js +1 -0
- package/build/development/shared/components/WithTooltip/Tooltip.js.map +1 -1
- package/build/development/shared/components/WithTooltip/index.js +7 -7
- package/build/development/shared/components/WithTooltip/index.js.map +1 -1
- package/build/development/shared/components/YouTubeVideo/index.js +9 -9
- package/build/development/shared/components/YouTubeVideo/index.js.map +1 -1
- package/build/development/shared/components/index.js.map +1 -1
- package/build/development/shared/components/selectors/CustomDropdown/Options/index.js +1 -1
- package/build/development/shared/components/selectors/CustomDropdown/Options/index.js.map +1 -1
- package/build/development/shared/components/selectors/CustomDropdown/index.js +11 -11
- package/build/development/shared/components/selectors/CustomDropdown/index.js.map +1 -1
- package/build/development/shared/components/selectors/NativeDropdown/index.js +12 -12
- package/build/development/shared/components/selectors/NativeDropdown/index.js.map +1 -1
- package/build/development/shared/components/selectors/Switch/index.js +7 -7
- package/build/development/shared/components/selectors/Switch/index.js.map +1 -1
- package/build/development/shared/utils/config.js.map +1 -1
- package/build/development/shared/utils/globalState.js.map +1 -1
- package/build/development/shared/utils/index.js.map +1 -1
- package/build/development/shared/utils/jest/E2eSsrEnv.js +14 -10
- package/build/development/shared/utils/jest/E2eSsrEnv.js.map +1 -1
- package/build/development/shared/utils/splitComponent.js +1 -0
- package/build/development/shared/utils/splitComponent.js.map +1 -1
- package/build/development/shared/utils/time.js.map +1 -1
- package/build/production/client/getInj.js.map +1 -1
- package/build/production/client/index.js.map +1 -1
- package/build/production/client/init.js.map +1 -1
- package/build/production/index.js +4 -1
- package/build/production/index.js.map +1 -1
- package/build/production/server/Cache.js.map +1 -1
- package/build/production/server/index.js +5 -5
- package/build/production/server/index.js.map +1 -1
- package/build/production/server/renderer.js +7 -7
- package/build/production/server/renderer.js.map +1 -1
- package/build/production/server/server.js +4 -4
- package/build/production/server/server.js.map +1 -1
- package/build/production/shared/components/Button/index.js +1 -1
- package/build/production/shared/components/Button/index.js.map +1 -1
- package/build/production/shared/components/Checkbox/index.js +1 -1
- package/build/production/shared/components/Checkbox/index.js.map +1 -1
- package/build/production/shared/components/GenericLink/index.js +2 -2
- package/build/production/shared/components/GenericLink/index.js.map +1 -1
- package/build/production/shared/components/Input/index.js +1 -1
- package/build/production/shared/components/Input/index.js.map +1 -1
- package/build/production/shared/components/Link.js.map +1 -1
- package/build/production/shared/components/Modal/index.js +1 -1
- package/build/production/shared/components/Modal/index.js.map +1 -1
- package/build/production/shared/components/NavLink.js.map +1 -1
- package/build/production/shared/components/PageLayout/index.js +1 -1
- package/build/production/shared/components/PageLayout/index.js.map +1 -1
- package/build/production/shared/components/TextArea/index.js +1 -1
- package/build/production/shared/components/TextArea/index.js.map +1 -1
- package/build/production/shared/components/Throbber/index.js +1 -1
- package/build/production/shared/components/Throbber/index.js.map +1 -1
- package/build/production/shared/components/WithTooltip/Tooltip.js.map +1 -1
- package/build/production/shared/components/WithTooltip/index.js +1 -1
- package/build/production/shared/components/WithTooltip/index.js.map +1 -1
- package/build/production/shared/components/YouTubeVideo/index.js +1 -1
- package/build/production/shared/components/YouTubeVideo/index.js.map +1 -1
- package/build/production/shared/components/index.js.map +1 -1
- package/build/production/shared/components/selectors/CustomDropdown/Options/index.js +1 -1
- package/build/production/shared/components/selectors/CustomDropdown/Options/index.js.map +1 -1
- package/build/production/shared/components/selectors/CustomDropdown/index.js +1 -1
- package/build/production/shared/components/selectors/CustomDropdown/index.js.map +1 -1
- package/build/production/shared/components/selectors/NativeDropdown/index.js +1 -1
- package/build/production/shared/components/selectors/NativeDropdown/index.js.map +1 -1
- package/build/production/shared/components/selectors/Switch/index.js +1 -1
- package/build/production/shared/components/selectors/Switch/index.js.map +1 -1
- package/build/production/shared/utils/config.js.map +1 -1
- package/build/production/shared/utils/globalState.js.map +1 -1
- package/build/production/shared/utils/index.js.map +1 -1
- package/build/production/shared/utils/jest/E2eSsrEnv.js +8 -7
- package/build/production/shared/utils/jest/E2eSsrEnv.js.map +1 -1
- package/build/production/shared/utils/splitComponent.js.map +1 -1
- package/build/production/shared/utils/time.js.map +1 -1
- package/build/web/client/getInj.js +54 -0
- package/build/web/client/getInj.js.map +1 -0
- package/build/web/client/index.js +38 -0
- package/build/web/client/index.js.map +1 -0
- package/build/web/client/init.js +49 -0
- package/build/web/client/init.js.map +1 -0
- package/build/web/index.js +25 -0
- package/build/web/index.js.map +1 -0
- package/build/web/server/Cache.js +54 -0
- package/build/web/server/Cache.js.map +1 -0
- package/build/web/server/index.js +211 -0
- package/build/web/server/index.js.map +1 -0
- package/build/web/server/renderer.js +516 -0
- package/build/web/server/renderer.js.map +1 -0
- package/build/web/server/server.js +231 -0
- package/build/web/server/server.js.map +1 -0
- package/build/web/server/utils/errors.js +103 -0
- package/build/web/server/utils/errors.js.map +1 -0
- package/build/web/server/utils/index.js +4 -0
- package/build/web/server/utils/index.js.map +1 -0
- package/build/web/shared/components/Button/index.js +90 -0
- package/build/web/shared/components/Button/index.js.map +1 -0
- package/build/web/shared/components/Button/style.scss +54 -0
- package/build/web/shared/components/Checkbox/index.js +35 -0
- package/build/web/shared/components/Checkbox/index.js.map +1 -0
- package/build/web/shared/components/Checkbox/theme.scss +63 -0
- package/build/web/shared/components/GenericLink/index.js +107 -0
- package/build/web/shared/components/GenericLink/index.js.map +1 -0
- package/build/web/shared/components/GenericLink/style.scss +3 -0
- package/build/web/shared/components/Input/index.js +71 -0
- package/build/web/shared/components/Input/index.js.map +1 -0
- package/build/web/shared/components/Input/theme.scss +52 -0
- package/build/web/shared/components/Link.js +20 -0
- package/build/web/shared/components/Link.js.map +1 -0
- package/build/web/shared/components/Modal/base-theme.scss +38 -0
- package/build/web/shared/components/Modal/index.js +128 -0
- package/build/web/shared/components/Modal/index.js.map +1 -0
- package/build/web/shared/components/Modal/styles.scss +3 -0
- package/build/web/shared/components/NavLink.js +13 -0
- package/build/web/shared/components/NavLink.js.map +1 -0
- package/build/web/shared/components/PageLayout/base-theme.scss +30 -0
- package/build/web/shared/components/PageLayout/index.js +38 -0
- package/build/web/shared/components/PageLayout/index.js.map +1 -0
- package/build/web/shared/components/TextArea/index.js +106 -0
- package/build/web/shared/components/TextArea/index.js.map +1 -0
- package/build/web/shared/components/TextArea/style.scss +68 -0
- package/build/web/shared/components/Throbber/index.js +26 -0
- package/build/web/shared/components/Throbber/index.js.map +1 -0
- package/build/web/shared/components/Throbber/theme.scss +26 -0
- package/build/web/shared/components/WithTooltip/Tooltip.js +226 -0
- package/build/web/shared/components/WithTooltip/Tooltip.js.map +1 -0
- package/build/web/shared/components/WithTooltip/default-theme.scss +36 -0
- package/build/web/shared/components/WithTooltip/index.js +117 -0
- package/build/web/shared/components/WithTooltip/index.js.map +1 -0
- package/build/web/shared/components/YouTubeVideo/base.scss +15 -0
- package/build/web/shared/components/YouTubeVideo/index.js +56 -0
- package/build/web/shared/components/YouTubeVideo/index.js.map +1 -0
- package/build/web/shared/components/YouTubeVideo/throbber.scss +11 -0
- package/build/web/shared/components/index.js +18 -0
- package/build/web/shared/components/index.js.map +1 -0
- package/build/web/shared/components/selectors/CustomDropdown/Options/index.js +79 -0
- package/build/web/shared/components/selectors/CustomDropdown/Options/index.js.map +1 -0
- package/build/web/shared/components/selectors/CustomDropdown/Options/style.scss +6 -0
- package/build/web/shared/components/selectors/CustomDropdown/index.js +120 -0
- package/build/web/shared/components/selectors/CustomDropdown/index.js.map +1 -0
- package/build/web/shared/components/selectors/CustomDropdown/theme.scss +118 -0
- package/build/web/shared/components/selectors/NativeDropdown/index.js +82 -0
- package/build/web/shared/components/selectors/NativeDropdown/index.js.map +1 -0
- package/build/web/shared/components/selectors/NativeDropdown/theme.scss +81 -0
- package/build/web/shared/components/selectors/Switch/index.js +49 -0
- package/build/web/shared/components/selectors/Switch/index.js.map +1 -0
- package/build/web/shared/components/selectors/Switch/theme.scss +39 -0
- package/build/web/shared/components/selectors/common.js +12 -0
- package/build/web/shared/components/selectors/common.js.map +1 -0
- package/build/web/shared/components/selectors/index.js +4 -0
- package/build/web/shared/components/selectors/index.js.map +1 -0
- package/build/web/shared/utils/config.js +21 -0
- package/build/web/shared/utils/config.js.map +1 -0
- package/build/web/shared/utils/globalState.js +15 -0
- package/build/web/shared/utils/globalState.js.map +1 -0
- package/build/web/shared/utils/index.js +13 -0
- package/build/web/shared/utils/index.js.map +1 -0
- package/build/web/shared/utils/isomorphy/buildInfo.js +44 -0
- package/build/web/shared/utils/isomorphy/buildInfo.js.map +1 -0
- package/build/web/shared/utils/isomorphy/environment-check.js +17 -0
- package/build/web/shared/utils/isomorphy/environment-check.js.map +1 -0
- package/build/web/shared/utils/isomorphy/index.js +36 -0
- package/build/web/shared/utils/isomorphy/index.js.map +1 -0
- package/build/web/shared/utils/jest/E2eSsrEnv.js +230 -0
- package/build/web/shared/utils/jest/E2eSsrEnv.js.map +1 -0
- package/build/web/shared/utils/jest/global.js +4 -0
- package/build/web/shared/utils/jest/global.js.map +1 -0
- package/build/web/shared/utils/jest/index.js +165 -0
- package/build/web/shared/utils/jest/index.js.map +1 -0
- package/build/web/shared/utils/splitComponent.js +221 -0
- package/build/web/shared/utils/splitComponent.js.map +1 -0
- package/build/web/shared/utils/time.js +85 -0
- package/build/web/shared/utils/time.js.map +1 -0
- package/build/web/shared/utils/webpack.js +61 -0
- package/build/web/shared/utils/webpack.js.map +1 -0
- package/build/web/styles/_global/reset.css +52 -0
- package/build/web/styles/_mixins/fonts.scss +40 -0
- package/build/web/styles/_mixins/media.scss +150 -0
- package/build/web/styles/_mixins/typography.scss +83 -0
- package/build/web/styles/global.scss +11 -0
- package/build/web/styles/mixins.scss +6 -0
- package/config/babel/node-ssr.js +5 -2
- package/config/webpack/app-base.js +10 -7
- package/config/webpack/app-development.js +3 -2
- package/package.json +29 -40
- package/src/index.ts +7 -1
- package/src/server/index.ts +6 -8
- package/src/server/renderer.tsx +7 -9
- package/src/server/server.ts +3 -5
- package/src/shared/components/Modal/base-theme.scss +1 -1
- package/src/shared/components/PageLayout/base-theme.scss +1 -1
- package/src/shared/utils/jest/E2eSsrEnv.ts +8 -1
- package/tstyche.config.json +1 -1
- package/build/development/__chunk_groups__.json +0 -6
- package/build/development/style.css +0 -909
- package/build/development/web.bundle.js +0 -752
- package/build/production/__chunk_groups__.json +0 -6
- package/build/production/style.css +0 -2
- package/build/production/style.css.map +0 -1
- package/build/production/web.bundle.js +0 -3
- package/build/production/web.bundle.js.LICENSE.txt +0 -9
- package/build/production/web.bundle.js.map +0 -1
- package/dev-styles.js +0 -1
- package/null.js +0 -1
- package/prod-styles.js +0 -1
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { useEffect, useLayoutEffect, useRef, useState } from 'react';
|
|
2
|
+
import themed from '@dr.pogodin/react-themes';
|
|
3
|
+
import defaultTheme from "./style.scss";
|
|
4
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
5
|
+
const TextArea = ({
|
|
6
|
+
disabled,
|
|
7
|
+
error,
|
|
8
|
+
label,
|
|
9
|
+
onBlur,
|
|
10
|
+
onChange,
|
|
11
|
+
onKeyDown,
|
|
12
|
+
placeholder,
|
|
13
|
+
testId,
|
|
14
|
+
theme,
|
|
15
|
+
value
|
|
16
|
+
}) => {
|
|
17
|
+
const hiddenAreaRef = useRef(null);
|
|
18
|
+
const [height, setHeight] = useState();
|
|
19
|
+
const textAreaRef = useRef(null);
|
|
20
|
+
const [localValue, setLocalValue] = useState(value ?? '');
|
|
21
|
+
if (value !== undefined && localValue !== value) setLocalValue(value);
|
|
22
|
+
|
|
23
|
+
// This resizes text area's height when its width is changed for any reason.
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
const el = hiddenAreaRef.current;
|
|
26
|
+
if (!el) return undefined;
|
|
27
|
+
const cb = () => {
|
|
28
|
+
setHeight(el.scrollHeight);
|
|
29
|
+
};
|
|
30
|
+
const observer = new ResizeObserver(cb);
|
|
31
|
+
observer.observe(el);
|
|
32
|
+
return () => {
|
|
33
|
+
observer.disconnect();
|
|
34
|
+
};
|
|
35
|
+
}, []);
|
|
36
|
+
|
|
37
|
+
// Resizes the text area when its content is modified.
|
|
38
|
+
//
|
|
39
|
+
// NOTE: useLayoutEffect() instead of useEffect() makes difference here,
|
|
40
|
+
// as it helps to avoid visible "content/height" jumps (i.e. with just
|
|
41
|
+
// useEffect() it becomes visible how the content is modified first,
|
|
42
|
+
// and then input height is incremented, if necessary).
|
|
43
|
+
// See: https://github.com/birdofpreyru/react-utils/issues/313
|
|
44
|
+
useLayoutEffect(() => {
|
|
45
|
+
const el = hiddenAreaRef.current;
|
|
46
|
+
if (el) setHeight(el.scrollHeight);
|
|
47
|
+
}, [localValue]);
|
|
48
|
+
let containerClassName = theme.container;
|
|
49
|
+
if (error) containerClassName += ` ${theme.error}`;
|
|
50
|
+
return /*#__PURE__*/_jsxs("div", {
|
|
51
|
+
className: containerClassName,
|
|
52
|
+
onFocus: () => {
|
|
53
|
+
textAreaRef.current?.focus();
|
|
54
|
+
},
|
|
55
|
+
children: [label === undefined ? null : /*#__PURE__*/_jsx("div", {
|
|
56
|
+
className: theme.label,
|
|
57
|
+
children: label
|
|
58
|
+
}), /*#__PURE__*/_jsx("textarea", {
|
|
59
|
+
className: `${theme.textarea} ${theme.hidden}`
|
|
60
|
+
|
|
61
|
+
// This text area is hidden underneath the primary one below,
|
|
62
|
+
// and it is used for text measurements, to implement auto-scaling
|
|
63
|
+
// of the primary textarea's height.
|
|
64
|
+
,
|
|
65
|
+
readOnly: true,
|
|
66
|
+
ref: hiddenAreaRef
|
|
67
|
+
|
|
68
|
+
// The "-1" value of "tabIndex" removes this hidden text area from
|
|
69
|
+
// the tab-focus-chain.
|
|
70
|
+
,
|
|
71
|
+
tabIndex: -1
|
|
72
|
+
|
|
73
|
+
// NOTE: With empty string value ("") the scrolling height of this text
|
|
74
|
+
// area is zero, thus collapsing <TextArea> height below the single line
|
|
75
|
+
// input height. To avoid it we fallback to whitespace (" ") character
|
|
76
|
+
// here.
|
|
77
|
+
,
|
|
78
|
+
value: localValue || ' '
|
|
79
|
+
}), /*#__PURE__*/_jsx("textarea", {
|
|
80
|
+
className: theme.textarea,
|
|
81
|
+
"data-testid": process.env.NODE_ENV === 'production' ? undefined : testId,
|
|
82
|
+
disabled: disabled,
|
|
83
|
+
onBlur: onBlur
|
|
84
|
+
|
|
85
|
+
// When value is "undefined" the text area is not-managed, and we should
|
|
86
|
+
// manage it internally for the measurement / resizing functionality
|
|
87
|
+
// to work.
|
|
88
|
+
,
|
|
89
|
+
onChange: value === undefined ? e => {
|
|
90
|
+
setLocalValue(e.target.value);
|
|
91
|
+
} : onChange,
|
|
92
|
+
onKeyDown: onKeyDown,
|
|
93
|
+
placeholder: placeholder,
|
|
94
|
+
ref: textAreaRef,
|
|
95
|
+
style: {
|
|
96
|
+
height
|
|
97
|
+
},
|
|
98
|
+
value: localValue
|
|
99
|
+
}), error && error !== true ? /*#__PURE__*/_jsx("div", {
|
|
100
|
+
className: theme.errorMessage,
|
|
101
|
+
children: error
|
|
102
|
+
}) : null]
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
export default themed(TextArea, 'TextArea', defaultTheme);
|
|
106
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["useEffect","useLayoutEffect","useRef","useState","themed","defaultTheme","jsx","_jsx","jsxs","_jsxs","TextArea","disabled","error","label","onBlur","onChange","onKeyDown","placeholder","testId","theme","value","hiddenAreaRef","height","setHeight","textAreaRef","localValue","setLocalValue","undefined","el","current","cb","scrollHeight","observer","ResizeObserver","observe","disconnect","containerClassName","container","className","onFocus","focus","children","textarea","hidden","readOnly","ref","tabIndex","process","env","NODE_ENV","e","target","style","errorMessage"],"sources":["../../../../../src/shared/components/TextArea/index.tsx"],"sourcesContent":["import {\n type ChangeEventHandler,\n type FocusEventHandler,\n type FunctionComponent,\n type KeyboardEventHandler,\n type ReactNode,\n useEffect,\n useLayoutEffect,\n useRef,\n useState,\n} from 'react';\n\nimport themed, { type Theme } from '@dr.pogodin/react-themes';\n\nimport defaultTheme from './style.scss';\n\ntype ThemeKeyT = 'container' | 'error' | 'errorMessage' | 'hidden' | 'label'\n | 'textarea';\n\ntype Props = {\n disabled?: boolean;\n error?: ReactNode;\n label?: string;\n onBlur?: FocusEventHandler<HTMLTextAreaElement>;\n onChange?: ChangeEventHandler<HTMLTextAreaElement>;\n onKeyDown?: KeyboardEventHandler<HTMLTextAreaElement>;\n placeholder?: string;\n testId?: string;\n theme: Theme<ThemeKeyT>;\n value?: string;\n};\n\nconst TextArea: FunctionComponent<Props> = ({\n disabled,\n error,\n label,\n onBlur,\n onChange,\n onKeyDown,\n placeholder,\n testId,\n theme,\n value,\n}) => {\n const hiddenAreaRef = useRef<HTMLTextAreaElement>(null);\n const [height, setHeight] = useState<number | undefined>();\n\n const textAreaRef = useRef<HTMLTextAreaElement>(null);\n\n const [localValue, setLocalValue] = useState(value ?? '');\n if (value !== undefined && localValue !== value) setLocalValue(value);\n\n // This resizes text area's height when its width is changed for any reason.\n useEffect(() => {\n const el = hiddenAreaRef.current;\n if (!el) return undefined;\n\n const cb = () => {\n setHeight(el.scrollHeight);\n };\n const observer = new ResizeObserver(cb);\n observer.observe(el);\n\n return () => {\n observer.disconnect();\n };\n }, []);\n\n // Resizes the text area when its content is modified.\n //\n // NOTE: useLayoutEffect() instead of useEffect() makes difference here,\n // as it helps to avoid visible \"content/height\" jumps (i.e. with just\n // useEffect() it becomes visible how the content is modified first,\n // and then input height is incremented, if necessary).\n // See: https://github.com/birdofpreyru/react-utils/issues/313\n useLayoutEffect(() => {\n const el = hiddenAreaRef.current;\n if (el) setHeight(el.scrollHeight);\n }, [localValue]);\n\n let containerClassName = theme.container;\n if (error) containerClassName += ` ${theme.error}`;\n\n return (\n <div\n className={containerClassName}\n onFocus={() => {\n textAreaRef.current?.focus();\n }}\n >\n {label === undefined ? null : <div className={theme.label}>{label}</div>}\n <textarea\n className={`${theme.textarea} ${theme.hidden}`}\n\n // This text area is hidden underneath the primary one below,\n // and it is used for text measurements, to implement auto-scaling\n // of the primary textarea's height.\n readOnly\n ref={hiddenAreaRef}\n\n // The \"-1\" value of \"tabIndex\" removes this hidden text area from\n // the tab-focus-chain.\n tabIndex={-1}\n\n // NOTE: With empty string value (\"\") the scrolling height of this text\n // area is zero, thus collapsing <TextArea> height below the single line\n // input height. To avoid it we fallback to whitespace (\" \") character\n // here.\n value={localValue || ' '}\n />\n <textarea\n className={theme.textarea}\n data-testid={process.env.NODE_ENV === 'production' ? undefined : testId}\n disabled={disabled}\n onBlur={onBlur}\n\n // When value is \"undefined\" the text area is not-managed, and we should\n // manage it internally for the measurement / resizing functionality\n // to work.\n onChange={\n value === undefined\n ? (e) => {\n setLocalValue(e.target.value);\n } : onChange\n }\n onKeyDown={onKeyDown}\n placeholder={placeholder}\n ref={textAreaRef}\n style={{ height }}\n value={localValue}\n />\n {error && error !== true\n ? <div className={theme.errorMessage}>{error}</div>\n : null}\n </div>\n );\n};\n\nexport default themed(TextArea, 'TextArea', defaultTheme);\n"],"mappings":"AAAA,SAMEA,SAAS,EACTC,eAAe,EACfC,MAAM,EACNC,QAAQ,QACH,OAAO;AAEd,OAAOC,MAAM,MAAsB,0BAA0B;AAE7D,OAAOC,YAAY;AAAqB,SAAAC,GAAA,IAAAC,IAAA,EAAAC,IAAA,IAAAC,KAAA;AAkBxC,MAAMC,QAAkC,GAAGA,CAAC;EAC1CC,QAAQ;EACRC,KAAK;EACLC,KAAK;EACLC,MAAM;EACNC,QAAQ;EACRC,SAAS;EACTC,WAAW;EACXC,MAAM;EACNC,KAAK;EACLC;AACF,CAAC,KAAK;EACJ,MAAMC,aAAa,GAAGnB,MAAM,CAAsB,IAAI,CAAC;EACvD,MAAM,CAACoB,MAAM,EAAEC,SAAS,CAAC,GAAGpB,QAAQ,CAAqB,CAAC;EAE1D,MAAMqB,WAAW,GAAGtB,MAAM,CAAsB,IAAI,CAAC;EAErD,MAAM,CAACuB,UAAU,EAAEC,aAAa,CAAC,GAAGvB,QAAQ,CAACiB,KAAK,IAAI,EAAE,CAAC;EACzD,IAAIA,KAAK,KAAKO,SAAS,IAAIF,UAAU,KAAKL,KAAK,EAAEM,aAAa,CAACN,KAAK,CAAC;;EAErE;EACApB,SAAS,CAAC,MAAM;IACd,MAAM4B,EAAE,GAAGP,aAAa,CAACQ,OAAO;IAChC,IAAI,CAACD,EAAE,EAAE,OA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EAAEnB,KAAK,CAACuB,QAAS;MAC1B,eAAaK,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,YAAY,GAAGtB,SAAS,GAAGT,MAAO;MACxEP,QAAQ,EAAEA,QAAS;MACnBG,MAAM,EAAEA;;MAER;MACA;MACA;MAAA;MACAC,QAAQ,EACNK,KAAK,KAAKO,SAAS,GACduB,CAAC,IAAK;QACPxB,aAAa,CAACwB,CAAC,CAACC,MAAM,CAAC/B,KAAK,CAAC;MAC/B,CAAC,GAAGL,QACP;MACDC,SAAS,EAAEA,SAAU;MACrBC,WAAW,EAAEA,WAAY;MACzB4B,GAAG,EAAErB,WAAY;MACjB4B,KAAK,EAAE;QAAE9B;MAAO,CAAE;MAClBF,KAAK,EAAEK;IAAW,CACnB,CAAC,EACDb,KAAK,IAAIA,KAAK,KAAK,IAAI,gBACpBL,IAAA;MAAK+B,SAAS,EAAEnB,KAAK,CAACkC,YAAa;MAAAZ,QAAA,EAAE7B;IAAK,CAAM,CAAC,GACjD,IAAI;EAAA,CACL,CAAC;AAEV,CAAC;AAED,eAAeR,MAAM,CAACM,QAAQ,EAAE,UAAU,EAAEL,YAAY,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
@use "sass:color";
|
|
2
|
+
|
|
3
|
+
*,
|
|
4
|
+
.context,
|
|
5
|
+
.ad.hoc {
|
|
6
|
+
&.container {
|
|
7
|
+
display: inline-flex;
|
|
8
|
+
flex-wrap: wrap;
|
|
9
|
+
margin: 0.1em;
|
|
10
|
+
position: relative;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
&.label {
|
|
14
|
+
margin: 0 0.3em;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
&.textarea {
|
|
18
|
+
background: white;
|
|
19
|
+
border: 1px solid gray;
|
|
20
|
+
border-radius: 0.3em;
|
|
21
|
+
box-sizing: border-box;
|
|
22
|
+
font: inherit;
|
|
23
|
+
height: 0;
|
|
24
|
+
outline: none;
|
|
25
|
+
overflow: hidden;
|
|
26
|
+
padding: 0.3em 0.3em calc(0.3em + 1px);
|
|
27
|
+
resize: none;
|
|
28
|
+
width: 100%;
|
|
29
|
+
|
|
30
|
+
&:focus {
|
|
31
|
+
border-color: blue;
|
|
32
|
+
box-shadow: 0 0 3px 1px lightblue;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
&::placeholder {
|
|
36
|
+
color: gray;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
&:disabled {
|
|
40
|
+
border-color: color.adjust($color: gray, $alpha: -0.66);
|
|
41
|
+
cursor: not-allowed;
|
|
42
|
+
color: color.adjust($color: gray, $alpha: -0.66);
|
|
43
|
+
user-select: none;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
&.error &.textarea {
|
|
48
|
+
border-color: red;
|
|
49
|
+
|
|
50
|
+
&:focus { box-shadow: 0 0 3px 1px orangered }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
&.errorMessage {
|
|
54
|
+
color: red;
|
|
55
|
+
font-size: 0.8em;
|
|
56
|
+
font-style: italic;
|
|
57
|
+
padding-right: 0.6em;
|
|
58
|
+
text-align: right;
|
|
59
|
+
width: 100%;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
&.hidden {
|
|
63
|
+
// NOTE: We hide it this way, as setting "display: none" will interfere
|
|
64
|
+
// with measurements, making the hidden input height zero.
|
|
65
|
+
opacity: 0;
|
|
66
|
+
position: absolute;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import themed from '@dr.pogodin/react-themes';
|
|
2
|
+
import defaultTheme from "./theme.scss";
|
|
3
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
4
|
+
/**
|
|
5
|
+
* Throbber is an "action in progress" indicator, which renders
|
|
6
|
+
* three bouncing circles as a simple pending activity indicator,
|
|
7
|
+
* and can be further themed to a certain degree.
|
|
8
|
+
* @param {object} [props] Component properties.
|
|
9
|
+
* @param {ThrobberTheme} [props.theme] _Ad hoc_ theme.
|
|
10
|
+
* @param {...any} [props....]
|
|
11
|
+
* [Other theming properties](https://www.npmjs.com/package/@dr.pogodin/react-themes#themed-component-properties)
|
|
12
|
+
*/
|
|
13
|
+
const Throbber = ({
|
|
14
|
+
theme
|
|
15
|
+
}) => /*#__PURE__*/_jsxs("span", {
|
|
16
|
+
className: theme.container,
|
|
17
|
+
children: [/*#__PURE__*/_jsx("span", {
|
|
18
|
+
className: theme.circle
|
|
19
|
+
}), /*#__PURE__*/_jsx("span", {
|
|
20
|
+
className: theme.circle
|
|
21
|
+
}), /*#__PURE__*/_jsx("span", {
|
|
22
|
+
className: theme.circle
|
|
23
|
+
})]
|
|
24
|
+
});
|
|
25
|
+
export default themed(Throbber, 'Throbber', defaultTheme);
|
|
26
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["themed","defaultTheme","jsx","_jsx","jsxs","_jsxs","Throbber","theme","className","container","children","circle"],"sources":["../../../../../src/shared/components/Throbber/index.tsx"],"sourcesContent":["import themed, { type Theme } from '@dr.pogodin/react-themes';\n\nimport defaultTheme from './theme.scss';\n\ntype ThemeKeyT = 'bouncing' | 'circle' | 'container';\n\ntype PropsT = {\n theme: Theme<ThemeKeyT>;\n};\n\n/**\n * Throbber is an \"action in progress\" indicator, which renders\n * three bouncing circles as a simple pending activity indicator,\n * and can be further themed to a certain degree.\n * @param {object} [props] Component properties.\n * @param {ThrobberTheme} [props.theme] _Ad hoc_ theme.\n * @param {...any} [props....]\n * [Other theming properties](https://www.npmjs.com/package/@dr.pogodin/react-themes#themed-component-properties)\n */\nconst Throbber: React.FunctionComponent<PropsT> = ({ theme }) => (\n <span className={theme.container}>\n <span className={theme.circle} />\n <span className={theme.circle} />\n <span className={theme.circle} />\n </span>\n);\n\nexport default themed(Throbber, 'Throbber', defaultTheme);\n"],"mappings":"AAAA,OAAOA,MAAM,MAAsB,0BAA0B;AAE7D,OAAOC,YAAY;AAAqB,SAAAC,GAAA,IAAAC,IAAA,EAAAC,IAAA,IAAAC,KAAA;AAQxC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,QAAyC,GAAGA,CAAC;EAAEC;AAAM,CAAC,kBAC1DF,KAAA;EAAMG,SAAS,EAAED,KAAK,CAACE,SAAU;EAAAC,QAAA,gBAC/BP,IAAA;IAAMK,SAAS,EAAED,KAAK,CAACI;EAAO,CAAE,CAAC,eACjCR,IAAA;IAAMK,SAAS,EAAED,KAAK,CAACI;EAAO,CAAE,CAAC,eACjCR,IAAA;IAAMK,SAAS,EAAED,KAAK,CAACI;EAAO,CAAE,CAAC;AAAA,CAC7B,CACP;AAED,eAAeX,MAAM,CAACM,QAAQ,EAAE,UAAU,EAAEL,YAAY,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
@keyframes bouncing {
|
|
2
|
+
from { top: -0.3em; }
|
|
3
|
+
to { top: 0.3em; }
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
*,
|
|
7
|
+
.context,
|
|
8
|
+
.ad.hoc {
|
|
9
|
+
&.container {
|
|
10
|
+
display: inline-block;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
&.circle {
|
|
14
|
+
animation: bouncing 0.4s ease-in infinite alternate;
|
|
15
|
+
background: black;
|
|
16
|
+
border-radius: 0.3em;
|
|
17
|
+
display: inline-block;
|
|
18
|
+
height: 0.6em;
|
|
19
|
+
margin: 0 0.1em;
|
|
20
|
+
position: relative;
|
|
21
|
+
width: 0.6em;
|
|
22
|
+
|
|
23
|
+
&:first-child { animation-delay: -0.2s; }
|
|
24
|
+
&:last-child { animation-delay: 0.2s; }
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The actual tooltip component. It is rendered outside the regular document
|
|
3
|
+
* hierarchy, and with sub-components managed without React to achieve the best
|
|
4
|
+
* performance during animation.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { useImperativeHandle, useRef } from 'react';
|
|
8
|
+
import { createPortal } from 'react-dom';
|
|
9
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
10
|
+
/**
|
|
11
|
+
* Valid placements of the rendered tooltip. They will be overriden when
|
|
12
|
+
* necessary to fit the tooltip within the viewport.
|
|
13
|
+
*/
|
|
14
|
+
export let PLACEMENTS = /*#__PURE__*/function (PLACEMENTS) {
|
|
15
|
+
PLACEMENTS["ABOVE_CURSOR"] = "ABOVE_CURSOR";
|
|
16
|
+
PLACEMENTS["ABOVE_ELEMENT"] = "ABOVE_ELEMENT";
|
|
17
|
+
PLACEMENTS["BELOW_CURSOR"] = "BELOW_CURSOR";
|
|
18
|
+
PLACEMENTS["BELOW_ELEMENT"] = "BELOW_ELEMENT";
|
|
19
|
+
return PLACEMENTS;
|
|
20
|
+
}({});
|
|
21
|
+
const ARROW_STYLE_DOWN = ['border-bottom-color:transparent', 'border-left-color:transparent', 'border-right-color:transparent'].join(';');
|
|
22
|
+
const ARROW_STYLE_UP = ['border-top-color:transparent', 'border-left-color:transparent', 'border-right-color:transparent'].join(';');
|
|
23
|
+
/**
|
|
24
|
+
* Generates bounding client rectangles for tooltip components.
|
|
25
|
+
* @ignore
|
|
26
|
+
* @param tooltip DOM references to the tooltip components.
|
|
27
|
+
* @param tooltip.arrow
|
|
28
|
+
* @param tooltip.container
|
|
29
|
+
* @return Object holding tooltip rectangles in
|
|
30
|
+
* two fields.
|
|
31
|
+
*/
|
|
32
|
+
function calcTooltipRects(tooltip) {
|
|
33
|
+
return {
|
|
34
|
+
arrow: tooltip.arrow.getBoundingClientRect(),
|
|
35
|
+
container: tooltip.container.getBoundingClientRect()
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Calculates the document viewport size.
|
|
41
|
+
* @ignore
|
|
42
|
+
* @return {{x, y, width, height}}
|
|
43
|
+
*/
|
|
44
|
+
function calcViewportRect() {
|
|
45
|
+
const {
|
|
46
|
+
scrollX,
|
|
47
|
+
scrollY
|
|
48
|
+
} = window;
|
|
49
|
+
const {
|
|
50
|
+
documentElement: {
|
|
51
|
+
clientHeight,
|
|
52
|
+
clientWidth
|
|
53
|
+
}
|
|
54
|
+
} = document;
|
|
55
|
+
return {
|
|
56
|
+
bottom: scrollY + clientHeight,
|
|
57
|
+
left: scrollX,
|
|
58
|
+
right: scrollX + clientWidth,
|
|
59
|
+
top: scrollY
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Calculates tooltip and arrow positions for the placement just above
|
|
65
|
+
* the cursor.
|
|
66
|
+
* @ignore
|
|
67
|
+
* @param {number} x Cursor page-x position.
|
|
68
|
+
* @param {number} y Cursor page-y position.
|
|
69
|
+
* @param {object} tooltipRects Bounding client rectangles of tooltip parts.
|
|
70
|
+
* @param {object} tooltipRects.arrow
|
|
71
|
+
* @param {object} tooltipRects.container
|
|
72
|
+
* @return {object} Contains the following fields:
|
|
73
|
+
* - {number} arrowX
|
|
74
|
+
* - {number} arrowY
|
|
75
|
+
* - {number} containerX
|
|
76
|
+
* - {number} containerY
|
|
77
|
+
* - {string} baseArrowStyle
|
|
78
|
+
*/
|
|
79
|
+
function calcPositionAboveXY(x, y, tooltipRects) {
|
|
80
|
+
const {
|
|
81
|
+
arrow,
|
|
82
|
+
container
|
|
83
|
+
} = tooltipRects;
|
|
84
|
+
return {
|
|
85
|
+
arrowX: 0.5 * (container.width - arrow.width),
|
|
86
|
+
arrowY: container.height,
|
|
87
|
+
containerX: x - container.width / 2,
|
|
88
|
+
containerY: y - container.height - arrow.height / 1.5,
|
|
89
|
+
// TODO: Instead of already setting the base style here, we should
|
|
90
|
+
// introduce a set of constants for arrow directions, which will help
|
|
91
|
+
// to do checks dependant on the arrow direction.
|
|
92
|
+
baseArrowStyle: ARROW_STYLE_DOWN
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// const HIT = {
|
|
97
|
+
// NONE: false,
|
|
98
|
+
// LEFT: 'LEFT',
|
|
99
|
+
// RIGHT: 'RIGHT',
|
|
100
|
+
// TOP: 'TOP',
|
|
101
|
+
// BOTTOM: 'BOTTOM',
|
|
102
|
+
// };
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Checks whether
|
|
106
|
+
* @param {object} pos
|
|
107
|
+
* @param {object} tooltipRects
|
|
108
|
+
* @param {object} viewportRect
|
|
109
|
+
* @return {HIT}
|
|
110
|
+
*/
|
|
111
|
+
// function checkViewportFit(pos, tooltipRects, viewportRect) {
|
|
112
|
+
// const { containerX, containerY } = pos;
|
|
113
|
+
// if (containerX < viewportRect.left + 6) return HIT.LEFT;
|
|
114
|
+
// if (containerX > viewportRect.right - 6) return HIT.RIGHT;
|
|
115
|
+
// return HIT.NONE;
|
|
116
|
+
// }
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Shifts tooltip horizontally to fit into the viewport, while keeping
|
|
120
|
+
* the arrow pointed to the XY point.
|
|
121
|
+
* @param {number} x
|
|
122
|
+
* @param {number} y
|
|
123
|
+
* @param {object} pos
|
|
124
|
+
* @param {number} pageXOffset
|
|
125
|
+
* @param {number} pageXWidth
|
|
126
|
+
*/
|
|
127
|
+
// function xPageFitCorrection(x, y, pos, pageXOffset, pageXWidth) {
|
|
128
|
+
// if (pos.containerX < pageXOffset + 6) {
|
|
129
|
+
// pos.containerX = pageXOffset + 6;
|
|
130
|
+
// pos.arrowX = Math.max(6, pageX - containerX - arrowRect.width / 2);
|
|
131
|
+
// } else {
|
|
132
|
+
// const maxX = pageXOffset + docRect.width - containerRect.width - 6;
|
|
133
|
+
// if (containerX > maxX) {
|
|
134
|
+
// containerX = maxX;
|
|
135
|
+
// arrowX = Math.min(
|
|
136
|
+
// containerRect.width - 6,
|
|
137
|
+
// pageX - containerX - arrowRect.width / 2,
|
|
138
|
+
// );
|
|
139
|
+
// }
|
|
140
|
+
// }
|
|
141
|
+
// }
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Sets positions of tooltip components to point the tooltip to the specified
|
|
145
|
+
* page point.
|
|
146
|
+
* @ignore
|
|
147
|
+
* @param pageX
|
|
148
|
+
* @param pageY
|
|
149
|
+
* @param placement
|
|
150
|
+
* @param element DOM reference to the element wrapped by the tooltip.
|
|
151
|
+
* @param tooltip
|
|
152
|
+
* @param tooltip.arrow DOM reference to the tooltip arrow.
|
|
153
|
+
* @param tooltip.container DOM reference to the tooltip container.
|
|
154
|
+
*/
|
|
155
|
+
function setComponentPositions(pageX, pageY, placement, element, tooltip) {
|
|
156
|
+
const tooltipRects = calcTooltipRects(tooltip);
|
|
157
|
+
const viewportRect = calcViewportRect();
|
|
158
|
+
|
|
159
|
+
/* Default container coords: tooltip at the top. */
|
|
160
|
+
const pos = calcPositionAboveXY(pageX, pageY, tooltipRects);
|
|
161
|
+
if (pos.containerX < viewportRect.left + 6) {
|
|
162
|
+
pos.containerX = viewportRect.left + 6;
|
|
163
|
+
pos.arrowX = Math.max(6, pageX - pos.containerX - tooltipRects.arrow.width / 2);
|
|
164
|
+
} else {
|
|
165
|
+
const maxX = viewportRect.right - 6 - tooltipRects.container.width;
|
|
166
|
+
if (pos.containerX > maxX) {
|
|
167
|
+
pos.containerX = maxX;
|
|
168
|
+
pos.arrowX = Math.min(tooltipRects.container.width - 6, pageX - pos.containerX - tooltipRects.arrow.width / 2);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/* If tooltip has not enough space on top - make it bottom tooltip. */
|
|
173
|
+
if (pos.containerY < viewportRect.top + 6) {
|
|
174
|
+
pos.containerY += tooltipRects.container.height + 2 * tooltipRects.arrow.height;
|
|
175
|
+
pos.arrowY -= tooltipRects.container.height + tooltipRects.arrow.height;
|
|
176
|
+
pos.baseArrowStyle = ARROW_STYLE_UP;
|
|
177
|
+
}
|
|
178
|
+
const containerStyle = `left:${pos.containerX}px;top:${pos.containerY}px`;
|
|
179
|
+
tooltip.container.setAttribute('style', containerStyle);
|
|
180
|
+
const arrowStyle = `${pos.baseArrowStyle};left:${pos.arrowX}px;top:${pos.arrowY}px`;
|
|
181
|
+
tooltip.arrow.setAttribute('style', arrowStyle);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/* The Tooltip component itself. */
|
|
185
|
+
const Tooltip = ({
|
|
186
|
+
children,
|
|
187
|
+
ref,
|
|
188
|
+
theme
|
|
189
|
+
}) => {
|
|
190
|
+
// NOTE: The way it has to be implemented, for clean mounting and unmounting
|
|
191
|
+
// at the client side, the <Tooltip> is fully mounted into DOM in the next
|
|
192
|
+
// rendering cycles, and only then it can be correctly measured and positioned.
|
|
193
|
+
// Thus, when we create the <Tooltip> we have to record its target positioning
|
|
194
|
+
// details, and then apply them when it is created.
|
|
195
|
+
|
|
196
|
+
const arrowRef = useRef(null);
|
|
197
|
+
const containerRef = useRef(null);
|
|
198
|
+
const contentRef = useRef(null);
|
|
199
|
+
const pointTo = (pageX, pageY, placement, element) => {
|
|
200
|
+
if (!arrowRef.current || !containerRef.current || !contentRef.current) {
|
|
201
|
+
throw Error('Internal error');
|
|
202
|
+
}
|
|
203
|
+
setComponentPositions(pageX, pageY, placement, element, {
|
|
204
|
+
arrow: arrowRef.current,
|
|
205
|
+
container: containerRef.current,
|
|
206
|
+
content: contentRef.current
|
|
207
|
+
});
|
|
208
|
+
};
|
|
209
|
+
useImperativeHandle(ref, () => ({
|
|
210
|
+
pointTo
|
|
211
|
+
}));
|
|
212
|
+
return /*#__PURE__*/createPortal(/*#__PURE__*/_jsxs("div", {
|
|
213
|
+
className: theme.container,
|
|
214
|
+
ref: containerRef,
|
|
215
|
+
children: [/*#__PURE__*/_jsx("div", {
|
|
216
|
+
className: theme.arrow,
|
|
217
|
+
ref: arrowRef
|
|
218
|
+
}), /*#__PURE__*/_jsx("div", {
|
|
219
|
+
className: theme.content,
|
|
220
|
+
ref: contentRef,
|
|
221
|
+
children: children
|
|
222
|
+
})]
|
|
223
|
+
}), document.body);
|
|
224
|
+
};
|
|
225
|
+
export default Tooltip;
|
|
226
|
+
//# sourceMappingURL=Tooltip.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Tooltip.js","names":["useImperativeHandle","useRef","createPortal","jsx","_jsx","jsxs","_jsxs","PLACEMENTS","ARROW_STYLE_DOWN","join","ARROW_STYLE_UP","calcTooltipRects","tooltip","arrow","getBoundingClientRect","container","calcViewportRect","scrollX","scrollY","window","documentElement","clientHeight","clientWidth","document","bottom","left","right","top","calcPositionAboveXY","x","y","tooltipRects","arrowX","width","arrowY","height","containerX","containerY","baseArrowStyle","setComponentPositions","pageX","pageY","placement","element","viewportRect","pos","Math","max","maxX","min","containerStyle","setAttribute","arrowStyle","Tooltip","children","ref","theme","arrowRef","containerRef","contentRef","pointTo","current","Error","content","className","body"],"sources":["../../../../../src/shared/components/WithTooltip/Tooltip.tsx"],"sourcesContent":["/**\n * The actual tooltip component. It is rendered outside the regular document\n * hierarchy, and with sub-components managed without React to achieve the best\n * performance during animation.\n */\n\nimport {\n type FunctionComponent,\n type ReactNode,\n type RefObject,\n useImperativeHandle,\n useRef,\n} from 'react';\n\nimport { createPortal } from 'react-dom';\n\nimport type { Theme } from '@dr.pogodin/react-themes';\n\n/**\n * Valid placements of the rendered tooltip. They will be overriden when\n * necessary to fit the tooltip within the viewport.\n */\nexport enum PLACEMENTS {\n ABOVE_CURSOR = 'ABOVE_CURSOR',\n ABOVE_ELEMENT = 'ABOVE_ELEMENT',\n BELOW_CURSOR = 'BELOW_CURSOR',\n BELOW_ELEMENT = 'BELOW_ELEMENT',\n}\n\nconst ARROW_STYLE_DOWN = [\n 'border-bottom-color:transparent',\n 'border-left-color:transparent',\n 'border-right-color:transparent',\n].join(';');\n\nconst ARROW_STYLE_UP = [\n 'border-top-color:transparent',\n 'border-left-color:transparent',\n 'border-right-color:transparent',\n].join(';');\n\ntype ComponentsT = {\n container: HTMLDivElement;\n arrow: HTMLDivElement;\n content: HTMLDivElement;\n};\n\nexport type ThemeKeysT = 'appearance' | 'arrow' | 'content' | 'container';\n\ntype TooltipThemeT = Theme<ThemeKeysT>;\n\ntype TooltipRectsT = {\n arrow: DOMRect;\n container: DOMRect;\n};\n\n/**\n * Generates bounding client rectangles for tooltip components.\n * @ignore\n * @param tooltip DOM references to the tooltip components.\n * @param tooltip.arrow\n * @param tooltip.container\n * @return Object holding tooltip rectangles in\n * two fields.\n */\nfunction calcTooltipRects(tooltip: ComponentsT): TooltipRectsT {\n return {\n arrow: tooltip.arrow.getBoundingClientRect(),\n container: tooltip.container.getBoundingClientRect(),\n };\n}\n\n/**\n * Calculates the document viewport size.\n * @ignore\n * @return {{x, y, width, height}}\n */\nfunction calcViewportRect() {\n const { scrollX, scrollY } = window;\n const { documentElement: { clientHeight, clientWidth } } = document;\n return {\n bottom: scrollY + clientHeight,\n left: scrollX,\n right: scrollX + clientWidth,\n top: scrollY,\n };\n}\n\n/**\n * Calculates tooltip and arrow positions for the placement just above\n * the cursor.\n * @ignore\n * @param {number} x Cursor page-x position.\n * @param {number} y Cursor page-y position.\n * @param {object} tooltipRects Bounding client rectangles of tooltip parts.\n * @param {object} tooltipRects.arrow\n * @param {object} tooltipRects.container\n * @return {object} Contains the following fields:\n * - {number} arrowX\n * - {number} arrowY\n * - {number} containerX\n * - {number} containerY\n * - {string} baseArrowStyle\n */\nfunction calcPositionAboveXY(\n x: number,\n y: number,\n tooltipRects: TooltipRectsT,\n) {\n const { arrow, container } = tooltipRects;\n return {\n arrowX: 0.5 * (container.width - arrow.width),\n arrowY: container.height,\n containerX: x - container.width / 2,\n containerY: y - container.height - arrow.height / 1.5,\n\n // TODO: Instead of already setting the base style here, we should\n // introduce a set of constants for arrow directions, which will help\n // to do checks dependant on the arrow direction.\n baseArrowStyle: ARROW_STYLE_DOWN,\n };\n}\n\n// const HIT = {\n// NONE: false,\n// LEFT: 'LEFT',\n// RIGHT: 'RIGHT',\n// TOP: 'TOP',\n// BOTTOM: 'BOTTOM',\n// };\n\n/**\n * Checks whether\n * @param {object} pos\n * @param {object} tooltipRects\n * @param {object} viewportRect\n * @return {HIT}\n */\n// function checkViewportFit(pos, tooltipRects, viewportRect) {\n// const { containerX, containerY } = pos;\n// if (containerX < viewportRect.left + 6) return HIT.LEFT;\n// if (containerX > viewportRect.right - 6) return HIT.RIGHT;\n// return HIT.NONE;\n// }\n\n/**\n * Shifts tooltip horizontally to fit into the viewport, while keeping\n * the arrow pointed to the XY point.\n * @param {number} x\n * @param {number} y\n * @param {object} pos\n * @param {number} pageXOffset\n * @param {number} pageXWidth\n */\n// function xPageFitCorrection(x, y, pos, pageXOffset, pageXWidth) {\n// if (pos.containerX < pageXOffset + 6) {\n// pos.containerX = pageXOffset + 6;\n// pos.arrowX = Math.max(6, pageX - containerX - arrowRect.width / 2);\n// } else {\n// const maxX = pageXOffset + docRect.width - containerRect.width - 6;\n// if (containerX > maxX) {\n// containerX = maxX;\n// arrowX = Math.min(\n// containerRect.width - 6,\n// pageX - containerX - arrowRect.width / 2,\n// );\n// }\n// }\n// }\n\n/**\n * Sets positions of tooltip components to point the tooltip to the specified\n * page point.\n * @ignore\n * @param pageX\n * @param pageY\n * @param placement\n * @param element DOM reference to the element wrapped by the tooltip.\n * @param tooltip\n * @param tooltip.arrow DOM reference to the tooltip arrow.\n * @param tooltip.container DOM reference to the tooltip container.\n */\nfunction setComponentPositions(\n pageX: number,\n pageY: number,\n placement: PLACEMENTS | undefined,\n element: HTMLElement | undefined,\n tooltip: ComponentsT,\n) {\n const tooltipRects = calcTooltipRects(tooltip);\n const viewportRect = calcViewportRect();\n\n /* Default container coords: tooltip at the top. */\n const pos = calcPositionAboveXY(pageX, pageY, tooltipRects);\n\n if (pos.containerX < viewportRect.left + 6) {\n pos.containerX = viewportRect.left + 6;\n pos.arrowX = Math.max(\n 6,\n pageX - pos.containerX - tooltipRects.arrow.width / 2,\n );\n } else {\n const maxX = viewportRect.right - 6 - tooltipRects.container.width;\n if (pos.containerX > maxX) {\n pos.containerX = maxX;\n pos.arrowX = Math.min(\n tooltipRects.container.width - 6,\n pageX - pos.containerX - tooltipRects.arrow.width / 2,\n );\n }\n }\n\n /* If tooltip has not enough space on top - make it bottom tooltip. */\n if (pos.containerY < viewportRect.top + 6) {\n pos.containerY += tooltipRects.container.height\n + 2 * tooltipRects.arrow.height;\n pos.arrowY -= tooltipRects.container.height\n + tooltipRects.arrow.height;\n pos.baseArrowStyle = ARROW_STYLE_UP;\n }\n\n const containerStyle = `left:${pos.containerX}px;top:${pos.containerY}px`;\n tooltip.container.setAttribute('style', containerStyle);\n\n const arrowStyle = `${pos.baseArrowStyle};left:${pos.arrowX}px;top:${pos.arrowY}px`;\n tooltip.arrow.setAttribute('style', arrowStyle);\n}\n\n/* The Tooltip component itself. */\nconst Tooltip: FunctionComponent<{\n children?: ReactNode;\n ref?: RefObject<unknown>;\n theme: TooltipThemeT;\n}> = ({ children, ref, theme }) => {\n // NOTE: The way it has to be implemented, for clean mounting and unmounting\n // at the client side, the <Tooltip> is fully mounted into DOM in the next\n // rendering cycles, and only then it can be correctly measured and positioned.\n // Thus, when we create the <Tooltip> we have to record its target positioning\n // details, and then apply them when it is created.\n\n const arrowRef = useRef<HTMLDivElement>(null);\n const containerRef = useRef<HTMLDivElement>(null);\n const contentRef = useRef<HTMLDivElement>(null);\n\n const pointTo = (\n pageX: number,\n pageY: number,\n placement: PLACEMENTS,\n element: HTMLElement,\n ) => {\n if (!arrowRef.current || !containerRef.current || !contentRef.current) {\n throw Error('Internal error');\n }\n\n setComponentPositions(pageX, pageY, placement, element, {\n arrow: arrowRef.current,\n container: containerRef.current,\n content: contentRef.current,\n });\n };\n useImperativeHandle(ref, () => ({ pointTo }));\n\n return createPortal(\n (\n <div className={theme.container} ref={containerRef}>\n <div className={theme.arrow} ref={arrowRef} />\n <div className={theme.content} ref={contentRef}>{children}</div>\n </div>\n ),\n document.body,\n );\n};\n\nexport default Tooltip;\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;;AAEA,SAIEA,mBAAmB,EACnBC,MAAM,QACD,OAAO;AAEd,SAASC,YAAY,QAAQ,WAAW;AAAC,SAAAC,GAAA,IAAAC,IAAA,EAAAC,IAAA,IAAAC,KAAA;AAIzC;AACA;AACA;AACA;AACA,WAAYC,UAAU,0BAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAA,OAAVA,UAAU;AAAA;AAOtB,MAAMC,gBAAgB,GAAG,CACvB,iCAAiC,EACjC,+BAA+B,EAC/B,gCAAgC,CACjC,CAACC,IAAI,CAAC,GAAG,CAAC;AAEX,MAAMC,cAAc,GAAG,CACrB,8BAA8B,EAC9B,+BAA+B,EAC/B,gCAAgC,CACjC,CAACD,IAAI,CAAC,GAAG,CAAC;AAiBX;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASE,gBAAgBA,CAACC,OAAoB,EAAiB;EAC7D,OAAO;IACLC,KAAK,EAAED,OAAO,CAACC,KAAK,CAACC,qBAAqB,CAAC,CAAC;IAC5CC,SAAS,EAAEH,OAAO,CAACG,SAAS,CAACD,qBAAqB,CAAC;EACrD,CAAC;AACH;;AAEA;AACA;AACA;AACA;AACA;AACA,SAASE,gBAAgBA,CAAA,EAAG;EAC1B,MAAM;IAAEC,OAAO;IAAEC;EAAQ,CAAC,GAAGC,MAAM;EACnC,MAAM;IAAEC,eAAe,EAAE;MAAEC,YAAY;MAAEC;IAAY;EAAE,CAAC,GAAGC,QAAQ;EACnE,OAAO;IACLC,MAAM,EAAEN,OAAO,GAAGG,YAAY;IAC9BI,IAAI,EAAER,OAAO;IACbS,KAAK,EAAET,OAAO,GAAGK,WAAW;IAC5BK,GAAG,EAAET;EACP,CAAC;AACH;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;A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AC;AAED,eAAeZ,OAAO","ignoreList":[]}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
@keyframes appearance {
|
|
2
|
+
from { opacity: 0; }
|
|
3
|
+
to { opacity: 1; }
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
*,
|
|
7
|
+
.ad.hoc,
|
|
8
|
+
.context {
|
|
9
|
+
&.arrow {
|
|
10
|
+
border: 0.6em solid grey;
|
|
11
|
+
pointer-events: none;
|
|
12
|
+
position: absolute;
|
|
13
|
+
width: 0;
|
|
14
|
+
height: 0;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/*
|
|
18
|
+
&.content { }
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
&.container {
|
|
22
|
+
background: grey;
|
|
23
|
+
border-radius: 0.3em;
|
|
24
|
+
color: white;
|
|
25
|
+
display: inline-block;
|
|
26
|
+
padding: 0 0.3em;
|
|
27
|
+
position: absolute;
|
|
28
|
+
top: 0;
|
|
29
|
+
left: 0;
|
|
30
|
+
animation: appearance 0.6s;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
&.wrapper {
|
|
34
|
+
display: inline-block;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/* global window */
|
|
2
|
+
|
|
3
|
+
import { useEffect, useRef, useState } from 'react';
|
|
4
|
+
import themed from '@dr.pogodin/react-themes';
|
|
5
|
+
import Tooltip, { PLACEMENTS } from "./Tooltip";
|
|
6
|
+
import defaultTheme from "./default-theme.scss";
|
|
7
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
8
|
+
/**
|
|
9
|
+
* Implements a simple to use and themeable tooltip component, _e.g._
|
|
10
|
+
* ```js
|
|
11
|
+
* <WithTooltip tip="This is example tooltip.">
|
|
12
|
+
* <p>Hover to see the tooltip.</p>
|
|
13
|
+
* </WithTooltip>
|
|
14
|
+
* ```
|
|
15
|
+
* **Children:** Children are rendered in the place of `<WithTooltip>`,
|
|
16
|
+
* and when hovered the tooltip is shown. By default the wrapper itself is
|
|
17
|
+
* `<div>` block with `display: inline-block`.
|
|
18
|
+
* @param tip – Anything React is able to render,
|
|
19
|
+
* _e.g._ a tooltip text. This will be the tooltip content.
|
|
20
|
+
* @param {WithTooltipTheme} props.theme _Ad hoc_ theme.
|
|
21
|
+
*/
|
|
22
|
+
const Wrapper = ({
|
|
23
|
+
children,
|
|
24
|
+
placement = PLACEMENTS.ABOVE_CURSOR,
|
|
25
|
+
tip,
|
|
26
|
+
theme
|
|
27
|
+
}) => {
|
|
28
|
+
const {
|
|
29
|
+
current: heap
|
|
30
|
+
} = useRef({
|
|
31
|
+
lastCursorX: 0,
|
|
32
|
+
lastCursorY: 0,
|
|
33
|
+
timerId: undefined,
|
|
34
|
+
triggeredByTouch: false
|
|
35
|
+
});
|
|
36
|
+
const tooltipRef = useRef(null);
|
|
37
|
+
const wrapperRef = useRef(null);
|
|
38
|
+
const [showTooltip, setShowTooltip] = useState(false);
|
|
39
|
+
const updatePortalPosition = (cursorX, cursorY) => {
|
|
40
|
+
if (showTooltip) {
|
|
41
|
+
const wrapperRect = wrapperRef.current.getBoundingClientRect();
|
|
42
|
+
if (cursorX < wrapperRect.left || cursorX > wrapperRect.right || cursorY < wrapperRect.top || cursorY > wrapperRect.bottom) {
|
|
43
|
+
setShowTooltip(false);
|
|
44
|
+
} else if (tooltipRef.current) {
|
|
45
|
+
tooltipRef.current.pointTo(cursorX + window.scrollX, cursorY + window.scrollY, placement, wrapperRef.current);
|
|
46
|
+
}
|
|
47
|
+
} else {
|
|
48
|
+
heap.lastCursorX = cursorX;
|
|
49
|
+
heap.lastCursorY = cursorY;
|
|
50
|
+
|
|
51
|
+
// If tooltip was triggered by a touch, we delay its opening by a bit,
|
|
52
|
+
// to ensure it was not a touch-click - in the case of touch click we
|
|
53
|
+
// want to do the click, rather than show the tooltip, and the delay
|
|
54
|
+
// gives click handler a chance to abort the tooltip openning.
|
|
55
|
+
if (heap.triggeredByTouch) {
|
|
56
|
+
heap.timerId ??= setTimeout(() => {
|
|
57
|
+
heap.triggeredByTouch = false;
|
|
58
|
+
heap.timerId = undefined;
|
|
59
|
+
setShowTooltip(true);
|
|
60
|
+
}, 300);
|
|
61
|
+
|
|
62
|
+
// Otherwise we can just open the tooltip right away.
|
|
63
|
+
} else setShowTooltip(true);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
if (showTooltip && tip !== null) {
|
|
68
|
+
// This is necessary to ensure that even when a single mouse event
|
|
69
|
+
// arrives to a tool-tipped component, the tooltip is correctly positioned
|
|
70
|
+
// once opened (because similar call above does not have effect until
|
|
71
|
+
// the tooltip is fully mounted, and that is delayed to future rendering
|
|
72
|
+
// cycle due to the implementation).
|
|
73
|
+
if (tooltipRef.current) {
|
|
74
|
+
tooltipRef.current.pointTo(heap.lastCursorX + window.scrollX, heap.lastCursorY + window.scrollY, placement, wrapperRef.current);
|
|
75
|
+
}
|
|
76
|
+
const listener = () => {
|
|
77
|
+
setShowTooltip(false);
|
|
78
|
+
};
|
|
79
|
+
window.addEventListener('scroll', listener);
|
|
80
|
+
return () => {
|
|
81
|
+
window.removeEventListener('scroll', listener);
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
return undefined;
|
|
85
|
+
}, [heap.lastCursorX, heap.lastCursorY, placement, showTooltip, tip]);
|
|
86
|
+
return /*#__PURE__*/_jsxs("div", {
|
|
87
|
+
className: theme.wrapper,
|
|
88
|
+
onClick: () => {
|
|
89
|
+
if (heap.timerId) {
|
|
90
|
+
clearTimeout(heap.timerId);
|
|
91
|
+
heap.timerId = undefined;
|
|
92
|
+
heap.triggeredByTouch = false;
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
onMouseLeave: () => {
|
|
96
|
+
setShowTooltip(false);
|
|
97
|
+
},
|
|
98
|
+
onMouseMove: e => {
|
|
99
|
+
updatePortalPosition(e.clientX, e.clientY);
|
|
100
|
+
},
|
|
101
|
+
onTouchStart: () => {
|
|
102
|
+
heap.triggeredByTouch = true;
|
|
103
|
+
},
|
|
104
|
+
ref: wrapperRef,
|
|
105
|
+
role: "presentation",
|
|
106
|
+
children: [showTooltip && tip !== null ? /*#__PURE__*/_jsx(Tooltip, {
|
|
107
|
+
ref: tooltipRef,
|
|
108
|
+
theme: theme,
|
|
109
|
+
children: tip
|
|
110
|
+
}) : null, children]
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
const ThemedWrapper = themed(Wrapper, 'WithTooltip', defaultTheme);
|
|
114
|
+
const e = ThemedWrapper;
|
|
115
|
+
e.PLACEMENTS = PLACEMENTS;
|
|
116
|
+
export default e;
|
|
117
|
+
//# sourceMappingURL=index.js.map
|