@humandialog/forms.svelte 1.7.15 → 1.7.17

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.
@@ -3,6 +3,7 @@ import { isDeviceSmallerThan } from "../utils";
3
3
  import { pushToolsActionsOperations, popToolsActionsOperations, fabHiddenDueToPopup } from "../stores";
4
4
  import { FaTimes } from "svelte-icons/fa";
5
5
  import Icon from "./icon.svelte";
6
+ import { usePreventScroll } from "./react-aria/preventScroll";
6
7
  let x;
7
8
  let y;
8
9
  let visible = false;
@@ -13,6 +14,7 @@ let rootElement;
13
14
  let internalElement;
14
15
  let closeButtonPos = "";
15
16
  let maxHeight = 0;
17
+ let preventScrollRestorer = null;
16
18
  export async function show(around, _toolbar, _props = {}) {
17
19
  if (around instanceof DOMRect) {
18
20
  x = around.left;
@@ -37,6 +39,8 @@ export async function show(around, _toolbar, _props = {}) {
37
39
  props.onSizeChanged = () => onSizeChanged();
38
40
  hide_window_indicator = 0;
39
41
  window.addEventListener("click", on_before_window_click, true);
42
+ if (isDeviceSmallerThan("sm"))
43
+ preventScrollRestorer = usePreventScroll();
40
44
  if (isDeviceSmallerThan("sm")) {
41
45
  $fabHiddenDueToPopup = true;
42
46
  }
@@ -57,6 +61,10 @@ export function hide() {
57
61
  cssPosition = calculatePosition(x, y, around_rect, false, false);
58
62
  window.removeEventListener("click", on_before_window_click, true);
59
63
  rootElement?.removeEventListener("click", on_before_container_click, true);
64
+ if (preventScrollRestorer) {
65
+ preventScrollRestorer();
66
+ preventScrollRestorer = null;
67
+ }
60
68
  }
61
69
  export function isSameToolbar(_toolbar) {
62
70
  return _toolbar == toolbar;
@@ -114,7 +122,7 @@ function calculatePosition(x2, y2, around, visible2, fresh) {
114
122
  y2 = screenRect.bottom - maxHeight - margin;
115
123
  result = `left: ${x2}px; bottom: ${margin}px; width: ${width}px; max-height: ${maxHeight}px; display: block`;
116
124
  }
117
- closeButtonPos = `right: 0.5rem; top: calc(${y2}px + 0.25rem)`;
125
+ closeButtonPos = `right: 0.5rem; bottom: calc(${margin + maxHeight}px - 1.75rem)`;
118
126
  } else {
119
127
  let myRect = null;
120
128
  if (!fresh) {
@@ -161,9 +169,10 @@ function calculatePosition(x2, y2, around, visible2, fresh) {
161
169
  {/if}
162
170
  </div>
163
171
 
164
- <style>
172
+ <!-- use usePreventScroll instead -->
173
+ <!--style>
165
174
  :global(body:has(#__hd_svelte_floating_container[visible="true"]))
166
175
  {
167
176
  overflow: hidden;
168
177
  }
169
- </style>
178
+ </style-->
@@ -13,6 +13,7 @@ import {
13
13
  SHOW_MENU_LEFT
14
14
  } from "./menu";
15
15
  import { FaTimes } from "svelte-icons/fa";
16
+ import { usePreventScroll } from "./react-aria/preventScroll";
16
17
  export let widthPx = 400;
17
18
  export let menu_items_id_prefix = "__hd_svelte_menuitem_";
18
19
  export let owner_menu_item = void 0;
@@ -49,8 +50,9 @@ function calculatePosition(x2, y2, visible2, fresh) {
49
50
  myRect = null;
50
51
  }
51
52
  }
53
+ let maxHeight;
52
54
  if (myRect) {
53
- let maxHeight = screenRect.height / 2 - margin;
55
+ maxHeight = screenRect.height / 2 - margin;
54
56
  if (myRect.height < maxHeight)
55
57
  maxHeight = myRect.height;
56
58
  const width = screenRect.width - 2 * margin;
@@ -58,13 +60,13 @@ function calculatePosition(x2, y2, visible2, fresh) {
58
60
  y2 = screenRect.bottom - maxHeight - margin;
59
61
  result = `left: ${x2}px; bottom: ${margin}px; width: ${width}px; max-height: ${maxHeight}px; display: block`;
60
62
  } else {
61
- const maxHeight = screenRect.height / 2 - margin;
63
+ maxHeight = screenRect.height / 2 - margin;
62
64
  const width = screenRect.width - 2 * margin;
63
65
  x2 = margin;
64
66
  y2 = screenRect.bottom - maxHeight - margin;
65
67
  result = `left: ${x2}px; bottom: ${margin}px; width: ${width}px; max-height: ${maxHeight}px; display: block`;
66
68
  }
67
- closeButtonPos = `right: 0.5rem; top: calc(${y2}px + 0.25rem)`;
69
+ closeButtonPos = `right: 0.5rem; bottom: calc(${margin + maxHeight}px - 1.75rem)`;
68
70
  } else {
69
71
  let myRect = null;
70
72
  if (!fresh) {
@@ -164,6 +166,7 @@ function intersects(lpRect1, lpRect2) {
164
166
  const bottom = Math.min(lpRect1.bottom, lpRect2.bottom);
165
167
  return left <= right && top <= bottom;
166
168
  }
169
+ let preventScrollRestorer = null;
167
170
  export async function show(around, _operations, preference = 0) {
168
171
  if (around instanceof DOMRect) {
169
172
  switch (preference) {
@@ -199,6 +202,8 @@ export async function show(around, _operations, preference = 0) {
199
202
  if (is_root_menu) {
200
203
  hide_window_indicator = 0;
201
204
  window.addEventListener("click", on_before_window_click, true);
205
+ if (isDeviceSmallerThan("sm"))
206
+ preventScrollRestorer = usePreventScroll();
202
207
  }
203
208
  if (isDeviceSmallerThan("sm")) {
204
209
  $fabHiddenDueToPopup = true;
@@ -221,6 +226,10 @@ export function hide() {
221
226
  css_position = calculatePosition(x, y, false, false);
222
227
  window.removeEventListener("click", on_before_window_click, true);
223
228
  menu_root?.removeEventListener("click", on_before_container_click, true);
229
+ if (preventScrollRestorer) {
230
+ preventScrollRestorer();
231
+ preventScrollRestorer = null;
232
+ }
224
233
  }
225
234
  export function getRenderedRect() {
226
235
  if (menu_root)
@@ -485,9 +494,11 @@ function isOperationDisabled(operation) {
485
494
  {/each}
486
495
  </div>
487
496
 
488
- <style>
489
- :global(body:has(#__hd_svelte_contextmenu[visible="true"]))
497
+ <!-- use usePreventScroll instead -->
498
+ <!--style>
499
+ :global(#__hd_svelte_layout_root:has(#__hd_svelte_contextmenu[visible="true"]))
490
500
  {
491
501
  overflow: hidden;
502
+ position: fixed;
492
503
  }
493
- </style>
504
+ </style-->
@@ -77,7 +77,6 @@ export function show(x, y, up = false) {
77
77
  setTimeout(() => {
78
78
  const rect = paletteElement.getBoundingClientRect();
79
79
  closeButtonPos = `right: 0.5rem; top: 0.25rem`;
80
- console.log("closeButtonPos", closeButtonPos);
81
80
  }, 0);
82
81
  }
83
82
  }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Calls all functions in the order they were chained with the same arguments.
3
+ */
4
+ export declare function chain(...callbacks: any[]): (...args: any[]) => void;
@@ -0,0 +1,23 @@
1
+ /*
2
+ * Copyright 2020 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+ /**
13
+ * Calls all functions in the order they were chained with the same arguments.
14
+ */
15
+ export function chain(...callbacks) {
16
+ return (...args) => {
17
+ for (let callback of callbacks) {
18
+ if (typeof callback === 'function') {
19
+ callback(...args);
20
+ }
21
+ }
22
+ };
23
+ }
@@ -0,0 +1 @@
1
+ export declare function getScrollParent(node: Element, checkForOverflow?: boolean): Element;
@@ -0,0 +1,22 @@
1
+ /*
2
+ * Copyright 2020 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+ import { isScrollable } from './isScrollable';
13
+ export function getScrollParent(node, checkForOverflow) {
14
+ let scrollableNode = node;
15
+ if (isScrollable(scrollableNode, checkForOverflow)) {
16
+ scrollableNode = scrollableNode.parentElement;
17
+ }
18
+ while (scrollableNode && !isScrollable(scrollableNode, checkForOverflow)) {
19
+ scrollableNode = scrollableNode.parentElement;
20
+ }
21
+ return scrollableNode || document.scrollingElement || document.documentElement;
22
+ }
@@ -0,0 +1 @@
1
+ export declare function isScrollable(node: Element | null, checkForOverflow?: boolean): boolean;
@@ -0,0 +1,22 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+ export function isScrollable(node, checkForOverflow) {
13
+ if (!node) {
14
+ return false;
15
+ }
16
+ let style = window.getComputedStyle(node);
17
+ let isScrollable = /(auto|scroll)/.test(style.overflow + style.overflowX + style.overflowY);
18
+ if (isScrollable && checkForOverflow) {
19
+ isScrollable = node.scrollHeight !== node.clientHeight || node.scrollWidth !== node.clientWidth;
20
+ }
21
+ return isScrollable;
22
+ }
@@ -0,0 +1,8 @@
1
+ interface Event {
2
+ altKey: boolean;
3
+ ctrlKey: boolean;
4
+ metaKey: boolean;
5
+ }
6
+ export declare function isCtrlKeyPressed(e: Event): boolean;
7
+ export declare function willOpenKeyboard(target: Element): boolean;
8
+ export {};
@@ -0,0 +1,35 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+ import { isMac } from './platform';
13
+ export function isCtrlKeyPressed(e) {
14
+ if (isMac()) {
15
+ return e.metaKey;
16
+ }
17
+ return e.ctrlKey;
18
+ }
19
+ // HTML input types that do not cause the software keyboard to appear.
20
+ const nonTextInputTypes = new Set([
21
+ 'checkbox',
22
+ 'radio',
23
+ 'range',
24
+ 'color',
25
+ 'file',
26
+ 'image',
27
+ 'button',
28
+ 'submit',
29
+ 'reset'
30
+ ]);
31
+ export function willOpenKeyboard(target) {
32
+ return ((target instanceof HTMLInputElement && !nonTextInputTypes.has(target.type)) ||
33
+ target instanceof HTMLTextAreaElement ||
34
+ (target instanceof HTMLElement && target.isContentEditable));
35
+ }
@@ -0,0 +1,9 @@
1
+ export declare const isMac: () => boolean;
2
+ export declare const isIPhone: () => boolean;
3
+ export declare const isIPad: () => boolean;
4
+ export declare const isIOS: () => boolean;
5
+ export declare const isAppleDevice: () => boolean;
6
+ export declare const isWebKit: () => boolean;
7
+ export declare const isChrome: () => boolean;
8
+ export declare const isAndroid: () => boolean;
9
+ export declare const isFirefox: () => boolean;
@@ -0,0 +1,65 @@
1
+ /*
2
+ * Copyright 2020 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+ function testUserAgent(re) {
13
+ if (typeof window === 'undefined' || window.navigator == null) {
14
+ return false;
15
+ }
16
+ let brands = window.navigator['userAgentData']?.brands;
17
+ return Array.isArray(brands) && brands.some((brand) => re.test(brand.brand)) ||
18
+ re.test(window.navigator.userAgent);
19
+ }
20
+ function testPlatform(re) {
21
+ return typeof window !== 'undefined' && window.navigator != null
22
+ ? re.test(window.navigator['userAgentData']?.platform || window.navigator.platform)
23
+ : false;
24
+ }
25
+ function cached(fn) {
26
+ if (process.env.NODE_ENV === 'test') {
27
+ return fn;
28
+ }
29
+ let res = null;
30
+ return () => {
31
+ if (res == null) {
32
+ res = fn();
33
+ }
34
+ return res;
35
+ };
36
+ }
37
+ export const isMac = cached(function () {
38
+ return testPlatform(/^Mac/i);
39
+ });
40
+ export const isIPhone = cached(function () {
41
+ return testPlatform(/^iPhone/i);
42
+ });
43
+ export const isIPad = cached(function () {
44
+ return testPlatform(/^iPad/i) ||
45
+ // iPadOS 13 lies and says it's a Mac, but we can distinguish by detecting touch support.
46
+ (isMac() && navigator.maxTouchPoints > 1);
47
+ });
48
+ export const isIOS = cached(function () {
49
+ return isIPhone() || isIPad();
50
+ });
51
+ export const isAppleDevice = cached(function () {
52
+ return isMac() || isIOS();
53
+ });
54
+ export const isWebKit = cached(function () {
55
+ return testUserAgent(/AppleWebKit/i) && !isChrome();
56
+ });
57
+ export const isChrome = cached(function () {
58
+ return testUserAgent(/Chrome/i);
59
+ });
60
+ export const isAndroid = cached(function () {
61
+ return testUserAgent(/Android/i);
62
+ });
63
+ export const isFirefox = cached(function () {
64
+ return testUserAgent(/Firefox/i);
65
+ });
@@ -0,0 +1,11 @@
1
+ interface PreventScrollOptions {
2
+ /** Whether the scroll lock is disabled. */
3
+ isDisabled?: boolean;
4
+ }
5
+ /**
6
+ * Prevents scrolling on the document body on mount, and
7
+ * restores it on unmount. Also ensures that content does not
8
+ * shift due to the scrollbars disappearing.
9
+ */
10
+ export declare function usePreventScroll(options?: PreventScrollOptions): void;
11
+ export {};
@@ -0,0 +1,246 @@
1
+ /*
2
+ * Copyright 2020 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+ import { isIOS } from './platform';
13
+ import { willOpenKeyboard } from './keybaord';
14
+ import { isScrollable } from './isScrollable';
15
+ import { getScrollParent } from './getScrollParent';
16
+ import { chain } from './chain';
17
+ const visualViewport = typeof document !== 'undefined' && window.visualViewport;
18
+ // The number of active usePreventScroll calls. Used to determine whether to revert back to the original page style/scroll position
19
+ let preventScrollCount = 0;
20
+ let restore;
21
+ /**
22
+ * Prevents scrolling on the document body on mount, and
23
+ * restores it on unmount. Also ensures that content does not
24
+ * shift due to the scrollbars disappearing.
25
+ */
26
+ /*export function usePreventScroll(options: PreventScrollOptions = {}): void {
27
+ let {isDisabled} = options;
28
+
29
+ useLayoutEffect(() => {
30
+ if (isDisabled) {
31
+ return;
32
+ }
33
+
34
+ preventScrollCount++;
35
+ if (preventScrollCount === 1) {
36
+ if (isIOS()) {
37
+ restore = preventScrollMobileSafari();
38
+ } else {
39
+ restore = preventScrollStandard();
40
+ }
41
+ }
42
+
43
+ return () => {
44
+ preventScrollCount--;
45
+ if (preventScrollCount === 0) {
46
+ restore();
47
+ }
48
+ };
49
+ }, [isDisabled]);
50
+ }
51
+ */
52
+ export function usePreventScroll(options = {}) {
53
+ let { isDisabled } = options;
54
+ preventScrollCount++;
55
+ if (preventScrollCount === 1) {
56
+ if (isIOS()) {
57
+ restore = preventScrollMobileSafari();
58
+ }
59
+ else {
60
+ restore = preventScrollStandard();
61
+ }
62
+ }
63
+ return () => {
64
+ preventScrollCount--;
65
+ if (preventScrollCount === 0) {
66
+ restore();
67
+ }
68
+ };
69
+ }
70
+ // For most browsers, all we need to do is set `overflow: hidden` on the root element, and
71
+ // add some padding to prevent the page from shifting when the scrollbar is hidden.
72
+ function preventScrollStandard() {
73
+ let scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;
74
+ return chain(scrollbarWidth > 0 &&
75
+ // Use scrollbar-gutter when supported because it also works for fixed positioned elements.
76
+ ('scrollbarGutter' in document.documentElement.style
77
+ ? setStyle(document.documentElement, 'scrollbarGutter', 'stable')
78
+ : setStyle(document.documentElement, 'paddingRight', `${scrollbarWidth}px`)), setStyle(document.documentElement, 'overflow', 'hidden'));
79
+ }
80
+ // Mobile Safari is a whole different beast. Even with overflow: hidden,
81
+ // it still scrolls the page in many situations:
82
+ //
83
+ // 1. When the bottom toolbar and address bar are collapsed, page scrolling is always allowed.
84
+ // 2. When the keyboard is visible, the viewport does not resize. Instead, the keyboard covers part of
85
+ // it, so it becomes scrollable.
86
+ // 3. When tapping on an input, the page always scrolls so that the input is centered in the visual viewport.
87
+ // This may cause even fixed position elements to scroll off the screen.
88
+ // 4. When using the next/previous buttons in the keyboard to navigate between inputs, the whole page always
89
+ // scrolls, even if the input is inside a nested scrollable element that could be scrolled instead.
90
+ //
91
+ // In order to work around these cases, and prevent scrolling without jankiness, we do a few things:
92
+ //
93
+ // 1. Prevent default on `touchmove` events that are not in a scrollable element. This prevents touch scrolling
94
+ // on the window.
95
+ // 2. Set `overscroll-behavior: contain` on nested scrollable regions so they do not scroll the page when at
96
+ // the top or bottom. Work around a bug where this does not work when the element does not actually overflow
97
+ // by preventing default in a `touchmove` event. This is best effort: we can't prevent default when pinch
98
+ // zooming or when an element contains text selection, which may allow scrolling in some cases.
99
+ // 3. Prevent default on `touchend` events on input elements and handle focusing the element ourselves.
100
+ // 4. When focus moves to an input, create an off screen input and focus that temporarily. This prevents
101
+ // Safari from scrolling the page. After a small delay, focus the real input and scroll it into view
102
+ // ourselves, without scrolling the whole page.
103
+ function preventScrollMobileSafari() {
104
+ let scrollable;
105
+ let allowTouchMove = false;
106
+ let onTouchStart = (e) => {
107
+ // Store the nearest scrollable parent element from the element that the user touched.
108
+ let target = e.target;
109
+ scrollable = isScrollable(target) ? target : getScrollParent(target, true);
110
+ allowTouchMove = false;
111
+ // If the target is selected, don't preventDefault in touchmove to allow user to adjust selection.
112
+ let selection = target.ownerDocument.defaultView.getSelection();
113
+ if (selection && !selection.isCollapsed && selection.containsNode(target, true)) {
114
+ allowTouchMove = true;
115
+ }
116
+ // If this is a focused input element with a selected range, allow user to drag the selection handles.
117
+ if ('selectionStart' in target &&
118
+ 'selectionEnd' in target &&
119
+ target.selectionStart < target.selectionEnd &&
120
+ target.ownerDocument.activeElement === target) {
121
+ allowTouchMove = true;
122
+ }
123
+ };
124
+ // Prevent scrolling up when at the top and scrolling down when at the bottom
125
+ // of a nested scrollable area, otherwise mobile Safari will start scrolling
126
+ // the window instead.
127
+ // This must be applied before the touchstart event as of iOS 26, so inject it as a <style> element.
128
+ let style = document.createElement('style');
129
+ style.textContent = `
130
+ @layer {
131
+ * {
132
+ overscroll-behavior: contain;
133
+ }
134
+ }`.trim();
135
+ document.head.prepend(style);
136
+ let onTouchMove = (e) => {
137
+ // Allow pinch-zooming.
138
+ if (e.touches.length === 2 || allowTouchMove) {
139
+ return;
140
+ }
141
+ // Prevent scrolling the window.
142
+ if (!scrollable || scrollable === document.documentElement || scrollable === document.body) {
143
+ e.preventDefault();
144
+ return;
145
+ }
146
+ // overscroll-behavior should prevent scroll chaining, but currently does not
147
+ // if the element doesn't actually overflow. https://bugs.webkit.org/show_bug.cgi?id=243452
148
+ // This checks that both the width and height do not overflow, otherwise we might
149
+ // block horizontal scrolling too. In that case, adding `touch-action: pan-x` to
150
+ // the element will prevent vertical page scrolling. We can't add that automatically
151
+ // because it must be set before the touchstart event.
152
+ if (scrollable.scrollHeight === scrollable.clientHeight && scrollable.scrollWidth === scrollable.clientWidth) {
153
+ e.preventDefault();
154
+ }
155
+ };
156
+ let onBlur = (e) => {
157
+ let target = e.target;
158
+ let relatedTarget = e.relatedTarget;
159
+ if (relatedTarget && willOpenKeyboard(relatedTarget)) {
160
+ // Focus without scrolling the whole page, and then scroll into view manually.
161
+ relatedTarget.focus({ preventScroll: true });
162
+ scrollIntoViewWhenReady(relatedTarget, willOpenKeyboard(target));
163
+ }
164
+ else if (!relatedTarget) {
165
+ // When tapping the Done button on the keyboard, focus moves to the body.
166
+ // FocusScope will then restore focus back to the input. Later when tapping
167
+ // the same input again, it is already focused, so no blur event will fire,
168
+ // resulting in the flow above never running and Safari's native scrolling occurring.
169
+ // Instead, move focus to the parent focusable element (e.g. the dialog).
170
+ let focusable = target.parentElement?.closest('[tabindex]');
171
+ focusable?.focus({ preventScroll: true });
172
+ }
173
+ };
174
+ // Override programmatic focus to scroll into view without scrolling the whole page.
175
+ let focus = HTMLElement.prototype.focus;
176
+ HTMLElement.prototype.focus = function (opts) {
177
+ // Track whether the keyboard was already visible before.
178
+ let wasKeyboardVisible = document.activeElement != null && willOpenKeyboard(document.activeElement);
179
+ // Focus the element without scrolling the page.
180
+ focus.call(this, { ...opts, preventScroll: true });
181
+ if (!opts || !opts.preventScroll) {
182
+ scrollIntoViewWhenReady(this, wasKeyboardVisible);
183
+ }
184
+ };
185
+ let removeEvents = chain(addEvent(document, 'touchstart', onTouchStart, { passive: false, capture: true }), addEvent(document, 'touchmove', onTouchMove, { passive: false, capture: true }), addEvent(document, 'blur', onBlur, true));
186
+ return () => {
187
+ removeEvents();
188
+ style.remove();
189
+ HTMLElement.prototype.focus = focus;
190
+ };
191
+ }
192
+ // Sets a CSS property on an element, and returns a function to revert it to the previous value.
193
+ function setStyle(element, style, value) {
194
+ let cur = element.style[style];
195
+ element.style[style] = value;
196
+ return () => {
197
+ element.style[style] = cur;
198
+ };
199
+ }
200
+ // Adds an event listener to an element, and returns a function to remove it.
201
+ function addEvent(target, event, handler, options) {
202
+ // internal function, so it's ok to ignore the difficult to fix type error
203
+ // @ts-ignore
204
+ target.addEventListener(event, handler, options);
205
+ return () => {
206
+ // @ts-ignore
207
+ target.removeEventListener(event, handler, options);
208
+ };
209
+ }
210
+ function scrollIntoViewWhenReady(target, wasKeyboardVisible) {
211
+ if (wasKeyboardVisible || !visualViewport) {
212
+ // If the keyboard was already visible, scroll the target into view immediately.
213
+ scrollIntoView(target);
214
+ }
215
+ else {
216
+ // Otherwise, wait for the visual viewport to resize before scrolling so we can
217
+ // measure the correct position to scroll to.
218
+ visualViewport.addEventListener('resize', () => scrollIntoView(target), { once: true });
219
+ }
220
+ }
221
+ function scrollIntoView(target) {
222
+ let root = document.scrollingElement || document.documentElement;
223
+ let nextTarget = target;
224
+ while (nextTarget && nextTarget !== root) {
225
+ // Find the parent scrollable element and adjust the scroll position if the target is not already in view.
226
+ let scrollable = getScrollParent(nextTarget);
227
+ if (scrollable !== document.documentElement && scrollable !== document.body && scrollable !== nextTarget) {
228
+ let scrollableRect = scrollable.getBoundingClientRect();
229
+ let targetRect = nextTarget.getBoundingClientRect();
230
+ if (targetRect.top < scrollableRect.top || targetRect.bottom > scrollableRect.top + nextTarget.clientHeight) {
231
+ let bottom = scrollableRect.bottom;
232
+ if (visualViewport) {
233
+ bottom = Math.min(bottom, visualViewport.offsetTop + visualViewport.height);
234
+ }
235
+ // Center within the viewport.
236
+ let adjustment = (targetRect.top - scrollableRect.top) - ((bottom - scrollableRect.top) / 2 - targetRect.height / 2);
237
+ scrollable.scrollTo({
238
+ // Clamp to the valid range to prevent over-scrolling.
239
+ top: Math.max(0, Math.min(scrollable.scrollHeight - scrollable.clientHeight, scrollable.scrollTop + adjustment)),
240
+ behavior: 'smooth'
241
+ });
242
+ }
243
+ }
244
+ nextTarget = scrollable.parentElement;
245
+ }
246
+ }
package/dialog.svelte CHANGED
@@ -4,12 +4,15 @@ import { pushToolsActionsOperations, popToolsActionsOperations, fabHiddenDueToPo
4
4
  import { isDeviceSmallerThan } from "./utils";
5
5
  import { FaTimes } from "svelte-icons/fa";
6
6
  import { i18n } from "./i18n.js";
7
+ import { usePreventScroll } from "./components/react-aria/preventScroll";
7
8
  export let open = false;
9
+ let preventScrollRestorer = null;
8
10
  export function show(on_close_callback = void 0) {
9
11
  open = true;
10
12
  close_callback = on_close_callback;
11
13
  if (isDeviceSmallerThan("sm")) {
12
14
  $fabHiddenDueToPopup = true;
15
+ preventScrollRestorer = usePreventScroll();
13
16
  }
14
17
  }
15
18
  export function hide() {
@@ -17,6 +20,10 @@ export function hide() {
17
20
  return;
18
21
  open = false;
19
22
  $fabHiddenDueToPopup = false;
23
+ if (preventScrollRestorer) {
24
+ preventScrollRestorer();
25
+ preventScrollRestorer = null;
26
+ }
20
27
  }
21
28
  let root;
22
29
  afterUpdate(
@@ -50,9 +57,10 @@ let close_callback = void 0;
50
57
  </div>
51
58
  {/if}
52
59
 
53
- <style>
60
+ <!-- use usePreventScroll instead -->
61
+ <!--style>
54
62
  :global(body:has(#__hd_svelte_property_dialog_container[visible="true"]))
55
63
  {
56
64
  overflow: hidden;
57
65
  }
58
- </style>
66
+ </style-->
package/modal.svelte CHANGED
@@ -4,6 +4,7 @@ import { pushToolsActionsOperations, popToolsActionsOperations, fabHiddenDueToPo
4
4
  import { isDeviceSmallerThan } from "./utils";
5
5
  import { FaTimes } from "svelte-icons/fa";
6
6
  import { i18n } from "./i18n.js";
7
+ import { usePreventScroll } from "./components/react-aria/preventScroll";
7
8
  export let title = "";
8
9
  export let open = false;
9
10
  export let content = "";
@@ -17,11 +18,13 @@ export let okCaption = "OK";
17
18
  export let cancelCaption = i18n({ en: "Cancel", es: "Cancelar", pl: "Anuluj" });
18
19
  export let onOkCallback = void 0;
19
20
  export let onCancelCallback = void 0;
21
+ let preventScrollRestorer = null;
20
22
  export function show(on_close_callback = void 0) {
21
23
  open = true;
22
24
  close_callback = on_close_callback;
23
25
  if (isDeviceSmallerThan("sm")) {
24
26
  $fabHiddenDueToPopup = true;
27
+ preventScrollRestorer = usePreventScroll();
25
28
  }
26
29
  }
27
30
  export function hide() {
@@ -29,6 +32,10 @@ export function hide() {
29
32
  return;
30
33
  open = false;
31
34
  $fabHiddenDueToPopup = false;
35
+ if (preventScrollRestorer) {
36
+ preventScrollRestorer();
37
+ preventScrollRestorer = null;
38
+ }
32
39
  }
33
40
  let root;
34
41
  afterUpdate(
@@ -136,9 +143,11 @@ function on_cancel(event) {
136
143
  </div>
137
144
  {/if}
138
145
 
139
- <style>
146
+
147
+ <!-- use usePreventScroll instead -->
148
+ <!--style>
140
149
  :global(body:has(#__hd_svelte_modal_container[visible="true"]))
141
150
  {
142
151
  overflow: hidden;
143
152
  }
144
- </style>
153
+ </style-->
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@humandialog/forms.svelte",
3
- "version": "1.7.15",
3
+ "version": "1.7.17",
4
4
  "description": "Basic Svelte UI components for Object Reef applications",
5
5
  "devDependencies": {
6
6
  "@playwright/test": "^1.28.1",
@@ -138,6 +138,12 @@
138
138
  "./components/menu": "./components/menu.js",
139
139
  "./components/paginator.svelte": "./components/paginator.svelte",
140
140
  "./components/radio.svelte": "./components/radio.svelte",
141
+ "./components/react-aria/chain": "./components/react-aria/chain.js",
142
+ "./components/react-aria/getScrollParent": "./components/react-aria/getScrollParent.js",
143
+ "./components/react-aria/isScrollable": "./components/react-aria/isScrollable.js",
144
+ "./components/react-aria/keybaord": "./components/react-aria/keybaord.js",
145
+ "./components/react-aria/platform": "./components/react-aria/platform.js",
146
+ "./components/react-aria/preventScroll": "./components/react-aria/preventScroll.js",
141
147
  "./components/sidebar/sidebar.brand.svelte": "./components/sidebar/sidebar.brand.svelte",
142
148
  "./components/sidebar/sidebar.group.svelte": "./components/sidebar/sidebar.group.svelte",
143
149
  "./components/sidebar/sidebar.item.svelte": "./components/sidebar/sidebar.item.svelte",