Skip to content

feat: Add useMobile hook and breakpoints - #173

Merged
jperals merged 1 commit into
mainfrom
use-mobile
Nov 12, 2025
Merged

feat: Add useMobile hook and breakpoints#173
jperals merged 1 commit into
mainfrom
use-mobile

Conversation

@jperals

@jperalsjperals commented Nov 11, 2025

Copy link
Copy Markdown
Member

Extract the useMobile hook and the breakpoints from components into component-toolkit, making window.matchMedia more error-resilient in the way.

After this, components will be refactored to use the useMobile hook and the breakpoints from component-toolkit.

Related ticket: AWSUI-61447

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@codecov

codecovBot commented Nov 11, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.78%. Comparing base (d7ff55d) to head (c4caf56).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@ Coverage Diff @@## main #173 +/- ##
=======================================
Coverage 98.78% 98.78% =======================================
Files 36 37 +1 Lines 905 906 +1 Branches 242 254 +12 =======================================
+ Hits 894 895 +1 
Misses 11 11 

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jperals
jperals marked this pull request as ready for review November 11, 2025 17:18
@jperals
jperals requested a review from a team as a code ownerNovember 11, 2025 17:18
@jperals
jperals requested review from YueyingLu and removed request for a teamNovember 11, 2025 17:18
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

export function safeMatchMedia(element: HTMLElement, query: string) {

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved from src/internal/visual-mode/index.ts so that the new useMobile hook can use it too

* wouldn't know which one of them to use).
* Instead, we use the media query here in JS too.
*/
return safeMatchMedia(document.body, `(max-width: ${mobileBreakpoint}px)`);

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small difference with the existing implementation in components —I am not considering the case where window.matchMedia is falsy —TypeScript told me it is not possible and browser support quite agrees.

If, for any reason, in the very worst case, the call to matchMedia errors out, the error will be caught in safeMatchMedia.

['default', -1],
];

export const mobileBreakpoint = BREAKPOINT_MAPPING.filter(b => b[0] === 'xs')[0][1];

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is also necessary to extract the breakpoints because useMobile needs the mobileBreakpoint.

@jperals
jperals added this pull request to the merge queueNov 12, 2025
Merged via the queue into main with commit 1e2200fNov 12, 2025
45 checks passed
@jperals
jperals deleted the use-mobile branch November 12, 2025 12:43
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@jperals@YueyingLu