@tramvai/module-autoscroll 4.41.31 → 4.41.36

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
@@ -80,3 +80,20 @@ function Component() {
80
80
  return <Button onClick={navigateToWithAutoBehavior} />;
81
81
  }
82
82
  ```
83
+
84
+ ### ScrollTo top change
85
+
86
+ #### Global
87
+
88
+ ```tsx
89
+ import { AUTOSCROLL_SCROLL_TOP_TOKEN } from '@tramvai/module-autoscroll';
90
+ import { provide } from '@tramvai/core';
91
+
92
+ const providers = [
93
+ // ...,
94
+ provide({
95
+ provide: AUTOSCROLL_SCROLL_TOP_TOKEN,
96
+ useValue: -1, // default is 0
97
+ }),
98
+ ];
99
+ ```
@@ -2,16 +2,17 @@ import { useRef, useEffect } from 'react';
2
2
  import { useRoute, useUrl } from '@tramvai/module-router';
3
3
  import { optional } from '@tinkoff/dippy';
4
4
  import { useDi } from '@tramvai/react';
5
- import { AUTOSCROLL_BEHAVIOR_MODE_TOKEN } from '../tokens.es.js';
5
+ import { AUTOSCROLL_BEHAVIOR_MODE_TOKEN, AUTOSCROLL_SCROLL_TOP_TOKEN } from '../tokens.es.js';
6
6
 
7
7
  const DEFAULT_AUTOSCROLL_BEHAVIOR = 'smooth';
8
- const scrollToTop = (behavior) => {
8
+ const DEFAULT_AUTOSCROLL_SCROLL_TOP = 0;
9
+ const scrollToTop = (behavior, top) => {
9
10
  // В некоторых браузерах не поддерживается scrollTo с одним параметром
10
11
  try {
11
- window.scrollTo({ top: 0, left: 0, behavior });
12
+ window.scrollTo({ top, left: 0, behavior });
12
13
  }
13
14
  catch (error) {
14
- window.scrollTo(0, 0);
15
+ window.scrollTo(0, top);
15
16
  }
16
17
  };
17
18
  const isAutoScrollEnabled = (route) => {
@@ -33,7 +34,9 @@ const scrollToAnchor = (anchor, behavior) => {
33
34
  // Поведение с подскроллом похоже на
34
35
  // https://reacttraining.com/react-router/web/guides/scroll-restoration/scroll-to-top
35
36
  function Autoscroll() {
37
+ var _a;
36
38
  const globalScrollBehavior = useDi(optional(AUTOSCROLL_BEHAVIOR_MODE_TOKEN)) || DEFAULT_AUTOSCROLL_BEHAVIOR;
39
+ const scrollTop = (_a = useDi(optional(AUTOSCROLL_SCROLL_TOP_TOKEN))) !== null && _a !== void 0 ? _a : DEFAULT_AUTOSCROLL_SCROLL_TOP;
37
40
  const route = useRoute();
38
41
  const url = useUrl();
39
42
  const routeRef = useRef(url);
@@ -50,7 +53,7 @@ function Autoscroll() {
50
53
  }
51
54
  const scrollBehavior = ((_a = route === null || route === void 0 ? void 0 : route.navigateState) === null || _a === void 0 ? void 0 : _a.autoscrollBehavior) || globalScrollBehavior;
52
55
  if (!url.hash) {
53
- scrollToTop(scrollBehavior);
56
+ scrollToTop(scrollBehavior, scrollTop);
54
57
  shouldScroll.current = false;
55
58
  }
56
59
  else {
@@ -9,13 +9,14 @@ var react = require('@tramvai/react');
9
9
  var tokens = require('../tokens.js');
10
10
 
11
11
  const DEFAULT_AUTOSCROLL_BEHAVIOR = 'smooth';
12
- const scrollToTop = (behavior) => {
12
+ const DEFAULT_AUTOSCROLL_SCROLL_TOP = 0;
13
+ const scrollToTop = (behavior, top) => {
13
14
  // В некоторых браузерах не поддерживается scrollTo с одним параметром
14
15
  try {
15
- window.scrollTo({ top: 0, left: 0, behavior });
16
+ window.scrollTo({ top, left: 0, behavior });
16
17
  }
17
18
  catch (error) {
18
- window.scrollTo(0, 0);
19
+ window.scrollTo(0, top);
19
20
  }
20
21
  };
21
22
  const isAutoScrollEnabled = (route) => {
@@ -37,7 +38,9 @@ const scrollToAnchor = (anchor, behavior) => {
37
38
  // Поведение с подскроллом похоже на
38
39
  // https://reacttraining.com/react-router/web/guides/scroll-restoration/scroll-to-top
39
40
  function Autoscroll() {
41
+ var _a;
40
42
  const globalScrollBehavior = react.useDi(dippy.optional(tokens.AUTOSCROLL_BEHAVIOR_MODE_TOKEN)) || DEFAULT_AUTOSCROLL_BEHAVIOR;
43
+ const scrollTop = (_a = react.useDi(dippy.optional(tokens.AUTOSCROLL_SCROLL_TOP_TOKEN))) !== null && _a !== void 0 ? _a : DEFAULT_AUTOSCROLL_SCROLL_TOP;
41
44
  const route = moduleRouter.useRoute();
42
45
  const url = moduleRouter.useUrl();
43
46
  const routeRef = react$1.useRef(url);
@@ -54,7 +57,7 @@ function Autoscroll() {
54
57
  }
55
58
  const scrollBehavior = ((_a = route === null || route === void 0 ? void 0 : route.navigateState) === null || _a === void 0 ? void 0 : _a.autoscrollBehavior) || globalScrollBehavior;
56
59
  if (!url.hash) {
57
- scrollToTop(scrollBehavior);
60
+ scrollToTop(scrollBehavior, scrollTop);
58
61
  shouldScroll.current = false;
59
62
  }
60
63
  else {
package/lib/index.es.js CHANGED
@@ -3,7 +3,7 @@ import { Module } from '@tramvai/core';
3
3
  import { LAYOUT_OPTIONS } from '@tramvai/tokens-render';
4
4
  import { Autoscroll } from './components/Autoscroll.es.js';
5
5
  export { Autoscroll } from './components/Autoscroll.es.js';
6
- export { AUTOSCROLL_BEHAVIOR_MODE_TOKEN } from './tokens.es.js';
6
+ export { AUTOSCROLL_BEHAVIOR_MODE_TOKEN, AUTOSCROLL_SCROLL_TOP_TOKEN } from './tokens.es.js';
7
7
 
8
8
  let AutoscrollModule = class AutoscrollModule {
9
9
  };
package/lib/index.js CHANGED
@@ -28,3 +28,4 @@ exports.AutoscrollModule = tslib.__decorate([
28
28
 
29
29
  exports.Autoscroll = Autoscroll.Autoscroll;
30
30
  exports.AUTOSCROLL_BEHAVIOR_MODE_TOKEN = tokens.AUTOSCROLL_BEHAVIOR_MODE_TOKEN;
31
+ exports.AUTOSCROLL_SCROLL_TOP_TOKEN = tokens.AUTOSCROLL_SCROLL_TOP_TOKEN;
package/lib/tokens.d.ts CHANGED
@@ -3,4 +3,7 @@ export declare const AUTOSCROLL_BEHAVIOR_MODE_TOKEN: ("smooth" & {
3
3
  }) | ("auto" & {
4
4
  __type?: "base token" | undefined;
5
5
  });
6
+ export declare const AUTOSCROLL_SCROLL_TOP_TOKEN: number & {
7
+ __type?: "base token" | undefined;
8
+ };
6
9
  //# sourceMappingURL=tokens.d.ts.map
package/lib/tokens.es.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { createToken } from '@tinkoff/dippy';
2
2
 
3
3
  const AUTOSCROLL_BEHAVIOR_MODE_TOKEN = createToken('autoscroll behavior');
4
+ const AUTOSCROLL_SCROLL_TOP_TOKEN = createToken('autoscroll scroll top');
4
5
 
5
- export { AUTOSCROLL_BEHAVIOR_MODE_TOKEN };
6
+ export { AUTOSCROLL_BEHAVIOR_MODE_TOKEN, AUTOSCROLL_SCROLL_TOP_TOKEN };
package/lib/tokens.js CHANGED
@@ -5,5 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var dippy = require('@tinkoff/dippy');
6
6
 
7
7
  const AUTOSCROLL_BEHAVIOR_MODE_TOKEN = dippy.createToken('autoscroll behavior');
8
+ const AUTOSCROLL_SCROLL_TOP_TOKEN = dippy.createToken('autoscroll scroll top');
8
9
 
9
10
  exports.AUTOSCROLL_BEHAVIOR_MODE_TOKEN = AUTOSCROLL_BEHAVIOR_MODE_TOKEN;
11
+ exports.AUTOSCROLL_SCROLL_TOP_TOKEN = AUTOSCROLL_SCROLL_TOP_TOKEN;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/module-autoscroll",
3
- "version": "4.41.31",
3
+ "version": "4.41.36",
4
4
  "description": "Компонент с автопрокруткой к началу страницы",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -17,11 +17,11 @@
17
17
  "watch": "tsc -w"
18
18
  },
19
19
  "peerDependencies": {
20
- "@tramvai/react": "4.41.31",
20
+ "@tramvai/react": "4.41.36",
21
21
  "@tinkoff/dippy": "0.10.11",
22
- "@tramvai/core": "4.41.31",
23
- "@tramvai/module-router": "4.41.31",
24
- "@tramvai/tokens-render": "4.41.31",
22
+ "@tramvai/core": "4.41.36",
23
+ "@tramvai/module-router": "4.41.36",
24
+ "@tramvai/tokens-render": "4.41.36",
25
25
  "react": ">=16.14.0",
26
26
  "tslib": "^2.4.0"
27
27
  },