Skip to content

Commit

Permalink
Provider: add onOpen/onClose callbacks (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
kesha-antonov authored Apr 5, 2023
1 parent e504596 commit 7c84060
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
10 changes: 10 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ const App = () => {
};
}, [state]);

const onOpen = useCallback(() => {
console.log('App onOpen')
}, []);

const onClose = useCallback(() => {
console.log('App onClose')
}, []);

return (
<>
<AppContext.Provider value={appContextVariables}>
Expand All @@ -64,6 +72,8 @@ const App = () => {
iconComponent={FeatherIcon}
theme={state.theme}
safeAreaInsets={safeAreaInsets}
onOpen={onOpen}
onClose={onClose}
>
<NavigationContainer>
<Stack.Navigator
Expand Down
23 changes: 22 additions & 1 deletion src/components/provider/Provider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { memo, useEffect, useMemo } from 'react';
import { PortalProvider } from '@gorhom/portal';
import Animated, { useSharedValue } from 'react-native-reanimated';
import Animated, { useSharedValue, useAnimatedReaction, runOnJS } from 'react-native-reanimated';
import { GestureHandlerRootView } from 'react-native-gesture-handler';

// Components
Expand All @@ -26,6 +26,8 @@ const ProviderComponent = ({
theme: selectedTheme,
iconComponent,
safeAreaInsets,
onOpen,
onClose,
}: HoldMenuProviderProps) => {
if (iconComponent)
AnimatedIcon = Animated.createAnimatedComponent(iconComponent);
Expand All @@ -51,6 +53,25 @@ const ProviderComponent = ({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedTheme]);

useAnimatedReaction(
() => state.value,
state => {
switch (state) {
case CONTEXT_MENU_STATE.ACTIVE: {
if (onOpen)
runOnJS(onOpen)();
break
}
case CONTEXT_MENU_STATE.END: {
if (onClose)
runOnJS(onClose)();
break
}
}
},
[state]
);

const internalContextVariables = useMemo(
() => ({
state,
Expand Down
3 changes: 3 additions & 0 deletions src/components/provider/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ export interface HoldMenuProviderProps {
bottom: number;
left: number;
};

onOpen?: function;
onClose?: function;
}
28 changes: 28 additions & 0 deletions website/docs/props.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,34 @@ const safeAreaInsets = useSafeAreaProvider();
<HoldMenuProvider safeAreaInsets={safeAreaInsets} />;
```

### `onOpen`

Fires callback when menu is opened

#### Example

```tsx
const onOpen = useCallback(() => {
console.log('App onOpen')
}, []);

<HoldMenuProvider onOpen={onOpen} />;
```

### `onClose`

Fires callback when menu is opened

#### Example

```tsx
const onClose = useCallback(() => {
console.log('App onClose')
}, []);

<HoldMenuProvider onClose={onClose} />;
```

## HoldItem

### `items`
Expand Down

0 comments on commit 7c84060

Please sign in to comment.