mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: [`Share`](https://reactnative.dev/docs/share) currently does not support the `anchor` option in iOS, so share sheets will always be displayed in the middle of the screen on iPads and on the top left corner of the window on Mac Catalyst. This PR utilizes the `anchor` functionality already implemented in [`ActionSheetIOS`](https://reactnative.dev/docs/actionsheetios) to bring this support to `Share` on iOS. ## Changelog [iOS] [Changed] - type definition for the `options` parameter of `Share.share` (added optional `anchor` property) [iOS] [Added] - `anchor` option support for `Share` Pull Request resolved: https://github.com/facebook/react-native/pull/35008 Test Plan: Tested with modified `rn-tester` that utilizes the `anchor` option on iPad simulator. Marked all 3 changes in code.  ```js const SharedAction = () => { const [shared, setShared] = React.useState(); const ref = React.useRef(); /* create ref (1/3) */ const sharedAction = async () => { try { const result = await Share.share( { title: 'Create native apps', message: ('React Native combines the best parts of native development with React, a best-in-class JavaScript library for building user interfaces.': string), url: 'https://reactnative.dev/', }, { subject: 'MUST READ: Create native apps with React Native', dialogTitle: 'Share React Native Home Page', tintColor: 'blue', anchor: ref.current?._nativeTag, /* add anchor in options (2/3) */ }, ); if (result.action === Share.sharedAction) { setShared(result.action); } else if (result.action === Share.dismissedAction) { //iOS only, if dialog was dismissed setShared(null); } } catch (e) { console.error(e); } }; return ( <View style={styles.container}> <Text>action: {shared ? shared : 'null'}</Text> <Text style={styles.title}>Create native apps</Text> <Text> React Native combines the best parts of native development with React, a best-in-class JavaScript library for building user interfaces. </Text> {/* supply ref to Node (3/3) */} <Text ref={ref} style={styles.button} onPress={sharedAction}> SHARE </Text> </View> ); }; ``` Reviewed By: cipolleschi Differential Revision: D40459336 Pulled By: skinsshark fbshipit-source-id: 72fbb3905ea0b982bb7f4b99967d121cd482181a
81 lines
1.9 KiB
TypeScript
81 lines
1.9 KiB
TypeScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
*/
|
|
|
|
import {ColorValue} from '../StyleSheet/StyleSheet';
|
|
|
|
export type ShareContent =
|
|
| {
|
|
title?: string | undefined;
|
|
message: string;
|
|
}
|
|
| {
|
|
title?: string | undefined;
|
|
url: string;
|
|
};
|
|
|
|
export type ShareOptions = {
|
|
dialogTitle?: string | undefined;
|
|
excludedActivityTypes?: Array<string> | undefined;
|
|
tintColor?: ColorValue | undefined;
|
|
subject?: string | undefined;
|
|
anchor?: number | undefined;
|
|
};
|
|
|
|
export type ShareSharedAction = {
|
|
action: 'sharedAction';
|
|
activityType?: string | undefined;
|
|
};
|
|
|
|
export type ShareDismissedAction = {
|
|
action: 'dismissedAction';
|
|
};
|
|
|
|
export type ShareAction = ShareSharedAction | ShareDismissedAction;
|
|
|
|
export interface ShareStatic {
|
|
/**
|
|
* Open a dialog to share text content.
|
|
*
|
|
* In iOS, Returns a Promise which will be invoked an object containing `action`, `activityType`.
|
|
* If the user dismissed the dialog, the Promise will still be resolved with action being `Share.dismissedAction`
|
|
* and all the other keys being undefined.
|
|
*
|
|
* In Android, Returns a Promise which always be resolved with action being `Share.sharedAction`.
|
|
*
|
|
* ### Content
|
|
*
|
|
* - `message` - a message to share
|
|
* - `title` - title of the message
|
|
*
|
|
* #### iOS
|
|
*
|
|
* - `url` - an URL to share
|
|
*
|
|
* At least one of URL and message is required.
|
|
*
|
|
* ### Options
|
|
*
|
|
* #### iOS
|
|
*
|
|
* - `excludedActivityTypes`
|
|
* - `tintColor`
|
|
*
|
|
* #### Android
|
|
*
|
|
* - `dialogTitle`
|
|
*
|
|
*/
|
|
share(content: ShareContent, options?: ShareOptions): Promise<ShareAction>;
|
|
sharedAction: 'sharedAction';
|
|
dismissedAction: 'dismissedAction';
|
|
}
|
|
|
|
export const Share: ShareStatic;
|
|
export type Share = ShareStatic;
|