@hortonstudio/main 1.2.14 → 1.2.18

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,214 @@
1
+ export const init = () => {
2
+ // Find all dropdown wrappers
3
+ const dropdownWrappers = document.querySelectorAll('[data-hs-nav-dropdown="wrapper"]');
4
+
5
+ // Global array to track all dropdown instances
6
+ const allDropdowns = [];
7
+
8
+ // Function to close all dropdowns except the specified one
9
+ const closeAllDropdowns = (exceptWrapper = null) => {
10
+ allDropdowns.forEach(dropdown => {
11
+ if (dropdown.wrapper !== exceptWrapper && dropdown.isOpen) {
12
+ dropdown.closeDropdown();
13
+ }
14
+ });
15
+ };
16
+
17
+ dropdownWrappers.forEach(wrapper => {
18
+ const animationDuration = 0.3;
19
+
20
+ // Find elements within this wrapper
21
+ const toggle = wrapper.querySelector('a'); // the toggle link
22
+ const list = wrapper.querySelector('[data-hs-nav-dropdown="list"]');
23
+ const contain = list.querySelector('[data-hs-nav-dropdown="container"]');
24
+ const arrow = toggle.querySelector('[data-hs-nav-dropdown="arrow"]');
25
+ const text = toggle.querySelector('[data-hs-nav-dropdown="text"]'); // find the text element
26
+
27
+ // Set initial states with GSAP
28
+ gsap.set(contain, { yPercent: -110 });
29
+ gsap.set(list, { display: 'none' });
30
+ gsap.set(arrow, { rotation: 0, scale: 1, x: 0, color: '' });
31
+ gsap.set(text, { scale: 1, color: '' }); // empty string = default color
32
+
33
+ // Track if dropdown is open
34
+ let isOpen = false;
35
+ let currentTimeline = null;
36
+
37
+ // Open animation
38
+ function openDropdown() {
39
+ if (isOpen) return;
40
+
41
+ // Kill any existing timeline
42
+ if (currentTimeline) {
43
+ currentTimeline.kill();
44
+ }
45
+
46
+ // Close all other dropdowns first
47
+ closeAllDropdowns(wrapper);
48
+
49
+ isOpen = true;
50
+
51
+ // Update ARIA states
52
+ toggle.setAttribute('aria-expanded', 'true');
53
+ list.setAttribute('aria-hidden', 'false');
54
+
55
+ // GSAP animation
56
+ currentTimeline = gsap.timeline();
57
+ currentTimeline.set(list, { display: 'flex' })
58
+ .to(contain, {
59
+ yPercent: 0,
60
+ duration: animationDuration,
61
+ ease: 'ease'
62
+ }, 0)
63
+ .to(arrow, {
64
+ rotation: 90,
65
+ scale: 1.2,
66
+ x: 4,
67
+ color: 'var(--swatch--brand)',
68
+ duration: animationDuration,
69
+ ease: 'ease'
70
+ }, 0)
71
+ .to(text, {
72
+ scale: 1.1,
73
+ color: 'var(--swatch--brand)',
74
+ duration: animationDuration,
75
+ ease: 'ease'
76
+ }, 0);
77
+ }
78
+
79
+ // Close animation
80
+ function closeDropdown() {
81
+ if (!isOpen) return;
82
+
83
+ // Kill any existing timeline
84
+ if (currentTimeline) {
85
+ currentTimeline.kill();
86
+ }
87
+
88
+ // Check if focus should be restored to toggle
89
+ const shouldRestoreFocus = list.contains(document.activeElement);
90
+
91
+ isOpen = false;
92
+ currentMenuItemIndex = -1;
93
+
94
+ // Update ARIA states
95
+ toggle.setAttribute('aria-expanded', 'false');
96
+ list.setAttribute('aria-hidden', 'true');
97
+
98
+ // Temporarily remove role="menu" to help screen readers understand menu is closed
99
+ const originalRole = list.getAttribute('role');
100
+ list.removeAttribute('role');
101
+
102
+ // GSAP animation
103
+ currentTimeline = gsap.timeline();
104
+ currentTimeline.to(contain, {
105
+ yPercent: -110,
106
+ duration: animationDuration,
107
+ ease: 'ease'
108
+ }, 0)
109
+ .to(arrow, {
110
+ rotation: 0,
111
+ scale: 1,
112
+ x: 0,
113
+ color: '', // back to default color
114
+ duration: animationDuration,
115
+ ease: 'ease'
116
+ }, 0)
117
+ .to(text, {
118
+ scale: 1,
119
+ color: '', // back to default color
120
+ duration: animationDuration,
121
+ ease: 'ease'
122
+ }, 0)
123
+ .set(list, { display: 'none' })
124
+ .call(() => {
125
+ // Restore role after animation completes
126
+ list.setAttribute('role', originalRole || 'menu');
127
+ });
128
+
129
+ // Restore focus to toggle only if focus was inside dropdown
130
+ if (shouldRestoreFocus) {
131
+ // Small delay to ensure screen reader announces the state change
132
+ setTimeout(() => {
133
+ toggle.focus();
134
+ }, 50);
135
+ }
136
+ }
137
+
138
+ // Get all menu items for navigation
139
+ const menuItems = list.querySelectorAll('a, button, [role="menuitem"]');
140
+ let currentMenuItemIndex = -1;
141
+
142
+ // Hover events
143
+ toggle.addEventListener('mouseenter', openDropdown);
144
+ wrapper.addEventListener('mouseleave', closeDropdown);
145
+
146
+ // Arrow key navigation within dropdown
147
+ list.addEventListener('keydown', function(e) {
148
+ if (!isOpen) return;
149
+
150
+ if (e.key === 'ArrowDown') {
151
+ e.preventDefault();
152
+ currentMenuItemIndex = (currentMenuItemIndex + 1) % menuItems.length;
153
+ menuItems[currentMenuItemIndex].focus();
154
+ } else if (e.key === 'ArrowUp') {
155
+ e.preventDefault();
156
+ currentMenuItemIndex = currentMenuItemIndex <= 0 ? menuItems.length - 1 : currentMenuItemIndex - 1;
157
+ menuItems[currentMenuItemIndex].focus();
158
+ } else if (e.key === 'Escape') {
159
+ e.preventDefault();
160
+ closeDropdown();
161
+ toggle.focus();
162
+ }
163
+ });
164
+
165
+ // Keyboard events for toggle
166
+ toggle.addEventListener('keydown', function(e) {
167
+ if (e.key === 'ArrowDown') {
168
+ e.preventDefault();
169
+ openDropdown();
170
+ // Focus first menu item after opening
171
+ if (menuItems.length > 0) {
172
+ currentMenuItemIndex = 0;
173
+ setTimeout(() => menuItems[0].focus(), 50);
174
+ }
175
+ } else if (e.key === ' ') {
176
+ e.preventDefault();
177
+ // Simple toggle: if closed open, if open close
178
+ if (isOpen) {
179
+ closeDropdown();
180
+ } else {
181
+ openDropdown();
182
+ }
183
+ } else if (e.key === 'ArrowUp' || e.key === 'Escape') {
184
+ e.preventDefault();
185
+ closeDropdown();
186
+ }
187
+ });
188
+
189
+ // Close dropdown when clicking outside
190
+ document.addEventListener('click', function(e) {
191
+ if (!wrapper.contains(e.target) && isOpen) {
192
+ closeDropdown();
193
+ }
194
+ });
195
+
196
+ // Add this dropdown instance to the global array
197
+ allDropdowns.push({
198
+ wrapper,
199
+ isOpen: () => isOpen,
200
+ closeDropdown
201
+ });
202
+ });
203
+
204
+ // Global focus management - close dropdown when tab focus moves outside
205
+ document.addEventListener('focusin', function(e) {
206
+ allDropdowns.forEach(dropdown => {
207
+ if (dropdown.isOpen() && !dropdown.wrapper.contains(e.target)) {
208
+ dropdown.closeDropdown();
209
+ }
210
+ });
211
+ });
212
+
213
+ return { result: 'navbar initialized' };
214
+ };
@@ -0,0 +1,29 @@
1
+ export async function init() {
2
+ const progressBar = document.querySelector('[data-hs-progress="bar"]');
3
+ const progressContent = document.querySelector('[data-hs-progress="wrapper"]');
4
+
5
+ // Check if elements exist before using them
6
+ if (!progressBar || !progressContent) {
7
+ return {
8
+ result: 'util-scroll-progress initialized'
9
+ };
10
+ }
11
+
12
+ gsap.set(progressBar, { width: "0%" });
13
+
14
+ // Create the scroll progress animation
15
+ gsap.to(progressBar, {
16
+ width: "100%",
17
+ ease: "none",
18
+ scrollTrigger: {
19
+ trigger: progressContent,
20
+ start: "top bottom",
21
+ end: "bottom bottom",
22
+ scrub: true
23
+ }
24
+ });
25
+
26
+ return {
27
+ result: 'util-scroll-progress initialized'
28
+ };
29
+ }
package/utils/toc.js ADDED
@@ -0,0 +1,77 @@
1
+ export async function init() {
2
+ const contentArea = document.querySelector('[data-hs-toc="content"]');
3
+ const tocList = document.querySelector('[data-hs-toc="list"]');
4
+
5
+ // Check main elements
6
+ if (!contentArea) { return; }
7
+ if (!tocList) { return; }
8
+ if (tocList.children.length === 0) { return; }
9
+
10
+ const template = tocList.children[0];
11
+ tocList.innerHTML = '';
12
+ const h2Headings = contentArea.querySelectorAll('h2');
13
+
14
+ // Create sections and wrap content
15
+ h2Headings.forEach((heading, index) => {
16
+ const sectionId = heading.textContent.toLowerCase()
17
+ .replace(/[^a-z0-9]+/g, '-')
18
+ .replace(/(^-|-$)/g, '');
19
+ const section = document.createElement('div');
20
+ section.id = sectionId;
21
+ heading.parentNode.insertBefore(section, heading);
22
+ section.appendChild(heading);
23
+ let nextElement = section.nextElementSibling;
24
+ while (nextElement && nextElement.tagName !== 'H2') {
25
+ const elementToMove = nextElement;
26
+ nextElement = nextElement.nextElementSibling;
27
+ section.appendChild(elementToMove);
28
+ }
29
+ });
30
+
31
+ // Create TOC entries
32
+ h2Headings.forEach((heading, index) => {
33
+ const tocItem = template.cloneNode(true);
34
+ const link = tocItem.querySelector('a');
35
+ const sectionId = heading.parentElement.id;
36
+ link.href = '#' + sectionId;
37
+
38
+ // Bold numbered text
39
+ const number = document.createElement('strong');
40
+ number.textContent = (index + 1) + '. ';
41
+
42
+ // Clear the link and add the number + text
43
+ link.innerHTML = '';
44
+ link.appendChild(number);
45
+ link.appendChild(document.createTextNode(heading.textContent));
46
+
47
+ // Add click handler for smooth scrolling
48
+ link.addEventListener('click', (e) => {
49
+ e.preventDefault();
50
+
51
+ const targetSection = document.getElementById(sectionId);
52
+ if (targetSection) {
53
+ targetSection.scrollIntoView({ behavior: 'smooth' });
54
+ // Focus on the section for accessibility (will only show outline for keyboard users due to CSS)
55
+ setTimeout(() => {
56
+ targetSection.focus();
57
+ }, 100);
58
+ }
59
+ });
60
+
61
+ // Ensure sections are focusable for keyboard users but use CSS to control focus visibility
62
+ const targetSection = document.getElementById(sectionId);
63
+ if (targetSection) {
64
+ targetSection.setAttribute('tabindex', '-1');
65
+ // Use focus-visible to only show outline for keyboard focus
66
+ targetSection.style.outline = 'none';
67
+ targetSection.style.setProperty('outline', 'none', 'important');
68
+ }
69
+
70
+ // Add item to the TOC list
71
+ tocList.appendChild(tocItem);
72
+ });
73
+
74
+ return {
75
+ result: 'util-toc initialized'
76
+ };
77
+ }