@edx/frontend-app-subscription-learner-dashboard 1.67.4 → 1.67.5
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/dist/containers/PopularCoursePanel/PopularCourses/index.js +3 -1
- package/dist/containers/PopularCoursePanel/PopularCourses/index.js.map +1 -1
- package/dist/containers/PopularProgramsPanel/PopularPrograms/index.js +3 -1
- package/dist/containers/PopularProgramsPanel/PopularPrograms/index.js.map +1 -1
- package/dist/hooks/useGridKeyboardNavigation.d.ts +11 -0
- package/dist/hooks/useGridKeyboardNavigation.js +96 -0
- package/dist/hooks/useGridKeyboardNavigation.js.map +1 -0
- package/package.json +1 -1
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { ProductCard } from '../../ProductCard';
|
|
3
|
+
import { useGridKeyboardNavigation } from '../../../hooks/useGridKeyboardNavigation';
|
|
3
4
|
export const PopularCourses = ({ items }) => {
|
|
4
|
-
|
|
5
|
+
const { gridRef, registerCard, handleKeyDown, setFocusedIndex } = useGridKeyboardNavigation({ columns: 4 });
|
|
6
|
+
return (_jsx("div", { "data-testid": "popular-courses-items", ref: gridRef, tabIndex: 0, onKeyDown: handleKeyDown, children: _jsx("div", { className: "row g-3 flex-nowrap flex-md-wrap overflow-auto", children: items.map((item, index) => (_jsx("div", { ref: node => registerCard(index, node), tabIndex: 0, className: "col-12 col-md-6 col-xl-3 d-flex mb-2", onFocus: () => setFocusedIndex(index), children: _jsx(ProductCard, { item: item }) }, item.objectID))) }) }));
|
|
5
7
|
};
|
|
6
8
|
export default PopularCourses;
|
|
7
9
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/containers/PopularCoursePanel/PopularCourses/index.tsx"],"names":[],"mappings":";AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/containers/PopularCoursePanel/PopularCourses/index.tsx"],"names":[],"mappings":";AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,EAAE,yBAAyB,EAAE,MAAM,sCAAsC,CAAC;AAEjF,MAAM,CAAC,MAAM,cAAc,GAA4B,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE;IACnE,MAAM,EAAE,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,eAAe,EAAE,GAAG,yBAAyB,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC;IAE5G,OAAO,CACL,6BACc,uBAAuB,EACnC,GAAG,EAAE,OAAO,EACZ,QAAQ,EAAE,CAAC,EACX,SAAS,EAAE,aAAa,YAExB,cAAK,SAAS,EAAC,gDAAgD,YAC5D,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,CAC1B,cAEE,GAAG,EAAE,IAAI,CAAC,EAAE,CAAC,YAAY,CAAC,KAAK,EAAE,IAAI,CAAC,EACtC,QAAQ,EAAE,CAAC,EACX,SAAS,EAAC,sCAAsC,EAChD,OAAO,EAAE,GAAG,EAAE,CAAC,eAAe,CAAC,KAAK,CAAC,YAErC,KAAC,WAAW,IAAC,IAAI,EAAE,IAAI,GAAI,IANtB,IAAI,CAAC,QAAQ,CAOd,CACP,CAAC,GACE,GACF,CACP,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,cAAc,CAAC","sourcesContent":["import { FC } from 'react';\n\nimport { ProductCard } from '../../ProductCard';\nimport { PopularCoursesProps } from './data/types';\nimport { useGridKeyboardNavigation } from '@src/hooks/useGridKeyboardNavigation';\n\nexport const PopularCourses: FC<PopularCoursesProps> = ({ items }) => {\n const { gridRef, registerCard, handleKeyDown, setFocusedIndex } = useGridKeyboardNavigation({ columns: 4 });\n\n return (\n <div\n data-testid=\"popular-courses-items\"\n ref={gridRef}\n tabIndex={0}\n onKeyDown={handleKeyDown}\n >\n <div className=\"row g-3 flex-nowrap flex-md-wrap overflow-auto\">\n {items.map((item, index) => (\n <div\n key={item.objectID}\n ref={node => registerCard(index, node)}\n tabIndex={0}\n className=\"col-12 col-md-6 col-xl-3 d-flex mb-2\"\n onFocus={() => setFocusedIndex(index)}\n >\n <ProductCard item={item} />\n </div>\n ))}\n </div>\n </div>\n );\n};\n\nexport default PopularCourses;\n"]}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { ProductCard } from '../../ProductCard';
|
|
3
|
+
import { useGridKeyboardNavigation } from '../../../hooks/useGridKeyboardNavigation';
|
|
3
4
|
export const PopularPrograms = ({ items }) => {
|
|
4
|
-
|
|
5
|
+
const { gridRef, registerCard, handleKeyDown, setFocusedIndex } = useGridKeyboardNavigation({ columns: 4 });
|
|
6
|
+
return (_jsx("div", { "data-testid": "popular-programs-items", ref: gridRef, tabIndex: 0, onKeyDown: handleKeyDown, children: _jsx("div", { className: "row g-3 flex-nowrap flex-md-wrap overflow-auto", children: items.map((item, index) => (_jsx("div", { ref: node => registerCard(index, node), tabIndex: 0, className: "col-12 col-md-6 col-xl-3 d-flex mb-2", onFocus: () => setFocusedIndex(index), children: _jsx(ProductCard, { item: item }) }, item.objectID))) }) }));
|
|
5
7
|
};
|
|
6
8
|
export default PopularPrograms;
|
|
7
9
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/containers/PopularProgramsPanel/PopularPrograms/index.tsx"],"names":[],"mappings":";AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/containers/PopularProgramsPanel/PopularPrograms/index.tsx"],"names":[],"mappings":";AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,EAAE,yBAAyB,EAAE,MAAM,sCAAsC,CAAC;AAEjF,MAAM,CAAC,MAAM,eAAe,GAA6B,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE;IACrE,MAAM,EAAE,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,eAAe,EAAE,GAAG,yBAAyB,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC,CAAC;IAE5G,OAAO,CACL,6BACc,wBAAwB,EACpC,GAAG,EAAE,OAAO,EACZ,QAAQ,EAAE,CAAC,EACX,SAAS,EAAE,aAAa,YAExB,cAAK,SAAS,EAAC,gDAAgD,YAC5D,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,CAC1B,cAEE,GAAG,EAAE,IAAI,CAAC,EAAE,CAAC,YAAY,CAAC,KAAK,EAAE,IAAI,CAAC,EACtC,QAAQ,EAAE,CAAC,EACX,SAAS,EAAC,sCAAsC,EAChD,OAAO,EAAE,GAAG,EAAE,CAAC,eAAe,CAAC,KAAK,CAAC,YAErC,KAAC,WAAW,IACV,IAAI,EAAE,IAAI,GACV,IARG,IAAI,CAAC,QAAQ,CASd,CACP,CAAC,GACE,GACF,CACP,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,eAAe,CAAC","sourcesContent":["import { FC } from 'react';\n\nimport { ProductCard } from '../../ProductCard';\nimport { PopularProgramsProps } from './data/types';\nimport { useGridKeyboardNavigation } from '@src/hooks/useGridKeyboardNavigation';\n\nexport const PopularPrograms: FC<PopularProgramsProps> = ({ items }) => {\n const { gridRef, registerCard, handleKeyDown, setFocusedIndex } = useGridKeyboardNavigation({ columns: 4 });\n\n return (\n <div\n data-testid=\"popular-programs-items\"\n ref={gridRef}\n tabIndex={0}\n onKeyDown={handleKeyDown}\n >\n <div className=\"row g-3 flex-nowrap flex-md-wrap overflow-auto\">\n {items.map((item, index) => (\n <div\n key={item.objectID}\n ref={node => registerCard(index, node)}\n tabIndex={0}\n className=\"col-12 col-md-6 col-xl-3 d-flex mb-2\"\n onFocus={() => setFocusedIndex(index)}\n >\n <ProductCard\n item={item}\n />\n </div>\n ))}\n </div>\n </div>\n );\n};\n\nexport default PopularPrograms;\n"]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { KeyboardEvent } from 'react';
|
|
2
|
+
interface UseGridKeyboardNavigationOptions {
|
|
3
|
+
columns?: number;
|
|
4
|
+
}
|
|
5
|
+
export declare const useGridKeyboardNavigation: ({ columns, }?: UseGridKeyboardNavigationOptions) => {
|
|
6
|
+
gridRef: import("react").MutableRefObject<HTMLDivElement | null>;
|
|
7
|
+
registerCard: (index: number, node: HTMLElement | null) => void;
|
|
8
|
+
handleKeyDown: (event: KeyboardEvent<HTMLElement>) => void;
|
|
9
|
+
setFocusedIndex: import("react").Dispatch<import("react").SetStateAction<number | null>>;
|
|
10
|
+
};
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { useCallback, useRef, useState } from 'react';
|
|
2
|
+
const ARROW_KEYS = new Set([
|
|
3
|
+
'ArrowUp',
|
|
4
|
+
'ArrowDown',
|
|
5
|
+
'ArrowLeft',
|
|
6
|
+
'ArrowRight',
|
|
7
|
+
]);
|
|
8
|
+
export const useGridKeyboardNavigation = ({ columns = 4, } = {}) => {
|
|
9
|
+
const gridRef = useRef(null);
|
|
10
|
+
const cardRefs = useRef([]);
|
|
11
|
+
const [focusedIndex, setFocusedIndex] = useState(null);
|
|
12
|
+
const focusCard = useCallback((index) => {
|
|
13
|
+
var _a;
|
|
14
|
+
const card = cardRefs.current[index];
|
|
15
|
+
if (!card) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
card.focus({ preventScroll: true });
|
|
19
|
+
(_a = card.scrollIntoView) === null || _a === void 0 ? void 0 : _a.call(card, { behavior: 'smooth', block: 'nearest' });
|
|
20
|
+
setFocusedIndex(index);
|
|
21
|
+
}, []);
|
|
22
|
+
const moveFocus = useCallback((currentIndex, key) => {
|
|
23
|
+
const totalCards = cardRefs.current.length;
|
|
24
|
+
if (!totalCards) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (currentIndex === null || currentIndex < 0) {
|
|
28
|
+
focusCard(0);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
let nextIndex = currentIndex;
|
|
32
|
+
switch (key) {
|
|
33
|
+
case 'ArrowRight':
|
|
34
|
+
if (currentIndex + 1 < totalCards) {
|
|
35
|
+
nextIndex = currentIndex + 1;
|
|
36
|
+
}
|
|
37
|
+
break;
|
|
38
|
+
case 'ArrowLeft':
|
|
39
|
+
if (currentIndex > 0) {
|
|
40
|
+
nextIndex = currentIndex - 1;
|
|
41
|
+
}
|
|
42
|
+
break;
|
|
43
|
+
case 'ArrowDown': {
|
|
44
|
+
const candidate = currentIndex + columns;
|
|
45
|
+
if (candidate < totalCards) {
|
|
46
|
+
nextIndex = candidate;
|
|
47
|
+
}
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
case 'ArrowUp': {
|
|
51
|
+
const candidate = currentIndex - columns;
|
|
52
|
+
if (candidate >= 0) {
|
|
53
|
+
nextIndex = candidate;
|
|
54
|
+
}
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
default:
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
focusCard(nextIndex);
|
|
61
|
+
}, [columns, focusCard]);
|
|
62
|
+
const handleKeyDown = useCallback((event) => {
|
|
63
|
+
var _a, _b;
|
|
64
|
+
if (!((_a = gridRef.current) === null || _a === void 0 ? void 0 : _a.contains(event.target))) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (ARROW_KEYS.has(event.key)) {
|
|
68
|
+
event.preventDefault();
|
|
69
|
+
const activeIndex = cardRefs.current.findIndex(card => card === null || card === void 0 ? void 0 : card.contains(document.activeElement));
|
|
70
|
+
moveFocus(activeIndex >= 0 ? activeIndex : focusedIndex, event.key);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (event.key === 'Enter') {
|
|
74
|
+
const activeIndex = cardRefs.current.findIndex(card => card === null || card === void 0 ? void 0 : card.contains(document.activeElement));
|
|
75
|
+
const index = activeIndex >= 0 ? activeIndex : focusedIndex;
|
|
76
|
+
if (index === null || index === undefined || index < 0) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const link = (_b = cardRefs.current[index]) === null || _b === void 0 ? void 0 : _b.querySelector('a[href]');
|
|
80
|
+
if (link) {
|
|
81
|
+
event.preventDefault();
|
|
82
|
+
link.click();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}, [focusedIndex, moveFocus]);
|
|
86
|
+
const registerCard = useCallback((index, node) => {
|
|
87
|
+
cardRefs.current[index] = node;
|
|
88
|
+
}, []);
|
|
89
|
+
return {
|
|
90
|
+
gridRef,
|
|
91
|
+
registerCard,
|
|
92
|
+
handleKeyDown,
|
|
93
|
+
setFocusedIndex,
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
//# sourceMappingURL=useGridKeyboardNavigation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useGridKeyboardNavigation.js","sourceRoot":"","sources":["../../src/hooks/useGridKeyboardNavigation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,WAAW,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAErE,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC;IACzB,SAAS;IACT,WAAW;IACX,WAAW;IACX,YAAY;CACb,CAAC,CAAC;AAMH,MAAM,CAAC,MAAM,yBAAyB,GAAG,CAAC,EACxC,OAAO,GAAG,CAAC,MACyB,EAAE,EAAE,EAAE;IAC1C,MAAM,OAAO,GAAG,MAAM,CAAwB,IAAI,CAAC,CAAC;IACpD,MAAM,QAAQ,GAAG,MAAM,CAAyB,EAAE,CAAC,CAAC;IACpD,MAAM,CAAC,Y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AAI,CAAE,QAAQ,CAAC,QAAQ,CAAC,aAAa,CAAC,CAC/C,CAAC;YACF,MAAM,KAAK,GAAG,WAAW,IAAI,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,YAAY,CAAC;YAC5D,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;gBACvD,OAAO;YACT,CAAC;YAED,MAAM,IAAI,GAAG,MAAA,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,0CAAE,aAAa,CAAC,SAAS,CAA6B,CAAC;YAC3F,IAAI,IAAI,EAAE,CAAC;gBACT,KAAK,CAAC,cAAc,EAAE,CAAC;gBACvB,IAAI,CAAC,KAAK,EAAE,CAAC;YACf,CAAC;QACH,CAAC;IACH,CAAC,EAAE,CAAC,YAAY,EAAE,SAAS,CAAC,CAAC,CAAC;IAE9B,MAAM,YAAY,GAAG,WAAW,CAAC,CAAC,KAAa,EAAE,IAAwB,EAAE,EAAE;QAC3E,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;IACjC,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO;QACL,OAAO;QACP,YAAY;QACZ,aAAa;QACb,eAAe;KAChB,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { KeyboardEvent, useCallback, useRef, useState } from 'react';\n\nconst ARROW_KEYS = new Set([\n 'ArrowUp',\n 'ArrowDown',\n 'ArrowLeft',\n 'ArrowRight',\n]);\n\ninterface UseGridKeyboardNavigationOptions {\n columns?: number,\n}\n\nexport const useGridKeyboardNavigation = ({\n columns = 4,\n}: UseGridKeyboardNavigationOptions = {}) => {\n const gridRef = useRef<HTMLDivElement | null>(null);\n const cardRefs = useRef<(HTMLElement | null)[]>([]);\n const [focusedIndex, setFocusedIndex] = useState<number | null>(null);\n\n const focusCard = useCallback((index: number) => {\n const card = cardRefs.current[index];\n\n if (!card) {\n return;\n }\n\n card.focus({ preventScroll: true });\n card.scrollIntoView?.({ behavior: 'smooth', block: 'nearest' });\n setFocusedIndex(index);\n }, []);\n\n const moveFocus = useCallback((currentIndex: number | null, key: string) => {\n const totalCards = cardRefs.current.length;\n\n if (!totalCards) {\n return;\n }\n\n if (currentIndex === null || currentIndex < 0) {\n focusCard(0);\n return;\n }\n\n let nextIndex = currentIndex;\n\n switch (key) {\n case 'ArrowRight':\n if (currentIndex + 1 < totalCards) {\n nextIndex = currentIndex + 1;\n }\n break;\n case 'ArrowLeft':\n if (currentIndex > 0) {\n nextIndex = currentIndex - 1;\n }\n break;\n case 'ArrowDown': {\n const candidate = currentIndex + columns;\n if (candidate < totalCards) {\n nextIndex = candidate;\n }\n break;\n }\n case 'ArrowUp': {\n const candidate = currentIndex - columns;\n if (candidate >= 0) {\n nextIndex = candidate;\n }\n break;\n }\n default:\n return;\n }\n\n focusCard(nextIndex);\n }, [columns, focusCard]);\n\n const handleKeyDown = useCallback((event: KeyboardEvent<HTMLElement>) => {\n if (!gridRef.current?.contains(event.target as Node)) {\n return;\n }\n\n if (ARROW_KEYS.has(event.key)) {\n event.preventDefault();\n const activeIndex = cardRefs.current.findIndex(\n card => card?.contains(document.activeElement),\n );\n moveFocus(activeIndex >= 0 ? activeIndex : focusedIndex, event.key);\n return;\n }\n\n if (event.key === 'Enter') {\n const activeIndex = cardRefs.current.findIndex(\n card => card?.contains(document.activeElement),\n );\n const index = activeIndex >= 0 ? activeIndex : focusedIndex;\n if (index === null || index === undefined || index < 0) {\n return;\n }\n\n const link = cardRefs.current[index]?.querySelector('a[href]') as HTMLAnchorElement | null;\n if (link) {\n event.preventDefault();\n link.click();\n }\n }\n }, [focusedIndex, moveFocus]);\n\n const registerCard = useCallback((index: number, node: HTMLElement | null) => {\n cardRefs.current[index] = node;\n }, []);\n\n return {\n gridRef,\n registerCard,\n handleKeyDown,\n setFocusedIndex,\n };\n};\n"]}
|