@player-ui/auto-scroll-manager-plugin-react 0.7.4-next.4 → 0.7.5--canary.434.14868

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.
@@ -0,0 +1,42 @@
1
+ import type { ReactPlayer, ReactPlayerPlugin } from "@player-ui/react";
2
+ import type { Player } from "@player-ui/react";
3
+ export declare enum ScrollType {
4
+ ValidationError = 0,
5
+ FirstAppearance = 1,
6
+ Unknown = 2
7
+ }
8
+ export interface AutoScrollManagerConfig {
9
+ /** Config to auto-scroll on load */
10
+ autoScrollOnLoad?: boolean;
11
+ /** Config to auto-focus on an error */
12
+ autoFocusOnErrorField?: boolean;
13
+ /** Optional function to get container element, which is used for calculating offset (default: document.body) */
14
+ getBaseElement?: () => HTMLElement | undefined | null;
15
+ /** Additional offset to be used (default: 0) */
16
+ offset?: number;
17
+ }
18
+ /** A plugin to manage scrolling behavior */
19
+ export declare class AutoScrollManagerPlugin implements ReactPlayerPlugin {
20
+ name: string;
21
+ /** Toggles if we should auto scroll to to the first failed validation on page load */
22
+ private autoScrollOnLoad;
23
+ /** Toggles if we should auto scroll to the first failed validation on navigation failure */
24
+ private autoFocusOnErrorField;
25
+ /** tracks if its the initial page render */
26
+ private initialRender;
27
+ /** tracks if the navigation failed */
28
+ private failedNavigation;
29
+ /** function to return the base of the scrollable area */
30
+ private getBaseElement;
31
+ /** static offset */
32
+ private offset;
33
+ /** map of scroll type to set of ids that are registered under that type */
34
+ private alreadyScrolledTo;
35
+ private scrollFn;
36
+ constructor(config: AutoScrollManagerConfig);
37
+ getFirstScrollableElement(idList: Set<string>, type: ScrollType): string;
38
+ calculateScroll(scrollableElements: Map<ScrollType, Set<string>>): string;
39
+ apply(player: Player): void;
40
+ applyReact(reactPlayer: ReactPlayer): void;
41
+ }
42
+ //# sourceMappingURL=plugin.d.ts.map
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Scroll to the given element
3
+ -* @param node Element to scroll to
4
+ -* @param baseElement Container element used to calculate offset
5
+ -* @param offset Additional offset
6
+ */
7
+ declare const _default: (node: HTMLElement, baseElement: HTMLElement, offset: number) => void;
8
+ export default _default;
9
+ //# sourceMappingURL=scrollIntoViewWithOffset.d.ts.map
package/dist/index.cjs.js DELETED
@@ -1,156 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var React = require('react');
6
- var seamlessScrollPolyfill = require('seamless-scroll-polyfill');
7
-
8
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
-
10
- var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
11
-
12
- var scrollIntoViewWithOffset = (node, baseElement, offset) => {
13
- seamlessScrollPolyfill.scrollTo(window, {
14
- behavior: "smooth",
15
- top: node.getBoundingClientRect().top - baseElement.getBoundingClientRect().top - offset
16
- });
17
- };
18
-
19
- const AutoScrollManagerContext = React__default["default"].createContext({ register: () => {
20
- } });
21
- const useRegisterAsScrollable = () => {
22
- const { register } = React__default["default"].useContext(AutoScrollManagerContext);
23
- return register;
24
- };
25
- const AutoScrollProvider = ({
26
- getElementToScrollTo,
27
- getBaseElement,
28
- offset,
29
- children
30
- }) => {
31
- const [scrollableMap, setScrollableMap] = React.useState(new Map());
32
- const updateScrollableMap = (key, value) => {
33
- setScrollableMap((prev) => {
34
- var _a;
35
- const nm = new Map(prev);
36
- if (!nm.get(key)) {
37
- nm.set(key, new Set());
38
- }
39
- (_a = nm.get(key)) == null ? void 0 : _a.add(value);
40
- return nm;
41
- });
42
- };
43
- const register = (data) => {
44
- updateScrollableMap(data.type, data.ref);
45
- };
46
- React.useEffect(() => {
47
- const node = document.getElementById(getElementToScrollTo(scrollableMap));
48
- if (node) {
49
- scrollIntoViewWithOffset(node, getBaseElement() || document.body, offset);
50
- }
51
- });
52
- return /* @__PURE__ */ React__default["default"].createElement(AutoScrollManagerContext.Provider, {
53
- value: { register }
54
- }, children);
55
- };
56
-
57
- exports.ScrollType = void 0;
58
- (function(ScrollType2) {
59
- ScrollType2[ScrollType2["ValidationError"] = 0] = "ValidationError";
60
- ScrollType2[ScrollType2["FirstAppearance"] = 1] = "FirstAppearance";
61
- ScrollType2[ScrollType2["Unknown"] = 2] = "Unknown";
62
- })(exports.ScrollType || (exports.ScrollType = {}));
63
- class AutoScrollManagerPlugin {
64
- constructor(config) {
65
- this.name = "auto-scroll-manager";
66
- var _a, _b, _c, _d;
67
- this.autoScrollOnLoad = (_a = config.autoScrollOnLoad) != null ? _a : false;
68
- this.autoFocusOnErrorField = (_b = config.autoFocusOnErrorField) != null ? _b : false;
69
- this.getBaseElement = (_c = config.getBaseElement) != null ? _c : () => null;
70
- this.offset = (_d = config.offset) != null ? _d : 0;
71
- this.initialRender = false;
72
- this.failedNavigation = false;
73
- this.alreadyScrolledTo = [];
74
- this.scrollFn = this.calculateScroll.bind(this);
75
- }
76
- getFirstScrollableElement(idList, type) {
77
- const highestElement = {
78
- id: "",
79
- ypos: 0
80
- };
81
- const ypos = window.scrollY;
82
- idList.forEach((id) => {
83
- const element = document.getElementById(id);
84
- if (type === 0 && (element == null ? void 0 : element.getAttribute("aria-invalid")) === "false") {
85
- return;
86
- }
87
- if (type === 1) {
88
- if (this.alreadyScrolledTo.indexOf(id) !== -1) {
89
- return;
90
- }
91
- this.alreadyScrolledTo.push(id);
92
- }
93
- const epos = element == null ? void 0 : element.getBoundingClientRect().top;
94
- if (epos !== void 0 && (epos + ypos < highestElement.ypos || highestElement.id === "")) {
95
- highestElement.id = id;
96
- highestElement.ypos = ypos + epos;
97
- }
98
- });
99
- return highestElement.id;
100
- }
101
- calculateScroll(scrollableElements) {
102
- let currentScroll = 1;
103
- if (this.initialRender) {
104
- if (this.autoScrollOnLoad) {
105
- currentScroll = 0;
106
- }
107
- this.initialRender = false;
108
- } else if (this.failedNavigation) {
109
- if (this.autoFocusOnErrorField) {
110
- currentScroll = 0;
111
- }
112
- this.failedNavigation = false;
113
- }
114
- const elementList = scrollableElements.get(currentScroll);
115
- if (elementList) {
116
- const element = this.getFirstScrollableElement(elementList, currentScroll);
117
- return element != null ? element : "";
118
- }
119
- return "";
120
- }
121
- apply(player) {
122
- player.hooks.flowController.tap(this.name, (fc) => {
123
- fc.hooks.flow.tap(this.name, (flow) => {
124
- flow.hooks.transition.tap(this.name, () => {
125
- this.initialRender = true;
126
- this.failedNavigation = false;
127
- this.alreadyScrolledTo = [];
128
- window.scroll(0, 0);
129
- });
130
- flow.hooks.skipTransition.intercept({
131
- call: () => {
132
- this.failedNavigation = true;
133
- }
134
- });
135
- });
136
- });
137
- }
138
- applyReact(reactPlayer) {
139
- reactPlayer.hooks.webComponent.tap(this.name, (Comp) => {
140
- return () => {
141
- const { scrollFn, getBaseElement, offset } = this;
142
- return /* @__PURE__ */ React__default["default"].createElement(AutoScrollProvider, {
143
- getElementToScrollTo: scrollFn,
144
- getBaseElement,
145
- offset
146
- }, /* @__PURE__ */ React__default["default"].createElement(Comp, null));
147
- };
148
- });
149
- }
150
- }
151
-
152
- exports.AutoScrollManagerContext = AutoScrollManagerContext;
153
- exports.AutoScrollManagerPlugin = AutoScrollManagerPlugin;
154
- exports.AutoScrollProvider = AutoScrollProvider;
155
- exports.useRegisterAsScrollable = useRegisterAsScrollable;
156
- //# sourceMappingURL=index.cjs.js.map
package/dist/index.d.ts DELETED
@@ -1,69 +0,0 @@
1
- import React, { PropsWithChildren } from 'react';
2
- import { ReactPlayerPlugin, ReactPlayer } from '@player-ui/react';
3
- import { Player } from '@player-ui/player';
4
-
5
- interface AutoScrollProviderProps {
6
- /** Return the element to scroll to based on the registered types */
7
- getElementToScrollTo: (scrollableElements: Map<ScrollType, Set<string>>) => string;
8
- /** Optional function to get container element, which is used for calculating offset (default: document.body) */
9
- getBaseElement: () => HTMLElement | undefined | null;
10
- /** Additional offset to be used (default: 0) */
11
- offset: number;
12
- }
13
- interface RegisterData {
14
- /** when to scroll to the target */
15
- type: ScrollType;
16
- /** the html id to scroll to */
17
- ref: string;
18
- }
19
- declare type ScrollFunction = (registerData: RegisterData) => void;
20
- declare const AutoScrollManagerContext: React.Context<{
21
- /** function to register a scroll target */
22
- register: ScrollFunction;
23
- }>;
24
- /** hook to register as a scroll target */
25
- declare const useRegisterAsScrollable: () => ScrollFunction;
26
- /** Component to handle scrolling */
27
- declare const AutoScrollProvider: ({ getElementToScrollTo, getBaseElement, offset, children, }: PropsWithChildren<AutoScrollProviderProps>) => JSX.Element;
28
-
29
- declare enum ScrollType {
30
- ValidationError = 0,
31
- FirstAppearance = 1,
32
- Unknown = 2
33
- }
34
- interface AutoScrollManagerConfig {
35
- /** Config to auto-scroll on load */
36
- autoScrollOnLoad?: boolean;
37
- /** Config to auto-focus on an error */
38
- autoFocusOnErrorField?: boolean;
39
- /** Optional function to get container element, which is used for calculating offset (default: document.body) */
40
- getBaseElement?: () => HTMLElement | undefined | null;
41
- /** Additional offset to be used (default: 0) */
42
- offset?: number;
43
- }
44
- /** A plugin to manage scrolling behavior */
45
- declare class AutoScrollManagerPlugin implements ReactPlayerPlugin {
46
- name: string;
47
- /** Toggles if we should auto scroll to to the first failed validation on page load */
48
- private autoScrollOnLoad;
49
- /** Toggles if we should auto scroll to the first failed validation on navigation failure */
50
- private autoFocusOnErrorField;
51
- /** tracks if its the initial page render */
52
- private initialRender;
53
- /** tracks if the navigation failed */
54
- private failedNavigation;
55
- /** function to return the base of the scrollable area */
56
- private getBaseElement;
57
- /** static offset */
58
- private offset;
59
- /** map of scroll type to set of ids that are registered under that type */
60
- private alreadyScrolledTo;
61
- private scrollFn;
62
- constructor(config: AutoScrollManagerConfig);
63
- getFirstScrollableElement(idList: Set<string>, type: ScrollType): string;
64
- calculateScroll(scrollableElements: Map<ScrollType, Set<string>>): string;
65
- apply(player: Player): void;
66
- applyReact(reactPlayer: ReactPlayer): void;
67
- }
68
-
69
- export { AutoScrollManagerConfig, AutoScrollManagerContext, AutoScrollManagerPlugin, AutoScrollProvider, AutoScrollProviderProps, RegisterData, ScrollFunction, ScrollType, useRegisterAsScrollable };