@knitui/components 1.2.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/lib/commonjs/Avatar/Avatar.js +32 -10
  2. package/lib/commonjs/Avatar/Avatar.js.map +1 -1
  3. package/lib/commonjs/Input/shared.js +10 -1
  4. package/lib/commonjs/Input/shared.js.map +1 -1
  5. package/lib/commonjs/InputBase/InputBase.js +22 -1
  6. package/lib/commonjs/InputBase/InputBase.js.map +1 -1
  7. package/lib/commonjs/Modal/index.js +18 -0
  8. package/lib/commonjs/Modal/index.js.map +1 -1
  9. package/lib/commonjs/Modal/modal-base.js +20 -15
  10. package/lib/commonjs/Modal/modal-base.js.map +1 -1
  11. package/lib/commonjs/Modal/modal-stack.js +87 -0
  12. package/lib/commonjs/Modal/modal-stack.js.map +1 -0
  13. package/lib/commonjs/Portal/OverlayPortal.js +7 -1
  14. package/lib/commonjs/Portal/OverlayPortal.js.map +1 -1
  15. package/lib/commonjs/Portal/index.js +19 -0
  16. package/lib/commonjs/Portal/index.js.map +1 -1
  17. package/lib/commonjs/Portal/teleported-layer-scope.js +72 -0
  18. package/lib/commonjs/Portal/teleported-layer-scope.js.map +1 -0
  19. package/lib/commonjs/ScrollArea/ScrollArea.js +23 -1
  20. package/lib/commonjs/ScrollArea/ScrollArea.js.map +1 -1
  21. package/lib/commonjs/ScrollArea/ScrollArea.native.js +70 -61
  22. package/lib/commonjs/ScrollArea/ScrollArea.native.js.map +1 -1
  23. package/lib/commonjs/UnstyledButton/UnstyledButton.js +4 -0
  24. package/lib/commonjs/UnstyledButton/UnstyledButton.js.map +1 -1
  25. package/lib/commonjs/index.js +37 -0
  26. package/lib/commonjs/index.js.map +1 -1
  27. package/lib/commonjs/internal/register-icon-color.js +7 -0
  28. package/lib/commonjs/internal/register-icon-color.js.map +1 -0
  29. package/lib/commonjs/internal/style-props.js +17 -0
  30. package/lib/commonjs/internal/style-props.js.map +1 -1
  31. package/lib/module/Avatar/Avatar.js +30 -9
  32. package/lib/module/Avatar/Avatar.js.map +1 -1
  33. package/lib/module/Input/shared.js +10 -1
  34. package/lib/module/Input/shared.js.map +1 -1
  35. package/lib/module/InputBase/InputBase.js +22 -1
  36. package/lib/module/InputBase/InputBase.js.map +1 -1
  37. package/lib/module/Modal/index.js +1 -0
  38. package/lib/module/Modal/index.js.map +1 -1
  39. package/lib/module/Modal/modal-base.js +20 -15
  40. package/lib/module/Modal/modal-base.js.map +1 -1
  41. package/lib/module/Modal/modal-stack.js +80 -0
  42. package/lib/module/Modal/modal-stack.js.map +1 -0
  43. package/lib/module/Portal/OverlayPortal.js +8 -2
  44. package/lib/module/Portal/OverlayPortal.js.map +1 -1
  45. package/lib/module/Portal/index.js +1 -0
  46. package/lib/module/Portal/index.js.map +1 -1
  47. package/lib/module/Portal/teleported-layer-scope.js +66 -0
  48. package/lib/module/Portal/teleported-layer-scope.js.map +1 -0
  49. package/lib/module/ScrollArea/ScrollArea.js +23 -1
  50. package/lib/module/ScrollArea/ScrollArea.js.map +1 -1
  51. package/lib/module/ScrollArea/ScrollArea.native.js +70 -61
  52. package/lib/module/ScrollArea/ScrollArea.native.js.map +1 -1
  53. package/lib/module/UnstyledButton/UnstyledButton.js +4 -0
  54. package/lib/module/UnstyledButton/UnstyledButton.js.map +1 -1
  55. package/lib/module/index.js +4 -3
  56. package/lib/module/index.js.map +1 -1
  57. package/lib/module/internal/register-icon-color.js +8 -0
  58. package/lib/module/internal/register-icon-color.js.map +1 -0
  59. package/lib/module/internal/style-props.js +17 -0
  60. package/lib/module/internal/style-props.js.map +1 -1
  61. package/lib/typescript/Avatar/Avatar.d.ts +18 -7
  62. package/lib/typescript/Avatar/Avatar.d.ts.map +1 -1
  63. package/lib/typescript/Image/Image.d.ts +7 -7
  64. package/lib/typescript/Image/Image.native.d.ts +7 -7
  65. package/lib/typescript/Image/createImage.d.ts +9 -9
  66. package/lib/typescript/Input/shared.d.ts.map +1 -1
  67. package/lib/typescript/InputBase/InputBase.d.ts.map +1 -1
  68. package/lib/typescript/Modal/index.d.ts +1 -0
  69. package/lib/typescript/Modal/index.d.ts.map +1 -1
  70. package/lib/typescript/Modal/modal-base.d.ts +2 -2
  71. package/lib/typescript/Modal/modal-base.d.ts.map +1 -1
  72. package/lib/typescript/Modal/modal-stack.d.ts +45 -0
  73. package/lib/typescript/Modal/modal-stack.d.ts.map +1 -0
  74. package/lib/typescript/Portal/OverlayPortal.d.ts.map +1 -1
  75. package/lib/typescript/Portal/index.d.ts +1 -0
  76. package/lib/typescript/Portal/index.d.ts.map +1 -1
  77. package/lib/typescript/Portal/teleported-layer-scope.d.ts +41 -0
  78. package/lib/typescript/Portal/teleported-layer-scope.d.ts.map +1 -0
  79. package/lib/typescript/ScrollArea/ScrollArea.d.ts.map +1 -1
  80. package/lib/typescript/ScrollArea/ScrollArea.native.d.ts.map +1 -1
  81. package/lib/typescript/UnstyledButton/UnstyledButton.d.ts.map +1 -1
  82. package/lib/typescript/index.d.ts +4 -3
  83. package/lib/typescript/index.d.ts.map +1 -1
  84. package/lib/typescript/internal/register-icon-color.d.ts +2 -0
  85. package/lib/typescript/internal/register-icon-color.d.ts.map +1 -0
  86. package/lib/typescript/internal/style-props.d.ts +17 -0
  87. package/lib/typescript/internal/style-props.d.ts.map +1 -1
  88. package/package.json +9 -6
  89. package/src/Avatar/Avatar.tsx +33 -7
  90. package/src/Input/shared.tsx +24 -1
  91. package/src/InputBase/InputBase.tsx +36 -1
  92. package/src/Modal/index.ts +1 -0
  93. package/src/Modal/modal-base.tsx +17 -18
  94. package/src/Modal/modal-stack.ts +100 -0
  95. package/src/Portal/OverlayPortal.tsx +9 -2
  96. package/src/Portal/index.ts +6 -0
  97. package/src/Portal/teleported-layer-scope.tsx +73 -0
  98. package/src/ScrollArea/ScrollArea.native.tsx +85 -74
  99. package/src/ScrollArea/ScrollArea.tsx +47 -1
  100. package/src/UnstyledButton/UnstyledButton.tsx +4 -0
  101. package/src/index.ts +10 -0
  102. package/src/internal/register-icon-color.ts +6 -0
  103. package/src/internal/style-props.ts +45 -14
@@ -215,6 +215,24 @@ const textareaHostSizeVariant = tableVariant(
215
215
  const toInputLoaderSize = (size: InputSize | undefined) =>
216
216
  controlIconSize(toInputTokenSizeKey(size));
217
217
 
218
+ /** The root's flags, which a caller can't override into an inconsistent state. */
219
+ const CONTROLLED_ROOT_PROPS = new Set([
220
+ "ref",
221
+ "size",
222
+ "radius",
223
+ "variant",
224
+ "pointer",
225
+ "multiline",
226
+ "focused",
227
+ "error",
228
+ "disabled",
229
+ ]);
230
+
231
+ const omitControlledRootProps = <T extends object>(props: T): Partial<T> =>
232
+ Object.fromEntries(
233
+ Object.entries(props).filter(([key]) => !CONTROLLED_ROOT_PROPS.has(key)),
234
+ ) as Partial<T>;
235
+
218
236
  export const InputRootFrame = styled(Box, {
219
237
  displayName: "InputRoot",
220
238
  flexDirection: "row",
@@ -507,6 +525,9 @@ export function InputChrome(props: InputChromeProps) {
507
525
  const mergedLeftSectionProps = s.merge("leftSection", leftSectionProps);
508
526
  const mergedRightSectionProps = s.merge("rightSection", rightSectionProps);
509
527
  const { onPress: wrapperOnPress, ...rootProps } = s.merge("root", wrapperProps);
528
+ // Everything but the controlled flags, re-applied after them: v3 resolves styles in
529
+ // call-site order, so a caller's `borderColor` has to land after `focused`/`error`.
530
+ const callerStyles = omitControlledRootProps(rootProps);
510
531
 
511
532
  const leftContent = getSectionContent({
512
533
  loading,
@@ -549,7 +570,8 @@ export function InputChrome(props: InputChromeProps) {
549
570
  <InputRootFrame
550
571
  // Consumer wrapperProps are spread FIRST so the component's own controlled
551
572
  // state (size/variant/error/disabled/focused/…, ref, and the press handler)
552
- // always wins and can never be overridden into an inconsistent state.
573
+ // always wins and can never be overridden into an inconsistent state. Their
574
+ // styles are spread again after those flags, so they still apply.
553
575
  {...rootProps}
554
576
  ref={rootRef}
555
577
  size={size}
@@ -563,6 +585,7 @@ export function InputChrome(props: InputChromeProps) {
563
585
  focused={focused}
564
586
  error={!!error}
565
587
  disabled={disabled}
588
+ {...callerStyles}
566
589
  onPress={disabled ? undefined : handleRootPress}
567
590
  >
568
591
  {leftContent ? (
@@ -13,6 +13,40 @@ export interface InputBaseProps extends InputBaseInputProps, InputBaseWrapperPro
13
13
  wrapperProps?: InputBaseWrapperProps & InputProps["wrapperProps"];
14
14
  }
15
15
 
16
+ /**
17
+ * Props that place the field among its siblings. They belong on the outer wrapper:
18
+ * on the inner input, `flex={1}` in a row would size the input inside a wrapper that
19
+ * stays at its content width.
20
+ */
21
+ const LAYOUT_PROPS = new Set([
22
+ "flex",
23
+ "flexGrow",
24
+ "flexShrink",
25
+ "flexBasis",
26
+ "alignSelf",
27
+ "width",
28
+ "minWidth",
29
+ "maxWidth",
30
+ "margin",
31
+ "marginHorizontal",
32
+ "marginVertical",
33
+ "marginTop",
34
+ "marginBottom",
35
+ "marginLeft",
36
+ "marginRight",
37
+ ]);
38
+
39
+ const splitLayoutProps = (
40
+ props: InputBaseInputProps,
41
+ ): { layout: Record<string, unknown>; inputProps: InputBaseInputProps } => {
42
+ const layout: Record<string, unknown> = {};
43
+ const inputProps: Record<string, unknown> = {};
44
+ for (const [key, value] of Object.entries(props)) {
45
+ (LAYOUT_PROPS.has(key) ? layout : inputProps)[key] = value;
46
+ }
47
+ return { layout, inputProps: inputProps as InputBaseInputProps };
48
+ };
49
+
16
50
  export const InputBase = styleable<InputBaseProps>(Input, function InputBase(props, ref) {
17
51
  const {
18
52
  label,
@@ -32,7 +66,7 @@ export const InputBase = styleable<InputBaseProps>(Input, function InputBase(pro
32
66
  size = "md",
33
67
  ...others
34
68
  } = props;
35
- const inputProps: InputBaseInputProps = others;
69
+ const { layout, inputProps } = splitLayoutProps(others);
36
70
 
37
71
  return (
38
72
  <Input.Wrapper
@@ -50,6 +84,7 @@ export const InputBase = styleable<InputBaseProps>(Input, function InputBase(pro
50
84
  inputWrapperOrder={inputWrapperOrder}
51
85
  labelElement={labelElement}
52
86
  size={size}
87
+ {...layout}
53
88
  {...wrapperProps}
54
89
  >
55
90
  <Input
@@ -1 +1,2 @@
1
1
  export * from "./Modal";
2
+ export { type EscapeLayerOptions, hasOpenEscapeLayer, useEscapeLayer } from "./modal-stack";
@@ -10,6 +10,7 @@ import { slotStyles, type SlotStyles } from "../internal/styles";
10
10
  import { Overlay, type OverlayProps } from "../Overlay";
11
11
  import { OverlayPortal, useOverlayHost } from "../Portal";
12
12
  import { ModalBaseInner } from "./modal-base-inner";
13
+ import { useEscapeLayer } from "./modal-stack";
13
14
 
14
15
  /**
15
16
  * Shared scaffolding for `Modal` and `Drawer` — mirrors Mantine's `ModalBase`.
@@ -21,8 +22,8 @@ import { ModalBaseInner } from "./modal-base-inner";
21
22
  * - paints a full-cover `Overlay` scrim BEHIND the content (content frames carry
22
23
  * `position:"relative", zIndex:1`; the scrim sits at `zIndex:0`), so a click on
23
24
  * the empty area around the content hits the scrim and closes,
24
- * - wires the web-only side effects: Escape to close, body scroll-lock, and
25
- * return-focus on close.
25
+ * - wires the web-only side effects: Escape to close (top layer of a stack
26
+ * only, see `modal-stack.ts`), body scroll-lock, and return-focus on close.
26
27
  *
27
28
  * `trapFocus` traps keyboard focus within the content on web via the shared
28
29
  * `useFocusTrap` hook (no-op on native — there is no DOM focus model, the same
@@ -242,26 +243,24 @@ export function ModalBase(props: ModalBaseProps) {
242
243
  };
243
244
  }, [opened]);
244
245
 
245
- // The remaining web-only overlay side effects — Escape-to-close and body
246
- // scroll-lock. Inert on native (no keydown stream or body element). Escape is
247
- // ignored when a descendant already handled it (`defaultPrevented`), e.g. a
248
- // nested Select closing its own dropdown.
249
- React.useEffect(() => {
250
- if (!opened || typeof document === "undefined") return;
251
-
252
- const onKeyDown = (event: KeyboardEvent) => {
253
- if (event.key === "Escape" && !event.defaultPrevented) onCloseRef.current();
254
- };
255
- if (closeOnEscape) document.addEventListener("keydown", onKeyDown);
246
+ // Escape: every open layer registers on the shared modal stack, which routes
247
+ // the key to the TOP layer only — a stack of modals unwinds one per press, and
248
+ // focus returns to each layer's own opener via the effect above. Registered
249
+ // regardless of `closeOnEscape` so a non-dismissable top layer swallows Escape
250
+ // rather than letting it close the layer beneath. A descendant that already
251
+ // handled Escape (`defaultPrevented`, e.g. a nested Select closing its own
252
+ // dropdown) is respected. The latest `closeOnEscape` is read at keydown time.
253
+ useEscapeLayer(opened, { closeOnEscape, onClose: () => onCloseRef.current() });
256
254
 
255
+ // Body scroll-lock (web; inert on native — no body element).
256
+ React.useEffect(() => {
257
+ if (!opened || !lockScroll || typeof document === "undefined") return;
257
258
  const previousOverflow = document.body.style.overflow;
258
- if (lockScroll) document.body.style.overflow = "hidden";
259
-
259
+ document.body.style.overflow = "hidden";
260
260
  return () => {
261
- if (closeOnEscape) document.removeEventListener("keydown", onKeyDown);
262
- if (lockScroll) document.body.style.overflow = previousOverflow;
261
+ document.body.style.overflow = previousOverflow;
263
262
  };
264
- }, [opened, closeOnEscape, lockScroll]);
263
+ }, [opened, lockScroll]);
265
264
 
266
265
  // The full-cover positioning layer: scrim + the consumer's content frame. The
267
266
  // `display` toggle only matters on the `keepMounted` path (where the node never
@@ -0,0 +1,100 @@
1
+ import * as React from "react";
2
+
3
+ /**
4
+ * Module-level stack of the open modal layers (`Modal`, `Drawer`, and a modal
5
+ * `Sheet` through {@link useEscapeLayer}), in open order. One shared `document` keydown listener routes Escape to the TOP entry
6
+ * only, so a stack of modals unwinds one layer per press instead of every open
7
+ * layer closing at once (each layer used to own its own document listener).
8
+ *
9
+ * Every open layer registers, including those with `closeOnEscape={false}`: such
10
+ * a layer still occupies the top, so Escape is swallowed rather than falling
11
+ * through to the layer beneath it.
12
+ *
13
+ * Inert on native (no `document`); the stack still tracks but nothing listens.
14
+ */
15
+
16
+ export interface ModalStackEntry {
17
+ /**
18
+ * Open-order key from {@link nextModalOrder}, taken on the render that opens
19
+ * the layer. Render order is parent-first, whereas effects run child-first, so
20
+ * registering in effect order would put an outer modal mounted open alongside
21
+ * an inner one ON TOP of it. The stack is kept sorted by this key instead.
22
+ */
23
+ order: number;
24
+ /** Read at keydown time so the latest prop wins without re-registering. */
25
+ closeOnEscape: () => boolean;
26
+ onClose: () => void;
27
+ }
28
+
29
+ const stack: ModalStackEntry[] = [];
30
+ let counter = 0;
31
+
32
+ /** A fresh, increasing open-order key (see {@link ModalStackEntry.order}). */
33
+ export function nextModalOrder(): number {
34
+ counter += 1;
35
+ return counter;
36
+ }
37
+
38
+ function onKeyDown(event: KeyboardEvent) {
39
+ if (event.key !== "Escape" || event.defaultPrevented) return;
40
+ const top = stack[stack.length - 1];
41
+ if (!top || !top.closeOnEscape()) return;
42
+ // Mark the event handled so any other Escape consumer (and a layer that
43
+ // becomes the top later in this same dispatch) leaves it alone.
44
+ event.preventDefault();
45
+ top.onClose();
46
+ }
47
+
48
+ /** Push `entry` onto the stack; returns the matching unregister. */
49
+ export function registerModalLayer(entry: ModalStackEntry): () => void {
50
+ let index = stack.length;
51
+ while (index > 0 && stack[index - 1].order > entry.order) index -= 1;
52
+ stack.splice(index, 0, entry);
53
+ if (stack.length === 1 && typeof document !== "undefined") {
54
+ document.addEventListener("keydown", onKeyDown);
55
+ }
56
+ return () => {
57
+ const index = stack.lastIndexOf(entry);
58
+ if (index !== -1) stack.splice(index, 1);
59
+ if (stack.length === 0 && typeof document !== "undefined") {
60
+ document.removeEventListener("keydown", onKeyDown);
61
+ }
62
+ };
63
+ }
64
+
65
+ /** Whether any modal layer is open (so a non-modal surface leaves Escape to it). */
66
+ export function hasOpenEscapeLayer(): boolean {
67
+ return stack.length > 0;
68
+ }
69
+
70
+ export interface EscapeLayerOptions {
71
+ /** Close on Escape while this layer is the top one. @default true */
72
+ closeOnEscape?: boolean;
73
+ /** Called when Escape reaches this layer. */
74
+ onClose: () => void;
75
+ }
76
+
77
+ /**
78
+ * Put a modal layer on the shared Escape stack while `opened`: Escape then
79
+ * closes only the topmost open layer, whichever component it belongs to. The
80
+ * open order is taken on the render that opens the layer (see
81
+ * {@link ModalStackEntry.order}); the latest options are read at keydown time.
82
+ * Web only in effect; on native there is no keydown stream.
83
+ */
84
+ export function useEscapeLayer(opened: boolean, options: EscapeLayerOptions): void {
85
+ const optionsRef = React.useRef(options);
86
+ optionsRef.current = options;
87
+ const wasOpenedRef = React.useRef(false);
88
+ const orderRef = React.useRef(0);
89
+ if (opened && !wasOpenedRef.current) orderRef.current = nextModalOrder();
90
+ wasOpenedRef.current = opened;
91
+
92
+ React.useEffect(() => {
93
+ if (!opened) return undefined;
94
+ return registerModalLayer({
95
+ order: orderRef.current,
96
+ closeOnEscape: () => optionsRef.current.closeOnEscape ?? true,
97
+ onClose: () => optionsRef.current.onClose(),
98
+ });
99
+ }, [opened]);
100
+ }
@@ -1,7 +1,9 @@
1
1
  import * as React from "react";
2
2
  import { Portal } from "react-native-teleport";
3
3
 
4
- import { useScreenFocused } from "@knitui/core";
4
+ import { isWeb, useScreenFocused } from "@knitui/core";
5
+
6
+ import { useRegisterTeleportedLayer } from "./teleported-layer-scope";
5
7
 
6
8
  export interface OverlayPortalProps {
7
9
  /** Host to teleport into (usually `useOverlayHost()`); omit to render inline. */
@@ -22,5 +24,10 @@ export interface OverlayPortalProps {
22
24
  */
23
25
  export function OverlayPortal({ hostName, children }: OverlayPortalProps) {
24
26
  const focused = useScreenFocused();
25
- return <Portal hostName={focused ? hostName : undefined}>{children}</Portal>;
27
+ const target = focused ? hostName : undefined;
28
+ // Native: while teleported, tell the scrollers this layer sits inside (in
29
+ // React) to stop capturing its first tap to dismiss the keyboard (see
30
+ // `teleported-layer-scope.tsx`). Web has no such capture.
31
+ useRegisterTeleportedLayer(!isWeb && target !== undefined);
32
+ return <Portal hostName={target}>{children}</Portal>;
26
33
  }
@@ -5,6 +5,12 @@ export {
5
5
  useTopmostOverlayHost,
6
6
  } from "./OverlayHost";
7
7
  export { OverlayPortal, type OverlayPortalProps } from "./OverlayPortal";
8
+ export {
9
+ resolvePersistTaps,
10
+ type TeleportedLayerScope,
11
+ useRegisterTeleportedLayer,
12
+ useTeleportedLayerScope,
13
+ } from "./teleported-layer-scope";
8
14
  export type { OverlayHostProps, PortalHostProps, PortalProps, PortalProviderProps } from "./types";
9
15
  /**
10
16
  * Portal system — a thin re-export of `react-native-teleport`.
@@ -0,0 +1,73 @@
1
+ import * as React from "react";
2
+
3
+ /**
4
+ * Lets a scroller know that an overlay rendered INSIDE it in React (a `Sheet`,
5
+ * `Modal`, `Drawer` or dropdown teleported on screen through `OverlayPortal`)
6
+ * is currently open.
7
+ *
8
+ * Why (native): teleporting moves the native view, but in React the layer stays
9
+ * where it was rendered, e.g. a comment sheet opened from a row of a feed list.
10
+ * React Native's responder system walks the React tree, so that list's
11
+ * `ScrollView` is an ancestor of every button in the sheet. With the default
12
+ * `keyboardShouldPersistTaps="never"` the ScrollView CAPTURES a touch that starts
13
+ * while the keyboard is up, only to dismiss the keyboard, and the sheet's button
14
+ * never gets the first tap. A descendant cannot outbid an ancestor's capture, so
15
+ * the scroller has to step back: while a layer is registered here it uses
16
+ * `"handled"`, which leaves the tap to the control it lands on and still
17
+ * dismisses the keyboard on a tap nothing handles.
18
+ *
19
+ * Scopes nest: a layer registers with the nearest scroller, which forwards the
20
+ * registration to every scroller above it. Web has no such capture, so
21
+ * `OverlayPortal` only registers on native.
22
+ */
23
+
24
+ type RegisterLayer = () => () => void;
25
+
26
+ const TeleportedLayerContext = React.createContext<RegisterLayer | null>(null);
27
+
28
+ export interface TeleportedLayerScope {
29
+ /** At least one teleported layer rendered inside this scope is open. */
30
+ hasOpenLayer: boolean;
31
+ /** Wrap the scroller's children so layers inside it can register. */
32
+ Provider: typeof TeleportedLayerContext.Provider;
33
+ register: RegisterLayer;
34
+ }
35
+
36
+ /** Scroller side: open a scope and learn whether a layer inside it is open. */
37
+ export function useTeleportedLayerScope(): TeleportedLayerScope {
38
+ const parent = React.useContext(TeleportedLayerContext);
39
+ const [count, setCount] = React.useState(0);
40
+ const register = React.useCallback<RegisterLayer>(() => {
41
+ setCount((c) => c + 1);
42
+ const releaseParent = parent?.();
43
+ let released = false;
44
+ return () => {
45
+ if (released) return;
46
+ released = true;
47
+ setCount((c) => c - 1);
48
+ releaseParent?.();
49
+ };
50
+ }, [parent]);
51
+ return { hasOpenLayer: count > 0, Provider: TeleportedLayerContext.Provider, register };
52
+ }
53
+
54
+ /** Layer side: register with the enclosing scroller(s) while `active`. */
55
+ export function useRegisterTeleportedLayer(active: boolean): void {
56
+ const register = React.useContext(TeleportedLayerContext);
57
+ React.useEffect(() => {
58
+ if (!active || !register) return;
59
+ return register();
60
+ }, [active, register]);
61
+ }
62
+
63
+ /**
64
+ * The `keyboardShouldPersistTaps` a native scroller should use: the consumer's
65
+ * explicit value wins; otherwise `"handled"` while a layer inside it is open.
66
+ */
67
+ export function resolvePersistTaps<T>(
68
+ explicit: T | undefined,
69
+ hasOpenLayer: boolean,
70
+ ): T | "handled" | undefined {
71
+ if (explicit !== undefined) return explicit;
72
+ return hasOpenLayer ? "handled" : undefined;
73
+ }
@@ -30,6 +30,7 @@ import { useReducedTransition } from "../internal/motion";
30
30
  import { resolveThemeColor } from "../internal/resolve-theme-color";
31
31
  import { scopeTransition } from "../internal/style-props";
32
32
  import { slotStyles } from "../internal/styles";
33
+ import { resolvePersistTaps, useTeleportedLayerScope } from "../Portal/teleported-layer-scope";
33
34
  import {
34
35
  DEFAULT_HIDE_DELAY,
35
36
  DEFAULT_IDLE_SCROLLBAR_SIZE,
@@ -894,6 +895,11 @@ const ScrollAreaComponent = React.forwardRef<ScrollAreaHandle, ScrollAreaProps>(
894
895
  const padRight = shouldOffsetAxis(offsetScrollbars, "y") ? scrollbarSize : 0;
895
896
  const padBottom = shouldOffsetAxis(offsetScrollbars, "x") ? scrollbarSize : 0;
896
897
 
898
+ // A sheet/modal teleported from inside this area still has the ScrollView as
899
+ // its React ancestor; while one is open, step back to `"handled"` so its
900
+ // controls get the first tap with the keyboard up (`teleported-layer-scope`).
901
+ const layerScope = useTeleportedLayerScope();
902
+
897
903
  // Shared native-ScrollView props (single-axis + Autosize). `viewportRef` lands on
898
904
  // the scroller. `nestedScrollEnabled` is what makes a vertical area scroll when it
899
905
  // sits inside another vertical ScrollView on Android (inner consumes first, then
@@ -909,7 +915,10 @@ const ScrollAreaComponent = React.forwardRef<ScrollAreaHandle, ScrollAreaProps>(
909
915
  showsHorizontalScrollIndicator: false,
910
916
  scrollEventThrottle: 16,
911
917
  nestedScrollEnabled: true,
912
- keyboardShouldPersistTaps: keyboardShouldPersistTaps as never,
918
+ keyboardShouldPersistTaps: resolvePersistTaps(
919
+ keyboardShouldPersistTaps,
920
+ layerScope.hasOpenLayer,
921
+ ) as never,
913
922
  ...(onRefresh
914
923
  ? {
915
924
  refreshControl: (
@@ -1004,81 +1013,83 @@ const ScrollAreaComponent = React.forwardRef<ScrollAreaHandle, ScrollAreaProps>(
1004
1013
  };
1005
1014
 
1006
1015
  return (
1007
- <ScrollAreaFrame onLayout={handleFrameLayout} {...rest}>
1008
- {content}
1009
-
1010
- {anyShadow && (
1011
- <>
1012
- {renderEdgeShadow("top")}
1013
- {renderEdgeShadow("bottom")}
1014
- {renderEdgeShadow("left")}
1015
- {renderEdgeShadow("right")}
1016
- </>
1017
- )}
1018
-
1019
- {axes.y && (
1020
- <ScrollAreaScrollbar
1021
- ref={yMove.ref}
1022
- {...yMove.rootProps}
1023
- top={0}
1024
- right={0}
1025
- bottom={hasCorner ? scrollbarSize : 0}
1026
- width={scrollbarSize}
1027
- opacity={showY ? 1 : 0}
1028
- pointerEvents={showY ? "auto" : "none"}
1029
- {...scopeTransition(fade, ANIMATE_OPACITY)}
1030
- {...mergedScrollbarProps}
1031
- >
1032
- {/* Plain Animated.View carries the per-frame geometry (height/offset/
1033
- inset); the inner Tamagui Thumb fills it and supplies colour/radius/
1034
- `thumbProps`. Keeps reanimated off the styled component. */}
1035
- <Animated.View
1036
- style={[
1037
- { position: "absolute", top: 0, height: yBar.size },
1038
- yThumbInsetStyle,
1039
- yThumbStyle,
1040
- ]}
1016
+ <layerScope.Provider value={layerScope.register}>
1017
+ <ScrollAreaFrame onLayout={handleFrameLayout} {...rest}>
1018
+ {content}
1019
+
1020
+ {anyShadow && (
1021
+ <>
1022
+ {renderEdgeShadow("top")}
1023
+ {renderEdgeShadow("bottom")}
1024
+ {renderEdgeShadow("left")}
1025
+ {renderEdgeShadow("right")}
1026
+ </>
1027
+ )}
1028
+
1029
+ {axes.y && (
1030
+ <ScrollAreaScrollbar
1031
+ ref={yMove.ref}
1032
+ {...yMove.rootProps}
1033
+ top={0}
1034
+ right={0}
1035
+ bottom={hasCorner ? scrollbarSize : 0}
1036
+ width={scrollbarSize}
1037
+ opacity={showY ? 1 : 0}
1038
+ pointerEvents={showY ? "auto" : "none"}
1039
+ {...scopeTransition(fade, ANIMATE_OPACITY)}
1040
+ {...mergedScrollbarProps}
1041
1041
  >
1042
- <ScrollAreaThumb top={0} left={0} right={0} bottom={0} {...mergedThumbProps} />
1043
- </Animated.View>
1044
- </ScrollAreaScrollbar>
1045
- )}
1046
-
1047
- {axes.x && (
1048
- <ScrollAreaScrollbar
1049
- ref={xMove.ref}
1050
- {...xMove.rootProps}
1051
- left={0}
1052
- right={hasCorner ? scrollbarSize : 0}
1053
- bottom={0}
1054
- height={scrollbarSize}
1055
- opacity={showX ? 1 : 0}
1056
- pointerEvents={showX ? "auto" : "none"}
1057
- {...scopeTransition(fade, ANIMATE_OPACITY)}
1058
- {...mergedScrollbarProps}
1059
- >
1060
- <Animated.View
1061
- style={[
1062
- { position: "absolute", left: 0, width: xBar.size },
1063
- xThumbInsetStyle,
1064
- xThumbStyle,
1065
- ]}
1042
+ {/* Plain Animated.View carries the per-frame geometry (height/offset/
1043
+ inset); the inner Tamagui Thumb fills it and supplies colour/radius/
1044
+ `thumbProps`. Keeps reanimated off the styled component. */}
1045
+ <Animated.View
1046
+ style={[
1047
+ { position: "absolute", top: 0, height: yBar.size },
1048
+ yThumbInsetStyle,
1049
+ yThumbStyle,
1050
+ ]}
1051
+ >
1052
+ <ScrollAreaThumb top={0} left={0} right={0} bottom={0} {...mergedThumbProps} />
1053
+ </Animated.View>
1054
+ </ScrollAreaScrollbar>
1055
+ )}
1056
+
1057
+ {axes.x && (
1058
+ <ScrollAreaScrollbar
1059
+ ref={xMove.ref}
1060
+ {...xMove.rootProps}
1061
+ left={0}
1062
+ right={hasCorner ? scrollbarSize : 0}
1063
+ bottom={0}
1064
+ height={scrollbarSize}
1065
+ opacity={showX ? 1 : 0}
1066
+ pointerEvents={showX ? "auto" : "none"}
1067
+ {...scopeTransition(fade, ANIMATE_OPACITY)}
1068
+ {...mergedScrollbarProps}
1066
1069
  >
1067
- <ScrollAreaThumb top={0} left={0} right={0} bottom={0} {...mergedThumbProps} />
1068
- </Animated.View>
1069
- </ScrollAreaScrollbar>
1070
- )}
1071
-
1072
- {hasCorner && (
1073
- <ScrollAreaCorner
1074
- width={scrollbarSize}
1075
- height={scrollbarSize}
1076
- opacity={showX && showY ? 1 : 0}
1077
- {...scopeTransition(fade, ANIMATE_OPACITY)}
1078
- {...mergedCornerProps}
1079
- />
1080
- )}
1081
- </ScrollAreaFrame>
1070
+ <Animated.View
1071
+ style={[
1072
+ { position: "absolute", left: 0, width: xBar.size },
1073
+ xThumbInsetStyle,
1074
+ xThumbStyle,
1075
+ ]}
1076
+ >
1077
+ <ScrollAreaThumb top={0} left={0} right={0} bottom={0} {...mergedThumbProps} />
1078
+ </Animated.View>
1079
+ </ScrollAreaScrollbar>
1080
+ )}
1081
+
1082
+ {hasCorner && (
1083
+ <ScrollAreaCorner
1084
+ width={scrollbarSize}
1085
+ height={scrollbarSize}
1086
+ opacity={showX && showY ? 1 : 0}
1087
+ {...scopeTransition(fade, ANIMATE_OPACITY)}
1088
+ {...mergedCornerProps}
1089
+ />
1090
+ )}
1091
+ </ScrollAreaFrame>
1092
+ </layerScope.Provider>
1082
1093
  );
1083
1094
  },
1084
1095
  );
@@ -175,6 +175,48 @@ type ScrollAreaFrameProps = Omit<GetProps<typeof ScrollAreaFrame>, "type" | "sha
175
175
 
176
176
  export interface ScrollAreaProps extends ScrollAreaFrameProps, ScrollAreaOwnProps {}
177
177
 
178
+ /** ARIA roles whose widgets consume Space, Home/End or the arrow keys themselves. */
179
+ const KEY_OWNING_ROLES = new Set([
180
+ "button",
181
+ "checkbox",
182
+ "combobox",
183
+ "grid",
184
+ "listbox",
185
+ "menu",
186
+ "menubar",
187
+ "menuitem",
188
+ "menuitemcheckbox",
189
+ "menuitemradio",
190
+ "option",
191
+ "radio",
192
+ "radiogroup",
193
+ "scrollbar",
194
+ "searchbox",
195
+ "slider",
196
+ "spinbutton",
197
+ "switch",
198
+ "tab",
199
+ "tablist",
200
+ "textbox",
201
+ "tree",
202
+ "treegrid",
203
+ ]);
204
+
205
+ /**
206
+ * Whether a keydown target handles the scroll keys itself, so the viewport must
207
+ * not turn them into a scroll: native form controls, editable content, and ARIA
208
+ * widgets. The browser still applies its own default for these keys.
209
+ */
210
+ function ownsScrollKeys(target: EventTarget | null): boolean {
211
+ if (!target || typeof (target as Element).closest !== "function") return false;
212
+ const el = target as HTMLElement;
213
+ if (el.isContentEditable) return true;
214
+ const tag = el.tagName;
215
+ if (tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT" || tag === "BUTTON") return true;
216
+ const role = el.getAttribute("role");
217
+ return role !== null && KEY_OWNING_ROLES.has(role);
218
+ }
219
+
178
220
  /* Local style narrowing for the web-only CSS props on the viewport element. */
179
221
  type ViewportStyle = NonNullable<BoxProps["style"]> & {
180
222
  overflowX?: "auto" | "hidden" | "scroll" | "visible";
@@ -771,7 +813,11 @@ const ScrollAreaComponent = React.forwardRef<ScrollAreaHandle, ScrollAreaProps>(
771
813
  const handleKeyDown = React.useCallback(
772
814
  (event: React.KeyboardEvent<HTMLElement>) => {
773
815
  const node = viewportNodeRef.current as HTMLElement | null;
774
- if (!node) return;
816
+ if (!node || event.defaultPrevented) return;
817
+ // Keys bubbling up from a field or widget inside the viewport belong to
818
+ // it (Space types a space, Home/End/arrows move the caret or the widget's
819
+ // selection). Leave them alone; a non-interactive descendant still scrolls.
820
+ if (event.target !== event.currentTarget && ownsScrollKeys(event.target)) return;
775
821
  const delta = keyToScrollDelta(
776
822
  event.key,
777
823
  keyStep,
@@ -24,6 +24,10 @@ export const UnstyledButtonFrame = styled(Box, {
24
24
  ...webCursor("pointer"),
25
25
  backgroundColor: "transparent",
26
26
  borderWidth: 0,
27
+ // A real `<button>` carries the browser's padding and margin. Stating them here
28
+ // means the reset applies by default, and `padding` / `margin` props still win.
29
+ padding: 0,
30
+ margin: 0,
27
31
  userSelect: "none",
28
32
  ...focusRingStyle,
29
33
  // Tactile press-dip — the same base affordance `Button`/`ActionIcon`/`Chip`