Skip to content

[utils] Move clamp, areArraysEqual, shadowDom, and formatNumber to utils#5167

Open
romgrk wants to merge 2 commits into
mui:masterfrom
romgrk:move-stdlib-utils-phase1
Open

[utils] Move clamp, areArraysEqual, shadowDom, and formatNumber to utils#5167
romgrk wants to merge 2 commits into
mui:masterfrom
romgrk:move-stdlib-utils-phase1

Conversation

@romgrk

@romgrk romgrk commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Summary

First phase of moving domain-agnostic primitives out of @base-ui/react's internals/ and utils/ into the shared @base-ui/utils stdlib (usable across MUI + Base UI). These are pure, dependency-clean utilities with no Base UI-specific semantics.

Moved (history preserved via git mv):

  • clamp — pure math
  • areArraysEqual — reimplemented inspired by fastArrayCompare from @mui/x-internals: referential-equality short-circuit, early length bail-out, and a reverse loop that inlines the element check when no custom comparer is passed. Uses Object.is for the default comparison (consistent with the sibling itemEquality util) so NaN values are treated as equal. The optional itemComparer parameter is retained for the Select/Combobox call sites.
  • shadowDomactiveElement, contains, getTarget (shadow-DOM-safe traversal)
  • formatNumber + its stringifyLocale dependency

Changes

  • All import sites repointed to @base-ui/utils/* (17 clamp, 4 areArraysEqual, 8 formatNumber, 1 shadowDom, 1 stringifyLocale).
  • Dropped the now-obsolete ./internals/{areArraysEqual,clamp,shadowDom} export subpaths from packages/react/package.json. The moved files are auto-exported via the utils package's ./* glob.
  • Added a test for areArraysEqual (previously untested); clamp, formatNumber, and stringifyLocale tests moved alongside their sources and now run under the @base-ui/utils project.

Follow-up

A subsequent phase will import the broader set of generic utilities currently living in @mui/x-internals (e.g. hash, isDeepEqual, throttle, disposable, …) into @base-ui/utils, in coordination with a parallel mui-x PR that re-exports them from the stdlib. That work is intentionally kept out of this PR to keep the move small and reviewable.

Verification

  • pnpm typescript
  • pnpm eslint
  • pnpm prettier
  • pnpm test:jsdom clamp areArraysEqual formatNumber stringifyLocale ✓ (13 tests)

🤖 Generated with Claude Code

Relocate a first batch of domain-agnostic primitives from the react
package into the shared @base-ui/utils stdlib (usable across MUI + Base UI):

- clamp
- areArraysEqual (reimplemented with a referential short-circuit and a
  reverse loop, inspired by fastArrayCompare from @mui/x-internals)
- shadowDom (activeElement, contains, getTarget)
- formatNumber + stringifyLocale

Update all import sites to @base-ui/utils/* and drop the now-obsolete
./internals/{areArraysEqual,clamp,shadowDom} export subpaths.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@pkg-pr-new

pkg-pr-new Bot commented Jul 3, 2026

Copy link
Copy Markdown

commit: 32f5fcb

@romgrk romgrk marked this pull request as draft July 3, 2026 17:33
@romgrk romgrk changed the title [utils] Move clamp, areArraysEqual, shadowDom, and formatNumber to utils [utils] unify with x-internals Jul 3, 2026
@code-infra-dashboard

code-infra-dashboard Bot commented Jul 3, 2026

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+61B(+0.01%) 🔺+31B(+0.02%)

Details of bundle changes

Performance

Total duration: 1,247.83 ms +90.32 ms(+7.8%) | Renders: 78 (+0)

Test Duration Renders
Checkbox mount (500 instances) 65.56 ms ▼-16.92 ms(-20.5%) 1 (+0)

13 tests within noise — details


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Jul 3, 2026

Copy link
Copy Markdown

Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 32f5fcb
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a4ab308830d500008916ea2
😎 Deploy Preview https://deploy-preview-5167--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@romgrk romgrk force-pushed the move-stdlib-utils-phase1 branch from 55e8cbb to 120d582 Compare July 5, 2026 19:12
@romgrk romgrk changed the title [utils] unify with x-internals [utils] Move clamp, areArraysEqual, shadowDom, and formatNumber to utils Jul 5, 2026
@romgrk romgrk added type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature. internal Behind-the-scenes enhancement. Formerly called “core”. labels Jul 5, 2026
@romgrk romgrk marked this pull request as ready for review July 5, 2026 19:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal Behind-the-scenes enhancement. Formerly called “core”. type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant