Skip to content

Commit b2f5f43

Browse files
authored
fix: expose ref on ActionSheetProvider for statically invoking (#283)
This fixes an unknown breaking change from 4.0.0 where attempting to put a ref directly on `ActionSheetProvider` would error. For simplicity, the ref on that component will now return an object containing `showActionSheetWithOptions()`, but for backwards compatibility `getContext()` is also available.
1 parent fc9cefa commit b2f5f43

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/ActionSheetProvider.tsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ import * as React from 'react';
44
import NativeActionSheet from './ActionSheet';
55
import CustomActionSheet from './ActionSheet/CustomActionSheet';
66
import { Provider } from './context';
7-
import { ActionSheetOptions } from './types';
7+
import { ActionSheetOptions, ActionSheetProviderRef } from './types';
88

99
interface Props {
1010
children: React.ReactNode;
1111
useNativeDriver?: boolean;
1212
useCustomActionSheet?: boolean;
1313
}
1414

15-
export default function ActionSheetProvider({
16-
children,
17-
useNativeDriver,
18-
useCustomActionSheet = false,
19-
}: Props) {
15+
export default React.forwardRef<ActionSheetProviderRef, Props>(function ActionSheetProvider(
16+
{ children, useNativeDriver, useCustomActionSheet = false },
17+
ref
18+
) {
2019
const actionSheetRef = React.useRef<NativeActionSheet>(null);
2120

2221
const context = React.useMemo(
@@ -30,6 +29,16 @@ export default function ActionSheetProvider({
3029
[actionSheetRef]
3130
);
3231

32+
React.useImperativeHandle(
33+
ref,
34+
() => ({
35+
// backwards compatible with 13.x before context was being passed right on the ref
36+
getContext: () => context,
37+
showActionSheetWithOptions: context.showActionSheetWithOptions,
38+
}),
39+
[context]
40+
);
41+
3342
const ActionSheet = React.useMemo(
3443
() => (useCustomActionSheet ? CustomActionSheet : NativeActionSheet),
3544
[useCustomActionSheet]
@@ -42,4 +51,4 @@ export default function ActionSheetProvider({
4251
</ActionSheet>
4352
</Provider>
4453
);
45-
}
54+
});

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ export interface ActionSheetProps {
88
) => void;
99
}
1010

11+
export interface ActionSheetProviderRef extends ActionSheetProps {
12+
/**
13+
* @deprecated Simply call `showActionSheetWithOptions()` directly from the ref now
14+
*/
15+
getContext: () => ActionSheetProps;
16+
}
17+
1118
// for iOS
1219
export interface ActionSheetIOSOptions {
1320
options: string[];

0 commit comments

Comments
 (0)