tyrell-components 1.0.0-RC14 → 1.0.0-RC16

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 (287) hide show
  1. package/README.md +53 -53
  2. package/css/tyrell-theme.css +32 -4
  3. package/css/tyrell.css +10 -15
  4. package/dist/tyrell-colors-static.css +1 -316
  5. package/dist/tyrell-theme.css +1 -1443
  6. package/dist/tyrell.css +1 -1390
  7. package/dist/tyrell.js +1 -1
  8. package/lib/base/ty-component.d.ts +14 -0
  9. package/lib/base/ty-component.d.ts.map +1 -0
  10. package/lib/base/ty-component.js +223 -196
  11. package/lib/base/ty-component.js.map +1 -0
  12. package/lib/components/button.d.ts +1 -1
  13. package/lib/components/button.d.ts.map +1 -0
  14. package/lib/components/button.js +267 -259
  15. package/lib/components/button.js.map +1 -0
  16. package/lib/components/calendar-month.d.ts +2 -2
  17. package/lib/components/calendar-month.d.ts.map +1 -0
  18. package/lib/components/calendar-month.js +440 -416
  19. package/lib/components/calendar-month.js.map +1 -0
  20. package/lib/components/calendar-navigation.d.ts +11 -0
  21. package/lib/components/calendar-navigation.d.ts.map +1 -0
  22. package/lib/components/calendar-navigation.js +272 -261
  23. package/lib/components/calendar-navigation.js.map +1 -0
  24. package/lib/components/calendar.d.ts +31 -3
  25. package/lib/components/calendar.d.ts.map +1 -0
  26. package/lib/components/calendar.js +785 -603
  27. package/lib/components/calendar.js.map +1 -0
  28. package/lib/components/checkbox.d.ts +1 -1
  29. package/lib/components/checkbox.d.ts.map +1 -0
  30. package/lib/components/checkbox.js +273 -272
  31. package/lib/components/checkbox.js.map +1 -0
  32. package/lib/components/copy.d.ts +1 -1
  33. package/lib/components/copy.d.ts.map +1 -0
  34. package/lib/components/copy.js +287 -278
  35. package/lib/components/copy.js.map +1 -0
  36. package/lib/components/date-picker.d.ts +16 -10
  37. package/lib/components/date-picker.d.ts.map +1 -0
  38. package/lib/components/date-picker.js +1068 -1122
  39. package/lib/components/date-picker.js.map +1 -0
  40. package/lib/components/dropdown.d.ts +403 -0
  41. package/lib/components/dropdown.d.ts.map +1 -0
  42. package/lib/components/dropdown.js +1315 -0
  43. package/lib/components/dropdown.js.map +1 -0
  44. package/lib/components/file-upload.d.ts +4 -0
  45. package/lib/components/file-upload.js +418 -404
  46. package/lib/components/icon.d.ts.map +1 -0
  47. package/lib/components/icon.js +141 -141
  48. package/lib/components/icon.js.map +1 -0
  49. package/lib/components/input.d.ts +1 -1
  50. package/lib/components/input.d.ts.map +1 -0
  51. package/lib/components/input.js +593 -596
  52. package/lib/components/input.js.map +1 -0
  53. package/lib/components/modal.d.ts +2 -8
  54. package/lib/components/modal.d.ts.map +1 -0
  55. package/lib/components/modal.js +309 -319
  56. package/lib/components/modal.js.map +1 -0
  57. package/lib/components/multiselect.d.ts +389 -0
  58. package/lib/components/multiselect.d.ts.map +1 -0
  59. package/lib/components/multiselect.js +1301 -0
  60. package/lib/components/multiselect.js.map +1 -0
  61. package/lib/components/option.d.ts.map +1 -0
  62. package/lib/components/option.js +167 -167
  63. package/lib/components/option.js.map +1 -0
  64. package/lib/components/popup.d.ts.map +1 -0
  65. package/lib/components/popup.js +236 -233
  66. package/lib/components/popup.js.map +1 -0
  67. package/lib/components/radio.d.ts +17 -2
  68. package/lib/components/radio.js +437 -406
  69. package/lib/components/resize-observer.d.ts.map +1 -0
  70. package/lib/components/resize-observer.js +67 -67
  71. package/lib/components/resize-observer.js.map +1 -0
  72. package/lib/components/scroll-container.d.ts +3 -2
  73. package/lib/components/scroll-container.d.ts.map +1 -0
  74. package/lib/components/scroll-container.js +289 -294
  75. package/lib/components/scroll-container.js.map +1 -0
  76. package/lib/components/select.d.ts +85 -18
  77. package/lib/components/select.js +1647 -1529
  78. package/lib/components/selected-tags.d.ts +1 -1
  79. package/lib/components/selected-tags.js +188 -182
  80. package/lib/components/step.d.ts.map +1 -0
  81. package/lib/components/step.js +62 -62
  82. package/lib/components/step.js.map +1 -0
  83. package/lib/components/switch.d.ts +1 -1
  84. package/lib/components/switch.js +214 -213
  85. package/lib/components/tab.d.ts +49 -1
  86. package/lib/components/tab.d.ts.map +1 -0
  87. package/lib/components/tab.js +203 -62
  88. package/lib/components/tab.js.map +1 -0
  89. package/lib/components/tabs.d.ts +15 -2
  90. package/lib/components/tabs.d.ts.map +1 -0
  91. package/lib/components/tabs.js +576 -496
  92. package/lib/components/tabs.js.map +1 -0
  93. package/lib/components/tag.d.ts +5 -8
  94. package/lib/components/tag.d.ts.map +1 -0
  95. package/lib/components/tag.js +235 -235
  96. package/lib/components/tag.js.map +1 -0
  97. package/lib/components/textarea.d.ts +5 -4
  98. package/lib/components/textarea.d.ts.map +1 -0
  99. package/lib/components/textarea.js +458 -456
  100. package/lib/components/textarea.js.map +1 -0
  101. package/lib/components/tooltip.d.ts +4 -3
  102. package/lib/components/tooltip.d.ts.map +1 -0
  103. package/lib/components/tooltip.js +293 -283
  104. package/lib/components/tooltip.js.map +1 -0
  105. package/lib/components/wizard.d.ts +5 -4
  106. package/lib/components/wizard.d.ts.map +1 -0
  107. package/lib/components/wizard.js +359 -395
  108. package/lib/components/wizard.js.map +1 -0
  109. package/lib/icons/fontawesome/brands.d.ts.map +1 -0
  110. package/lib/icons/fontawesome/brands.js.map +1 -0
  111. package/lib/icons/fontawesome/regular.d.ts.map +1 -0
  112. package/lib/icons/fontawesome/regular.js.map +1 -0
  113. package/lib/icons/fontawesome/solid.d.ts.map +1 -0
  114. package/lib/icons/fontawesome/solid.js.map +1 -0
  115. package/lib/icons/heroicons/micro.d.ts.map +1 -0
  116. package/lib/icons/heroicons/micro.js +392 -392
  117. package/lib/icons/heroicons/micro.js.map +1 -0
  118. package/lib/icons/heroicons/mini.d.ts.map +1 -0
  119. package/lib/icons/heroicons/mini.js +382 -382
  120. package/lib/icons/heroicons/mini.js.map +1 -0
  121. package/lib/icons/heroicons/outline.d.ts.map +1 -0
  122. package/lib/icons/heroicons/outline.js +337 -337
  123. package/lib/icons/heroicons/outline.js.map +1 -0
  124. package/lib/icons/heroicons/solid.d.ts.map +1 -0
  125. package/lib/icons/heroicons/solid.js +407 -407
  126. package/lib/icons/heroicons/solid.js.map +1 -0
  127. package/lib/icons/lucide.d.ts.map +1 -0
  128. package/lib/icons/lucide.js +22843 -22843
  129. package/lib/icons/lucide.js.map +1 -0
  130. package/lib/icons/material/filled.d.ts.map +1 -0
  131. package/lib/icons/material/filled.js +9653 -9653
  132. package/lib/icons/material/filled.js.map +1 -0
  133. package/lib/icons/material/outlined.d.ts.map +1 -0
  134. package/lib/icons/material/outlined.js +10271 -10271
  135. package/lib/icons/material/outlined.js.map +1 -0
  136. package/lib/icons/material/round.d.ts.map +1 -0
  137. package/lib/icons/material/round.js +10495 -10495
  138. package/lib/icons/material/round.js.map +1 -0
  139. package/lib/icons/material/sharp.d.ts.map +1 -0
  140. package/lib/icons/material/sharp.js +9905 -9905
  141. package/lib/icons/material/sharp.js.map +1 -0
  142. package/lib/icons/material/two-tone.d.ts.map +1 -0
  143. package/lib/icons/material/two-tone.js +12792 -12792
  144. package/lib/icons/material/two-tone.js.map +1 -0
  145. package/lib/index.d.ts +1 -1
  146. package/lib/index.d.ts.map +1 -0
  147. package/lib/index.js +21 -21
  148. package/lib/index.js.map +1 -0
  149. package/lib/styles/button.d.ts.map +1 -0
  150. package/lib/styles/button.js +200 -302
  151. package/lib/styles/button.js.map +1 -0
  152. package/lib/styles/calendar-month.d.ts +3 -0
  153. package/lib/styles/calendar-month.d.ts.map +1 -0
  154. package/lib/styles/calendar-month.js +141 -185
  155. package/lib/styles/calendar-month.js.map +1 -0
  156. package/lib/styles/calendar-navigation.d.ts +1 -1
  157. package/lib/styles/calendar-navigation.d.ts.map +1 -0
  158. package/lib/styles/calendar-navigation.js +100 -86
  159. package/lib/styles/calendar-navigation.js.map +1 -0
  160. package/lib/styles/calendar.d.ts +3 -2
  161. package/lib/styles/calendar.d.ts.map +1 -0
  162. package/lib/styles/calendar.js +83 -9
  163. package/lib/styles/calendar.js.map +1 -0
  164. package/lib/styles/checkbox.js +62 -77
  165. package/lib/styles/copy.d.ts.map +1 -0
  166. package/lib/styles/copy.js +71 -91
  167. package/lib/styles/copy.js.map +1 -0
  168. package/lib/styles/custom-scrollbar.js +80 -100
  169. package/lib/styles/date-picker.d.ts.map +1 -0
  170. package/lib/styles/date-picker.js +295 -335
  171. package/lib/styles/date-picker.js.map +1 -0
  172. package/lib/styles/dropdown.d.ts +12 -0
  173. package/lib/styles/dropdown.d.ts.map +1 -0
  174. package/lib/styles/dropdown.js +725 -0
  175. package/lib/styles/dropdown.js.map +1 -0
  176. package/lib/styles/field-size.d.ts +6 -0
  177. package/lib/styles/field-size.js +91 -0
  178. package/lib/styles/file-upload.js +139 -169
  179. package/lib/styles/icon.d.ts.map +1 -0
  180. package/lib/styles/icon.js +92 -156
  181. package/lib/styles/icon.js.map +1 -0
  182. package/lib/styles/input.d.ts.map +1 -0
  183. package/lib/styles/input.js +125 -241
  184. package/lib/styles/input.js.map +1 -0
  185. package/lib/styles/modal.d.ts.map +1 -0
  186. package/lib/styles/modal.js +67 -102
  187. package/lib/styles/modal.js.map +1 -0
  188. package/lib/styles/multiselect.d.ts +6 -0
  189. package/lib/styles/multiselect.d.ts.map +1 -0
  190. package/lib/styles/multiselect.js +568 -0
  191. package/lib/styles/multiselect.js.map +1 -0
  192. package/lib/styles/option.d.ts +1 -1
  193. package/lib/styles/option.d.ts.map +1 -0
  194. package/lib/styles/option.js +47 -67
  195. package/lib/styles/option.js.map +1 -0
  196. package/lib/styles/popup.d.ts +1 -1
  197. package/lib/styles/popup.d.ts.map +1 -0
  198. package/lib/styles/popup.js +37 -65
  199. package/lib/styles/popup.js.map +1 -0
  200. package/lib/styles/radio.js +75 -96
  201. package/lib/styles/resize-observer.d.ts.map +1 -0
  202. package/lib/styles/resize-observer.js +2 -5
  203. package/lib/styles/resize-observer.js.map +1 -0
  204. package/lib/styles/scroll-container.d.ts +1 -1
  205. package/lib/styles/scroll-container.d.ts.map +1 -0
  206. package/lib/styles/scroll-container.js +101 -131
  207. package/lib/styles/scroll-container.js.map +1 -0
  208. package/lib/styles/select-base.js +596 -773
  209. package/lib/styles/select.d.ts +1 -1
  210. package/lib/styles/select.js +146 -228
  211. package/lib/styles/step.d.ts +1 -1
  212. package/lib/styles/step.d.ts.map +1 -0
  213. package/lib/styles/step.js +15 -30
  214. package/lib/styles/step.js.map +1 -0
  215. package/lib/styles/switch.js +37 -55
  216. package/lib/styles/tab.d.ts +1 -1
  217. package/lib/styles/tab.d.ts.map +1 -0
  218. package/lib/styles/tab.js +86 -29
  219. package/lib/styles/tab.js.map +1 -0
  220. package/lib/styles/tabs.d.ts +1 -1
  221. package/lib/styles/tabs.d.ts.map +1 -0
  222. package/lib/styles/tabs.js +190 -180
  223. package/lib/styles/tabs.js.map +1 -0
  224. package/lib/styles/tag.d.ts.map +1 -0
  225. package/lib/styles/tag.js +137 -191
  226. package/lib/styles/tag.js.map +1 -0
  227. package/lib/styles/textarea.d.ts +1 -1
  228. package/lib/styles/textarea.d.ts.map +1 -0
  229. package/lib/styles/textarea.js +205 -271
  230. package/lib/styles/textarea.js.map +1 -0
  231. package/lib/styles/tooltip.d.ts.map +1 -0
  232. package/lib/styles/tooltip.js +3 -3
  233. package/lib/styles/tooltip.js.map +1 -0
  234. package/lib/styles/wizard.d.ts +9 -5
  235. package/lib/styles/wizard.d.ts.map +1 -0
  236. package/lib/styles/wizard.js +301 -245
  237. package/lib/styles/wizard.js.map +1 -0
  238. package/lib/types/common.d.ts +4 -2
  239. package/lib/types/common.d.ts.map +1 -0
  240. package/lib/types/common.js +6 -5
  241. package/lib/types/common.js.map +1 -0
  242. package/lib/utils/calendar-utils.d.ts.map +1 -0
  243. package/lib/utils/calendar-utils.js +100 -100
  244. package/lib/utils/calendar-utils.js.map +1 -0
  245. package/lib/utils/custom-scrollbar.js +272 -272
  246. package/lib/utils/flavor-sheet.js +14 -14
  247. package/lib/utils/icon-registry.d.ts.map +1 -0
  248. package/lib/utils/icon-registry.js +127 -127
  249. package/lib/utils/icon-registry.js.map +1 -0
  250. package/lib/utils/loader-registry.js +3 -3
  251. package/lib/utils/locale.d.ts.map +1 -0
  252. package/lib/utils/locale.js +34 -34
  253. package/lib/utils/locale.js.map +1 -0
  254. package/lib/utils/mobile.js +1 -1
  255. package/lib/utils/number-format.d.ts.map +1 -0
  256. package/lib/utils/number-format.js +54 -54
  257. package/lib/utils/number-format.js.map +1 -0
  258. package/lib/utils/parse-boolean.d.ts +39 -0
  259. package/lib/utils/parse-boolean.d.ts.map +1 -0
  260. package/lib/utils/parse-boolean.js +23 -0
  261. package/lib/utils/parse-boolean.js.map +1 -0
  262. package/lib/utils/positioning.d.ts.map +1 -0
  263. package/lib/utils/positioning.js +212 -212
  264. package/lib/utils/positioning.js.map +1 -0
  265. package/lib/utils/property-capture.d.ts.map +1 -0
  266. package/lib/utils/property-capture.js +36 -36
  267. package/lib/utils/property-capture.js.map +1 -0
  268. package/lib/utils/property-manager.d.ts +1 -1
  269. package/lib/utils/property-manager.d.ts.map +1 -0
  270. package/lib/utils/property-manager.js +127 -125
  271. package/lib/utils/property-manager.js.map +1 -0
  272. package/lib/utils/resize-observer.d.ts.map +1 -0
  273. package/lib/utils/resize-observer.js +23 -23
  274. package/lib/utils/resize-observer.js.map +1 -0
  275. package/lib/utils/scroll-lock.d.ts.map +1 -0
  276. package/lib/utils/scroll-lock.js +57 -57
  277. package/lib/utils/scroll-lock.js.map +1 -0
  278. package/lib/utils/styles.d.ts.map +1 -0
  279. package/lib/utils/styles.js +17 -17
  280. package/lib/utils/styles.js.map +1 -0
  281. package/lib/utils/validity.d.ts +26 -0
  282. package/lib/utils/validity.js +53 -0
  283. package/lib/version.d.ts +1 -1
  284. package/lib/version.d.ts.map +1 -0
  285. package/lib/version.js +1 -1
  286. package/lib/version.js.map +1 -0
  287. package/package.json +28 -7
@@ -2,554 +2,634 @@ import { ensureStyles } from '../utils/styles.js';
2
2
  import { tabsStyles } from '../styles/tabs.js';
3
3
  const eventHandlers = new WeakMap();
4
4
  const resizeObservers = new WeakMap();
5
+ const shownTabs = new WeakMap();
6
+ const instanceSheets = new WeakMap();
7
+ const pendingInitialSnap = new WeakMap();
8
+ let userInputs = 0;
9
+ const countInput = () => { userInputs++; };
10
+ addEventListener('pointerdown', countInput, { capture: true, passive: true });
11
+ addEventListener('keydown', countInput, { capture: true, passive: true });
12
+ const awaitedTabs = new WeakMap();
13
+ let snapCount = 0;
5
14
  function getTabsAttributes(el) {
6
- return {
7
- width: el.getAttribute('width') || '100%',
8
- height: el.getAttribute('height') || '400px',
9
- active: el.getAttribute('active'),
10
- placement: (el.getAttribute('placement') || 'top'),
11
- };
15
+ return {
16
+ placement: (el.getAttribute('placement') || 'top'),
17
+ fixed: el.hasAttribute('fixed'),
18
+ };
12
19
  }
13
20
  function getChildTabs(el) {
14
- return Array.from(el.querySelectorAll('ty-tab'));
21
+ return Array.from(el.querySelectorAll(':scope > ty-tab'));
15
22
  }
16
23
  function getTabId(tab) {
17
- return tab.getAttribute('id');
24
+ return tab.getAttribute('id');
25
+ }
26
+ function getSlotLabel(tabsEl, tabId) {
27
+ return tabsEl.querySelector(`:scope > [slot='label-${tabId}']`);
18
28
  }
19
29
  function hasSlotLabel(tabsEl, tabId) {
20
- return tabsEl.querySelector(`[slot='label-${tabId}']`) !== null;
30
+ return getSlotLabel(tabsEl, tabId) !== null;
21
31
  }
22
32
  function getTabLabelType(tabsEl, tab) {
23
- const tabId = getTabId(tab);
24
- if (tabId && hasSlotLabel(tabsEl, tabId)) {
25
- return 'slot';
26
- }
27
- return 'text';
33
+ const tabId = getTabId(tab);
34
+ if (tabId && hasSlotLabel(tabsEl, tabId)) {
35
+ return 'slot';
36
+ }
37
+ return 'text';
28
38
  }
29
39
  function isTabDisabled(tab) {
30
- return tab.hasAttribute('disabled');
40
+ return tab.hasAttribute('disabled');
31
41
  }
32
42
  function hasCustomMarker(el) {
33
- return el.querySelector('[slot="marker"]') !== null;
43
+ return el.querySelector(':scope > [slot="marker"]') !== null;
34
44
  }
35
45
  function findTabIndex(tabs, tabId) {
36
- const index = tabs.findIndex(tab => getTabId(tab) === tabId);
37
- return index >= 0 ? index : undefined;
46
+ const index = tabs.findIndex(tab => getTabId(tab) === tabId);
47
+ return index >= 0 ? index : undefined;
48
+ }
49
+ function currentActive(el) {
50
+ return shownTabs.get(el) ?? null;
38
51
  }
39
52
  function getActiveTabId(el, tabs) {
40
- const activeAttr = el.getAttribute('active');
41
- if (activeAttr && findTabIndex(tabs, activeAttr) !== undefined) {
42
- return activeAttr;
43
- }
44
- if (tabs.length > 0) {
45
- return getTabId(tabs[0]);
46
- }
47
- return null;
48
- }
49
- function setActiveTab(el, tabId) {
50
- el.setAttribute('active', tabId);
53
+ const ids = tabs.map(getTabId).filter((id) => !!id);
54
+ const activeAttr = el.getAttribute('active');
55
+ if (activeAttr && ids.includes(activeAttr))
56
+ return activeAttr;
57
+ const shown = shownTabs.get(el);
58
+ if (shown && ids.includes(shown))
59
+ return shown;
60
+ return ids[0] ?? null;
61
+ }
62
+ function activateTab(el, tabId) {
63
+ el.setAttribute('active', tabId);
64
+ syncShownTab(el, true);
51
65
  }
52
66
  function dispatchTabChangeEvent(el, activeId, activeIndex, previousId, previousIndex) {
53
- const event = new CustomEvent('ty-tab-change', {
54
- detail: {
55
- activeId,
56
- activeIndex,
57
- previousId,
58
- previousIndex,
59
- },
60
- bubbles: true,
61
- cancelable: false,
62
- });
63
- el.dispatchEvent(event);
67
+ const event = new CustomEvent('ty-tab-change', {
68
+ detail: {
69
+ activeId,
70
+ activeIndex,
71
+ previousId,
72
+ previousIndex,
73
+ },
74
+ bubbles: true,
75
+ cancelable: false,
76
+ });
77
+ el.dispatchEvent(event);
64
78
  }
65
79
  function handleTabClick(el, tabId, event) {
66
- event.preventDefault();
67
- event.stopPropagation();
68
- setActiveTab(el, tabId);
80
+ event.preventDefault();
81
+ event.stopPropagation();
82
+ activateTab(el, tabId);
69
83
  }
70
84
  function cleanupEventListeners(el) {
71
- const handlers = eventHandlers.get(el);
72
- if (!handlers)
73
- return;
74
- const shadowRoot = el.shadowRoot;
75
- if (!shadowRoot)
76
- return;
77
- handlers.tabClickHandlers.forEach((handler, tabId) => {
78
- const button = shadowRoot.querySelector(`[data-tab-id='${tabId}']`);
79
- if (button) {
80
- button.removeEventListener('pointerdown', handler);
81
- }
82
- });
83
- handlers.tabKeyClickHandlers.forEach((handler, tabId) => {
84
- const button = shadowRoot.querySelector(`[data-tab-id='${tabId}']`);
85
- if (button) {
86
- button.removeEventListener('click', handler);
87
- }
88
- });
89
- handlers.tabClickHandlers.clear();
90
- handlers.tabKeyClickHandlers.clear();
85
+ const handlers = eventHandlers.get(el);
86
+ if (!handlers)
87
+ return;
88
+ const shadowRoot = el.shadowRoot;
89
+ if (!shadowRoot)
90
+ return;
91
+ handlers.tabClickHandlers.forEach((handler, tabId) => {
92
+ const button = shadowRoot.querySelector(`[data-tab-id='${tabId}']`);
93
+ if (button) {
94
+ button.removeEventListener('click', handler);
95
+ }
96
+ });
97
+ handlers.tabKeydownHandlers.forEach((handler, tabId) => {
98
+ const button = shadowRoot.querySelector(`[data-tab-id='${tabId}']`);
99
+ if (button) {
100
+ button.removeEventListener('keydown', handler);
101
+ }
102
+ });
103
+ handlers.tabClickHandlers.clear();
104
+ handlers.tabKeydownHandlers.clear();
105
+ }
106
+ function handleTabKeydown(el, shadowRoot, tabId, e) {
107
+ if (!['ArrowRight', 'ArrowLeft', 'Home', 'End'].includes(e.key))
108
+ return;
109
+ const ids = Array.from(shadowRoot.querySelectorAll('.tab-buttons [data-tab-id]:not([disabled])')).map((b) => b.getAttribute('data-tab-id'));
110
+ const i = ids.indexOf(tabId);
111
+ if (i < 0 || ids.length === 0)
112
+ return;
113
+ e.preventDefault();
114
+ const next = e.key === 'Home' ? ids[0]
115
+ : e.key === 'End' ? ids[ids.length - 1]
116
+ : e.key === 'ArrowRight' ? ids[(i + 1) % ids.length]
117
+ : ids[(i - 1 + ids.length) % ids.length];
118
+ if (next === tabId)
119
+ return;
120
+ handleTabClick(el, next, e);
121
+ requestAnimationFrame(() => {
122
+ shadowRoot.querySelector(`.tab-buttons [data-tab-id='${next}']`)?.focus();
123
+ });
91
124
  }
92
125
  function setupEventListeners(el, shadowRoot, tabs) {
93
- cleanupEventListeners(el);
94
- const handlers = {
95
- tabClickHandlers: new Map(),
96
- tabKeyClickHandlers: new Map(),
97
- };
98
- tabs.forEach((tab) => {
99
- const tabId = getTabId(tab);
100
- if (!tabId)
101
- return;
102
- const button = shadowRoot.querySelector(`[data-tab-id='${tabId}']`);
103
- if (button) {
104
- const handler = (e) => handleTabClick(el, tabId, e);
105
- const keyClickHandler = (e) => {
106
- if (e.detail === 0)
107
- handleTabClick(el, tabId, e);
108
- };
109
- button.addEventListener('pointerdown', handler);
110
- button.addEventListener('click', keyClickHandler);
111
- handlers.tabClickHandlers.set(tabId, handler);
112
- handlers.tabKeyClickHandlers.set(tabId, keyClickHandler);
113
- }
114
- });
115
- eventHandlers.set(el, handlers);
126
+ cleanupEventListeners(el);
127
+ const handlers = {
128
+ tabClickHandlers: new Map(),
129
+ tabKeydownHandlers: new Map(),
130
+ };
131
+ tabs.forEach((tab) => {
132
+ const tabId = getTabId(tab);
133
+ if (!tabId)
134
+ return;
135
+ const button = shadowRoot.querySelector(`[data-tab-id='${tabId}']`);
136
+ if (button) {
137
+ const handler = (e) => handleTabClick(el, tabId, e);
138
+ const keydownHandler = (e) => handleTabKeydown(el, shadowRoot, tabId, e);
139
+ button.addEventListener('click', handler);
140
+ button.addEventListener('keydown', keydownHandler);
141
+ handlers.tabClickHandlers.set(tabId, handler);
142
+ handlers.tabKeydownHandlers.set(tabId, keydownHandler);
143
+ }
144
+ });
145
+ eventHandlers.set(el, handlers);
146
+ }
147
+ function carouselTransform(activeIndex) {
148
+ return `translateX(${activeIndex * -100}%)`;
149
+ }
150
+ function snapNextChange(container) {
151
+ if (!container)
152
+ return;
153
+ const token = String(++snapCount);
154
+ container.setAttribute('data-snap', token);
155
+ requestAnimationFrame(() => requestAnimationFrame(() => {
156
+ if (container.getAttribute('data-snap') === token)
157
+ container.removeAttribute('data-snap');
158
+ }));
116
159
  }
117
160
  function updateTransform(el, activeIndex) {
118
- const shadowRoot = el.shadowRoot;
119
- if (!shadowRoot)
120
- return;
121
- const panelsWrapper = shadowRoot.querySelector('.panels-wrapper');
122
- if (!panelsWrapper)
123
- return;
124
- const containerWidth = el.offsetWidth;
125
- const offsetPx = activeIndex * containerWidth;
126
- panelsWrapper.style.transform = `translateX(-${offsetPx}px)`;
161
+ const panelsWrapper = el.shadowRoot?.querySelector('.panels-wrapper');
162
+ if (panelsWrapper)
163
+ panelsWrapper.style.transform = carouselTransform(activeIndex);
164
+ }
165
+ function setAttr(node, name, value) {
166
+ if (node.getAttribute(name) !== value)
167
+ node.setAttribute(name, value);
168
+ }
169
+ function syncInstanceSheet(el, shownId) {
170
+ const shadowRoot = el.shadowRoot;
171
+ if (!shadowRoot)
172
+ return;
173
+ let sheet = instanceSheets.get(el);
174
+ if (!sheet) {
175
+ sheet = new CSSStyleSheet();
176
+ sheet.replaceSync(':host {} ::slotted(ty-tab) {}');
177
+ const hide = sheet.cssRules[1].style;
178
+ hide.setProperty('opacity', '0', 'important');
179
+ hide.setProperty('visibility', 'hidden', 'important');
180
+ hide.setProperty('pointer-events', 'none', 'important');
181
+ shadowRoot.adoptedStyleSheets = [...shadowRoot.adoptedStyleSheets, sheet];
182
+ instanceSheets.set(el, sheet);
183
+ }
184
+ const host = sheet.cssRules[0].style;
185
+ for (const prop of ['width', 'height']) {
186
+ const value = el.getAttribute(prop);
187
+ if (value)
188
+ host.setProperty(prop, value, 'important');
189
+ else
190
+ host.removeProperty(prop);
191
+ }
192
+ sheet.cssRules[1].selectorText = shownId
193
+ ? `::slotted(ty-tab:not(#${CSS.escape(shownId)}))`
194
+ : '::slotted(ty-tab)';
127
195
  }
128
196
  function updateAriaAttributes(el, shadowRoot, activeId) {
129
- const tabs = getChildTabs(el);
130
- tabs.forEach((tab, idx) => {
131
- const tabId = getTabId(tab);
132
- if (!tabId)
133
- return;
134
- const button = shadowRoot.querySelector(`[data-tab-id='${tabId}']`);
135
- const isActive = tabId === activeId;
136
- if (button) {
137
- button.setAttribute('aria-selected', String(isActive));
138
- button.setAttribute('tabindex', isActive ? '0' : '-1');
139
- button.setAttribute('data-active', String(isActive));
140
- const slottedLabel = el.querySelector(`[slot='label-${tabId}']`);
141
- if (slottedLabel) {
142
- slottedLabel.setAttribute('data-active', String(isActive));
143
- }
144
- }
145
- });
197
+ const tabs = getChildTabs(el);
198
+ tabs.forEach((tab, idx) => {
199
+ const tabId = getTabId(tab);
200
+ if (!tabId)
201
+ return;
202
+ const button = shadowRoot.querySelector(`[data-tab-id='${tabId}']`);
203
+ const isActive = tabId === activeId;
204
+ if (button) {
205
+ button.setAttribute('aria-selected', String(isActive));
206
+ button.setAttribute('tabindex', isActive ? '0' : '-1');
207
+ button.setAttribute('data-active', String(isActive));
208
+ const slottedLabel = getSlotLabel(el, tabId);
209
+ if (slottedLabel) {
210
+ setAttr(slottedLabel, 'data-active', String(isActive));
211
+ }
212
+ }
213
+ });
146
214
  }
147
215
  function updatePanelInteraction(el, activeId) {
148
- const tabs = getChildTabs(el);
149
- tabs.forEach((tab) => {
150
- const tabId = getTabId(tab);
151
- if (!tabId)
152
- return;
153
- const isActive = tabId === activeId;
154
- tab.setAttribute('data-active', String(isActive));
155
- if (isActive) {
156
- tab.style.pointerEvents = 'auto';
157
- tab.style.opacity = '1';
158
- }
159
- else {
160
- tab.style.pointerEvents = 'none';
161
- tab.style.opacity = '0';
162
- }
163
- });
216
+ syncInstanceSheet(el, activeId);
217
+ getChildTabs(el).forEach((tab) => {
218
+ const tabId = getTabId(tab);
219
+ tab.setShown?.(!!tabId && tabId === activeId);
220
+ if (!tabId)
221
+ return;
222
+ setAttr(tab, 'data-active', String(tabId === activeId));
223
+ });
164
224
  }
165
225
  function calculateMarkerPosition(el, shadowRoot, activeId) {
166
- const button = shadowRoot.querySelector(`[data-tab-id='${activeId}']`);
167
- const buttonsContainer = shadowRoot.querySelector('.tab-buttons');
168
- if (!button || !buttonsContainer)
169
- return null;
170
- const buttonRect = button.getBoundingClientRect();
171
- const left = button.offsetLeft;
172
- const top = button.offsetTop;
173
- return {
174
- left,
175
- top,
176
- width: buttonRect.width,
177
- height: buttonRect.height,
178
- };
179
- }
180
- function updateMarker(el, activeId) {
181
- const shadowRoot = el.shadowRoot;
182
- if (!shadowRoot)
183
- return;
184
- const marker = shadowRoot.querySelector('.marker-wrapper');
185
- if (!marker)
186
- return;
187
- const position = calculateMarkerPosition(el, shadowRoot, activeId);
188
- if (!position)
189
- return;
190
- const snap = !marker.style.left || marker.offsetWidth === 0;
191
- if (snap)
192
- marker.style.transition = 'none';
193
- marker.style.left = `${position.left}px`;
194
- marker.style.top = `${position.top}px`;
195
- marker.style.width = `${position.width}px`;
196
- marker.style.height = `${position.height}px`;
197
- if (snap) {
198
- marker.offsetWidth;
199
- marker.style.transition = '';
200
- }
201
- }
202
- function updateActiveTabState(el, tabId, previousId) {
203
- const tabs = getChildTabs(el);
204
- const shadowRoot = el.shadowRoot;
205
- if (!shadowRoot)
206
- return;
207
- const currentIndex = previousId ? findTabIndex(tabs, previousId) : undefined;
208
- const newIndex = findTabIndex(tabs, tabId);
209
- if (previousId === tabId || newIndex === undefined)
210
- return;
211
- el.style.setProperty('--active-index', String(newIndex));
212
- updateTransform(el, newIndex);
213
- updateAriaAttributes(el, shadowRoot, tabId);
214
- updatePanelInteraction(el, tabId);
215
- updateMarker(el, tabId);
216
- const newPanel = tabs[newIndex];
217
- if (newPanel?.resetScroll) {
218
- newPanel.resetScroll();
219
- }
220
- dispatchTabChangeEvent(el, tabId, newIndex, previousId, currentIndex ?? null);
226
+ const button = shadowRoot.querySelector(`[data-tab-id='${activeId}']`);
227
+ const buttonsContainer = shadowRoot.querySelector('.tab-buttons');
228
+ if (!button || !buttonsContainer)
229
+ return null;
230
+ const left = button.offsetLeft;
231
+ const top = button.offsetTop;
232
+ const width = button.offsetWidth;
233
+ const height = button.offsetHeight;
234
+ return { left, top, width, height };
235
+ }
236
+ function updateMarker(el, activeId, forceSnap = false) {
237
+ const shadowRoot = el.shadowRoot;
238
+ if (!shadowRoot)
239
+ return;
240
+ const marker = shadowRoot.querySelector('.marker-wrapper');
241
+ if (!marker)
242
+ return;
243
+ const position = calculateMarkerPosition(el, shadowRoot, activeId);
244
+ if (!position)
245
+ return;
246
+ const snap = forceSnap || !marker.style.left || marker.offsetWidth === 0;
247
+ if (snap)
248
+ marker.style.transition = 'none';
249
+ marker.style.left = `${position.left}px`;
250
+ marker.style.top = `${position.top}px`;
251
+ marker.style.width = `${position.width}px`;
252
+ marker.style.height = `${position.height}px`;
253
+ if (snap) {
254
+ marker.offsetWidth;
255
+ marker.style.transition = '';
256
+ }
257
+ }
258
+ function syncShownTab(el, user = false) {
259
+ const shadowRoot = el.shadowRoot;
260
+ if (!shadowRoot)
261
+ return;
262
+ const tabs = getChildTabs(el);
263
+ const previousId = shownTabs.get(el) ?? null;
264
+ const tabId = getActiveTabId(el, tabs);
265
+ const newIndex = (tabId ? findTabIndex(tabs, tabId) : undefined) ?? 0;
266
+ const container = shadowRoot.querySelector('.tabs-container');
267
+ if (tabId === previousId) {
268
+ const wrapper = shadowRoot.querySelector('.panels-wrapper');
269
+ if (wrapper && wrapper.style.transform !== carouselTransform(newIndex)) {
270
+ snapNextChange(container);
271
+ wrapper.style.transform = carouselTransform(newIndex);
272
+ }
273
+ return;
274
+ }
275
+ shownTabs.set(el, tabId);
276
+ const initial = previousId === null;
277
+ const arrived = !user && tabId !== null && awaitedTabs.get(el) === tabId;
278
+ awaitedTabs.delete(el);
279
+ const requested = el.getAttribute('active');
280
+ if (initial && requested && requested !== tabId)
281
+ awaitedTabs.set(el, requested);
282
+ const snap = !user && (initial || arrived || pendingInitialSnap.get(el) === userInputs);
283
+ if (initial)
284
+ pendingInitialSnap.set(el, userInputs);
285
+ else if (!arrived)
286
+ pendingInitialSnap.delete(el);
287
+ if (snap)
288
+ snapNextChange(container);
289
+ updateTransform(el, newIndex);
290
+ updateAriaAttributes(el, shadowRoot, tabId || '');
291
+ updatePanelInteraction(el, tabId);
292
+ if (!tabId || initial)
293
+ return;
294
+ updateMarker(el, tabId, snap);
295
+ scrollActiveIntoView(el, tabId, !snap);
296
+ shadowRoot.querySelectorAll('.tab-overflow-item').forEach((item) => {
297
+ item.setAttribute('data-active', String(item.dataset.tabId === tabId));
298
+ });
299
+ tabs[newIndex]?.resetScroll?.();
300
+ if (arrived)
301
+ return;
302
+ const previousIndex = findTabIndex(tabs, previousId);
303
+ dispatchTabChangeEvent(el, tabId, newIndex, previousIndex === undefined ? null : previousId, previousIndex ?? null);
304
+ tabs[newIndex]?._requestLoad?.(newIndex);
221
305
  }
222
306
  function setupResizeObserver(el) {
223
- const oldObserver = resizeObservers.get(el);
224
- if (oldObserver) {
225
- oldObserver.disconnect();
226
- }
227
- const observer = new ResizeObserver((entries) => {
228
- const entry = entries[0];
229
- const { width } = getTabsAttributes(el);
230
- const tabs = getChildTabs(el);
231
- const activeId = getActiveTabId(el, tabs);
232
- const activeIndex = activeId ? findTabIndex(tabs, activeId) : 0;
233
- if (width.includes('%')) {
234
- el.style.setProperty('--tabs-page-width', `${entry.contentRect.width}px`);
235
- }
236
- if (activeIndex !== undefined) {
237
- updateTransform(el, activeIndex);
238
- }
239
- if (activeId) {
240
- updateMarker(el, activeId);
241
- }
242
- updateOverflow(el);
243
- });
244
- observer.observe(el);
245
- resizeObservers.set(el, observer);
307
+ const oldObserver = resizeObservers.get(el);
308
+ if (oldObserver) {
309
+ oldObserver.disconnect();
310
+ }
311
+ const observer = new ResizeObserver(() => {
312
+ const activeId = currentActive(el);
313
+ updateOverflow(el);
314
+ if (activeId) {
315
+ updateMarker(el, activeId);
316
+ scrollActiveIntoView(el, activeId, false);
317
+ }
318
+ });
319
+ observer.observe(el);
320
+ resizeObservers.set(el, observer);
246
321
  }
247
322
  function cleanupResizeObserver(el) {
248
- const observer = resizeObservers.get(el);
249
- if (observer) {
250
- observer.disconnect();
251
- resizeObservers.delete(el);
252
- }
323
+ const observer = resizeObservers.get(el);
324
+ if (observer) {
325
+ observer.disconnect();
326
+ resizeObservers.delete(el);
327
+ }
253
328
  }
254
329
  function renderTabButtons(tabsEl, tabs, activeId) {
255
- const buttons = tabs.map((tab) => {
256
- const tabId = getTabId(tab);
257
- if (!tabId)
258
- return '';
259
- const labelType = getTabLabelType(tabsEl, tab);
260
- const textLabel = labelType === 'text' ? (tab.getAttribute('label') || 'Tab') : '';
261
- const disabled = isTabDisabled(tab);
262
- const active = tabId === activeId;
263
- return `<button
264
- class="tab-button"
265
- role="tab"
266
- data-tab-id="${tabId}"
267
- id="tab-${tabId}"
268
- aria-selected="${active}"
269
- tabindex="${active ? '0' : '-1'}"
270
- data-active="${active}"
271
- ${disabled ? 'disabled aria-disabled="true"' : ''}
272
- >
273
- ${labelType === 'slot' ? `<slot name="label-${tabId}"></slot>` : textLabel}
274
- </button>`;
275
- }).join('');
276
- return `
277
- <div class="tab-buttons" role="tablist" part="buttons-container">
278
- <div class="marker-wrapper" part="marker-wrapper">
279
- ${!hasCustomMarker(tabsEl) ? '<div class="default-marker"></div>' : ''}
280
- <slot name="marker"></slot>
281
- </div>
282
- ${buttons}
283
- </div>
284
- `;
330
+ const buttons = tabs.map((tab) => {
331
+ const tabId = getTabId(tab);
332
+ if (!tabId)
333
+ return '';
334
+ const labelType = getTabLabelType(tabsEl, tab);
335
+ const textLabel = labelType === 'text' ? (tab.getAttribute('label') || 'Tab') : '';
336
+ const disabled = isTabDisabled(tab);
337
+ const active = tabId === activeId;
338
+ return `<button
339
+ class="tab-button"
340
+ role="tab"
341
+ data-tab-id="${tabId}"
342
+ id="tab-${tabId}"
343
+ aria-selected="${active}"
344
+ tabindex="${active ? '0' : '-1'}"
345
+ data-active="${active}"
346
+ ${disabled ? 'disabled aria-disabled="true"' : ''}
347
+ >
348
+ ${labelType === 'slot' ? `<slot name="label-${tabId}"></slot>` : `<span class="tab-label">${textLabel}</span>`}
349
+ </button>`;
350
+ }).join('');
351
+ return `
352
+ <div class="tab-buttons" part="buttons-container">
353
+ <div class="tab-strip" role="tablist" part="strip">
354
+ <div class="marker-wrapper" part="marker-wrapper">
355
+ ${!hasCustomMarker(tabsEl) ? '<div class="default-marker"></div>' : ''}
356
+ <slot name="marker"></slot>
357
+ </div>
358
+ ${buttons}
359
+ </div>
360
+ </div>
361
+ `;
285
362
  }
286
363
  function renderButtonsOnly(tabsEl, tabs, activeId) {
287
- return tabs.map((tab) => {
288
- const tabId = getTabId(tab);
289
- if (!tabId)
290
- return '';
291
- const labelType = getTabLabelType(tabsEl, tab);
292
- const textLabel = labelType === 'text' ? (tab.getAttribute('label') || 'Tab') : '';
293
- const disabled = isTabDisabled(tab);
294
- const active = tabId === activeId;
295
- return `<button
296
- class="tab-button"
297
- role="tab"
298
- data-tab-id="${tabId}"
299
- id="tab-${tabId}"
300
- aria-selected="${active}"
301
- tabindex="${active ? '0' : '-1'}"
302
- data-active="${active}"
303
- ${disabled ? 'disabled aria-disabled="true"' : ''}
304
- >
305
- ${labelType === 'slot' ? `<slot name="label-${tabId}"></slot>` : textLabel}
306
- </button>`;
307
- }).join('');
308
- }
309
- const OVERFLOW_TRIGGER_WIDTH = 40;
364
+ return tabs.map((tab) => {
365
+ const tabId = getTabId(tab);
366
+ if (!tabId)
367
+ return '';
368
+ const labelType = getTabLabelType(tabsEl, tab);
369
+ const textLabel = labelType === 'text' ? (tab.getAttribute('label') || 'Tab') : '';
370
+ const disabled = isTabDisabled(tab);
371
+ const active = tabId === activeId;
372
+ return `<button
373
+ class="tab-button"
374
+ role="tab"
375
+ data-tab-id="${tabId}"
376
+ id="tab-${tabId}"
377
+ aria-selected="${active}"
378
+ tabindex="${active ? '0' : '-1'}"
379
+ data-active="${active}"
380
+ ${disabled ? 'disabled aria-disabled="true"' : ''}
381
+ >
382
+ ${labelType === 'slot' ? `<slot name="label-${tabId}"></slot>` : `<span class="tab-label">${textLabel}</span>`}
383
+ </button>`;
384
+ }).join('');
385
+ }
310
386
  function getTabMenuLabel(tabsEl, tab) {
311
- const tabId = getTabId(tab);
312
- if (tabId && getTabLabelType(tabsEl, tab) === 'slot') {
313
- const slotted = tabsEl.querySelector(`[slot='label-${tabId}']`);
314
- return slotted?.textContent?.trim() || tabId;
315
- }
316
- return tab.getAttribute('label') || 'Tab';
387
+ const tabId = getTabId(tab);
388
+ if (tabId && getTabLabelType(tabsEl, tab) === 'slot') {
389
+ const slotted = getSlotLabel(tabsEl, tabId);
390
+ return slotted?.textContent?.trim() || tabId;
391
+ }
392
+ return tab.getAttribute('label') || 'Tab';
317
393
  }
318
394
  function removeOverflowMenu(shadowRoot) {
319
- shadowRoot.querySelector('.tab-overflow-trigger')?.remove();
320
- }
321
- function renderOverflowMenu(el, container, hiddenButtons, allTabs) {
322
- if (hiddenButtons.length === 0)
323
- return;
324
- const tabsById = new Map(allTabs.map((tab) => [getTabId(tab), tab]));
325
- const trigger = document.createElement('button');
326
- trigger.className = 'tab-overflow-trigger';
327
- trigger.type = 'button';
328
- trigger.setAttribute('aria-label', `${hiddenButtons.length} more tabs`);
329
- trigger.textContent = '⋯';
330
- const popup = document.createElement('ty-popup');
331
- popup.setAttribute('placement', 'bottom');
332
- const menu = document.createElement('div');
333
- menu.className = 'tab-overflow-menu';
334
- menu.setAttribute('role', 'menu');
335
- hiddenButtons.forEach((btn) => {
336
- const tabId = btn.dataset.tabId;
337
- const tab = tabId ? tabsById.get(tabId) : undefined;
338
- if (!tabId || !tab)
339
- return;
340
- const item = document.createElement('button');
341
- item.type = 'button';
342
- item.className = 'tab-overflow-item';
343
- item.setAttribute('role', 'menuitem');
344
- item.textContent = getTabMenuLabel(el, tab);
345
- if (isTabDisabled(tab)) {
346
- item.disabled = true;
347
- }
348
- else {
349
- item.addEventListener('click', () => {
350
- setActiveTab(el, tabId);
351
- popup.closePopup?.();
352
- });
353
- }
354
- menu.appendChild(item);
355
- });
356
- popup.appendChild(menu);
357
- trigger.appendChild(popup);
358
- container.appendChild(trigger);
395
+ shadowRoot.querySelector('.tab-overflow-trigger')?.remove();
396
+ }
397
+ function renderOverflowMenu(el, container, allTabs) {
398
+ const activeId = getActiveTabId(el, allTabs);
399
+ const trigger = document.createElement('button');
400
+ trigger.className = 'tab-overflow-trigger';
401
+ trigger.type = 'button';
402
+ trigger.setAttribute('aria-label', `${allTabs.length} tabs`);
403
+ trigger.textContent = '⋯';
404
+ const popup = document.createElement('ty-popup');
405
+ popup.setAttribute('placement', 'bottom');
406
+ const menu = document.createElement('div');
407
+ menu.className = 'tab-overflow-menu';
408
+ menu.setAttribute('role', 'menu');
409
+ allTabs.forEach((tab) => {
410
+ const tabId = getTabId(tab);
411
+ if (!tabId)
412
+ return;
413
+ const item = document.createElement('button');
414
+ item.type = 'button';
415
+ item.className = 'tab-overflow-item';
416
+ item.setAttribute('role', 'menuitem');
417
+ item.setAttribute('data-tab-id', tabId);
418
+ item.setAttribute('data-active', String(tabId === activeId));
419
+ item.textContent = getTabMenuLabel(el, tab);
420
+ if (isTabDisabled(tab)) {
421
+ item.disabled = true;
422
+ }
423
+ else {
424
+ item.addEventListener('click', () => {
425
+ activateTab(el, tabId);
426
+ popup.closePopup?.();
427
+ });
428
+ }
429
+ menu.appendChild(item);
430
+ });
431
+ popup.appendChild(menu);
432
+ trigger.appendChild(popup);
433
+ container.appendChild(trigger);
359
434
  }
360
435
  function updateOverflow(el) {
361
- const shadowRoot = el.shadowRoot;
362
- if (!shadowRoot)
363
- return;
364
- const container = shadowRoot.querySelector('.tab-buttons');
365
- if (!container)
366
- return;
367
- removeOverflowMenu(shadowRoot);
368
- const buttons = Array.from(container.querySelectorAll('.tab-button'));
369
- buttons.forEach((b) => b.classList.remove('overflow-hidden'));
370
- if (buttons.length === 0)
371
- return;
372
- const available = container.clientWidth;
373
- if (available <= 0)
374
- return;
375
- let total = 0;
376
- let cutoff = buttons.length;
377
- for (let i = 0; i < buttons.length; i++) {
378
- total += buttons[i].offsetWidth;
379
- if (total > available) {
380
- cutoff = i;
381
- break;
382
- }
383
- }
384
- if (cutoff >= buttons.length)
385
- return;
386
- total = 0;
387
- cutoff = buttons.length;
388
- for (let i = 0; i < buttons.length; i++) {
389
- total += buttons[i].offsetWidth;
390
- if (total > available - OVERFLOW_TRIGGER_WIDTH) {
391
- cutoff = i;
392
- break;
393
- }
394
- }
395
- cutoff = Math.max(cutoff, 1);
396
- const tabs = getChildTabs(el);
397
- const activeId = getActiveTabId(el, tabs);
398
- const hidden = new Set(buttons.slice(cutoff));
399
- const activeButton = activeId ? buttons.find((b) => b.dataset.tabId === activeId) : undefined;
400
- if (activeButton && hidden.has(activeButton)) {
401
- const lastVisible = buttons[cutoff - 1];
402
- hidden.delete(activeButton);
403
- hidden.add(lastVisible);
404
- }
405
- buttons.forEach((b) => b.classList.toggle('overflow-hidden', hidden.has(b)));
406
- renderOverflowMenu(el, container, buttons.filter((b) => hidden.has(b)), tabs);
436
+ const shadowRoot = el.shadowRoot;
437
+ if (!shadowRoot)
438
+ return;
439
+ const container = shadowRoot.querySelector('.tab-buttons');
440
+ const strip = shadowRoot.querySelector('.tab-strip');
441
+ if (!container || !strip)
442
+ return;
443
+ if (getTabsAttributes(el).fixed) {
444
+ removeOverflowMenu(shadowRoot);
445
+ strip.classList.add('unclipped');
446
+ strip.style.removeProperty('--fade-left');
447
+ strip.style.removeProperty('--fade-right');
448
+ return;
449
+ }
450
+ const savedScrollLeft = strip.scrollLeft;
451
+ removeOverflowMenu(shadowRoot);
452
+ if (strip.clientWidth <= 0)
453
+ return;
454
+ const buttons = strip.querySelectorAll('.tab-button');
455
+ const last = buttons[buttons.length - 1];
456
+ const contentWidth = last ? last.offsetLeft + last.offsetWidth : 0;
457
+ const stripWidth = strip.clientWidth;
458
+ const hidden = contentWidth - stripWidth;
459
+ strip.classList.toggle('unclipped', hidden <= 1);
460
+ if (hidden <= 1) {
461
+ strip.scrollLeft = savedScrollLeft;
462
+ updateEdgeFades(strip);
463
+ return;
464
+ }
465
+ renderOverflowMenu(el, container, getChildTabs(el));
466
+ const triggerCost = container.clientWidth - strip.clientWidth;
467
+ if (hidden <= triggerCost) {
468
+ removeOverflowMenu(shadowRoot);
469
+ }
470
+ strip.scrollLeft = savedScrollLeft;
471
+ updateEdgeFades(strip);
472
+ }
473
+ function updateEdgeFades(strip) {
474
+ const maxScroll = strip.scrollWidth - strip.clientWidth;
475
+ const fade = (distance) => `${distance > 1 ? Math.min(28, distance) : 0}px`;
476
+ strip.style.setProperty('--fade-left', fade(strip.scrollLeft));
477
+ strip.style.setProperty('--fade-right', fade(maxScroll - strip.scrollLeft));
478
+ }
479
+ function scrollActiveIntoView(el, activeId, smooth) {
480
+ const shadowRoot = el.shadowRoot;
481
+ if (!shadowRoot)
482
+ return;
483
+ const strip = shadowRoot.querySelector('.tab-strip');
484
+ const button = shadowRoot.querySelector(`[data-tab-id='${activeId}']`);
485
+ if (!strip || !button || strip.clientWidth <= 0)
486
+ return;
487
+ const centered = button.offsetLeft + button.offsetWidth / 2 - strip.clientWidth / 2;
488
+ const target = Math.max(0, Math.min(centered, strip.scrollWidth - strip.clientWidth));
489
+ if (Math.round(target) !== Math.round(strip.scrollLeft)) {
490
+ const reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
491
+ strip.scrollTo({ left: target, behavior: smooth && !reduce ? 'smooth' : 'auto' });
492
+ }
407
493
  }
408
494
  function render(el) {
409
- const shadowRoot = el.shadowRoot;
410
- if (!shadowRoot)
411
- return;
412
- const { width, height, placement } = getTabsAttributes(el);
413
- const tabs = getChildTabs(el);
414
- const activeId = getActiveTabId(el, tabs);
415
- const activeIndex = activeId ? (findTabIndex(tabs, activeId) ?? 0) : 0;
416
- tabs.forEach((tab) => {
417
- const tabId = getTabId(tab);
418
- if (!tabId)
419
- return;
420
- tab.setAttribute('role', 'tabpanel');
421
- tab.setAttribute('aria-labelledby', `tab-${tabId}`);
422
- tab.setAttribute('tabindex', '0');
423
- });
424
- const existingContainer = shadowRoot.querySelector('.tabs-container');
425
- const existingButtons = shadowRoot.querySelector('.tab-buttons');
426
- const existingViewport = shadowRoot.querySelector('.panels-viewport');
427
- ensureStyles(shadowRoot, { css: tabsStyles, id: 'ty-tabs' });
428
- if (tabs.length > 7) {
429
- console.warn(`[ty-tabs] More than 7 tabs detected (${tabs.length} tabs). ` +
430
- 'This may cause overflow and poor UX. ' +
431
- 'Consider using sidebar navigation, accordion menu, or other patterns.');
432
- }
433
- el.style.setProperty('--tabs-width', width.includes('%') ? '100%' : width);
434
- el.style.setProperty('--tabs-height', height);
435
- el.style.setProperty('--active-index', String(activeIndex));
436
- if (existingContainer && existingButtons && existingViewport) {
437
- existingContainer.setAttribute('data-placement', placement);
438
- existingButtons.querySelector('.tab-overflow-trigger')?.remove();
439
- const allButtons = existingButtons.querySelectorAll('.tab-button');
440
- allButtons.forEach(button => button.remove());
441
- const buttonsHtml = renderButtonsOnly(el, tabs, activeId);
442
- const tempDiv = document.createElement('div');
443
- tempDiv.innerHTML = buttonsHtml;
444
- Array.from(tempDiv.children).forEach(button => {
445
- existingButtons.appendChild(button);
446
- });
447
- setupEventListeners(el, shadowRoot, tabs);
448
- updateAriaAttributes(el, shadowRoot, activeId || '');
449
- requestAnimationFrame(() => {
450
- const buttons = shadowRoot.querySelector('.tab-buttons');
451
- if (buttons) {
452
- const buttonsHeight = buttons.offsetHeight;
453
- el.style.setProperty('--buttons-height', `${buttonsHeight}px`);
454
- }
455
- updateTransform(el, activeIndex);
456
- if (activeId) {
457
- updateMarker(el, activeId);
458
- }
459
- updateOverflow(el);
460
- });
461
- if (activeId) {
462
- updatePanelInteraction(el, activeId);
463
- }
464
- }
465
- else {
466
- shadowRoot.innerHTML = `
467
- <div class="tabs-container" data-placement="${placement}">
468
- ${renderTabButtons(el, tabs, activeId)}
469
- <div class="panels-viewport" part="panels-container">
470
- <div class="panels-wrapper">
471
- <slot></slot>
472
- </div>
473
- </div>
474
- </div>
475
- `;
476
- requestAnimationFrame(() => {
477
- const buttons = shadowRoot.querySelector('.tab-buttons');
478
- if (buttons) {
479
- const buttonsHeight = buttons.offsetHeight;
480
- el.style.setProperty('--buttons-height', `${buttonsHeight}px`);
481
- }
482
- updateTransform(el, activeIndex);
483
- if (activeId) {
484
- updateMarker(el, activeId);
485
- }
486
- updateOverflow(el);
487
- });
488
- setupEventListeners(el, shadowRoot, tabs);
489
- updateAriaAttributes(el, shadowRoot, activeId || '');
490
- setupResizeObserver(el);
491
- if (activeId) {
492
- updatePanelInteraction(el, activeId);
493
- }
494
- const markerSlot = shadowRoot.querySelector('slot[name="marker"]');
495
- if (markerSlot) {
496
- const updateDefaultMarkerVisibility = () => {
497
- const defaultMarker = shadowRoot.querySelector('.default-marker');
498
- if (defaultMarker) {
499
- const hasCustom = markerSlot.assignedElements().length > 0;
500
- defaultMarker.style.display = hasCustom ? 'none' : '';
501
- }
502
- };
503
- markerSlot.addEventListener('slotchange', updateDefaultMarkerVisibility);
504
- updateDefaultMarkerVisibility();
505
- }
506
- }
495
+ const shadowRoot = el.shadowRoot;
496
+ if (!shadowRoot)
497
+ return;
498
+ const { placement, fixed } = getTabsAttributes(el);
499
+ const tabs = getChildTabs(el);
500
+ const activeId = getActiveTabId(el, tabs);
501
+ const activeIndex = activeId ? (findTabIndex(tabs, activeId) ?? 0) : 0;
502
+ tabs.forEach((tab) => tab.setPanelLabel?.(getTabMenuLabel(el, tab)));
503
+ const existingContainer = shadowRoot.querySelector('.tabs-container');
504
+ const existingButtons = shadowRoot.querySelector('.tab-buttons');
505
+ const existingStrip = shadowRoot.querySelector('.tab-strip');
506
+ const existingViewport = shadowRoot.querySelector('.panels-viewport');
507
+ ensureStyles(shadowRoot, { css: tabsStyles, id: 'ty-tabs' });
508
+ if (existingContainer && existingButtons && existingStrip && existingViewport) {
509
+ existingContainer.setAttribute('data-placement', placement);
510
+ existingContainer.toggleAttribute('data-fixed', fixed);
511
+ const savedScrollLeft = existingStrip.scrollLeft;
512
+ existingButtons.querySelector('.tab-overflow-trigger')?.remove();
513
+ const allButtons = existingStrip.querySelectorAll('.tab-button');
514
+ allButtons.forEach(button => button.remove());
515
+ const buttonsHtml = renderButtonsOnly(el, tabs, activeId);
516
+ const tempDiv = document.createElement('div');
517
+ tempDiv.innerHTML = buttonsHtml;
518
+ Array.from(tempDiv.children).forEach(button => {
519
+ existingStrip.appendChild(button);
520
+ });
521
+ existingStrip.scrollLeft = savedScrollLeft;
522
+ setupEventListeners(el, shadowRoot, tabs);
523
+ updateAriaAttributes(el, shadowRoot, activeId || '');
524
+ const firstShow = !shownTabs.get(el);
525
+ requestAnimationFrame(() => {
526
+ const currentId = currentActive(el);
527
+ updateOverflow(el);
528
+ if (currentId) {
529
+ updateMarker(el, currentId, firstShow);
530
+ scrollActiveIntoView(el, currentId, !firstShow);
531
+ }
532
+ });
533
+ updatePanelInteraction(el, activeId);
534
+ }
535
+ else {
536
+ shadowRoot.innerHTML = `
537
+ <div class="tabs-container" data-placement="${placement}"${fixed ? ' data-fixed' : ''}>
538
+ ${renderTabButtons(el, tabs, activeId)}
539
+ <div class="panels-viewport" part="panels-container">
540
+ <div class="panels-wrapper" style="transform: ${carouselTransform(activeIndex)}">
541
+ <slot></slot>
542
+ </div>
543
+ </div>
544
+ </div>
545
+ `;
546
+ requestAnimationFrame(() => {
547
+ const currentId = currentActive(el);
548
+ updateOverflow(el);
549
+ if (currentId) {
550
+ updateMarker(el, currentId);
551
+ scrollActiveIntoView(el, currentId, false);
552
+ }
553
+ });
554
+ setupEventListeners(el, shadowRoot, tabs);
555
+ updateAriaAttributes(el, shadowRoot, activeId || '');
556
+ const strip = shadowRoot.querySelector('.tab-strip');
557
+ strip?.addEventListener('scroll', () => updateEdgeFades(strip), { passive: true });
558
+ updatePanelInteraction(el, activeId);
559
+ const markerSlot = shadowRoot.querySelector('slot[name="marker"]');
560
+ if (markerSlot) {
561
+ const updateDefaultMarkerVisibility = () => {
562
+ const defaultMarker = shadowRoot.querySelector('.default-marker');
563
+ if (defaultMarker) {
564
+ const hasCustom = markerSlot.assignedElements().length > 0;
565
+ defaultMarker.style.display = hasCustom ? 'none' : '';
566
+ }
567
+ };
568
+ markerSlot.addEventListener('slotchange', updateDefaultMarkerVisibility);
569
+ updateDefaultMarkerVisibility();
570
+ }
571
+ }
507
572
  }
508
573
  function cleanup(el) {
509
- cleanupEventListeners(el);
510
- cleanupResizeObserver(el);
574
+ cleanupEventListeners(el);
575
+ cleanupResizeObserver(el);
511
576
  }
512
577
  export class TyTabs extends HTMLElement {
513
- static get observedAttributes() {
514
- return ['width', 'height', 'active', 'placement'];
515
- }
516
- constructor() {
517
- super();
518
- this._childObserver = null;
519
- this._childRenderQueued = false;
520
- this.attachShadow({ mode: 'open' });
521
- }
522
- connectedCallback() {
523
- render(this);
524
- this._childObserver = new MutationObserver(() => {
525
- if (this._childRenderQueued)
526
- return;
527
- this._childRenderQueued = true;
528
- queueMicrotask(() => {
529
- this._childRenderQueued = false;
530
- if (this.isConnected)
531
- render(this);
532
- });
533
- });
534
- this._childObserver.observe(this, { childList: true });
535
- }
536
- disconnectedCallback() {
537
- this._childObserver?.disconnect();
538
- this._childObserver = null;
539
- cleanup(this);
540
- }
541
- attributeChangedCallback(name, oldValue, newValue) {
542
- if (name === 'active') {
543
- if (newValue) {
544
- updateActiveTabState(this, newValue, oldValue);
545
- }
546
- render(this);
547
- }
548
- else {
549
- render(this);
550
- }
551
- }
578
+ static get observedAttributes() {
579
+ return ['width', 'height', 'active', 'placement', 'fixed'];
580
+ }
581
+ constructor() {
582
+ super();
583
+ this._childObserver = null;
584
+ this._updateQueued = false;
585
+ this._structureDirty = false;
586
+ this.attachShadow({ mode: 'open' });
587
+ }
588
+ get activeTab() {
589
+ return shownTabs.get(this) ?? null;
590
+ }
591
+ connectedCallback() {
592
+ render(this);
593
+ syncShownTab(this);
594
+ setupResizeObserver(this);
595
+ this._childObserver = new MutationObserver(() => this._queueUpdate(true));
596
+ this._childObserver.observe(this, { childList: true });
597
+ }
598
+ disconnectedCallback() {
599
+ this._childObserver?.disconnect();
600
+ this._childObserver = null;
601
+ cleanup(this);
602
+ }
603
+ _queueUpdate(structure) {
604
+ this._structureDirty || (this._structureDirty = structure);
605
+ if (this._updateQueued)
606
+ return;
607
+ this._updateQueued = true;
608
+ queueMicrotask(() => {
609
+ this._updateQueued = false;
610
+ if (!this.isConnected)
611
+ return;
612
+ if (this._childObserver?.takeRecords().length)
613
+ this._structureDirty = true;
614
+ if (this._structureDirty) {
615
+ this._structureDirty = false;
616
+ render(this);
617
+ }
618
+ syncShownTab(this);
619
+ });
620
+ }
621
+ attributeChangedCallback(name, oldValue, newValue) {
622
+ if (!this.isConnected)
623
+ return;
624
+ if (name === 'active') {
625
+ if (oldValue !== newValue)
626
+ awaitedTabs.delete(this);
627
+ this._queueUpdate(false);
628
+ }
629
+ else
630
+ render(this);
631
+ }
552
632
  }
553
633
  if (!customElements.get('ty-tabs')) {
554
- customElements.define('ty-tabs', TyTabs);
634
+ customElements.define('ty-tabs', TyTabs);
555
635
  }