@ndla/ui 19.1.0 → 20.0.0

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.
Files changed (83) hide show
  1. package/es/MyNdla/Resource/FolderInput.js +29 -36
  2. package/es/NDLAFilm/FilmSlideshow.js +8 -8
  3. package/es/Search/LoadingWrapper.js +2 -2
  4. package/es/Search/SearchResult.js +1 -1
  5. package/es/SearchTypeResult/SearchTypeResult.js +3 -3
  6. package/es/TreeStructure/FolderItem.js +54 -38
  7. package/es/TreeStructure/FolderItems.js +29 -35
  8. package/es/TreeStructure/FolderNameInput.js +12 -16
  9. package/es/TreeStructure/TreeStructure.js +64 -91
  10. package/es/TreeStructure/arrowNavigation.js +44 -0
  11. package/es/TreeStructure/helperFunctions.js +41 -35
  12. package/es/index.js +0 -1
  13. package/es/locale/messages-en.js +4 -1
  14. package/es/locale/messages-nb.js +4 -1
  15. package/es/locale/messages-nn.js +4 -1
  16. package/es/locale/messages-se.js +4 -1
  17. package/es/locale/messages-sma.js +4 -1
  18. package/lib/MyNdla/Resource/FolderInput.d.ts +2 -2
  19. package/lib/MyNdla/Resource/FolderInput.js +26 -33
  20. package/lib/NDLAFilm/FilmSlideshow.js +10 -10
  21. package/lib/Search/LoadingWrapper.js +3 -3
  22. package/lib/Search/SearchResult.js +2 -2
  23. package/lib/SearchTypeResult/SearchTypeResult.js +4 -4
  24. package/lib/TreeStructure/FolderItem.d.ts +6 -3
  25. package/lib/TreeStructure/FolderItem.js +55 -38
  26. package/lib/TreeStructure/FolderItems.d.ts +1 -1
  27. package/lib/TreeStructure/FolderItems.js +29 -35
  28. package/lib/TreeStructure/FolderNameInput.d.ts +3 -2
  29. package/lib/TreeStructure/FolderNameInput.js +13 -17
  30. package/lib/TreeStructure/TreeStructure.d.ts +1 -6
  31. package/lib/TreeStructure/TreeStructure.js +63 -92
  32. package/lib/TreeStructure/TreeStructure.types.d.ts +13 -20
  33. package/lib/TreeStructure/arrowNavigation.d.ts +9 -0
  34. package/lib/TreeStructure/arrowNavigation.js +54 -0
  35. package/lib/TreeStructure/helperFunctions.d.ts +3 -4
  36. package/lib/TreeStructure/helperFunctions.js +45 -35
  37. package/lib/index.d.ts +0 -1
  38. package/lib/index.js +0 -9
  39. package/lib/locale/messages-en.d.ts +3 -0
  40. package/lib/locale/messages-en.js +4 -1
  41. package/lib/locale/messages-nb.d.ts +3 -0
  42. package/lib/locale/messages-nb.js +4 -1
  43. package/lib/locale/messages-nn.d.ts +3 -0
  44. package/lib/locale/messages-nn.js +4 -1
  45. package/lib/locale/messages-se.d.ts +3 -0
  46. package/lib/locale/messages-se.js +4 -1
  47. package/lib/locale/messages-sma.d.ts +3 -0
  48. package/lib/locale/messages-sma.js +4 -1
  49. package/package.json +11 -10
  50. package/src/MyNdla/Resource/FolderInput.tsx +41 -44
  51. package/src/NDLAFilm/FilmSlideshow.tsx +1 -1
  52. package/src/Search/LoadingWrapper.tsx +1 -1
  53. package/src/Search/SearchResult.jsx +1 -1
  54. package/src/SearchTypeResult/SearchTypeResult.tsx +1 -1
  55. package/src/TreeStructure/FolderItem.tsx +63 -40
  56. package/src/TreeStructure/FolderItems.tsx +26 -19
  57. package/src/TreeStructure/FolderNameInput.tsx +10 -12
  58. package/src/TreeStructure/TreeStructure.tsx +56 -71
  59. package/src/TreeStructure/TreeStructure.types.ts +13 -17
  60. package/src/TreeStructure/arrowNavigation.ts +53 -0
  61. package/src/TreeStructure/helperFunctions.ts +17 -25
  62. package/src/index.ts +0 -2
  63. package/src/locale/messages-en.ts +3 -0
  64. package/src/locale/messages-nb.ts +3 -0
  65. package/src/locale/messages-nn.ts +3 -0
  66. package/src/locale/messages-se.ts +3 -0
  67. package/src/locale/messages-sma.ts +3 -0
  68. package/es/Spinner/Spinner.js +0 -42
  69. package/es/Spinner/index.js +0 -2
  70. package/es/TreeStructure/keyboardNavigation/keyboardNavigation.js +0 -194
  71. package/es/TreeStructure/keyboardNavigation/keyboardNavigation.types.js +0 -0
  72. package/lib/Spinner/Spinner.d.ts +0 -16
  73. package/lib/Spinner/Spinner.js +0 -54
  74. package/lib/Spinner/index.d.ts +0 -2
  75. package/lib/Spinner/index.js +0 -13
  76. package/lib/TreeStructure/keyboardNavigation/keyboardNavigation.d.ts +0 -11
  77. package/lib/TreeStructure/keyboardNavigation/keyboardNavigation.js +0 -198
  78. package/lib/TreeStructure/keyboardNavigation/keyboardNavigation.types.d.ts +0 -26
  79. package/lib/TreeStructure/keyboardNavigation/keyboardNavigation.types.js +0 -1
  80. package/src/Spinner/Spinner.tsx +0 -46
  81. package/src/Spinner/index.ts +0 -3
  82. package/src/TreeStructure/keyboardNavigation/keyboardNavigation.ts +0 -161
  83. package/src/TreeStructure/keyboardNavigation/keyboardNavigation.types.ts +0 -28
@@ -1,161 +0,0 @@
1
- /**
2
- * Copyright (c) 2022-present, NDLA.
3
- *
4
- * This source code is licensed under the GPLv3 license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- */
8
-
9
- import { FolderStructureProps, SetFocusedFolderId } from '../TreeStructure.types';
10
- import { KeyboardNavigationProps, ElementWithKeyFocusProps } from './keyboardNavigation.types';
11
- import { MAX_LEVEL_FOR_FOLDERS } from '../TreeStructure';
12
-
13
- export const KEYBOARD_KEYS_OF_INTEREST = ['ArrowDown', 'ArrowUp', 'ArrowRight', 'ArrowLeft', ' '];
14
-
15
- // Traverse upwards, incase parent is last element of its parent..
16
- const traverseUpwards = (
17
- inital: FolderStructureProps[],
18
- setFocusedFolderId: SetFocusedFolderId,
19
- paths: number[],
20
- index: number,
21
- ) => {
22
- let findParent: FolderStructureProps[] = inital;
23
- const parentNextIds: (string | false)[] = [];
24
- paths.forEach((pathIndex) => {
25
- const nextParent = findParent ? findParent[pathIndex + 1] : undefined;
26
- parentNextIds.push(nextParent?.id || false);
27
- findParent = findParent[pathIndex].data || [];
28
- });
29
- if (!parentNextIds.length) {
30
- parentNextIds.push(findParent[index + 1]?.id || false);
31
- }
32
- // We use a reversed version of parentNextIds, filtered out falses, to find the next element
33
- // No newId? We are at the end of the tree so we wont update.
34
- const newId = parentNextIds.reverse().filter((id) => id)[0];
35
- if (newId) {
36
- setFocusedFolderId(newId);
37
- }
38
- };
39
-
40
- const keyboardNavigation = ({
41
- e,
42
- data,
43
- onToggleOpen,
44
- setFocusedFolderId,
45
- focusedFolderId: id,
46
- openFolders,
47
- }: KeyboardNavigationProps): string | undefined => {
48
- if (e.key === ' ' && document.activeElement?.nodeName === 'INPUT') {
49
- return;
50
- }
51
-
52
- // We are navigating in the tree.
53
- // We need to find the next folder in the tree
54
- const elementWithKeyFocus: ElementWithKeyFocusProps = {
55
- paths: [],
56
- index: 0,
57
- };
58
-
59
- const updatePathToElementWithKeyFocus = (
60
- data: FolderStructureProps[],
61
- paths: number[],
62
- parent: FolderStructureProps[],
63
- parentId?: string,
64
- ): boolean =>
65
- data.some(({ data: childData, id: dataId, url }, _index) => {
66
- if (dataId === id) {
67
- elementWithKeyFocus.paths = paths;
68
- elementWithKeyFocus.index = _index;
69
- elementWithKeyFocus.isOpen = openFolders.includes(dataId) && childData && childData.length > 0;
70
- elementWithKeyFocus.data = childData;
71
- elementWithKeyFocus.parent = parent;
72
- elementWithKeyFocus.parentId = parentId;
73
- elementWithKeyFocus.url = url;
74
- return true;
75
- }
76
- return childData ? updatePathToElementWithKeyFocus(childData, [...paths, _index], [...childData], dataId) : false;
77
- });
78
- if (!updatePathToElementWithKeyFocus(data, [], data)) {
79
- // Couldn't find its location in the tree.
80
- // This should not happen, reset its value to root.
81
- setFocusedFolderId(e.key === 'ArrowDown' ? data[0]?.id : undefined);
82
- return;
83
- }
84
- e.preventDefault();
85
- e.stopPropagation();
86
-
87
- if (e.key === ' ') {
88
- const simulatedEvent = new MouseEvent('click', {
89
- bubbles: true,
90
- cancelable: true,
91
- view: window,
92
- });
93
- document.activeElement && document.activeElement.dispatchEvent(simulatedEvent);
94
- return;
95
- }
96
-
97
- if (e.key === 'ArrowRight') {
98
- if (
99
- !elementWithKeyFocus.isOpen &&
100
- elementWithKeyFocus.data?.length &&
101
- id &&
102
- elementWithKeyFocus.paths.length < MAX_LEVEL_FOR_FOLDERS - 1
103
- ) {
104
- onToggleOpen(id);
105
- }
106
- return;
107
- }
108
- if (e.key === 'ArrowLeft') {
109
- if (id && elementWithKeyFocus.isOpen) {
110
- onToggleOpen(id);
111
- }
112
- return;
113
- }
114
-
115
- if (!id && e.key === 'ArrowDown') {
116
- setFocusedFolderId(data[0]?.id);
117
- return;
118
- }
119
- if (!id) {
120
- return;
121
- }
122
- // Move up
123
- if (e.key === 'ArrowUp') {
124
- if (elementWithKeyFocus.index > 0) {
125
- // Move upwards to the parent folder
126
- setFocusedFolderId(
127
- elementWithKeyFocus.parent ? elementWithKeyFocus.parent[elementWithKeyFocus.index - 1]?.id : undefined,
128
- );
129
- } else if (elementWithKeyFocus.paths.length > 0) {
130
- elementWithKeyFocus.paths.pop();
131
- let findParent = data;
132
- elementWithKeyFocus.paths.forEach((index) => {
133
- findParent = findParent[index].data as FolderStructureProps[];
134
- });
135
- const parentsCurrentIndex = findParent.findIndex(({ id }) => id === elementWithKeyFocus.parentId);
136
- setFocusedFolderId(findParent[parentsCurrentIndex]?.id);
137
- }
138
- return;
139
- }
140
-
141
- if (elementWithKeyFocus.isOpen) {
142
- if (elementWithKeyFocus.data?.length) {
143
- setFocusedFolderId(elementWithKeyFocus.data[0]?.id);
144
- } else {
145
- // move to next child of parent if any... need new traverse :-/
146
- traverseUpwards(data, setFocusedFolderId, elementWithKeyFocus.paths, elementWithKeyFocus.index);
147
- }
148
- return;
149
- }
150
-
151
- if (elementWithKeyFocus.parent && elementWithKeyFocus.index < elementWithKeyFocus.parent?.length - 1) {
152
- // Move downwards to the next child
153
- setFocusedFolderId(elementWithKeyFocus.parent[elementWithKeyFocus.index + 1]?.id);
154
- return;
155
- }
156
-
157
- traverseUpwards(data, setFocusedFolderId, elementWithKeyFocus.paths, elementWithKeyFocus.index);
158
- return;
159
- };
160
-
161
- export default keyboardNavigation;
@@ -1,28 +0,0 @@
1
- /**
2
- * Copyright (c) 2022-present, NDLA.
3
- *
4
- * This source code is licensed under the GPLv3 license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- */
8
-
9
- import { FolderStructureProps, SetFocusedFolderId } from '../TreeStructure.types';
10
-
11
- export interface KeyboardNavigationProps {
12
- e: React.KeyboardEvent<HTMLElement>;
13
- data: FolderStructureProps[];
14
- setFocusedFolderId: SetFocusedFolderId;
15
- openFolders: string[];
16
- onToggleOpen: (id: string) => void;
17
- focusedFolderId: string | undefined;
18
- }
19
-
20
- export interface ElementWithKeyFocusProps {
21
- paths: number[];
22
- index: number;
23
- data?: FolderStructureProps[];
24
- parent?: FolderStructureProps[];
25
- parentId?: string;
26
- isOpen?: boolean;
27
- url?: string;
28
- }