mtrl 0.2.8 → 0.3.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 (64) hide show
  1. package/index.ts +4 -0
  2. package/package.json +1 -1
  3. package/src/components/button/button.ts +34 -5
  4. package/src/components/navigation/api.ts +131 -96
  5. package/src/components/navigation/features/controller.ts +273 -0
  6. package/src/components/navigation/features/items.ts +133 -64
  7. package/src/components/navigation/navigation.ts +17 -2
  8. package/src/components/navigation/system/core.ts +302 -0
  9. package/src/components/navigation/system/events.ts +240 -0
  10. package/src/components/navigation/system/index.ts +184 -0
  11. package/src/components/navigation/system/mobile.ts +278 -0
  12. package/src/components/navigation/system/state.ts +77 -0
  13. package/src/components/navigation/system/types.ts +364 -0
  14. package/src/components/slider/config.ts +20 -2
  15. package/src/components/slider/features/controller.ts +737 -0
  16. package/src/components/slider/features/handlers.ts +18 -16
  17. package/src/components/slider/features/index.ts +3 -2
  18. package/src/components/slider/features/range.ts +104 -0
  19. package/src/components/slider/schema.ts +141 -0
  20. package/src/components/slider/slider.ts +34 -13
  21. package/src/components/switch/api.ts +16 -0
  22. package/src/components/switch/config.ts +1 -18
  23. package/src/components/switch/features.ts +198 -0
  24. package/src/components/switch/index.ts +1 -0
  25. package/src/components/switch/switch.ts +3 -3
  26. package/src/components/switch/types.ts +14 -2
  27. package/src/components/textfield/api.ts +53 -0
  28. package/src/components/textfield/features.ts +322 -0
  29. package/src/components/textfield/textfield.ts +8 -0
  30. package/src/components/textfield/types.ts +12 -3
  31. package/src/components/timepicker/clockdial.ts +1 -4
  32. package/src/core/compose/features/textinput.ts +15 -2
  33. package/src/core/composition/features/dom.ts +45 -0
  34. package/src/core/composition/features/icon.ts +131 -0
  35. package/src/core/composition/features/index.ts +12 -0
  36. package/src/core/composition/features/label.ts +155 -0
  37. package/src/core/composition/features/layout.ts +47 -0
  38. package/src/core/composition/index.ts +26 -0
  39. package/src/core/index.ts +1 -1
  40. package/src/core/layout/README.md +350 -0
  41. package/src/core/layout/array.ts +181 -0
  42. package/src/core/layout/create.ts +55 -0
  43. package/src/core/layout/index.ts +26 -0
  44. package/src/core/layout/object.ts +124 -0
  45. package/src/core/layout/processor.ts +58 -0
  46. package/src/core/layout/result.ts +85 -0
  47. package/src/core/layout/types.ts +125 -0
  48. package/src/core/layout/utils.ts +136 -0
  49. package/src/index.ts +1 -0
  50. package/src/styles/abstract/_variables.scss +28 -0
  51. package/src/styles/components/_navigation-mobile.scss +244 -0
  52. package/src/styles/components/_navigation-system.scss +151 -0
  53. package/src/styles/components/_switch.scss +133 -69
  54. package/src/styles/components/_textfield.scss +259 -27
  55. package/demo/build.ts +0 -349
  56. package/demo/index.html +0 -110
  57. package/demo/main.js +0 -448
  58. package/demo/styles.css +0 -239
  59. package/server.ts +0 -86
  60. package/src/components/slider/features/slider.ts +0 -318
  61. package/src/components/slider/features/structure.ts +0 -181
  62. package/src/components/slider/features/ui.ts +0 -388
  63. package/src/components/textfield/constants.ts +0 -100
  64. package/src/core/layout/index.js +0 -95
@@ -0,0 +1,240 @@
1
+ // src/components/navigation/system/events.ts
2
+
3
+ import { NavigationSystemState } from './types';
4
+ import { NavigationItem, NavigationSection } from './types';
5
+
6
+ /**
7
+ * Registers rail navigation event handlers
8
+ *
9
+ * @param state - System state
10
+ * @param config - System configuration
11
+ * @param updateDrawerContent - Function to update drawer content
12
+ * @param showDrawer - Function to show the drawer
13
+ * @param hideDrawer - Function to hide the drawer
14
+ * @param systemApi - Reference to the public system API
15
+ */
16
+ export const registerRailEvents = (
17
+ state: NavigationSystemState,
18
+ config: any,
19
+ updateDrawerContent: (sectionId: string) => void,
20
+ showDrawer: () => void,
21
+ hideDrawer: () => void,
22
+ systemApi: any
23
+ ): void => {
24
+ const rail = state.rail;
25
+ if (!rail) return;
26
+
27
+ // Register for change events - will listen for when rail items are clicked
28
+ rail.on('change', (event: any) => {
29
+ // Extract ID from event data
30
+ const id = event?.id;
31
+
32
+ if (!id || state.processingChange) {
33
+ return;
34
+ }
35
+
36
+ // Check if this is a user action
37
+ const isUserAction = event?.source === 'userAction';
38
+
39
+ // Set processing flag to prevent loops
40
+ state.processingChange = true;
41
+
42
+ // Update active section
43
+ state.activeSection = id;
44
+
45
+ // Handle internally first - update drawer content
46
+ updateDrawerContent(id);
47
+
48
+ // Then notify external handlers
49
+ if (systemApi.onSectionChange && isUserAction) {
50
+ systemApi.onSectionChange(id, { source: isUserAction ? 'userClick' : 'programmatic' });
51
+ }
52
+
53
+ // Clear the processing flag after a delay
54
+ setTimeout(() => {
55
+ state.processingChange = false;
56
+ }, 50);
57
+ });
58
+
59
+ rail.on('mouseover', (event: any) => {
60
+ const id = event?.id;
61
+
62
+ // Set rail mouse state
63
+ state.mouseInRail = true;
64
+
65
+ // Clear any existing hover timer
66
+ clearTimeout(state.hoverTimer as number);
67
+ state.hoverTimer = null;
68
+
69
+ // Only schedule drawer operations if there's an ID
70
+ if (id) {
71
+ // Check if this section has items
72
+ if (state.items[id]?.items?.length > 0) {
73
+ // Has items - schedule drawer opening
74
+ state.hoverTimer = window.setTimeout(() => {
75
+ updateDrawerContent(id);
76
+ }, config.hoverDelay) as unknown as number;
77
+ } else {
78
+ // No items - hide drawer after a delay to prevent flickering
79
+ state.closeTimer = window.setTimeout(() => {
80
+ // Only hide if we're still in the rail but not in the drawer
81
+ if (state.mouseInRail && !state.mouseInDrawer) {
82
+ hideDrawer();
83
+ }
84
+ }, config.hoverDelay) as unknown as number;
85
+ }
86
+ }
87
+ });
88
+
89
+ rail.on('mouseenter', () => {
90
+ state.mouseInRail = true;
91
+
92
+ // Clear any pending drawer close timer when entering rail
93
+ clearTimeout(state.closeTimer as number);
94
+ state.closeTimer = null;
95
+ });
96
+
97
+ rail.on('mouseleave', () => {
98
+ state.mouseInRail = false;
99
+
100
+ // Clear any existing hover timer
101
+ clearTimeout(state.hoverTimer as number);
102
+ state.hoverTimer = null;
103
+
104
+ // Only set timer to hide drawer if we're not in drawer either
105
+ if (!state.mouseInDrawer) {
106
+ state.closeTimer = window.setTimeout(() => {
107
+ // Double-check we're still not in rail or drawer before hiding
108
+ if (!state.mouseInRail && !state.mouseInDrawer) {
109
+ hideDrawer();
110
+ }
111
+ }, config.closeDelay) as unknown as number;
112
+ }
113
+ });
114
+ };
115
+
116
+ /**
117
+ * Registers drawer navigation event handlers
118
+ *
119
+ * @param state - System state
120
+ * @param config - System configuration
121
+ * @param hideDrawer - Function to hide the drawer
122
+ * @param systemApi - Reference to the public system API
123
+ */
124
+ export const registerDrawerEvents = (
125
+ state: NavigationSystemState,
126
+ config: any,
127
+ hideDrawer: () => void,
128
+ systemApi: any
129
+ ): void => {
130
+ const drawer = state.drawer;
131
+ if (!drawer) return;
132
+
133
+ // Use the component's native event system
134
+ if (typeof drawer.on === 'function') {
135
+ // Handle item selection
136
+ drawer.on('change', (event: any) => {
137
+ const id = event.id;
138
+
139
+ state.activeSubsection = id;
140
+
141
+ // If configuration specifies to hide drawer on click, do so
142
+ if (config.hideDrawerOnClick) {
143
+ hideDrawer();
144
+ }
145
+
146
+ // Emit item selection event
147
+ if (systemApi.onItemSelect) {
148
+ systemApi.onItemSelect(event);
149
+ }
150
+ });
151
+
152
+ // Handle mouseenter/mouseleave for drawer
153
+ drawer.on('mouseenter', () => {
154
+ state.mouseInDrawer = true;
155
+
156
+ // Clear any hover and close timers
157
+ clearTimeout(state.hoverTimer as number);
158
+ clearTimeout(state.closeTimer as number);
159
+ state.hoverTimer = null;
160
+ state.closeTimer = null;
161
+ });
162
+
163
+ drawer.on('mouseleave', () => {
164
+ state.mouseInDrawer = false;
165
+
166
+ // Only set timer to hide drawer if we're not in rail
167
+ if (!state.mouseInRail) {
168
+ state.closeTimer = window.setTimeout(() => {
169
+ // Double-check we're still not in drawer or rail before hiding
170
+ if (!state.mouseInDrawer && !state.mouseInRail) {
171
+ hideDrawer();
172
+ }
173
+ }, config.closeDelay) as unknown as number;
174
+ }
175
+ });
176
+ }
177
+ };
178
+
179
+ /**
180
+ * Sets up window resize and orientation change handling
181
+ *
182
+ * @param state - System state
183
+ * @param checkMobileState - Function to check and update mobile state
184
+ */
185
+ export const setupResponsiveHandling = (
186
+ state: NavigationSystemState,
187
+ checkMobileState: () => void
188
+ ): void => {
189
+ // Setup responsive behavior
190
+ if (window.ResizeObserver) {
191
+ // Use ResizeObserver for better performance
192
+ state.resizeObserver = new ResizeObserver(() => {
193
+ checkMobileState();
194
+ });
195
+ state.resizeObserver.observe(document.body);
196
+ } else {
197
+ // Fallback to window resize event
198
+ window.addEventListener('resize', checkMobileState);
199
+ }
200
+
201
+ // Listen for orientation changes on mobile
202
+ window.addEventListener('orientationchange', () => {
203
+ // Small delay to ensure dimensions have updated
204
+ setTimeout(checkMobileState, 100);
205
+ });
206
+ };
207
+
208
+ /**
209
+ * Cleans up all event handlers and resources
210
+ *
211
+ * @param state - System state
212
+ * @param checkMobileState - Function reference to remove event handlers
213
+ */
214
+ export const cleanupEvents = (
215
+ state: NavigationSystemState,
216
+ checkMobileState: () => void
217
+ ): void => {
218
+ // Clean up resize observer
219
+ if (state.resizeObserver) {
220
+ state.resizeObserver.disconnect();
221
+ state.resizeObserver = null;
222
+ } else {
223
+ window.removeEventListener('resize', checkMobileState);
224
+ }
225
+
226
+ // Remove orientation change listener
227
+ window.removeEventListener('orientationchange', checkMobileState);
228
+
229
+ // Remove outside click handler
230
+ if (state.outsideClickHandler) {
231
+ const eventType = ('ontouchend' in window) ? 'touchend' : 'click';
232
+ document.removeEventListener(eventType, state.outsideClickHandler);
233
+ }
234
+
235
+ // Clear timers
236
+ clearTimeout(state.hoverTimer as number);
237
+ clearTimeout(state.closeTimer as number);
238
+ state.hoverTimer = null;
239
+ state.closeTimer = null;
240
+ };
@@ -0,0 +1,184 @@
1
+ // src/components/navigation/system/index.ts
2
+
3
+ import {
4
+ NavigationSystemConfig,
5
+ NavigationSystemState,
6
+ NavigationSystem,
7
+ ViewChangeEvent
8
+ } from './types';
9
+
10
+ import {
11
+ createInitialState,
12
+ createConfig,
13
+ createMobileConfig
14
+ } from './state';
15
+
16
+ import {
17
+ createRailNavigation,
18
+ createDrawerNavigation,
19
+ updateDrawerContent,
20
+ showDrawer as showDrawerCore,
21
+ hideDrawer as hideDrawerCore,
22
+ isDrawerVisible as isDrawerVisibleCore,
23
+ checkMobileState as checkMobileStateCore,
24
+ cleanupResources,
25
+ navigateTo as navigateToCore
26
+ } from './core';
27
+
28
+ import {
29
+ registerRailEvents,
30
+ registerDrawerEvents,
31
+ setupResponsiveHandling,
32
+ cleanupEvents
33
+ } from './events';
34
+
35
+ import {
36
+ setupMobileMode as setupMobileModeCore,
37
+ teardownMobileMode
38
+ } from './mobile';
39
+
40
+ /**
41
+ * Creates a complete navigation system with synchronized rail and drawer components
42
+ *
43
+ * @param options - System configuration options
44
+ * @returns Navigation system API
45
+ */
46
+ export const createNavigationSystem = (options: NavigationSystemConfig = {}): NavigationSystem => {
47
+ // Initialize state and configuration
48
+ const state = createInitialState(options);
49
+ const config = createConfig(options);
50
+ const mobileConfig = createMobileConfig(options);
51
+
52
+ // Create system API object with placeholders
53
+ const system: NavigationSystem = {
54
+ initialize: () => system,
55
+ cleanup: () => {},
56
+ navigateTo: () => {},
57
+ getRail: () => state.rail,
58
+ getDrawer: () => state.drawer,
59
+ getActiveSection: () => state.activeSection,
60
+ getActiveSubsection: () => state.activeSubsection,
61
+ showDrawer: () => {},
62
+ hideDrawer: () => {},
63
+ isDrawerVisible: () => false,
64
+ configure: () => system,
65
+ setProcessingChange: () => {},
66
+ isProcessingChange: () => false,
67
+ isMobile: () => state.isMobile,
68
+ checkMobileState: () => {},
69
+ onSectionChange: undefined,
70
+ onItemSelect: undefined,
71
+ onViewChange: undefined
72
+ };
73
+
74
+ // Implementation functions that use the state
75
+ const showDrawer = () => showDrawerCore(state, mobileConfig);
76
+ const hideDrawer = () => hideDrawerCore(state, mobileConfig);
77
+ const isDrawerVisible = () => isDrawerVisibleCore(state);
78
+
79
+ const updateDrawerContentWrapper = (sectionId: string) => {
80
+ updateDrawerContent(state, sectionId, showDrawer, hideDrawer);
81
+ };
82
+
83
+ const setupMobileMode = () => {
84
+ setupMobileModeCore(
85
+ state,
86
+ mobileConfig,
87
+ hideDrawer,
88
+ isDrawerVisible,
89
+ showDrawer
90
+ );
91
+ };
92
+
93
+ const checkMobileState = () => {
94
+ checkMobileStateCore(
95
+ state,
96
+ mobileConfig,
97
+ setupMobileMode,
98
+ () => teardownMobileMode(state, mobileConfig),
99
+ system
100
+ );
101
+ };
102
+
103
+ const navigateTo = (section: string, subsection?: string, silent?: boolean) => {
104
+ navigateToCore(state, section, subsection, silent);
105
+ };
106
+
107
+ // Implementation of the initialize method
108
+ const initialize = (): NavigationSystem => {
109
+ // Create rail component
110
+ state.rail = createRailNavigation(state, config);
111
+
112
+ // Create drawer component
113
+ state.drawer = createDrawerNavigation(state, config);
114
+
115
+ // Register event handlers
116
+ registerRailEvents(state, config, updateDrawerContentWrapper, showDrawer, hideDrawer, system);
117
+ registerDrawerEvents(state, config, hideDrawer, system);
118
+
119
+ // Set up responsive behavior
120
+ setupResponsiveHandling(state, checkMobileState);
121
+
122
+ // Set active section if specified
123
+ if (options.activeSection && state.items[options.activeSection]) {
124
+ state.activeSection = options.activeSection;
125
+
126
+ if (state.rail) {
127
+ state.rail.setActive(options.activeSection);
128
+ }
129
+
130
+ // Update drawer content without showing it
131
+ updateDrawerContentWrapper(options.activeSection);
132
+
133
+ // Only show drawer if expanded is explicitly true
134
+ if (options.expanded === true) {
135
+ showDrawer();
136
+ } else {
137
+ // Explicitly ensure drawer is hidden
138
+ hideDrawer();
139
+ }
140
+ }
141
+
142
+ // Check initial mobile state
143
+ checkMobileState();
144
+
145
+ return system;
146
+ };
147
+
148
+ // Implementation of the cleanup method
149
+ const cleanup = (): void => {
150
+ cleanupEvents(state, checkMobileState);
151
+ cleanupResources(state);
152
+ };
153
+
154
+ // Configure method implementation
155
+ const configure = (newConfig: Partial<NavigationSystemConfig>): NavigationSystem => {
156
+ Object.assign(options, newConfig);
157
+ Object.assign(config, createConfig({...options, ...newConfig}));
158
+ Object.assign(mobileConfig, createMobileConfig({...options, ...newConfig}));
159
+ return system;
160
+ };
161
+
162
+ // Assign implementations to system object
163
+ system.initialize = initialize;
164
+ system.cleanup = cleanup;
165
+ system.navigateTo = navigateTo;
166
+ system.showDrawer = showDrawer;
167
+ system.hideDrawer = hideDrawer;
168
+ system.isDrawerVisible = isDrawerVisible;
169
+ system.configure = configure;
170
+ system.setProcessingChange = (isProcessing: boolean) => {
171
+ state.processingChange = isProcessing;
172
+ };
173
+ system.isProcessingChange = () => state.processingChange;
174
+ system.isMobile = () => state.isMobile;
175
+ system.checkMobileState = checkMobileState;
176
+
177
+ // Return the uninitialized system
178
+ return system;
179
+ };
180
+
181
+ export default createNavigationSystem;
182
+
183
+ // Re-export types for external use
184
+ export * from './types';
@@ -0,0 +1,278 @@
1
+ // src/components/navigation/system/mobile.ts
2
+
3
+ import { NavigationSystemState } from './types';
4
+ import {
5
+ hasTouchSupport,
6
+ normalizeEvent,
7
+ TOUCH_CONFIG,
8
+ TOUCH_TARGETS
9
+ } from '../../../core/utils/mobile';
10
+
11
+ /**
12
+ * Creates and appends overlay element for mobile
13
+ *
14
+ * @param state - System state
15
+ * @param mobileConfig - Mobile configuration
16
+ * @param hideDrawer - Function to hide the drawer
17
+ * @returns Overlay element
18
+ */
19
+ export const createOverlay = (
20
+ state: NavigationSystemState,
21
+ mobileConfig: any,
22
+ hideDrawer: () => void
23
+ ): HTMLElement => {
24
+ if (state.overlayElement) return state.overlayElement;
25
+
26
+ state.overlayElement = document.createElement('div');
27
+ state.overlayElement.className = mobileConfig.overlayClass;
28
+ state.overlayElement.setAttribute('aria-hidden', 'true');
29
+ document.body.appendChild(state.overlayElement);
30
+
31
+ state.overlayElement.addEventListener('click', (event) => {
32
+ if (event.target === state.overlayElement) {
33
+ hideDrawer();
34
+ }
35
+ });
36
+
37
+ return state.overlayElement;
38
+ };
39
+
40
+ /**
41
+ * Creates and adds close button to the drawer
42
+ *
43
+ * @param state - System state
44
+ * @param mobileConfig - Mobile configuration
45
+ * @param hideDrawer - Function to hide the drawer
46
+ * @returns Close button element
47
+ */
48
+ export const createCloseButton = (
49
+ state: NavigationSystemState,
50
+ mobileConfig: any,
51
+ hideDrawer: () => void
52
+ ): HTMLElement | null => {
53
+ if (!state.drawer || state.closeButtonElement) return null;
54
+
55
+ state.closeButtonElement = document.createElement('button');
56
+ state.closeButtonElement.className = mobileConfig.closeButtonClass;
57
+ state.closeButtonElement.setAttribute('aria-label', 'Close navigation');
58
+ state.closeButtonElement.innerHTML = `
59
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
60
+ <line x1="18" y1="6" x2="6" y2="18"></line>
61
+ <line x1="6" y1="6" x2="18" y2="18"></line>
62
+ </svg>
63
+ `;
64
+
65
+ // Handle click event
66
+ state.closeButtonElement.addEventListener('click', () => {
67
+ hideDrawer();
68
+ });
69
+
70
+ // Apply touch-friendly styles if needed
71
+ if (hasTouchSupport() && mobileConfig.optimizeForTouch) {
72
+ state.closeButtonElement.style.minWidth = `${TOUCH_TARGETS.COMFORTABLE}px`;
73
+ state.closeButtonElement.style.minHeight = `${TOUCH_TARGETS.COMFORTABLE}px`;
74
+
75
+ // Add touch feedback
76
+ state.closeButtonElement.addEventListener('touchstart', () => {
77
+ state.closeButtonElement.classList.add('active');
78
+ }, { passive: true });
79
+
80
+ state.closeButtonElement.addEventListener('touchend', () => {
81
+ setTimeout(() => {
82
+ state.closeButtonElement.classList.remove('active');
83
+ }, TOUCH_CONFIG.FEEDBACK_DURATION);
84
+ }, { passive: true });
85
+ }
86
+
87
+ state.drawer.element.appendChild(state.closeButtonElement);
88
+ return state.closeButtonElement;
89
+ };
90
+
91
+ /**
92
+ * Sets up mobile mode features
93
+ *
94
+ * @param state - System state
95
+ * @param mobileConfig - Mobile configuration
96
+ * @param hideDrawer - Function to hide the drawer
97
+ * @param isDrawerVisible - Function to check if drawer is visible
98
+ */
99
+ export const setupMobileMode = (
100
+ state: NavigationSystemState,
101
+ mobileConfig: any,
102
+ hideDrawer: () => void,
103
+ isDrawerVisible: () => boolean
104
+ ): void => {
105
+ const drawer = state.drawer;
106
+ const rail = state.rail;
107
+
108
+ if (!drawer || !rail) return;
109
+
110
+ // Create mobile UI elements
111
+ createOverlay(state, mobileConfig, hideDrawer);
112
+ createCloseButton(state, mobileConfig, hideDrawer);
113
+
114
+ // Setup outside click handling
115
+ setupOutsideClickHandling(state, mobileConfig, hideDrawer, isDrawerVisible);
116
+
117
+ // Setup touch gestures if enabled
118
+ if (mobileConfig.enableSwipeGestures && hasTouchSupport()) {
119
+ setupTouchGestures(state, hideDrawer, isDrawerVisible);
120
+ }
121
+
122
+ // Hide drawer initially in mobile mode
123
+ hideDrawer();
124
+ };
125
+
126
+ /**
127
+ * Sets up outside click handling for mobile
128
+ *
129
+ * @param state - System state
130
+ * @param mobileConfig - Mobile configuration
131
+ * @param hideDrawer - Function to hide the drawer
132
+ * @param isDrawerVisible - Function to check if drawer is visible
133
+ */
134
+ export const setupOutsideClickHandling = (
135
+ state: NavigationSystemState,
136
+ mobileConfig: any,
137
+ hideDrawer: () => void,
138
+ isDrawerVisible: () => boolean
139
+ ): void => {
140
+ if (!mobileConfig.hideOnClickOutside) return;
141
+
142
+ // Only set up once
143
+ if (state.outsideClickHandlerSet) return;
144
+ state.outsideClickHandlerSet = true;
145
+
146
+ // Use either click or touchend event depending on device capability
147
+ const eventType = hasTouchSupport() ? 'touchend' : 'click';
148
+
149
+ // The handler function
150
+ const handleOutsideClick = (event: Event) => {
151
+ if (!state.isMobile || !isDrawerVisible()) return;
152
+
153
+ const normalizedEvent = normalizeEvent(event);
154
+ const target = normalizedEvent.target as HTMLElement;
155
+
156
+ // Don't close if clicking on drawer, rail, or excluded elements
157
+ if (state.drawer.element.contains(target) ||
158
+ state.rail.element.contains(target)) {
159
+ return;
160
+ }
161
+
162
+ // Close drawer - it's an outside click/touch
163
+ hideDrawer();
164
+ };
165
+
166
+ // Store handler for cleanup
167
+ state.outsideClickHandler = handleOutsideClick;
168
+
169
+ // Add listener
170
+ document.addEventListener(eventType, handleOutsideClick,
171
+ hasTouchSupport() ? { passive: true } : false);
172
+ };
173
+
174
+ /**
175
+ * Sets up touch gestures for mobile
176
+ *
177
+ * @param state - System state
178
+ * @param hideDrawer - Function to hide the drawer
179
+ * @param isDrawerVisible - Function to check if drawer is visible
180
+ */
181
+ export const setupTouchGestures = (
182
+ state: NavigationSystemState,
183
+ hideDrawer: () => void,
184
+ isDrawerVisible: () => boolean,
185
+ showDrawer?: () => void
186
+ ): void => {
187
+ const drawer = state.drawer;
188
+ const rail = state.rail;
189
+
190
+ if (!drawer || !rail) return;
191
+
192
+ let touchStartX = 0;
193
+ let touchStartY = 0;
194
+
195
+ // Rail swipe right to open drawer
196
+ rail.element.addEventListener('touchstart', (event: TouchEvent) => {
197
+ const touch = event.touches[0];
198
+ touchStartX = touch.clientX;
199
+ touchStartY = touch.clientY;
200
+ }, { passive: true });
201
+
202
+ rail.element.addEventListener('touchmove', (event: TouchEvent) => {
203
+ if (!state.isMobile || isDrawerVisible() || !showDrawer) return;
204
+
205
+ const touch = event.touches[0];
206
+ const deltaX = touch.clientX - touchStartX;
207
+ const deltaY = touch.clientY - touchStartY;
208
+
209
+ // Only consider horizontal swipes
210
+ if (Math.abs(deltaX) > Math.abs(deltaY) &&
211
+ deltaX > TOUCH_CONFIG.SWIPE_THRESHOLD) {
212
+ showDrawer();
213
+ }
214
+ }, { passive: true });
215
+
216
+ // Drawer swipe left to close
217
+ drawer.element.addEventListener('touchstart', (event: TouchEvent) => {
218
+ const touch = event.touches[0];
219
+ touchStartX = touch.clientX;
220
+ touchStartY = touch.clientY;
221
+ }, { passive: true });
222
+
223
+ // Use touchmove with transform for visual feedback
224
+ drawer.element.addEventListener('touchmove', (event: TouchEvent) => {
225
+ if (!state.isMobile || !isDrawerVisible()) return;
226
+
227
+ const touch = event.touches[0];
228
+ const deltaX = touch.clientX - touchStartX;
229
+
230
+ // Only apply transform for leftward swipes
231
+ if (deltaX < 0) {
232
+ // Apply transform with resistance
233
+ drawer.element.style.transform = `translateX(${deltaX / 2}px)`;
234
+
235
+ // Close if threshold reached
236
+ if (deltaX < -TOUCH_CONFIG.SWIPE_THRESHOLD) {
237
+ hideDrawer();
238
+ }
239
+ }
240
+ }, { passive: true });
241
+
242
+ // Reset transforms when touch ends
243
+ drawer.element.addEventListener('touchend', () => {
244
+ if (drawer.element.style.transform) {
245
+ drawer.element.style.transition = 'transform 0.2s ease';
246
+ drawer.element.style.transform = '';
247
+
248
+ setTimeout(() => {
249
+ drawer.element.style.transition = '';
250
+ }, 200);
251
+ }
252
+ }, { passive: true });
253
+ };
254
+
255
+ /**
256
+ * Tears down mobile-specific features
257
+ *
258
+ * @param state - System state
259
+ * @param mobileConfig - Mobile configuration
260
+ */
261
+ export const teardownMobileMode = (
262
+ state: NavigationSystemState,
263
+ mobileConfig: any
264
+ ): void => {
265
+ // Hide overlay
266
+ if (state.overlayElement) {
267
+ state.overlayElement.classList.remove('active');
268
+ state.overlayElement.setAttribute('aria-hidden', 'true');
269
+ }
270
+
271
+ // Hide close button
272
+ if (state.closeButtonElement) {
273
+ state.closeButtonElement.style.display = 'none';
274
+ }
275
+
276
+ // Remove body scroll lock if applied
277
+ document.body.classList.remove(mobileConfig.bodyLockClass);
278
+ };