@salutejs/plasma-new-hope 0.119.0-canary.1344.10282141568.0 → 0.119.0-canary.1344.10284816964.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. package/cjs/components/Modal/Modal.js +3 -1
  2. package/cjs/components/Modal/Modal.js.map +1 -1
  3. package/cjs/hooks/useFocusTrap.js +97 -0
  4. package/cjs/hooks/useFocusTrap.js.map +1 -0
  5. package/cjs/index.js +2 -0
  6. package/cjs/index.js.map +1 -1
  7. package/cjs/utils/focusManager.js +56 -0
  8. package/cjs/utils/focusManager.js.map +1 -0
  9. package/cjs/utils/scopeTab.js +37 -0
  10. package/cjs/utils/scopeTab.js.map +1 -0
  11. package/cjs/utils/tabbable.js +51 -0
  12. package/cjs/utils/tabbable.js.map +1 -0
  13. package/emotion/cjs/components/Modal/Modal.js +4 -3
  14. package/emotion/cjs/examples/plasma_b2c/components/Modal/Modal.stories.tsx +113 -1
  15. package/emotion/cjs/examples/plasma_web/components/Modal/Modal.stories.tsx +113 -1
  16. package/emotion/cjs/hooks/index.js +7 -0
  17. package/emotion/cjs/hooks/useFocusTrap.js +95 -0
  18. package/emotion/cjs/utils/focusManager.js +60 -0
  19. package/emotion/cjs/utils/scopeTab.js +35 -0
  20. package/emotion/cjs/utils/tabbable.js +46 -0
  21. package/emotion/cjs/utils/useFocusTrap.js +95 -0
  22. package/emotion/es/components/Modal/Modal.js +3 -2
  23. package/emotion/es/examples/plasma_b2c/components/Modal/Modal.stories.tsx +113 -1
  24. package/emotion/es/examples/plasma_web/components/Modal/Modal.stories.tsx +113 -1
  25. package/emotion/es/hooks/index.js +1 -0
  26. package/emotion/es/hooks/useFocusTrap.js +90 -0
  27. package/emotion/es/utils/focusManager.js +55 -0
  28. package/emotion/es/utils/scopeTab.js +30 -0
  29. package/emotion/es/utils/tabbable.js +40 -0
  30. package/emotion/es/utils/useFocusTrap.js +90 -0
  31. package/es/components/Modal/Modal.js +4 -2
  32. package/es/components/Modal/Modal.js.map +1 -1
  33. package/es/hooks/useFocusTrap.js +93 -0
  34. package/es/hooks/useFocusTrap.js.map +1 -0
  35. package/es/index.js +1 -0
  36. package/es/index.js.map +1 -1
  37. package/es/utils/focusManager.js +52 -0
  38. package/es/utils/focusManager.js.map +1 -0
  39. package/es/utils/scopeTab.js +33 -0
  40. package/es/utils/scopeTab.js.map +1 -0
  41. package/es/utils/tabbable.js +43 -0
  42. package/es/utils/tabbable.js.map +1 -0
  43. package/package.json +4 -4
  44. package/styled-components/cjs/components/Modal/Modal.js +4 -3
  45. package/styled-components/cjs/examples/plasma_b2c/components/Modal/Modal.stories.tsx +113 -1
  46. package/styled-components/cjs/examples/plasma_web/components/Modal/Modal.stories.tsx +113 -1
  47. package/styled-components/cjs/hooks/index.js +7 -0
  48. package/styled-components/cjs/hooks/useFocusTrap.js +95 -0
  49. package/styled-components/cjs/utils/focusManager.js +60 -0
  50. package/styled-components/cjs/utils/scopeTab.js +35 -0
  51. package/styled-components/cjs/utils/tabbable.js +46 -0
  52. package/styled-components/cjs/utils/useFocusTrap.js +95 -0
  53. package/styled-components/es/components/Modal/Modal.js +3 -2
  54. package/styled-components/es/examples/plasma_b2c/components/Modal/Modal.stories.tsx +113 -1
  55. package/styled-components/es/examples/plasma_web/components/Modal/Modal.stories.tsx +113 -1
  56. package/styled-components/es/hooks/index.js +1 -0
  57. package/styled-components/es/hooks/useFocusTrap.js +90 -0
  58. package/styled-components/es/utils/focusManager.js +55 -0
  59. package/styled-components/es/utils/scopeTab.js +30 -0
  60. package/styled-components/es/utils/tabbable.js +40 -0
  61. package/styled-components/es/utils/useFocusTrap.js +90 -0
  62. package/types/components/Modal/Modal.d.ts.map +1 -1
  63. package/types/hooks/index.d.ts +1 -0
  64. package/types/hooks/index.d.ts.map +1 -1
  65. package/types/hooks/useFocusTrap.d.ts +6 -0
  66. package/types/hooks/useFocusTrap.d.ts.map +1 -0
  67. package/types/utils/focusManager.d.ts +15 -0
  68. package/types/utils/focusManager.d.ts.map +1 -0
  69. package/types/utils/scopeTab.d.ts +7 -0
  70. package/types/utils/scopeTab.d.ts.map +1 -0
  71. package/types/utils/tabbable.d.ts +6 -0
  72. package/types/utils/tabbable.d.ts.map +1 -0
  73. package/types/utils/useFocusTrap.d.ts +6 -0
  74. package/types/utils/useFocusTrap.d.ts.map +1 -0
@@ -1,4 +1,4 @@
1
- import React, { useState } from 'react';
1
+ import React, { useCallback, useRef, useState } from 'react';
2
2
  import styled from '@emotion/styled';
3
3
  import type { ComponentProps } from 'react';
4
4
  import type { StoryObj, Meta } from '@storybook/react';
@@ -6,6 +6,7 @@ import { SSRProvider } from '@salutejs/plasma-core';
6
6
 
7
7
  import { PopupProvider, popupClasses } from '../Popup/Popup';
8
8
  import { Button } from '../Button/Button';
9
+ import { TextField } from '../TextField/TextField';
9
10
  import { WithTheme } from '../../../_helpers';
10
11
 
11
12
  import { Modal, modalClasses } from './Modal';
@@ -206,3 +207,114 @@ export const ModalDemo: StoryObj<StoryModalProps> = {
206
207
  },
207
208
  render: (args) => <StoryModalDemo {...args} />,
208
209
  };
210
+
211
+ const StyledModalAnimation = styled(Modal)`
212
+ /* stylelint-disable */
213
+ && .${popupClasses.root} {
214
+ animation: fadeIn 1s forwards;
215
+ }
216
+
217
+ &&.${popupClasses.endAnimation} .${popupClasses.root} {
218
+ animation: fadeOut 1s forwards;
219
+ }
220
+
221
+ && .${modalClasses.overlay} {
222
+ animation: overlayFadeIn 1s forwards;
223
+ }
224
+
225
+ &&.${popupClasses.endAnimation} .${modalClasses.overlay} {
226
+ animation: overlayFadeOut 1s forwards;
227
+ }
228
+ /* stylelint-enable */
229
+
230
+ @keyframes overlayFadeIn {
231
+ from {
232
+ opacity: 0;
233
+ }
234
+
235
+ to {
236
+ opacity: 1;
237
+ }
238
+ }
239
+
240
+ @keyframes overlayFadeOut {
241
+ from {
242
+ opacity: 1;
243
+ }
244
+
245
+ to {
246
+ opacity: 0;
247
+ }
248
+ }
249
+
250
+ @keyframes fadeIn {
251
+ from {
252
+ opacity: 0;
253
+ transform: translate(-50%, 100%);
254
+ }
255
+
256
+ to {
257
+ opacity: 1;
258
+ transform: translate(-50%, -50%);
259
+ }
260
+ }
261
+
262
+ @keyframes fadeOut {
263
+ from {
264
+ opacity: 1;
265
+ transform: translate(-50%, -50%);
266
+ }
267
+
268
+ to {
269
+ opacity: 0;
270
+ transform: translate(-50%, 100%);
271
+ }
272
+ }
273
+ `;
274
+
275
+ const StoryModalAnimationDemo = ({ placement, offsetX, offsetY, ...rest }: StoryModalProps) => {
276
+ const ref = useRef(null);
277
+ const [isOpen, setIsOpen] = useState(false);
278
+
279
+ const close = useCallback(() => {
280
+ setIsOpen(false);
281
+ }, []);
282
+
283
+ return (
284
+ <SSRProvider>
285
+ <PopupProvider>
286
+ <Button view="default" text="Открыть новое модальное окно" onClick={() => setIsOpen(!isOpen)} />
287
+ <StyledModalAnimation
288
+ id="modal"
289
+ frame="theme-root"
290
+ withAnimation
291
+ onClose={() => setIsOpen(false)}
292
+ isOpen={isOpen}
293
+ placement={placement}
294
+ offset={[offsetX, offsetY]}
295
+ initialFocusRef={ref}
296
+ {...rest}
297
+ >
298
+ <Content>
299
+ <TextField value="Text" onChange={() => {}} />
300
+ <TextField ref={ref} value="Text2" onChange={() => {}} />
301
+ <Button text="Закрыть" onClick={close} />
302
+ </Content>
303
+ </StyledModalAnimation>
304
+ </PopupProvider>
305
+ </SSRProvider>
306
+ );
307
+ };
308
+
309
+ export const ModalBottomAnimation: StoryObj<StoryModalProps> = {
310
+ args: {
311
+ placement: 'bottom',
312
+ withAnimation: true,
313
+ withBlur: false,
314
+ closeOnEsc: true,
315
+ closeOnOverlayClick: true,
316
+ offsetX: 0,
317
+ offsetY: 0,
318
+ },
319
+ render: (args) => <StoryModalAnimationDemo {...args} />,
320
+ };
@@ -9,6 +9,12 @@ Object.defineProperty(exports, "useDidMountEffect", {
9
9
  return _useDidMountEffect.useDidMountEffect;
10
10
  }
11
11
  });
12
+ Object.defineProperty(exports, "useFocusTrap", {
13
+ enumerable: true,
14
+ get: function get() {
15
+ return _useFocusTrap.useFocusTrap;
16
+ }
17
+ });
12
18
  Object.defineProperty(exports, "useForceUpdate", {
13
19
  enumerable: true,
14
20
  get: function get() {
@@ -40,6 +46,7 @@ Object.defineProperty(exports, "useUniqId", {
40
46
  }
41
47
  });
42
48
  var _useUniqId = /*#__PURE__*/require("./useUniqId");
49
+ var _useFocusTrap = /*#__PURE__*/require("./useFocusTrap");
43
50
  var _usePreviousValue = /*#__PURE__*/require("./usePreviousValue");
44
51
  var _useForceUpdate = /*#__PURE__*/require("./useForceUpdate");
45
52
  var _useDidMountEffect = /*#__PURE__*/require("./useDidMountEffect");
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useFocusTrap = void 0;
7
+ var _react = /*#__PURE__*/require("react");
8
+ var _focusManager = /*#__PURE__*/require("../utils/focusManager");
9
+ var _tabbable = /*#__PURE__*/require("../utils/tabbable");
10
+ var _scopeTab = /*#__PURE__*/require("../utils/scopeTab");
11
+ // Находим элемент для фокуса
12
+ var getFocusElement = function getFocusElement(node, firstFocusSelector) {
13
+ var focusElement = null;
14
+ if (firstFocusSelector) {
15
+ if (typeof firstFocusSelector === 'string') {
16
+ focusElement = node.querySelector(firstFocusSelector);
17
+ } else if (firstFocusSelector.current) {
18
+ focusElement = firstFocusSelector.current;
19
+ }
20
+ }
21
+ if (!focusElement) {
22
+ var children = Array.from(node.querySelectorAll(_tabbable.focusSelector));
23
+ focusElement = children.find(function (el) {
24
+ return (0, _tabbable.isTabble)(el);
25
+ }) || null;
26
+ }
27
+
28
+ // Если ничего не нашлось, то может ли сама нода быть под фокусом
29
+ if (!focusElement && (0, _tabbable.isFocusable)(node)) {
30
+ focusElement = node;
31
+ }
32
+ return focusElement;
33
+ };
34
+ var processNode = function processNode(node, firstFocusSelector, ref) {
35
+ if (ref !== null && ref !== void 0 && ref.current) {
36
+ ref.current.removeEventListener('animationend', function () {
37
+ processNode(node, firstFocusSelector, ref);
38
+ });
39
+ }
40
+ var focusElement = getFocusElement(node, firstFocusSelector);
41
+ if (focusElement) {
42
+ focusElement.focus();
43
+ }
44
+ };
45
+ var focusManager = /*#__PURE__*/new _focusManager.FocusManager();
46
+
47
+ /**
48
+ * Захватывает фокус внутри DOM node.
49
+ * */
50
+ var useFocusTrap = exports.useFocusTrap = function useFocusTrap() {
51
+ var active = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
52
+ var firstFocusSelector = arguments.length > 1 ? arguments[1] : undefined;
53
+ var focusAfterNode = arguments.length > 2 ? arguments[2] : undefined;
54
+ var focusAfterAnimation = arguments.length > 3 ? arguments[3] : undefined;
55
+ var ref = (0, _react.useRef)();
56
+ var setRef = (0, _react.useCallback)(function (node) {
57
+ if (ref.current) {
58
+ focusManager.teardownScopedFocus();
59
+ focusManager.returnFocus();
60
+ }
61
+ if (active && node) {
62
+ focusManager.setupScopedFocus(node);
63
+ focusManager.markForFocusAfter(focusAfterNode);
64
+
65
+ // Delay processing the HTML node by a frame. This ensures focus is assigned correctly.
66
+ setTimeout(function () {
67
+ if (ref !== null && ref !== void 0 && ref.current && node.ownerDocument && focusAfterAnimation) {
68
+ ref.current.addEventListener('animationend', function () {
69
+ processNode(node, firstFocusSelector, ref);
70
+ });
71
+ } else if (node.ownerDocument) {
72
+ processNode(node, firstFocusSelector);
73
+ }
74
+ });
75
+ ref.current = node;
76
+ return;
77
+ }
78
+ ref.current = null;
79
+ }, [active, firstFocusSelector]);
80
+ (0, _react.useEffect)(function () {
81
+ if (!active) {
82
+ return;
83
+ }
84
+ var handleKeyDown = function handleKeyDown(event) {
85
+ if (event.key === 'Tab' && ref.current) {
86
+ (0, _scopeTab.scopeTab)(ref.current, event);
87
+ }
88
+ };
89
+ document.addEventListener('keydown', handleKeyDown);
90
+ return function () {
91
+ document.removeEventListener('keydown', handleKeyDown);
92
+ };
93
+ }, [active]);
94
+ return setRef;
95
+ };
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.FocusManager = void 0;
7
+ var _tabbable = /*#__PURE__*/require("./tabbable");
8
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
9
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
10
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
11
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
12
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
13
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
14
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
15
+ /**
16
+ * Менеджер фокуса при открытии и закрытии нод при использовании focus-trap.
17
+ * Также необходим, чтобы фокус всегда должен находиться внутри необходимой ноды.
18
+ * */
19
+ var FocusManager = exports.FocusManager = /*#__PURE__*/_createClass(function FocusManager() {
20
+ var _this = this;
21
+ _classCallCheck(this, FocusManager);
22
+ // массив с элементами, которые нужно зафокусить после анмаунта
23
+ _defineProperty(this, "focusAfterElements", []);
24
+ // массив с trap нодами
25
+ _defineProperty(this, "focusNodes", []);
26
+ _defineProperty(this, "handleFocus", function () {
27
+ // Фокус всегда должен находиться внутри необходимой ноды
28
+ var focusNode = _this.focusNodes[_this.focusNodes.length - 1];
29
+ if (!focusNode || focusNode.contains(document.activeElement)) {
30
+ return;
31
+ }
32
+
33
+ // Выделяем первый tabbable элемент
34
+ var el = (0, _tabbable.findTabbableDescendants)(focusNode)[0] || focusNode;
35
+ el.focus();
36
+ });
37
+ // добавление на фокус после анмаунта
38
+ _defineProperty(this, "markForFocusAfter", function (focusAfterNode) {
39
+ var node = focusAfterNode && focusAfterNode.current ? focusAfterNode.current : document.activeElement;
40
+ _this.focusAfterElements.push(node);
41
+ });
42
+ // фокус на необходимый элемент
43
+ _defineProperty(this, "returnFocus", function () {
44
+ var _this$focusAfterEleme;
45
+ var toFocus = (_this$focusAfterEleme = _this.focusAfterElements.pop()) !== null && _this$focusAfterEleme !== void 0 ? _this$focusAfterEleme : null;
46
+ if (toFocus) {
47
+ toFocus.focus();
48
+ }
49
+ });
50
+ // при маунте ноды
51
+ _defineProperty(this, "setupScopedFocus", function (element) {
52
+ _this.focusNodes.push(element);
53
+ document.addEventListener('focusin', _this.handleFocus, true);
54
+ });
55
+ // при анмаунте
56
+ _defineProperty(this, "teardownScopedFocus", function () {
57
+ _this.focusNodes.pop();
58
+ document.removeEventListener('focusin', _this.handleFocus);
59
+ });
60
+ });
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.scopeTab = void 0;
7
+ var _tabbable = /*#__PURE__*/require("./tabbable");
8
+ /**
9
+ * Управлление фокусом лишь внутри ноды через tab
10
+ * @param node
11
+ * @param event
12
+ */
13
+ var scopeTab = exports.scopeTab = function scopeTab(node, event) {
14
+ var tabbable = (0, _tabbable.findTabbableDescendants)(node);
15
+ if (!tabbable.length) {
16
+ event.preventDefault();
17
+ return;
18
+ }
19
+
20
+ // смотрим, является ли элемент крайним - первый или последним
21
+ var finalTabbable = tabbable[event.shiftKey ? 0 : tabbable.length - 1];
22
+ var leavingFinalTabbable = finalTabbable === document.activeElement || node === document.activeElement;
23
+
24
+ // если не является, то передаем обработку таба самому браузеру
25
+ if (!leavingFinalTabbable) {
26
+ return;
27
+ }
28
+
29
+ // иначе зацкливаемся
30
+ event.preventDefault();
31
+ var target = tabbable[event.shiftKey ? tabbable.length - 1 : 0];
32
+ if (target) {
33
+ target.focus();
34
+ }
35
+ };
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.isVisible = exports.isTabble = exports.isFocusable = exports.focusSelector = exports.findTabbableDescendants = void 0;
7
+ var tabbableNode = /input|select|textarea|button|object/;
8
+ var focusSelector = exports.focusSelector = /*#__PURE__*/['input', 'select', 'textarea', 'a', 'button', 'object', '[tabindex]', 'audio[controls]', 'video[controls]', '[contenteditable]:not([contenteditable="false"])'].join(',');
9
+ var isHidden = function isHidden(el) {
10
+ return el.offsetWidth <= 0 && el.offsetHeight <= 0 || el.style.display === 'none' || el.style.visibility === 'hidden' || el.style.opacity === '0';
11
+ };
12
+ var isVisible = exports.isVisible = function isVisible(element, parentContainer) {
13
+ var parentElement = element;
14
+ while (parentElement) {
15
+ if (parentElement === parentContainer || parentElement === document.body) {
16
+ break;
17
+ }
18
+ if (isHidden(parentElement)) {
19
+ return false;
20
+ }
21
+ parentElement = parentElement.parentNode;
22
+ }
23
+ return true;
24
+ };
25
+ var getElementTabIndex = function getElementTabIndex(element) {
26
+ var tabIndex = element.getAttribute('tabindex');
27
+ return tabIndex === null ? NaN : parseInt(tabIndex, 10);
28
+ };
29
+ var isFocusable = exports.isFocusable = function isFocusable(element, parentContainer) {
30
+ var nodeName = element.nodeName.toLowerCase();
31
+ var isTabIndexNotNaN = !Number.isNaN(getElementTabIndex(element));
32
+ var res = tabbableNode.test(nodeName) && !element.disabled || (element instanceof HTMLAnchorElement ? element.href || isTabIndexNotNaN : isTabIndexNotNaN);
33
+ return Boolean(res) && isVisible(element, parentContainer);
34
+ };
35
+ var isTabble = exports.isTabble = function isTabble(element, parentContainer) {
36
+ var tabIndex = getElementTabIndex(element);
37
+ var isTabIndexNaN = Number.isNaN(tabIndex);
38
+ return (isTabIndexNaN || tabIndex >= 0) && isFocusable(element, parentContainer);
39
+ };
40
+
41
+ // Все элементы внутри данной ноды, до которых можно добраться табом
42
+ var findTabbableDescendants = exports.findTabbableDescendants = function findTabbableDescendants(element) {
43
+ return Array.from(element.querySelectorAll(focusSelector)).filter(function (el) {
44
+ return isTabble(el, element);
45
+ });
46
+ };
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useFocusTrap = void 0;
7
+ var _react = /*#__PURE__*/require("react");
8
+ var _focusManager = /*#__PURE__*/require("../utils/focusManager");
9
+ var _tabbable = /*#__PURE__*/require("../utils/tabbable");
10
+ var _scopeTab = /*#__PURE__*/require("../utils/scopeTab");
11
+ // Находим элемент для фокуса
12
+ var getFocusElement = function getFocusElement(node, firstFocusSelector) {
13
+ var focusElement = null;
14
+ if (firstFocusSelector) {
15
+ if (typeof firstFocusSelector === 'string') {
16
+ focusElement = node.querySelector(firstFocusSelector);
17
+ } else if (firstFocusSelector.current) {
18
+ focusElement = firstFocusSelector.current;
19
+ }
20
+ }
21
+ if (!focusElement) {
22
+ var children = Array.from(node.querySelectorAll(_tabbable.focusSelector));
23
+ focusElement = children.find(function (el) {
24
+ return (0, _tabbable.isTabble)(el);
25
+ }) || null;
26
+ }
27
+
28
+ // Если ничего не нашлось, то может ли сама нода быть под фокусом
29
+ if (!focusElement && (0, _tabbable.isFocusable)(node)) {
30
+ focusElement = node;
31
+ }
32
+ return focusElement;
33
+ };
34
+ var processNode = function processNode(node, firstFocusSelector, ref) {
35
+ if (ref !== null && ref !== void 0 && ref.current) {
36
+ ref.current.removeEventListener('animationend', function () {
37
+ processNode(node, firstFocusSelector, ref);
38
+ });
39
+ }
40
+ var focusElement = getFocusElement(node, firstFocusSelector);
41
+ if (focusElement) {
42
+ focusElement.focus();
43
+ }
44
+ };
45
+ var focusManager = /*#__PURE__*/new _focusManager.FocusManager();
46
+
47
+ /**
48
+ * Захватывает фокус внутри DOM node.
49
+ * */
50
+ var useFocusTrap = exports.useFocusTrap = function useFocusTrap() {
51
+ var active = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
52
+ var firstFocusSelector = arguments.length > 1 ? arguments[1] : undefined;
53
+ var focusAfterNode = arguments.length > 2 ? arguments[2] : undefined;
54
+ var activeAfterAnimation = arguments.length > 3 ? arguments[3] : undefined;
55
+ var ref = (0, _react.useRef)();
56
+ var setRef = (0, _react.useCallback)(function (node) {
57
+ if (ref.current) {
58
+ focusManager.teardownScopedFocus();
59
+ focusManager.returnFocus();
60
+ }
61
+ if (active && node) {
62
+ focusManager.setupScopedFocus(node);
63
+ focusManager.markForFocusAfter(focusAfterNode);
64
+
65
+ // Delay processing the HTML node by a frame. This ensures focus is assigned correctly.
66
+ setTimeout(function () {
67
+ if (ref !== null && ref !== void 0 && ref.current && node.ownerDocument && activeAfterAnimation) {
68
+ ref.current.addEventListener('animationend', function () {
69
+ processNode(node, firstFocusSelector, ref);
70
+ });
71
+ } else if (node.ownerDocument) {
72
+ processNode(node, firstFocusSelector);
73
+ }
74
+ });
75
+ ref.current = node;
76
+ return;
77
+ }
78
+ ref.current = null;
79
+ }, [active, firstFocusSelector]);
80
+ (0, _react.useEffect)(function () {
81
+ if (!active) {
82
+ return;
83
+ }
84
+ var handleKeyDown = function handleKeyDown(event) {
85
+ if (event.key === 'Tab' && ref.current) {
86
+ (0, _scopeTab.scopeTab)(ref.current, event);
87
+ }
88
+ };
89
+ document.addEventListener('keydown', handleKeyDown);
90
+ return function () {
91
+ document.removeEventListener('keydown', handleKeyDown);
92
+ };
93
+ }, [active]);
94
+ return setRef;
95
+ };
@@ -3,11 +3,12 @@ function _extends() { _extends = Object.assign ? Object.assign.bind() : function
3
3
  function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
4
4
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
5
5
  import React, { forwardRef, useCallback, useMemo } from 'react';
6
- import { useFocusTrap, useForkRef, safeUseId } from '@salutejs/plasma-core';
6
+ import { useForkRef, safeUseId } from '@salutejs/plasma-core';
7
7
  import { component } from '../../engines';
8
8
  import { popupConfig, usePopupContext } from '../Popup';
9
9
  import { Overlay } from '../Overlay';
10
10
  import { DEFAULT_Z_INDEX } from '../Popup/utils';
11
+ import { useFocusTrap } from '../../hooks';
11
12
  import { classes, tokens } from './Modal.tokens';
12
13
  import { useModal } from './hooks';
13
14
  import { base as viewCSS } from './variations/_view/base';
@@ -40,7 +41,7 @@ export var modalRoot = function modalRoot(Root) {
40
41
  view = _ref.view,
41
42
  isOpen = _ref.isOpen,
42
43
  rest = _objectWithoutProperties(_ref, _excluded);
43
- var trapRef = useFocusTrap(true, initialFocusRef, focusAfterRef);
44
+ var trapRef = useFocusTrap(true, initialFocusRef, focusAfterRef, true);
44
45
  var popupController = usePopupContext();
45
46
  var innerRef = useForkRef(trapRef, outerRootRef);
46
47
  var uniqId = safeUseId();
@@ -1,4 +1,4 @@
1
- import React, { useState } from 'react';
1
+ import React, { useCallback, useRef, useState } from 'react';
2
2
  import styled from '@emotion/styled';
3
3
  import type { ComponentProps } from 'react';
4
4
  import type { StoryObj, Meta } from '@storybook/react';
@@ -6,6 +6,7 @@ import { SSRProvider } from '@salutejs/plasma-core';
6
6
 
7
7
  import { PopupProvider, popupClasses } from '../Popup/Popup';
8
8
  import { Button } from '../Button/Button';
9
+ import { TextField } from '../TextField/TextField';
9
10
  import { WithTheme } from '../../../_helpers';
10
11
 
11
12
  import { Modal, modalClasses } from './Modal';
@@ -206,3 +207,114 @@ export const ModalDemo: StoryObj<StoryModalProps> = {
206
207
  },
207
208
  render: (args) => <StoryModalDemo {...args} />,
208
209
  };
210
+
211
+ const StyledModalAnimation = styled(Modal)`
212
+ /* stylelint-disable */
213
+ && .${popupClasses.root} {
214
+ animation: fadeIn 1s forwards;
215
+ }
216
+
217
+ &&.${popupClasses.endAnimation} .${popupClasses.root} {
218
+ animation: fadeOut 1s forwards;
219
+ }
220
+
221
+ && .${modalClasses.overlay} {
222
+ animation: overlayFadeIn 1s forwards;
223
+ }
224
+
225
+ &&.${popupClasses.endAnimation} .${modalClasses.overlay} {
226
+ animation: overlayFadeOut 1s forwards;
227
+ }
228
+ /* stylelint-enable */
229
+
230
+ @keyframes overlayFadeIn {
231
+ from {
232
+ opacity: 0;
233
+ }
234
+
235
+ to {
236
+ opacity: 1;
237
+ }
238
+ }
239
+
240
+ @keyframes overlayFadeOut {
241
+ from {
242
+ opacity: 1;
243
+ }
244
+
245
+ to {
246
+ opacity: 0;
247
+ }
248
+ }
249
+
250
+ @keyframes fadeIn {
251
+ from {
252
+ opacity: 0;
253
+ transform: translate(-50%, 100%);
254
+ }
255
+
256
+ to {
257
+ opacity: 1;
258
+ transform: translate(-50%, -50%);
259
+ }
260
+ }
261
+
262
+ @keyframes fadeOut {
263
+ from {
264
+ opacity: 1;
265
+ transform: translate(-50%, -50%);
266
+ }
267
+
268
+ to {
269
+ opacity: 0;
270
+ transform: translate(-50%, 100%);
271
+ }
272
+ }
273
+ `;
274
+
275
+ const StoryModalAnimationDemo = ({ placement, offsetX, offsetY, ...rest }: StoryModalProps) => {
276
+ const ref = useRef(null);
277
+ const [isOpen, setIsOpen] = useState(false);
278
+
279
+ const close = useCallback(() => {
280
+ setIsOpen(false);
281
+ }, []);
282
+
283
+ return (
284
+ <SSRProvider>
285
+ <PopupProvider>
286
+ <Button view="default" text="Открыть новое модальное окно" onClick={() => setIsOpen(!isOpen)} />
287
+ <StyledModalAnimation
288
+ id="modal"
289
+ frame="theme-root"
290
+ withAnimation
291
+ onClose={() => setIsOpen(false)}
292
+ isOpen={isOpen}
293
+ placement={placement}
294
+ offset={[offsetX, offsetY]}
295
+ initialFocusRef={ref}
296
+ {...rest}
297
+ >
298
+ <Content>
299
+ <TextField value="Text" onChange={() => {}} />
300
+ <TextField ref={ref} value="Text2" onChange={() => {}} />
301
+ <Button text="Закрыть" onClick={close} />
302
+ </Content>
303
+ </StyledModalAnimation>
304
+ </PopupProvider>
305
+ </SSRProvider>
306
+ );
307
+ };
308
+
309
+ export const ModalBottomAnimation: StoryObj<StoryModalProps> = {
310
+ args: {
311
+ placement: 'bottom',
312
+ withAnimation: true,
313
+ withBlur: false,
314
+ closeOnEsc: true,
315
+ closeOnOverlayClick: true,
316
+ offsetX: 0,
317
+ offsetY: 0,
318
+ },
319
+ render: (args) => <StoryModalAnimationDemo {...args} />,
320
+ };