@jsenv/dom 0.1.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 (101) hide show
  1. package/dist/jsenv_dom.js +9653 -0
  2. package/index.js +101 -0
  3. package/package.json +47 -0
  4. package/src/attr/add_attribute_effect.js +93 -0
  5. package/src/attr/attributes.js +32 -0
  6. package/src/demos/3_columns_resize_demo.html +84 -0
  7. package/src/demos/3_rows_resize_demo.html +89 -0
  8. package/src/demos/aside_and_main_demo.html +93 -0
  9. package/src/demos/coordinates_demo.html +450 -0
  10. package/src/demos/document_autoscroll_demo.html +517 -0
  11. package/src/demos/drag_gesture_constraints_demo.html +701 -0
  12. package/src/demos/drag_gesture_demo.html +1047 -0
  13. package/src/demos/drag_gesture_element_to_impact_demo.html +445 -0
  14. package/src/demos/drag_reference_element_demo.html +480 -0
  15. package/src/demos/flex_details_set_demo.html +302 -0
  16. package/src/demos/flex_details_set_demo_2.html +315 -0
  17. package/src/demos/visible_rect_demo.html +525 -0
  18. package/src/interaction/drag/constraint_feedback_line.js +92 -0
  19. package/src/interaction/drag/drag_constraint.js +659 -0
  20. package/src/interaction/drag/drag_debug_markers.js +635 -0
  21. package/src/interaction/drag/drag_element_positioner.js +382 -0
  22. package/src/interaction/drag/drag_gesture.js +566 -0
  23. package/src/interaction/drag/drag_resize_demo.html +571 -0
  24. package/src/interaction/drag/drag_to_move.js +301 -0
  25. package/src/interaction/drag/drag_to_resize_gesture.js +68 -0
  26. package/src/interaction/drag/drop_target_detection.js +148 -0
  27. package/src/interaction/drag/sticky_frontiers.js +160 -0
  28. package/src/interaction/element_log.js +8 -0
  29. package/src/interaction/event_marker.js +14 -0
  30. package/src/interaction/focus/active_element.js +33 -0
  31. package/src/interaction/focus/arrow_navigation.js +599 -0
  32. package/src/interaction/focus/element_is_focusable.js +57 -0
  33. package/src/interaction/focus/element_is_visible.js +36 -0
  34. package/src/interaction/focus/find_focusable.js +21 -0
  35. package/src/interaction/focus/focus_group.js +91 -0
  36. package/src/interaction/focus/focus_group_registry.js +12 -0
  37. package/src/interaction/focus/focus_nav.js +12 -0
  38. package/src/interaction/focus/focus_nav_event_marker.js +14 -0
  39. package/src/interaction/focus/focus_trap.js +105 -0
  40. package/src/interaction/focus/tab_navigation.js +128 -0
  41. package/src/interaction/focus/tests/focus_group_skip_tab_test.html +206 -0
  42. package/src/interaction/focus/tests/tree_focus_test.html +304 -0
  43. package/src/interaction/focus/tests/tree_focus_test.jsx +261 -0
  44. package/src/interaction/focus/tests/tree_focus_test_preact.html +13 -0
  45. package/src/interaction/isolate_interactions.js +161 -0
  46. package/src/interaction/keyboard.js +26 -0
  47. package/src/interaction/scroll/capture_scroll.js +47 -0
  48. package/src/interaction/scroll/is_scrollable.js +159 -0
  49. package/src/interaction/scroll/scroll_container.js +110 -0
  50. package/src/interaction/scroll/scroll_trap.js +44 -0
  51. package/src/interaction/scroll/scrollbar_size.js +20 -0
  52. package/src/interaction/scroll/wheel_through.js +138 -0
  53. package/src/iterable_weak_set.js +66 -0
  54. package/src/position/dom_coords.js +340 -0
  55. package/src/position/offset_parent.js +15 -0
  56. package/src/position/position_fixed.js +15 -0
  57. package/src/position/position_sticky.js +213 -0
  58. package/src/position/sticky_rect.js +79 -0
  59. package/src/position/visible_rect.js +482 -0
  60. package/src/pub_sub.js +28 -0
  61. package/src/size/can_take_size.js +11 -0
  62. package/src/size/details_content_full_height.js +63 -0
  63. package/src/size/flex_details_set.js +974 -0
  64. package/src/size/get_available_height.js +22 -0
  65. package/src/size/get_available_width.js +22 -0
  66. package/src/size/get_border_sizes.js +14 -0
  67. package/src/size/get_height.js +4 -0
  68. package/src/size/get_inner_height.js +15 -0
  69. package/src/size/get_inner_width.js +15 -0
  70. package/src/size/get_margin_sizes.js +10 -0
  71. package/src/size/get_max_height.js +57 -0
  72. package/src/size/get_max_width.js +47 -0
  73. package/src/size/get_min_height.js +14 -0
  74. package/src/size/get_min_width.js +14 -0
  75. package/src/size/get_padding_sizes.js +10 -0
  76. package/src/size/get_width.js +4 -0
  77. package/src/size/hooks/use_available_height.js +27 -0
  78. package/src/size/hooks/use_available_width.js +27 -0
  79. package/src/size/hooks/use_max_height.js +10 -0
  80. package/src/size/hooks/use_max_width.js +10 -0
  81. package/src/size/hooks/use_resize_status.js +62 -0
  82. package/src/size/resize.js +695 -0
  83. package/src/size/resolve_css_size.js +32 -0
  84. package/src/style/dom_styles.js +97 -0
  85. package/src/style/style_composition.js +78 -0
  86. package/src/style/style_controller.js +345 -0
  87. package/src/style/style_parsing.js +317 -0
  88. package/src/transition/demos/animation_resumption_test.xhtml +500 -0
  89. package/src/transition/demos/height_toggle_test.xhtml +515 -0
  90. package/src/transition/dom_transition.js +254 -0
  91. package/src/transition/easing.js +48 -0
  92. package/src/transition/group_transition.js +261 -0
  93. package/src/transition/transform_style_parser.js +32 -0
  94. package/src/transition/transition_playback.js +366 -0
  95. package/src/transition/transition_timeline.js +79 -0
  96. package/src/traversal.js +247 -0
  97. package/src/ui_transition/demos/content_states_transition_demo.html +628 -0
  98. package/src/ui_transition/demos/smooth_height_transition_demo.html +149 -0
  99. package/src/ui_transition/demos/transition_testing.html +354 -0
  100. package/src/ui_transition/ui_transition.js +1492 -0
  101. package/src/utils.js +69 -0
@@ -0,0 +1,213 @@
1
+ /**
2
+ * Position Sticky Polyfill
3
+ *
4
+ * This module provides a workaround for position:sticky limitations when used with
5
+ * overflow:auto/hidden parent elements (see https://github.com/w3c/csswg-drafts/issues/865).
6
+ *
7
+ * How it works:
8
+ * 1. Creates a placeholder clone of the sticky element to maintain document flow
9
+ * 2. Positions the real element using fixed positioning relative to viewport
10
+ * 3. Adjusts position on scroll to emulate position:sticky behavior
11
+ * 4. Handles parent boundary detection to keep element within its container
12
+ * 5. Updates dimensions on resize and DOM changes
13
+ *
14
+ * Usage:
15
+ * ```
16
+ * const cleanup = initPositionSticky(element);
17
+ * // Later when no longer needed
18
+ * cleanup();
19
+ * ```
20
+ *
21
+ * The element should have a CSS "top" value specified (e.g., top: 10px).
22
+ */
23
+
24
+ import { getScrollContainerSet } from "../interaction/scroll/scroll_container.js";
25
+ import { getHeight } from "../size/get_height.js";
26
+ import { getWidth } from "../size/get_width.js";
27
+ import { forceStyles, setStyles } from "../style/dom_styles.js";
28
+
29
+ import.meta.css = /* css */ `
30
+ [data-position-sticky-placeholder] {
31
+ opacity: 0 !important;
32
+ position: static !important;
33
+ width: auto !important;
34
+ height: auto !important;
35
+ }
36
+ `;
37
+
38
+ export const initPositionSticky = (element) => {
39
+ const computedStyle = getComputedStyle(element);
40
+ const topCssValue = computedStyle.top;
41
+ const top = parseFloat(topCssValue);
42
+ if (isNaN(top)) {
43
+ return () => {}; // Early return if no valid top value
44
+ }
45
+
46
+ // Skip polyfill if native position:sticky would work (no overflow:auto/hidden parents)
47
+ const scrollContainerSet = getScrollContainerSet(element);
48
+ check_overflow_on_containers: {
49
+ let hasOverflowHiddenOrAuto = false;
50
+ for (const scrollContainer of scrollContainerSet) {
51
+ const scrollContainerComputedStyle = getComputedStyle(scrollContainer);
52
+ const overflowX = scrollContainerComputedStyle.overflowX;
53
+ if (overflowX === "auto" || overflowX === "hidden") {
54
+ hasOverflowHiddenOrAuto = true;
55
+ break;
56
+ }
57
+ const overflowY = scrollContainerComputedStyle.overflowY;
58
+ if (overflowY === "auto" || overflowY === "hidden") {
59
+ hasOverflowHiddenOrAuto = true;
60
+ break;
61
+ }
62
+ }
63
+ if (!hasOverflowHiddenOrAuto) {
64
+ return () => {}; // Native sticky will work fine
65
+ }
66
+ }
67
+
68
+ const cleanupCallbackSet = new Set();
69
+ const cleanup = () => {
70
+ for (const cleanupCallback of cleanupCallbackSet) {
71
+ cleanupCallback();
72
+ }
73
+ cleanupCallbackSet.clear();
74
+ };
75
+
76
+ const parentElement = element.parentElement;
77
+ const createPlaceholderClone = () => {
78
+ const clone = element.cloneNode(true);
79
+ clone.setAttribute("data-position-sticky-placeholder", "");
80
+ clone.removeAttribute("data-sticky");
81
+ return clone;
82
+ };
83
+
84
+ let placeholder = createPlaceholderClone();
85
+ parentElement.insertBefore(placeholder, element);
86
+ cleanupCallbackSet.add(() => {
87
+ placeholder.remove();
88
+ });
89
+
90
+ let width = getWidth(element);
91
+ let height = getHeight(element);
92
+
93
+ const updateSize = () => {
94
+ const newPlaceholder = createPlaceholderClone();
95
+ parentElement.replaceChild(newPlaceholder, placeholder);
96
+ placeholder = newPlaceholder;
97
+ width = getWidth(placeholder);
98
+ height = getHeight(placeholder);
99
+ updatePosition();
100
+ };
101
+
102
+ const updatePosition = () => {
103
+ // Ensure placeholder dimensions match element
104
+ setStyles(placeholder, {
105
+ width: `${width}px`,
106
+ height: `${height}px`,
107
+ });
108
+
109
+ const placeholderRect = placeholder.getBoundingClientRect();
110
+ const parentRect = parentElement.getBoundingClientRect();
111
+
112
+ // Calculate left position in viewport coordinates (fixed positioning)
113
+ const leftPosition = placeholderRect.left;
114
+ element.style.left = `${Math.round(leftPosition)}px`;
115
+
116
+ // Determine if element should be sticky or at its natural position
117
+ let topPosition;
118
+ let isStuck = false;
119
+
120
+ // Check if we need to stick the element
121
+ if (placeholderRect.top <= top) {
122
+ // Element should be stuck at "top" position in the viewport
123
+ topPosition = top;
124
+ isStuck = true;
125
+
126
+ // But make sure it doesn't go beyond parent's bottom boundary
127
+ const parentBottom = parentRect.bottom;
128
+ const elementBottom = top + height;
129
+
130
+ if (elementBottom > parentBottom) {
131
+ // Adjust to stay within parent
132
+ topPosition = parentBottom - height;
133
+ }
134
+ } else {
135
+ // Element should be at its natural position in the flow
136
+ topPosition = placeholderRect.top;
137
+ }
138
+
139
+ element.style.top = `${topPosition}px`;
140
+ element.style.width = `${width}px`;
141
+ element.style.height = `${height}px`;
142
+
143
+ // Set attribute for potential styling
144
+ if (isStuck) {
145
+ element.setAttribute("data-sticky", "");
146
+ } else {
147
+ element.removeAttribute("data-sticky");
148
+ }
149
+ };
150
+
151
+ element_is_fixed: {
152
+ const restorePositionStyle = forceStyles(element, {
153
+ "position": "fixed",
154
+ "z-index": 1,
155
+ "will-change": "transform", // Hint for hardware acceleration
156
+ });
157
+ cleanupCallbackSet.add(restorePositionStyle);
158
+ }
159
+
160
+ updatePosition();
161
+
162
+ update_on_scroll: {
163
+ const handleScroll = () => {
164
+ updatePosition();
165
+ };
166
+
167
+ for (const scrollContainer of scrollContainerSet) {
168
+ scrollContainer.addEventListener("scroll", handleScroll, {
169
+ passive: true,
170
+ });
171
+ cleanupCallbackSet.add(() => {
172
+ scrollContainer.removeEventListener("scroll", handleScroll, {
173
+ passive: true,
174
+ });
175
+ });
176
+ }
177
+ }
178
+
179
+ update_on_parent_size_change: {
180
+ let animationFrame = null;
181
+ const resizeObserver = new ResizeObserver(() => {
182
+ if (animationFrame !== null) {
183
+ return;
184
+ }
185
+ animationFrame = requestAnimationFrame(() => {
186
+ animationFrame = null;
187
+ updateSize();
188
+ });
189
+ });
190
+ resizeObserver.observe(parentElement);
191
+ cleanupCallbackSet.add(() => {
192
+ resizeObserver.disconnect();
193
+ cancelAnimationFrame(animationFrame);
194
+ animationFrame = null;
195
+ });
196
+ }
197
+
198
+ update_on_dom_mutation: {
199
+ const mutationObserver = new MutationObserver(() => {
200
+ updateSize();
201
+ });
202
+ mutationObserver.observe(element, {
203
+ childList: true,
204
+ subtree: true,
205
+ characterData: true,
206
+ });
207
+ cleanupCallbackSet.add(() => {
208
+ mutationObserver.disconnect();
209
+ });
210
+ }
211
+
212
+ return cleanup;
213
+ };
@@ -0,0 +1,79 @@
1
+ import { getScrollContainer } from "../interaction/scroll/scroll_container.js";
2
+
3
+ export const stickyAsRelativeCoords = (
4
+ element,
5
+ referenceElement,
6
+ { scrollContainer = getScrollContainer(element) } = {},
7
+ ) => {
8
+ const hasStickyLeftAttribute = element.hasAttribute("data-sticky-left");
9
+ const hasTopStickyAttribute = element.hasAttribute("data-sticky-top");
10
+ if (!hasStickyLeftAttribute && !hasTopStickyAttribute) {
11
+ return null;
12
+ }
13
+ const elementRect = element.getBoundingClientRect();
14
+ const referenceElementRect = referenceElement.getBoundingClientRect();
15
+ const computedStyle = getComputedStyle(element);
16
+ const isDocumentScrolling = scrollContainer === document.documentElement;
17
+
18
+ let leftPosition;
19
+ let topPosition;
20
+ if (isDocumentScrolling) {
21
+ // For document scrolling: check if element is currently stuck and calculate offset
22
+
23
+ if (hasStickyLeftAttribute) {
24
+ const cssLeftValue = parseFloat(computedStyle.left) || 0;
25
+ const isStuckLeft = elementRect.left <= cssLeftValue;
26
+ if (isStuckLeft) {
27
+ const elementOffsetRelative =
28
+ elementRect.left - referenceElementRect.left;
29
+ leftPosition = elementOffsetRelative - cssLeftValue;
30
+ } else {
31
+ leftPosition = 0;
32
+ }
33
+ }
34
+ if (hasTopStickyAttribute) {
35
+ const cssTopValue = parseFloat(computedStyle.top) || 0;
36
+ const isStuckTop = elementRect.top <= cssTopValue;
37
+ if (isStuckTop) {
38
+ const elementOffsetRelative =
39
+ elementRect.top - referenceElementRect.top;
40
+ topPosition = elementOffsetRelative - cssTopValue;
41
+ } else {
42
+ topPosition = 0;
43
+ }
44
+ }
45
+ return [leftPosition, topPosition];
46
+ }
47
+
48
+ // For container scrolling: check if element is currently stuck and calculate offset
49
+ const scrollContainerRect = scrollContainer.getBoundingClientRect();
50
+ if (hasStickyLeftAttribute) {
51
+ const cssLeftValue = parseFloat(computedStyle.left) || 0;
52
+ // Check if element is stuck to the left edge of the scrollable container
53
+ const isStuckLeft =
54
+ elementRect.left <= scrollContainerRect.left + cssLeftValue;
55
+ if (isStuckLeft) {
56
+ // Element is stuck - calculate its offset relative to reference element
57
+ const elementOffsetRelative =
58
+ elementRect.left - referenceElementRect.left;
59
+ leftPosition = elementOffsetRelative - cssLeftValue;
60
+ } else {
61
+ // Element is not stuck - behaves like position: relative with no offset
62
+ leftPosition = 0;
63
+ }
64
+ }
65
+ if (hasTopStickyAttribute) {
66
+ const cssTopValue = parseFloat(computedStyle.top) || 0;
67
+ // Check if element is stuck to the top edge of the scrollable container
68
+ const isStuckTop = elementRect.top <= scrollContainerRect.top + cssTopValue;
69
+ if (isStuckTop) {
70
+ // Element is stuck - calculate its offset relative to reference element
71
+ const elementOffsetRelative = elementRect.top - referenceElementRect.top;
72
+ topPosition = elementOffsetRelative - cssTopValue;
73
+ } else {
74
+ // Element is not stuck - behaves like position: relative with no offset
75
+ topPosition = 0;
76
+ }
77
+ }
78
+ return [leftPosition, topPosition];
79
+ };