Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/react-aria/src/utils/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ function testUserAgent(re: RegExp) {
}

function testPlatform(re: RegExp) {
return typeof window !== 'undefined' && window.navigator != null
? re.test(window.navigator['userAgentData']?.platform || window.navigator.platform)
: false;
if (typeof window === 'undefined' || window.navigator == null) {
return false;
}

let platform = window.navigator['userAgentData']?.platform;
// The iOS platform hint is shared by iPhone and iPad.
return re.test(platform && platform !== 'iOS' ? platform : window.navigator.platform);
}

function cached(fn: () => boolean) {
Expand Down
33 changes: 33 additions & 0 deletions packages/react-aria/test/utils/platform.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2026 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

import {isAppleDevice, isIOS, isIPad, isIPhone} from '../../src/utils/platform';

describe('platform detection', function () {
afterEach(() => {
jest.restoreAllMocks();
Reflect.deleteProperty(navigator, 'userAgentData');
});

it.each(['iPhone', 'iPad'])('detects %s with an iOS platform hint', platform => {
jest.spyOn(navigator, 'platform', 'get').mockReturnValue(platform);
Object.defineProperty(navigator, 'userAgentData', {
configurable: true,
value: {platform: 'iOS'}
});

expect(isIPhone()).toBe(platform === 'iPhone');
expect(isIPad()).toBe(platform === 'iPad');
expect(isIOS()).toBe(true);
expect(isAppleDevice()).toBe(true);
});
});
Loading