@tpzdsp/next-toolkit 1.4.4 → 1.5.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tpzdsp/next-toolkit",
3
- "version": "1.4.4",
3
+ "version": "1.5.0",
4
4
  "description": "A reusable React component library for Next.js applications",
5
5
  "type": "module",
6
6
  "private": false,
@@ -24,7 +24,7 @@
24
24
  /* Component-specific styles */
25
25
  @layer components {
26
26
  .focus-yellow {
27
- @apply focus:outline focus:outline-[3px] focus:outline-[#ffbf47] focus:border-[#ffbf47] z-20;
27
+ @apply focus:border-[#ffbf47] focus:outline focus:outline-[3px] focus:outline-[#ffbf47];
28
28
  }
29
29
 
30
30
  .library-button {
@@ -16,8 +16,8 @@
16
16
  .ol-layer-switcher-panel button:focus,
17
17
  .ol-attribution ul li a:focus,
18
18
  .ol-attribution ul li a:focus-visible,
19
- .ol-geocoder ul.gcd-txt-result>li>a:focus,
20
- .ol-geocoder ul.gcd-txt-result>li>a:focus-visible {
19
+ .ol-geocoder ul.gcd-txt-result > li > a:focus,
20
+ .ol-geocoder ul.gcd-txt-result > li > a:focus-visible {
21
21
  outline: 3px solid #ffbf47 !important;
22
22
  border-color: #ffbf47 !important;
23
23
  z-index: 2;
@@ -32,7 +32,9 @@
32
32
 
33
33
  .ol-geocoder .gcd-txt-input:focus,
34
34
  .ol-geocoder .gcd-txt-input:focus-visible {
35
- box-shadow: inset 0 0 0 1px #ffbf47, inset 0 0 6px #ffbf47;
35
+ box-shadow:
36
+ inset 0 0 0 1px #ffbf47,
37
+ inset 0 0 6px #ffbf47;
36
38
  }
37
39
 
38
40
  /* Zoom control container */
@@ -57,7 +59,10 @@
57
59
  line-height: 1;
58
60
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.08);
59
61
  cursor: pointer;
60
- transition: border-color 0.2s, box-shadow 0.2s, background 0.2s;
62
+ transition:
63
+ border-color 0.2s,
64
+ box-shadow 0.2s,
65
+ background 0.2s;
61
66
  margin: 0;
62
67
  padding: 0;
63
68
  display: flex;
@@ -95,7 +100,10 @@
95
100
  line-height: 1;
96
101
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.08);
97
102
  cursor: pointer;
98
- transition: border-color 0.2s, box-shadow 0.2s, background 0.2s;
103
+ transition:
104
+ border-color 0.2s,
105
+ box-shadow 0.2s,
106
+ background 0.2s;
99
107
  margin: 0;
100
108
  padding: 0;
101
109
  display: flex;
@@ -213,11 +221,12 @@
213
221
  top: unset !important;
214
222
  }
215
223
 
216
- .ol-geocoder ul.gcd-txt-result>li:nth-child(odd),
217
- .ol-geocoder ul.gcd-txt-result>li:nth-child(even) { background-color: #fff;
224
+ .ol-geocoder ul.gcd-txt-result > li:nth-child(odd),
225
+ .ol-geocoder ul.gcd-txt-result > li:nth-child(even) {
226
+ background-color: #fff;
218
227
  background-color: #fff;
219
228
  }
220
229
 
221
- .ol-geocoder ul.gcd-txt-result>li>a:hover {
230
+ .ol-geocoder ul.gcd-txt-result > li > a:hover {
222
231
  background-color: #f3f2f1;
223
232
  }
@@ -1,3 +1,5 @@
1
+ 'use client';
2
+
1
3
  import { Link } from '../link/Link';
2
4
 
3
5
  export type SkipLinkProps = {
@@ -5,12 +7,30 @@ export type SkipLinkProps = {
5
7
  };
6
8
 
7
9
  export const SkipLink = ({ mainContentId = 'main-content' }: SkipLinkProps) => {
10
+ const handleActivate = () => {
11
+ // Let the browser scroll first, then move focus
12
+ setTimeout(() => {
13
+ const main = document.getElementById(mainContentId);
14
+
15
+ if (main) {
16
+ main.focus();
17
+ }
18
+ }, 0);
19
+ };
20
+
8
21
  return (
9
22
  <nav aria-label="Skip navigation">
10
23
  <Link
11
24
  className="bg-focus focus:relative focus:top-0 w-full absolute -top-full text-black
12
25
  visited:text-black hover:text-black p-3 skip-link"
13
26
  href={`#${mainContentId}`}
27
+ onClick={handleActivate}
28
+ onKeyDown={(event) => {
29
+ if (event.key === 'Enter' || event.key === ' ') {
30
+ event.preventDefault(); // Prevent default scroll/jump
31
+ handleActivate();
32
+ }
33
+ }}
14
34
  >
15
35
  Skip to main content
16
36
  </Link>
package/src/map/index.ts CHANGED
@@ -5,6 +5,8 @@ export * from './LayerSwitcherControl';
5
5
  export * from './utils';
6
6
  export * from './MapComponent';
7
7
  export * from './MapContext';
8
+ export * from './useKeyboardDrawing';
9
+ export * from './useVirtualCursor';
8
10
  export * from './osOpenNamesSearch';
9
11
  export * from './Popup';
10
12
  export * from './projections';
@@ -0,0 +1,114 @@
1
+ import { useCallback, useEffect, useState } from 'react';
2
+
3
+ import { Map as OlMap } from 'ol';
4
+ import { type Coordinate } from 'ol/coordinate';
5
+
6
+ import { useVirtualCursor } from './useVirtualCursor';
7
+ import { KeyboardKeys } from './utils';
8
+
9
+ type UseKeyboardDrawingProps = {
10
+ map: OlMap | undefined;
11
+ isDrawing: boolean;
12
+ addVertex: (coord: Coordinate) => void;
13
+ finishDrawing: () => void;
14
+ cancelDrawing: () => void;
15
+ };
16
+
17
+ const DELTA = 20; // Base movement in pixels
18
+
19
+ export const useKeyboardDrawing = ({
20
+ map,
21
+ isDrawing,
22
+ addVertex,
23
+ finishDrawing,
24
+ cancelDrawing,
25
+ }: UseKeyboardDrawingProps) => {
26
+ const [cursorPosition, setCursorPosition] = useState<Coordinate | null>(null);
27
+
28
+ // Integrate the virtual cursor overlay
29
+ useVirtualCursor(map, isDrawing, cursorPosition);
30
+
31
+ // Initialize cursor at map center when drawing starts
32
+ useEffect(() => {
33
+ if (isDrawing && map) {
34
+ setCursorPosition(map.getView().getCenter() ?? [0, 0]);
35
+ }
36
+
37
+ if (!isDrawing) {
38
+ setCursorPosition(null);
39
+ }
40
+ }, [isDrawing, map]);
41
+
42
+ // Move the virtual cursor by dx/dy steps
43
+ const moveCursor = useCallback(
44
+ (dx: number, dy: number) => {
45
+ if (!cursorPosition || !map) {
46
+ return;
47
+ }
48
+
49
+ const view = map.getView();
50
+ const resolution = view.getResolution() ?? 1;
51
+ const delta = DELTA * resolution;
52
+
53
+ setCursorPosition([
54
+ cursorPosition[0] + dx * delta,
55
+ cursorPosition[1] - dy * delta, // Invert Y for map coordinates
56
+ ]);
57
+ },
58
+ [cursorPosition, map],
59
+ );
60
+
61
+ // Handle keyboard navigation and update cursor
62
+ useEffect(() => {
63
+ if (!map || !isDrawing) {
64
+ return;
65
+ }
66
+
67
+ const handleKeyDown = (event: KeyboardEvent) => {
68
+ if (!isDrawing) {
69
+ return;
70
+ }
71
+
72
+ switch (event.key) {
73
+ case KeyboardKeys.ArrowUp:
74
+ moveCursor(0, -1);
75
+ event.preventDefault();
76
+ break;
77
+ case KeyboardKeys.ArrowDown:
78
+ moveCursor(0, 1);
79
+ event.preventDefault();
80
+ break;
81
+ case KeyboardKeys.ArrowLeft:
82
+ moveCursor(-1, 0);
83
+ event.preventDefault();
84
+ break;
85
+ case KeyboardKeys.ArrowRight:
86
+ moveCursor(1, 0);
87
+ event.preventDefault();
88
+ break;
89
+ case KeyboardKeys.Enter:
90
+ case KeyboardKeys.Space:
91
+ if (cursorPosition) {
92
+ addVertex(cursorPosition);
93
+ }
94
+
95
+ event.preventDefault();
96
+ break;
97
+ case KeyboardKeys.Escape:
98
+ cancelDrawing();
99
+ event.preventDefault();
100
+ break;
101
+ case KeyboardKeys.F:
102
+ finishDrawing();
103
+ event.preventDefault();
104
+ break;
105
+ default:
106
+ break;
107
+ }
108
+ };
109
+
110
+ window.addEventListener('keydown', handleKeyDown);
111
+
112
+ return () => window.removeEventListener('keydown', handleKeyDown);
113
+ }, [map, isDrawing, cursorPosition, addVertex, finishDrawing, cancelDrawing, moveCursor]);
114
+ };
@@ -0,0 +1,72 @@
1
+ import { useEffect, useRef } from 'react';
2
+
3
+ import { Map as OlMap } from 'ol';
4
+ import { type Coordinate } from 'ol/coordinate';
5
+ import Overlay from 'ol/Overlay';
6
+
7
+ const createCursorElement = (): HTMLDivElement => {
8
+ const virtualCursor = document.createElement('div');
9
+
10
+ virtualCursor.style.width = '18px';
11
+ virtualCursor.style.height = '18px';
12
+ virtualCursor.style.background = 'rgba(255, 193, 7, 0.8)';
13
+ virtualCursor.style.border = '2px solid #ffbf47';
14
+ virtualCursor.style.borderRadius = '50%';
15
+ virtualCursor.style.position = 'absolute';
16
+ virtualCursor.style.transform = 'translate(-50%, -50%)';
17
+ virtualCursor.style.pointerEvents = 'none';
18
+ virtualCursor.setAttribute('aria-hidden', 'true');
19
+
20
+ return virtualCursor;
21
+ };
22
+
23
+ export const useVirtualCursor = (
24
+ map: OlMap | undefined,
25
+ isActive: boolean,
26
+ position: Coordinate | null,
27
+ ) => {
28
+ const overlayRef = useRef<Overlay | null>(null);
29
+
30
+ useEffect(() => {
31
+ if (!map) {
32
+ return;
33
+ }
34
+
35
+ if (!overlayRef.current) {
36
+ const el = createCursorElement();
37
+
38
+ overlayRef.current = new Overlay({
39
+ element: el,
40
+ positioning: 'center-center',
41
+ stopEvent: false,
42
+ });
43
+ map.addOverlay(overlayRef.current);
44
+ }
45
+
46
+ const overlay = overlayRef.current;
47
+ const element = overlay?.getElement();
48
+
49
+ if (element) {
50
+ if (isActive && position) {
51
+ overlay.setPosition(position);
52
+ element.style.display = '';
53
+ } else {
54
+ overlay.setPosition(undefined);
55
+ element.style.display = 'none';
56
+ }
57
+ }
58
+
59
+ return () => {
60
+ if (overlayRef.current) {
61
+ const el = overlayRef.current.getElement();
62
+
63
+ if (el) {
64
+ el.style.display = 'none';
65
+ }
66
+
67
+ map.removeOverlay(overlayRef.current);
68
+ overlayRef.current = null;
69
+ }
70
+ };
71
+ }, [map, isActive, position]);
72
+ };
package/src/map/utils.ts CHANGED
@@ -38,3 +38,14 @@ export const getPopupPositionClass = (coordinate: number[], map: Map): PopupDire
38
38
 
39
39
  return 'bottom-right';
40
40
  };
41
+
42
+ export const KeyboardKeys = {
43
+ ArrowUp: 'ArrowUp',
44
+ ArrowDown: 'ArrowDown',
45
+ ArrowLeft: 'ArrowLeft',
46
+ ArrowRight: 'ArrowRight',
47
+ Enter: 'Enter',
48
+ Space: ' ',
49
+ Escape: 'Escape',
50
+ F: 'f',
51
+ } as const;