Skip to content

Commit 7b5ba57

Browse files
huntiefacebook-github-bot
authored andcommitted
Move ToastAndroid fallback to separate module, copy doc comments (#50427)
Summary: Pull Request resolved: #50427 Move non-Android implementation into `ToastFallback.js`. The added `Platform.OS` check allows compatibility when this import subpath is specified in package.json `"exports"`. Changelog: [Internal] Reviewed By: rubennorte Differential Revision: D72238079 fbshipit-source-id: 6ccc5c17a36015a3d76573b447b61467f47fb419
1 parent d13be57 commit 7b5ba57

File tree

5 files changed

+197
-32
lines changed

5 files changed

+197
-32
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @flow strict-local
9+
*/
10+
11+
import ToastAndroidFallback from './ToastAndroidFallback';
12+
13+
export default ToastAndroidFallback;

packages/react-native/Libraries/Components/ToastAndroid/ToastAndroid.js

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,10 @@
88
* @flow strict-local
99
*/
1010

11-
'use strict';
11+
// NOTE: This file supports backwards compatibility of subpath (deep) imports
12+
// from 'react-native' with platform-specific extensions. It can be deleted
13+
// once we remove the "./*" mapping from package.json "exports".
1214

13-
const ToastAndroid = {
14-
// Dummy fallback toast duration constants
15-
SHORT: (0: number),
16-
LONG: (0: number),
17-
// Dummy fallback toast gravity constants
18-
TOP: (0: number),
19-
BOTTOM: (0: number),
20-
CENTER: (0: number),
21-
22-
show: function (message: string, duration: number): void {
23-
console.warn('ToastAndroid is not supported on this platform.');
24-
},
25-
26-
showWithGravity: function (
27-
message: string,
28-
duration: number,
29-
gravity: number,
30-
): void {
31-
console.warn('ToastAndroid is not supported on this platform.');
32-
},
33-
34-
showWithGravityAndOffset: function (
35-
message: string,
36-
duration: number,
37-
gravity: number,
38-
xOffset: number,
39-
yOffset: number,
40-
): void {
41-
console.warn('ToastAndroid is not supported on this platform.');
42-
},
43-
};
15+
import ToastAndroid from './ToastAndroid';
4416

4517
export default ToastAndroid;
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @flow strict-local
9+
*/
10+
11+
/**
12+
* This exposes the native ToastAndroid module as a JS module. This has a function 'show'
13+
* which takes the following parameters:
14+
*
15+
* 1. String message: A string with the text to toast
16+
* 2. int duration: The duration of the toast. May be ToastAndroid.SHORT or ToastAndroid.LONG
17+
*
18+
* There is also a function `showWithGravity` to specify the layout gravity. May be
19+
* ToastAndroid.TOP, ToastAndroid.BOTTOM, ToastAndroid.CENTER
20+
*
21+
* **Note**: Starting from Android API level 30 (Android R) or higher, for apps targeting
22+
* that API level, setting toast gravity is a no-op for text toasts.
23+
* This means that in many cases `TOP`, `BOTTOM`, `CENTER`, or offsets may not have
24+
* any visible effect on actual toast positioning.
25+
*
26+
* Reference: https://developer.android.com/reference/android/widget/Toast#setGravity(int,%20int,%20int)
27+
*/
28+
declare const ToastAndroid: {
29+
/**
30+
* Indicates a short duration on the screen.
31+
*
32+
* Value: 2000 milliseconds (2 seconds).
33+
*/
34+
SHORT: number,
35+
36+
/**
37+
* Indicates a long duration on the screen.
38+
*
39+
* Value: 3500 milliseconds (3.5 seconds).
40+
*/
41+
LONG: number,
42+
43+
/**
44+
* Indicates that the toast message should appear at the top of the screen.
45+
*
46+
* **Note**: On Android R or later, this may not have any visible effect.
47+
*/
48+
TOP: number,
49+
50+
/**
51+
* Indicates that the toast message should appear at the bottom of the screen.
52+
*
53+
* **Note**: On Android R or later, this may not have any visible effect.
54+
*/
55+
BOTTOM: number,
56+
57+
/**
58+
* Indicates that the toast message should appear at the center of the screen.
59+
*
60+
* **Note**: On Android R or later, this may not have any visible effect.
61+
*/
62+
CENTER: number,
63+
64+
/**
65+
* Display a toast message for a specified duration.
66+
*
67+
* @param message A string with the text to toast.
68+
* @param duration The duration of the toast–either ToastAndroid.SHORT or ToastAndroid.LONG
69+
*/
70+
show: (message: string, duration: number) => void,
71+
72+
/**
73+
* Display a toast message for a specified duration with a given gravity.
74+
*
75+
* @param message A string with the text to display in the toast.
76+
* @param duration The duration of the toast.
77+
* May be `ToastAndroid.SHORT` or `ToastAndroid.LONG`.
78+
* @param gravity Positioning on the screen, e.g.,
79+
* `ToastAndroid.TOP`, `ToastAndroid.BOTTOM`, or `ToastAndroid.CENTER`.
80+
*
81+
* **Note**: On Android R (API 30) or later (when targeting API 30+), this setting may
82+
* not have any effect on text toast placement due to `setGravity` becoming a no-op.
83+
*/
84+
showWithGravity: (message: string, duration: number, gravity: number) => void,
85+
86+
/**
87+
* Display a toast message for a specified duration with a given gravity and custom offsets.
88+
*
89+
* @param message A string with the text to display in the toast.
90+
* @param duration The duration of the toast.
91+
* May be `ToastAndroid.SHORT` or `ToastAndroid.LONG`.
92+
* @param gravity Positioning on the screen, e.g.,
93+
* `ToastAndroid.TOP`, `ToastAndroid.BOTTOM`, or `ToastAndroid.CENTER`.
94+
* @param xOffset Horizontal offset from the given gravity.
95+
* @param yOffset Vertical offset from the given gravity.
96+
*
97+
* **Note**: On Android R (API 30) or later (when targeting API 30+), setting gravity
98+
* and offsets may not visibly affect the placement of text toasts.
99+
*/
100+
showWithGravityAndOffset: (
101+
message: string,
102+
duration: number,
103+
gravity: number,
104+
xOffset: number,
105+
yOffset: number,
106+
) => void,
107+
};
108+
109+
export default ToastAndroid;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @flow strict-local
9+
*/
10+
11+
'use strict';
12+
13+
const ToastAndroid = {
14+
// Dummy fallback toast duration constants
15+
SHORT: (0: number),
16+
LONG: (0: number),
17+
// Dummy fallback toast gravity constants
18+
TOP: (0: number),
19+
BOTTOM: (0: number),
20+
CENTER: (0: number),
21+
22+
show: function (message: string, duration: number): void {
23+
console.warn('ToastAndroid is not supported on this platform.');
24+
},
25+
26+
showWithGravity: function (
27+
message: string,
28+
duration: number,
29+
gravity: number,
30+
): void {
31+
console.warn('ToastAndroid is not supported on this platform.');
32+
},
33+
34+
showWithGravityAndOffset: function (
35+
message: string,
36+
duration: number,
37+
gravity: number,
38+
xOffset: number,
39+
yOffset: number,
40+
): void {
41+
console.warn('ToastAndroid is not supported on this platform.');
42+
},
43+
};
44+
45+
export default ToastAndroid;

packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3118,6 +3118,32 @@ declare export default typeof NativeToastAndroid;
31183118
`;
31193119

31203120
exports[`public API should not change unintentionally Libraries/Components/ToastAndroid/ToastAndroid.js 1`] = `
3121+
"declare export default typeof ToastAndroid;
3122+
"
3123+
`;
3124+
3125+
exports[`public API should not change unintentionally Libraries/Components/ToastAndroid/ToastAndroid.js.flow 1`] = `
3126+
"declare const ToastAndroid: {
3127+
SHORT: number,
3128+
LONG: number,
3129+
TOP: number,
3130+
BOTTOM: number,
3131+
CENTER: number,
3132+
show: (message: string, duration: number) => void,
3133+
showWithGravity: (message: string, duration: number, gravity: number) => void,
3134+
showWithGravityAndOffset: (
3135+
message: string,
3136+
duration: number,
3137+
gravity: number,
3138+
xOffset: number,
3139+
yOffset: number
3140+
) => void,
3141+
};
3142+
declare export default typeof ToastAndroid;
3143+
"
3144+
`;
3145+
3146+
exports[`public API should not change unintentionally Libraries/Components/ToastAndroid/ToastAndroidFallback.js 1`] = `
31213147
"declare const ToastAndroid: {
31223148
SHORT: number,
31233149
LONG: number,

0 commit comments

Comments
 (0)