Skip to content

Commit

Permalink
fix: support custom onHoverIn & onPressIn on top of normal behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardLindhout committed Jul 8, 2023
1 parent 4504802 commit 7d0c344
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
15 changes: 10 additions & 5 deletions src/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,18 @@ export default function Link<T extends BaseScreen>({
]
);

const onPressIn = React.useCallback(() => {
preloadElementInner();
preload(to, params);
}, [to, preload, preloadElementInner, params]);
const onPressInExternal = rest?.onPressIn;
const onPressIn = React.useCallback(
(e: GestureResponderEvent) => {
preloadElementInner();
preload(to, params);
onPressInExternal?.(e);
},
[to, preload, preloadElementInner, params, onPressInExternal]
);

if (skipLinkBehaviourIfPressIsDefined && onCustomPress) {
return children({ onPress: onCustomPress });
return children({ onPress: onCustomPress, onPressIn: onPressInExternal });
}
return children({
onPress: onPress,
Expand Down
51 changes: 39 additions & 12 deletions src/Link.web.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as React from 'react';
import type { BaseScreen, LinkProps, LinkRenderProps } from './navigationUtils';

import type { GestureResponderEvent } from 'react-native';
import type { MouseEvent, GestureResponderEvent } from 'react-native';
import useNavigation from './useNavigation';
import { generatePath } from './navigationUtils';
import useModal from './useModal';
import RidgeNavigationContext from './contexts/RidgeNavigationContext';

function isModifiedEvent(event: React.MouseEvent) {
return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
Expand Down Expand Up @@ -32,10 +33,14 @@ export default function Link<T extends BaseScreen>({
refresh: isRefreshInsteadOfPush,
...rest
}: LinkProps<T>) {
const { basePath } = React.useContext(RidgeNavigationContext);
const { inModal } = useModal();
const { push, replace, refresh, preload, preloadElement, currentRootKey } =
useNavigation();
const href = generatePath('/' + currentRootKey + to.path, params);
let href = generatePath('/' + currentRootKey + to.path, params);
if (basePath) {
href = '/' + basePath + href;
}

const lastPreloadedAt = React.useRef<number | null>(null);

Expand Down Expand Up @@ -106,17 +111,39 @@ export default function Link<T extends BaseScreen>({
preloadElement(to);
}, [preloadElement, to]);

const onPressInExternal = rest?.onPressIn;
const loadDataOnHover = linkMode === 'sensitive';
const onPressIn = React.useCallback(
(e: GestureResponderEvent) => {
if (!loadDataOnHover) {
preloadData();
}
onPressInExternal?.(e);
},
[loadDataOnHover, onPressInExternal, preloadData]
);

const onHoverInExternal = rest?.onHoverIn;
const onHoverIn = React.useCallback(
(e: MouseEvent) => {
if (loadDataOnHover) {
preloadDataAndElement();
} else {
preloadElementInner();
}
onHoverInExternal?.(e);
},
[
loadDataOnHover,
onHoverInExternal,
preloadDataAndElement,
preloadElementInner,
]
);

let baseProps: LinkRenderProps = {
onPressIn: rest.onPressIn
? rest.onPressIn
: linkMode === 'sensitive'
? undefined
: preloadData,
onHoverIn: rest.onHoverIn
? rest.onHoverIn
: linkMode === 'sensitive'
? preloadDataAndElement
: preloadElementInner,
onPressIn: onPressIn,
onHoverIn: onHoverIn,
onPress: onPress,
};
let childrenProps: LinkRenderProps =
Expand Down
1 change: 1 addition & 0 deletions src/NavigationProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ export default function NavigationProvider<ScreenItems extends BaseScreen[]>({
SuspenseContainer,
fluent,
goToUrl,
basePath,
}}
>
<BottomTabBadgeProvider>
Expand Down
1 change: 1 addition & 0 deletions src/contexts/RidgeNavigationContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const RidgeNavigationContext = React.createContext<{
...fluentScreens: FluentScreen[]
) => void;
goToUrl: string | undefined;
basePath: string | undefined;
}>(undefined as any);

export default RidgeNavigationContext;

0 comments on commit 7d0c344

Please sign in to comment.