An open source design system that's fully customizable and agent ready
TypeScript
perf(core): an overlay's eager import no longer carries the lazy BottomSheet's padding (#8550)
Christopher Chedeau946211065df6
diff --git a/.changeset/overlay-eager-padding.md b/.changeset/overlay-eager-padding.mdnew file mode 100644index 0000000000..6ad2c10f05--- /dev/null+++ b/.changeset/overlay-eager-padding.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[perf] Popover, DropdownMenu, Carousel, ContextMenu, DateRangeInput, Markdown, MoreMenu and Table no longer load BottomSheet's container padding styles up front. Popover loads a BottomSheet on demand for its phone presentation, and a bundler that splits code put the padding module both of them use into one chunk that loads with the component, keeping every export either side uses. `paddingStyles` and `overlayPaddingReset` now have their own modules, still exported from `@astryxdesign/core/Layout/padding.stylex`, and `isFocusDetached` has its own module, still exported from `@astryxdesign/core/utils`. A component that uses BottomSheet's padding itself still loads it. Rendering is unchanged.++@vjeuxdiff --git a/packages/core/src/Layer/useLayer.tsx b/packages/core/src/Layer/useLayer.tsxindex 5460b87ad5..a1b531ff3d 100644--- a/packages/core/src/Layer/useLayer.tsx+++ b/packages/core/src/Layer/useLayer.tsx@@ -57,7 +57,7 @@ import {import {preserveAnimations} from './preserveAnimations';import {useLayerContext} from './LayerContext';import {useIsomorphicLayoutEffect} from '../hooks/useIsomorphicLayoutEffect';-import {overlayPaddingReset} from '../Layout/padding.stylex';+import {overlayPaddingReset} from '../Layout/overlayPaddingReset.stylex';const styles = stylex.create({// Base reset for all layersdiff --git a/packages/core/src/Layout/index.ts b/packages/core/src/Layout/index.tsindex dcf4fe5c20..0a59d49181 100644--- a/packages/core/src/Layout/index.ts+++ b/packages/core/src/Layout/index.ts@@ -21,7 +21,7 @@ export type {// Overlay padding-variable reset (applied on overlay roots so the container// padding system does not cross a fixed/top-layer boundary)-export {overlayPaddingReset} from './padding.stylex';+export {overlayPaddingReset} from './overlayPaddingReset.stylex';// Edge compensation utilityexport {edgeCompSlot, EDGE_COMP_ATTR} from './edgeCompensation.stylex';diff --git a/packages/core/src/Layout/layoutReset.ts b/packages/core/src/Layout/layoutReset.tsindex 2fb3472fa1..145cc5ba4a 100644--- a/packages/core/src/Layout/layoutReset.ts+++ b/packages/core/src/Layout/layoutReset.ts@@ -18,7 +18,7 @@*/import * as stylex from '@stylexjs/stylex';-import {overlayPaddingReset} from './padding.stylex';+import {overlayPaddingReset} from './overlayPaddingReset.stylex';const resetBeyondOverlays = stylex.create({root: {diff --git a/packages/core/src/Layout/overlayPaddingReset.stylex.ts b/packages/core/src/Layout/overlayPaddingReset.stylex.tsnew file mode 100644index 0000000000..94c7192d5c--- /dev/null+++ b/packages/core/src/Layout/overlayPaddingReset.stylex.ts@@ -0,0 +1,73 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file overlayPaddingReset.stylex.ts+ * @input Uses @stylexjs/stylex+ * @output overlayPaddingReset: the container padding boundary at an overlay's root+ * @position Layout utility, re-exported by padding.stylex.ts and Layout/index.ts.+ * Its own module so an overlay's eager code (useLayer) imports it without+ * padding.stylex.ts: a lazily loaded overlay that uses the container padding+ * styles there then keeps them out of the eager chunk it shares with this+ * module.+ */++import * as stylex from '@stylexjs/stylex';++/**+ * Padding-variable reset for overlay roots (Dialog, BottomSheet, Drawer,+ * MobileNav, Lightbox, and every layer surface).+ *+ * ## Why an overlay needs this+ *+ * The container padding system talks to descendants through inherited custom+ * properties: a padded container announces its padding, and children read the+ * value either to apply it or to cancel it with a negative margin+ * (`Section`, `Divider`, `Layout`, `Table`).+ *+ * Inheritance follows the DOM, but an overlay leaves its parent's visual box —+ * a fixed/top-layer `<dialog>` is a DOM descendant of the padded page while+ * being nowhere near it on screen. It inherits values describing padding that+ * is not there, and its content compensates against phantom space: a `Section`+ * inside a 640px sheet rendered 672px wide and hung off both edges.+ *+ * Two families leak, and they need opposite treatments:+ *+ * - `--container-padding-*` -> `0px`. Descendants SUBTRACT these (bleed+ * margins). The overlay root has no padding of its own to escape, so the+ * honest answer is zero. Nested containers that do set padding (a Dialog's+ * content wrapper) re-announce their own values below this point, so+ * legitimate edge-to-edge bleed inside the overlay is unaffected.+ * - `--layout-padding-*` and `--_section-padding-propagated` -> `initial`.+ * Descendants ADD these, so zeroing them would strip padding rather than+ * restore it. `initial` makes each guaranteed-invalid, so readers fall+ * through their own `var(…, fallback)` chain and land on the theme default —+ * which is what an overlay at the top of the tree should show.+ *+ * `initial` is also why propagation moved off `--astryx-section-padding`: that+ * name is public theme surface, set at the theme root, and making it invalid+ * here would blank the theme's own section padding inside every overlay.+ *+ * Apply on the overlay's outermost styled element.+ *+ * @example+ * ```+ * <dialog {...stylex.props(styles.dialog, overlayPaddingReset.reset)} />+ * ```+ */+export const overlayPaddingReset = stylex.create({+ reset: {+ // Subtracted by descendants — the overlay root has no padding to escape.+ '--container-padding-inline-start': '0px',+ '--container-padding-inline-end': '0px',+ '--container-padding-block-start': '0px',+ '--container-padding-block-end': '0px',+ // Added by descendants — fall through to each reader's own default.+ '--layout-padding-outer-x': 'initial',+ '--layout-padding-outer-y': 'initial',+ '--layout-padding-own-outer-x': 'initial',+ '--layout-padding-own-outer-y': 'initial',+ '--layout-padding-inner-x': 'initial',+ '--layout-padding-inner-y': 'initial',+ '--_section-padding-propagated': 'initial',+ },+});diff --git a/packages/core/src/Layout/overlayPaddingReset.test.tsx b/packages/core/src/Layout/overlayPaddingReset.test.tsxindex cc24c87870..81d7b2ea4f 100644--- a/packages/core/src/Layout/overlayPaddingReset.test.tsx+++ b/packages/core/src/Layout/overlayPaddingReset.test.tsx@@ -187,7 +187,10 @@ describe('overlayPaddingReset', () => {// this; assert it where it is decided instead. (Verified in a browser: a// theme's 20px still reaches a Section inside a Dialog nested under a// 40px page Section.)- const source = readFileSync(join(__dirname, 'padding.stylex.ts'), 'utf8');+ const source = readFileSync(+ join(__dirname, 'overlayPaddingReset.stylex.ts'),+ 'utf8',+ );const reset = source.slice(source.indexOf('export const overlayPaddingReset'),);diff --git a/packages/core/src/Layout/padding.stylex.ts b/packages/core/src/Layout/padding.stylex.tsindex 857ef734d3..a61de4ed9c 100644--- a/packages/core/src/Layout/padding.stylex.ts+++ b/packages/core/src/Layout/padding.stylex.ts@@ -3,7 +3,9 @@/*** @file padding.stylex.ts* @input Uses @stylexjs/stylex, spacing from theme- * @output StyleX styles for numeric padding prop on layout components+ * @output StyleX styles for numeric padding prop on layout components, the+ * container padding variables, and (re-exported) paddingStyles and+ * overlayPaddingReset* @position Layout utility; used by Card, Section, and Layout sub-components*/@@ -11,6 +13,12 @@ import * as stylex from '@stylexjs/stylex';import {spacingVars} from '../theme/tokens.stylex';import type {SpacingStep} from '../utils/types';+// In their own modules so an overlay's eager code imports them without this+// one, and a lazily loaded overlay that uses the container padding styles+// below keeps those out of the chunk it shares with that code.+export {paddingStyles} from './paddingStyles.stylex';+export {overlayPaddingReset} from './overlayPaddingReset.stylex';+/*** Maps numeric SpacingStep values to SpacingToken keys used by the container system.*/@@ -28,79 +36,6 @@ export const spacingStepToToken: Record<SpacingStep, string> = {10: 'spacing10',};-/**- * Padding styles for all sides using spacing tokens.- * Each key applies uniform padding to all four sides.- */-export const paddingStyles = stylex.create({- 0: {- paddingInlineStart: spacingVars['--spacing-0'],- paddingInlineEnd: spacingVars['--spacing-0'],- paddingBlockStart: spacingVars['--spacing-0'],- paddingBlockEnd: spacingVars['--spacing-0'],- },- 0.5: {- paddingInlineStart: spacingVars['--spacing-0-5'],- paddingInlineEnd: spacingVars['--spacing-0-5'],- paddingBlockStart: spacingVars['--spacing-0-5'],- paddingBlockEnd: spacingVars['--spacing-0-5'],- },- 1: {- paddingInlineStart: spacingVars['--spacing-1'],- paddingInlineEnd: spacingVars['--spacing-1'],- paddingBlockStart: spacingVars['--spacing-1'],- paddingBlockEnd: spacingVars['--spacing-1'],- },- 1.5: {- paddingInlineStart: spacingVars['--spacing-1-5'],- paddingInlineEnd: spacingVars['--spacing-1-5'],- paddingBlockStart: spacingVars['--spacing-1-5'],- paddingBlockEnd: spacingVars['--spacing-1-5'],- },- 2: {- paddingInlineStart: spacingVars['--spacing-2'],- paddingInlineEnd: spacingVars['--spacing-2'],- paddingBlockStart: spacingVars['--spacing-2'],- paddingBlockEnd: spacingVars['--spacing-2'],- },- 3: {- paddingInlineStart: spacingVars['--spacing-3'],- paddingInlineEnd: spacingVars['--spacing-3'],- paddingBlockStart: spacingVars['--spacing-3'],- paddingBlockEnd: spacingVars['--spacing-3'],- },- 4: {- paddingInlineStart: spacingVars['--spacing-4'],- paddingInlineEnd: spacingVars['--spacing-4'],- paddingBlockStart: spacingVars['--spacing-4'],- paddingBlockEnd: spacingVars['--spacing-4'],- },- 5: {- paddingInlineStart: spacingVars['--spacing-5'],- paddingInlineEnd: spacingVars['--spacing-5'],- paddingBlockStart: spacingVars['--spacing-5'],- paddingBlockEnd: spacingVars['--spacing-5'],- },- 6: {- paddingInlineStart: spacingVars['--spacing-6'],- paddingInlineEnd: spacingVars['--spacing-6'],- paddingBlockStart: spacingVars['--spacing-6'],- paddingBlockEnd: spacingVars['--spacing-6'],- },- 8: {- paddingInlineStart: spacingVars['--spacing-8'],- paddingInlineEnd: spacingVars['--spacing-8'],- paddingBlockStart: spacingVars['--spacing-8'],- paddingBlockEnd: spacingVars['--spacing-8'],- },- 10: {- paddingInlineStart: spacingVars['--spacing-10'],- paddingInlineEnd: spacingVars['--spacing-10'],- paddingBlockStart: spacingVars['--spacing-10'],- paddingBlockEnd: spacingVars['--spacing-10'],- },-});-/*** Container padding inline CSS variable styles for edge compensation.* Sets --container-padding-inline-start and --container-padding-inline-end so@@ -474,62 +409,3 @@ export const sectionPaddingPropagationStyles = stylex.create({8: {'--_section-padding-propagated': spacingVars['--spacing-8']},10: {'--_section-padding-propagated': spacingVars['--spacing-10']},});--/**- * Padding-variable reset for overlay roots (Dialog, BottomSheet, Drawer,- * MobileNav, Lightbox, and every layer surface).- *- * ## Why an overlay needs this- *- * The container padding system talks to descendants through inherited custom- * properties: a padded container announces its padding, and children read the- * value either to apply it or to cancel it with a negative margin- * (`Section`, `Divider`, `Layout`, `Table`).- *- * Inheritance follows the DOM, but an overlay leaves its parent's visual box —- * a fixed/top-layer `<dialog>` is a DOM descendant of the padded page while- * being nowhere near it on screen. It inherits values describing padding that- * is not there, and its content compensates against phantom space: a `Section`- * inside a 640px sheet rendered 672px wide and hung off both edges (#5208).- *- * Two families leak, and they need opposite treatments:- *- * - `--container-padding-*` -> `0px`. Descendants SUBTRACT these (bleed- * margins). The overlay root has no padding of its own to escape, so the- * honest answer is zero. Nested containers that do set padding (a Dialog's- * content wrapper) re-announce their own values below this point, so- * legitimate edge-to-edge bleed inside the overlay is unaffected.- * - `--layout-padding-*` and `--_section-padding-propagated` -> `initial`.- * Descendants ADD these, so zeroing them would strip padding rather than- * restore it. `initial` makes each guaranteed-invalid, so readers fall- * through their own `var(…, fallback)` chain and land on the theme default —- * which is what an overlay at the top of the tree should show.- *- * `initial` is also why propagation moved off `--astryx-section-padding`: that- * name is public theme surface, set at the theme root, and making it invalid- * here would blank the theme's own section padding inside every overlay.- *- * Apply on the overlay's outermost styled element.- *- * @example- * ```- * <dialog {...stylex.props(styles.dialog, overlayPaddingReset.reset)} />- * ```- */-export const overlayPaddingReset = stylex.create({- reset: {- // Subtracted by descendants — the overlay root has no padding to escape.- '--container-padding-inline-start': '0px',- '--container-padding-inline-end': '0px',- '--container-padding-block-start': '0px',- '--container-padding-block-end': '0px',- // Added by descendants — fall through to each reader's own default.- '--layout-padding-outer-x': 'initial',- '--layout-padding-outer-y': 'initial',- '--layout-padding-own-outer-x': 'initial',- '--layout-padding-own-outer-y': 'initial',- '--layout-padding-inner-x': 'initial',- '--layout-padding-inner-y': 'initial',- '--_section-padding-propagated': 'initial',- },-});diff --git a/packages/core/src/Layout/paddingStyles.stylex.ts b/packages/core/src/Layout/paddingStyles.stylex.tsnew file mode 100644index 0000000000..1f034c5f51--- /dev/null+++ b/packages/core/src/Layout/paddingStyles.stylex.ts@@ -0,0 +1,87 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file paddingStyles.stylex.ts+ * @input Uses @stylexjs/stylex, spacing from theme+ * @output paddingStyles: uniform padding on all four sides by SpacingStep+ * @position Layout utility, re-exported by padding.stylex.ts. Its own module so an+ * overlay's eager code (usePopover) imports it without padding.stylex.ts:+ * a lazily loaded overlay that uses the container padding styles there then+ * keeps them out of the eager chunk it shares with this module.+ */++import * as stylex from '@stylexjs/stylex';+import {spacingVars} from '../theme/tokens.stylex';++/**+ * Padding styles for all sides using spacing tokens.+ * Each key applies uniform padding to all four sides.+ */+export const paddingStyles = stylex.create({+ 0: {+ paddingInlineStart: spacingVars['--spacing-0'],+ paddingInlineEnd: spacingVars['--spacing-0'],+ paddingBlockStart: spacingVars['--spacing-0'],+ paddingBlockEnd: spacingVars['--spacing-0'],+ },+ 0.5: {+ paddingInlineStart: spacingVars['--spacing-0-5'],+ paddingInlineEnd: spacingVars['--spacing-0-5'],+ paddingBlockStart: spacingVars['--spacing-0-5'],+ paddingBlockEnd: spacingVars['--spacing-0-5'],+ },+ 1: {+ paddingInlineStart: spacingVars['--spacing-1'],+ paddingInlineEnd: spacingVars['--spacing-1'],+ paddingBlockStart: spacingVars['--spacing-1'],+ paddingBlockEnd: spacingVars['--spacing-1'],+ },+ 1.5: {+ paddingInlineStart: spacingVars['--spacing-1-5'],+ paddingInlineEnd: spacingVars['--spacing-1-5'],+ paddingBlockStart: spacingVars['--spacing-1-5'],+ paddingBlockEnd: spacingVars['--spacing-1-5'],+ },+ 2: {+ paddingInlineStart: spacingVars['--spacing-2'],+ paddingInlineEnd: spacingVars['--spacing-2'],+ paddingBlockStart: spacingVars['--spacing-2'],+ paddingBlockEnd: spacingVars['--spacing-2'],+ },+ 3: {+ paddingInlineStart: spacingVars['--spacing-3'],+ paddingInlineEnd: spacingVars['--spacing-3'],+ paddingBlockStart: spacingVars['--spacing-3'],+ paddingBlockEnd: spacingVars['--spacing-3'],+ },+ 4: {+ paddingInlineStart: spacingVars['--spacing-4'],+ paddingInlineEnd: spacingVars['--spacing-4'],+ paddingBlockStart: spacingVars['--spacing-4'],+ paddingBlockEnd: spacingVars['--spacing-4'],+ },+ 5: {+ paddingInlineStart: spacingVars['--spacing-5'],+ paddingInlineEnd: spacingVars['--spacing-5'],+ paddingBlockStart: spacingVars['--spacing-5'],+ paddingBlockEnd: spacingVars['--spacing-5'],+ },+ 6: {+ paddingInlineStart: spacingVars['--spacing-6'],+ paddingInlineEnd: spacingVars['--spacing-6'],+ paddingBlockStart: spacingVars['--spacing-6'],+ paddingBlockEnd: spacingVars['--spacing-6'],+ },+ 8: {+ paddingInlineStart: spacingVars['--spacing-8'],+ paddingInlineEnd: spacingVars['--spacing-8'],+ paddingBlockStart: spacingVars['--spacing-8'],+ paddingBlockEnd: spacingVars['--spacing-8'],+ },+ 10: {+ paddingInlineStart: spacingVars['--spacing-10'],+ paddingInlineEnd: spacingVars['--spacing-10'],+ paddingBlockStart: spacingVars['--spacing-10'],+ paddingBlockEnd: spacingVars['--spacing-10'],+ },+});diff --git a/packages/core/src/Lightbox/Lightbox.tsx b/packages/core/src/Lightbox/Lightbox.tsxindex 144d032b96..3a8a39ec31 100644--- a/packages/core/src/Lightbox/Lightbox.tsx+++ b/packages/core/src/Lightbox/Lightbox.tsx@@ -43,7 +43,7 @@ import {layerStructureReset} from '../Layer/layerStructureReset.stylex';import {LayerContentBoundary} from '../Layer/layerScopedContext';import {layerClickBoundary} from '../Layer/layerClickBoundary';import {ModalAnnounceProvider} from '../hooks/ModalAnnounceProvider';-import {overlayPaddingReset} from '../Layout/padding.stylex';+import {overlayPaddingReset} from '../Layout/overlayPaddingReset.stylex';import {LayerDepthProvider} from '../Layer/LayerDepthContext';import {useLayerDismissal} from '../Layer/useLayerDismissal';diff --git a/packages/core/src/MobileNav/MobileNav.tsx b/packages/core/src/MobileNav/MobileNav.tsxindex bf357677d9..a09e28856c 100644--- a/packages/core/src/MobileNav/MobileNav.tsx+++ b/packages/core/src/MobileNav/MobileNav.tsx@@ -68,7 +68,7 @@ import {layerTextReset} from '../Layer/layerTextReset.stylex';import {layerStructureReset} from '../Layer/layerStructureReset.stylex';import {LayerContentBoundary} from '../Layer/layerScopedContext';import {layerClickBoundary} from '../Layer/layerClickBoundary';-import {overlayPaddingReset} from '../Layout/padding.stylex';+import {overlayPaddingReset} from '../Layout/overlayPaddingReset.stylex';import {LayerDepthProvider} from '../Layer/LayerDepthContext';import {ModalAnnounceProvider} from '../hooks/ModalAnnounceProvider';import {useLayerDismissal} from '../Layer/useLayerDismissal';diff --git a/packages/core/src/Popover/Popover.test.tsx b/packages/core/src/Popover/Popover.test.tsxindex 13369c1547..ff51e0f451 100644--- a/packages/core/src/Popover/Popover.test.tsx+++ b/packages/core/src/Popover/Popover.test.tsx@@ -1449,12 +1449,10 @@ describe('Popover', () => {describe('surface padding', () => {// StyleX's dev runtime names the style object a class came from- // ("padding__paddingStyles.3"), which is the padding rung the surface- // was handed.+ // ("paddingStyles__paddingStyles.3", after its module), which is the+ // padding rung the surface was handed.function paddingRungOf(surface: Element | null): string | null {- const match = surface?.className.match(- /padding__paddingStyles\.([\d.]+)/,- );+ const match = surface?.className.match(/__paddingStyles\.([\d.]+)/);return match ? match[1] : null;}diff --git a/packages/core/src/Popover/usePopover.tsx b/packages/core/src/Popover/usePopover.tsxindex 8a2ac8db62..677ec07921 100644--- a/packages/core/src/Popover/usePopover.tsx+++ b/packages/core/src/Popover/usePopover.tsx@@ -34,9 +34,9 @@ import {rtlStyles} from '../utils';import {useDevWarning} from '../hooks/useDevWarning';import {focusOutlineProps} from '../utils/focusOutline.stylex';import {mergeProps} from '../utils/mergeProps';-import {isFocusDetached} from '../utils/focusReturn';+import {isFocusDetached} from '../utils/isFocusDetached';import {themeProps} from '../utils/themeProps';-import {paddingStyles} from '../Layout/padding.stylex';+import {paddingStyles} from '../Layout/paddingStyles.stylex';import type {SpacingStep} from '../utils/types';import {stableClassName} from '../naming';import {diff --git a/packages/core/src/PowerSearch/PowerSearch.tsx b/packages/core/src/PowerSearch/PowerSearch.tsxindex 01ac502628..5d413620ef 100644--- a/packages/core/src/PowerSearch/PowerSearch.tsx+++ b/packages/core/src/PowerSearch/PowerSearch.tsx@@ -73,7 +73,7 @@ import {fontWeightVars,} from '../theme/tokens.stylex';import {useSize} from '../SizeContext/SizeContext';-import {isFocusDetached} from '../utils/focusReturn';+import {isFocusDetached} from '../utils/isFocusDetached';import {isRenderable} from '../utils/isRenderable';import {useInternalConfig} from './useInternalConfig';import {usePowerSearchSource} from './usePowerSearchSource';diff --git a/packages/core/src/Tokenizer/Tokenizer.tsx b/packages/core/src/Tokenizer/Tokenizer.tsxindex 56484ea7fb..95141fb739 100644--- a/packages/core/src/Tokenizer/Tokenizer.tsx+++ b/packages/core/src/Tokenizer/Tokenizer.tsx@@ -91,7 +91,7 @@ import type {} from '../Typeahead/types';import {composeEventHandlers, mergeProps} from '../utils';import {joinAriaIDs} from '../utils/inputAria';-import {isFocusDetached} from '../utils/focusReturn';+import {isFocusDetached} from '../utils/isFocusDetached';import {themeProps} from '../utils/themeProps';import type {SheetDraftChange} from '../internal/sheetDraft';import type {TokenizerBottomSheetProps} from './TokenizerBottomSheet';diff --git a/packages/core/src/utils/focusReturn.test.ts b/packages/core/src/utils/focusReturn.test.tsindex 13e7738927..2be7756c25 100644--- a/packages/core/src/utils/focusReturn.test.ts+++ b/packages/core/src/utils/focusReturn.test.ts@@ -3,11 +3,11 @@import {describe, it, expect, afterEach, beforeEach, vi} from 'vitest';import {focusForLastInput,- isFocusDetached,isFocusLost,moveFocusLeftBehind,noteFocusWithin,} from './focusReturn';+import {isFocusDetached} from './isFocusDetached';import {__resetInteractionModalityForTest,__startInteractionModalityTrackingForTest,diff --git a/packages/core/src/utils/focusReturn.ts b/packages/core/src/utils/focusReturn.tsindex be45e0a653..4da70689e9 100644--- a/packages/core/src/utils/focusReturn.ts+++ b/packages/core/src/utils/focusReturn.ts@@ -4,43 +4,20 @@* @file focusReturn.ts* @input The document's current active element and the shared interaction* modality.- * @output Whether focus is currently "detached" — on nothing in particular —- * or would be lost if a surface hid now; a focus move whose ring follows the- * last input; and the one path that moves focus out of content a change- * removed or hid (`noteFocusWithin`, `moveFocusLeftBehind`).- * @position Utility; used by overlay triggers (e.g. DateInput, DateTimeInput)- * that return focus to their control when the overlay closes, but only when- * the dismiss did not already move focus somewhere the user chose, and by- * overlays (Dialog, DropdownMenu) that move focus programmatically, and by- * disclosures that hide focused content. Only- * `isFocusDetached` is exported from the utils entry; the rest is internal.+ * @output Whether focus would be lost if a surface hid now; a focus move whose+ * ring follows the last input; and the one path that moves focus out of+ * content a change removed or hid (`noteFocusWithin`, `moveFocusLeftBehind`).+ * @position Utility; used by overlays (BottomSheet, Dialog, DropdownMenu) that+ * move focus programmatically, and by disclosures that hide focused+ * content. isFocusDetached lives in isFocusDetached.ts, so the utils entry,+ * which exports only it, does not reach this module.*/import {getInteractionModality,type InteractionModality,} from './interactionModality';--/**- * True when focus rests on nothing in particular — no active element, or the- * document body / root element.- *- * An overlay (popover, calendar, dropdown) that restores focus to its trigger- * on close should do so ONLY when this is true. A native `popover="auto"`- * light-dismiss fires its close synchronously with the pointer event that- * moved focus, so by the time the close handler runs, focus has already- * landed on whatever the user clicked — another field, a button, anywhere.- * Restoring focus unconditionally would yank it back and fight that click.- * The one case the browser leaves unresolved is Escape or a click on- * non-focusable empty space, which drops focus to the body; that is exactly- * when the trigger should reclaim it.- */-export function isFocusDetached(doc: Document = document): boolean {- const active = doc.activeElement;- return (- active == null || active === doc.body || active === doc.documentElement- );-}+import {isFocusDetached} from './isFocusDetached';/*** True when focus would be lost if `surface` hid now: nothing is focused (seediff --git a/packages/core/src/utils/index.ts b/packages/core/src/utils/index.tsindex c07928f4b7..5ca9917676 100644--- a/packages/core/src/utils/index.ts+++ b/packages/core/src/utils/index.ts@@ -76,7 +76,7 @@ export {characterCount, firstCharacter, truncateCharacters} from './characters';export {mergeProps} from './mergeProps';export {mergeRefs} from './mergeRefs';-export {isFocusDetached} from './focusReturn';+export {isFocusDetached} from './isFocusDetached';export {composeEventHandlers} from './composeEventHandlers';export {themeProps, themeDataAttributes} from './themeProps';export type {diff --git a/packages/core/src/utils/isFocusDetached.ts b/packages/core/src/utils/isFocusDetached.tsnew file mode 100644index 0000000000..060101ee6c--- /dev/null+++ b/packages/core/src/utils/isFocusDetached.ts@@ -0,0 +1,34 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file isFocusDetached.ts+ * @input The document's current active element+ * @output Whether focus is currently "detached": on nothing in particular+ * @position Utility, exported from utils/index.ts; used by overlay triggers+ * (e.g. DateInput, DateTimeInput) that return focus to their control when the+ * overlay closes, but only when the dismiss did not already move focus+ * somewhere the user chose. Apart from focusReturn.ts so the utils barrel+ * does not reach focusForLastInput: an overlay loaded with import() that+ * uses it then keeps it out of the chunk it shares with eager code.+ */++/**+ * True when focus rests on nothing in particular — no active element, or the+ * document body / root element.+ *+ * An overlay (popover, calendar, dropdown) that restores focus to its trigger+ * on close should do so ONLY when this is true. A native `popover="auto"`+ * light-dismiss fires its close synchronously with the pointer event that+ * moved focus, so by the time the close handler runs, focus has already+ * landed on whatever the user clicked — another field, a button, anywhere.+ * Restoring focus unconditionally would yank it back and fight that click.+ * The one case the browser leaves unresolved is Escape or a click on+ * non-focusable empty space, which drops focus to the body; that is exactly+ * when the trigger should reclaim it.+ */+export function isFocusDetached(doc: Document = document): boolean {+ const active = doc.activeElement;+ return (+ active == null || active === doc.body || active === doc.documentElement+ );+}