Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add onLongSelect #156

Merged
merged 9 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ The `SpatialNavigationNode` component receives the following props:
| `onFocus` | `function` | `undefined` | Callback function to be called when the node gains focus. |
| `onBlur` | `function` | `undefined` | Callback function to be called when the node loses focus. |
| `onSelect` | `function` | `undefined` | Callback function to be called when the node is selected. |
| `onLongSelect` | `function` | `onSelect` | Callback function to be called when the node is selected with long key press. |
| `onActive` | `function` | `undefined` | Callback function to be called when the node is made active by either itself or one of its descendants gaining focus. |
| `onInactive` | `function` | `undefined` | Callback function to be called when the node was active and due to an updated focus, is no longer active. |
| `orientation` | `'vertical' \| 'horizontal` | `'vertical'` | Determines the orientation of the node. |
Expand Down
1 change: 1 addition & 0 deletions packages/example/App.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './src/components/configureRemoteControl';
import { ThemeProvider } from '@emotion/react';
import { NavigationContainer } from '@react-navigation/native';
import { useWindowDimensions } from 'react-native';
Expand Down
1 change: 1 addition & 0 deletions packages/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"react": "18.2.0",
"react-native": "npm:[email protected]",
"react-native-keyevent": "^0.3.2",
"react-native-reanimated": "~3.10.1",
"react-native-safe-area-context": "4.10.1",
"react-native-screens": "3.31.1",
"react-native-svg": "15.2.0",
Expand Down
1 change: 1 addition & 0 deletions packages/example/src/components/configureRemoteControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ SpatialNavigation.configureRemoteControl({
[SupportedKeys.Up]: Directions.UP,
[SupportedKeys.Down]: Directions.DOWN,
[SupportedKeys.Enter]: Directions.ENTER,
[SupportedKeys.LongEnter]: Directions.LONG_ENTER,
[SupportedKeys.Back]: null,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@ import KeyEvent from 'react-native-keyevent';
import { RemoteControlManagerInterface } from './RemoteControlManager.interface';
import CustomEventEmitter from './CustomEventEmitter';

const LONG_PRESS_DURATION = 500;

class RemoteControlManager implements RemoteControlManagerInterface {
constructor() {
KeyEvent.onKeyDownListener(this.handleKeyDown);
KeyEvent.onKeyUpListener(this.handleKeyUp);
}

private eventEmitter = new CustomEventEmitter<{ keyDown: SupportedKeys }>();

private isEnterKeyDownPressed = false;
private longEnterTimeout: NodeJS.Timeout | null = null;

private handleLongEnter = () => {
this.longEnterTimeout = setTimeout(() => {
this.eventEmitter.emit('keyDown', SupportedKeys.LongEnter);
this.longEnterTimeout = null;
}, LONG_PRESS_DURATION);
};

private handleKeyDown = (keyEvent: { keyCode: number }) => {
const mappedKey = {
21: SupportedKeys.Left,
Expand All @@ -26,9 +39,36 @@ class RemoteControlManager implements RemoteControlManagerInterface {
return;
}

if (mappedKey === SupportedKeys.Enter) {
if (!this.isEnterKeyDownPressed) {
this.isEnterKeyDownPressed = true;
this.handleLongEnter();
}
return;
}

this.eventEmitter.emit('keyDown', mappedKey);
};

private handleKeyUp = (keyEvent: { keyCode: number }) => {
const mappedKey = {
66: SupportedKeys.Enter,
23: SupportedKeys.Enter,
}[keyEvent.keyCode];

if (!mappedKey) {
return;
}

if (mappedKey === SupportedKeys.Enter) {
this.isEnterKeyDownPressed = false;
if (this.longEnterTimeout) {
clearTimeout(this.longEnterTimeout);
this.eventEmitter.emit('keyDown', mappedKey);
}
}
};

addKeydownListener = (listener: (event: SupportedKeys) => boolean) => {
this.eventEmitter.on('keyDown', listener);
return listener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ class RemoteControlManager implements RemoteControlManagerInterface {
up: SupportedKeys.Up,
down: SupportedKeys.Down,
select: SupportedKeys.Enter,
longSelect: SupportedKeys.LongEnter,
}[evt.eventType];

if (!mappedKey) {
return;
}

// We only want to handle keydown fon long select
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"because it delays input and we don't want to add lag on all inputs"

(is that correct?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is because we don't want to handle twice the long select (when keydown and when keyup)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will refine the comment

if (mappedKey === SupportedKeys.LongEnter && evt.eventKeyAction === 1) {
return;
}

this.eventEmitter.emit('keyDown', mappedKey);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@ import { SupportedKeys } from './SupportedKeys';
import { RemoteControlManagerInterface } from './RemoteControlManager.interface';
import CustomEventEmitter from './CustomEventEmitter';

const LONG_PRESS_DURATION = 500;

class RemoteControlManager implements RemoteControlManagerInterface {
constructor() {
window.addEventListener('keydown', this.handleKeyDown);
window.addEventListener('keyup', this.handleKeyUp);
}

private eventEmitter = new CustomEventEmitter<{ keyDown: SupportedKeys }>();

private isEnterKeyDown = false;
private longEnterTimeout: NodeJS.Timeout | null = null;

private handleLongEnter = () => {
this.longEnterTimeout = setTimeout(() => {
this.eventEmitter.emit('keyDown', SupportedKeys.LongEnter);
this.longEnterTimeout = null;
}, LONG_PRESS_DURATION);
};

private handleKeyDown = (event: KeyboardEvent) => {
const mappedKey = {
ArrowRight: SupportedKeys.Right,
Expand All @@ -23,9 +36,35 @@ class RemoteControlManager implements RemoteControlManagerInterface {
return;
}

if (mappedKey === SupportedKeys.Enter) {
if (!this.isEnterKeyDown) {
this.isEnterKeyDown = true;
this.handleLongEnter();
}
return;
}

this.eventEmitter.emit('keyDown', mappedKey);
};

private handleKeyUp = (event: KeyboardEvent) => {
const mappedKey = {
Enter: SupportedKeys.Enter,
}[event.code];

if (!mappedKey) {
return;
}

if (mappedKey === SupportedKeys.Enter) {
this.isEnterKeyDown = false;
if (this.longEnterTimeout) {
clearTimeout(this.longEnterTimeout);
this.eventEmitter.emit('keyDown', mappedKey);
}
}
};

addKeydownListener = (listener: (event: SupportedKeys) => boolean) => {
this.eventEmitter.on('keyDown', listener);
return listener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ export enum SupportedKeys {
Left = 'Left',
Right = 'Right',
Enter = 'Enter',
LongEnter = 'LongEnter',
Back = 'Back',
}
29 changes: 23 additions & 6 deletions packages/example/src/modules/program/view/ProgramNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import { forwardRef } from 'react';
import { SpatialNavigationNodeRef } from '../../../../../lib/src/spatial-navigation/types/SpatialNavigationNodeRef';

import Animated, { useAnimatedStyle, useSharedValue, withTiming } from 'react-native-reanimated';

type Props = {
programInfo: ProgramInfo;
onSelect?: () => void;
Expand All @@ -15,20 +17,35 @@

export const ProgramNode = forwardRef<SpatialNavigationNodeRef, Props>(
({ programInfo, onSelect, indexRange, label, variant }: Props, ref) => {
const rotationZ = useSharedValue(0);

const rotate360 = () => {
rotationZ.value = withTiming(rotationZ.value + 360);
};

const animatedStyle = useAnimatedStyle(() => {

Check failure on line 26 in packages/example/src/modules/program/view/ProgramNode.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

ProgramList › renders the list with every items

[Reanimated] `useAnimatedStyle` was used without a dependency array or Babel plugin. Please explicitly pass a dependency array, or enable the Babel plugin. For more, see the docs: `https://docs.swmansion.com/react-native-reanimated/docs/guides/web-support#web-without-the-babel-plugin`. at useAnimatedStyle (../../node_modules/react-native-reanimated/lib/module/reanimated2/hook/useAnimatedStyle.ts:440:13) at src/modules/program/view/ProgramNode.tsx:26:43 at renderWithHooks (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:5608:18) at updateForwardRef (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:9169:20) at beginWork (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11400:16) at performUnitOfWork (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:15850:12) at workLoopSync (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:15784:5) at renderRootSync (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:15756:7) at performSyncWorkOnRoot (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:15461:20) at flushSyncCallbacks (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:2597:22) at flushActQueue (../../node_modules/react/cjs/react.development.js:2667:24) at act (../../node_modules/react/cjs/react.development.js:2521:11) at actImplementation (../../node_modules/@testing-library/react-native/src/act.ts:30:25) at renderWithAct (../../node_modules/@testing-library/react-native/src/render-act.ts:12:11) at renderInternal (../../node_modules/@testing-library/react-native/src/render.tsx:59:33) at renderInternal (../../node_modules/@testing-library/react-native/src/render.tsx:29:10) at renderWithProviders (src/modules/program/view/ProgramList.test.tsx:14:16) at Object.renderWithProviders (src/modules/program/view/ProgramList.test.tsx:27:20)

Check failure on line 26 in packages/example/src/modules/program/view/ProgramNode.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

ProgramList › renders the list and focus elements accordingly with inputs

[Reanimated] `useAnimatedStyle` was used without a dependency array or Babel plugin. Please explicitly pass a dependency array, or enable the Babel plugin. For more, see the docs: `https://docs.swmansion.com/react-native-reanimated/docs/guides/web-support#web-without-the-babel-plugin`. at useAnimatedStyle (../../node_modules/react-native-reanimated/lib/module/reanimated2/hook/useAnimatedStyle.ts:440:13) at src/modules/program/view/ProgramNode.tsx:26:43 at renderWithHooks (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:5608:18) at updateForwardRef (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:9169:20) at beginWork (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11400:16) at performUnitOfWork (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:15850:12) at workLoopSync (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:15784:5) at renderRootSync (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:15756:7) at performSyncWorkOnRoot (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:15461:20) at flushSyncCallbacks (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:2597:22) at flushActQueue (../../node_modules/react/cjs/react.development.js:2667:24) at act (../../node_modules/react/cjs/react.development.js:2521:11) at actImplementation (../../node_modules/@testing-library/react-native/src/act.ts:30:25) at renderWithAct (../../node_modules/@testing-library/react-native/src/render-act.ts:12:11) at renderInternal (../../node_modules/@testing-library/react-native/src/render.tsx:59:33) at renderInternal (../../node_modules/@testing-library/react-native/src/render.tsx:29:10) at renderWithProviders (src/modules/program/view/ProgramList.test.tsx:14:16) at Object.renderWithProviders (src/modules/program/view/ProgramList.test.tsx:34:20)

Check failure on line 26 in packages/example/src/modules/program/view/ProgramNode.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

ListWithVariableSize › node is still focusable after being removed

[Reanimated] `useAnimatedStyle` was used without a dependency array or Babel plugin. Please explicitly pass a dependency array, or enable the Babel plugin. For more, see the docs: `https://docs.swmansion.com/react-native-reanimated/docs/guides/web-support#web-without-the-babel-plugin`. at useAnimatedStyle (../../node_modules/react-native-reanimated/lib/module/reanimated2/hook/useAnimatedStyle.ts:440:13) at src/modules/program/view/ProgramNode.tsx:26:43 at renderWithHooks (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:5608:18) at updateForwardRef (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:9169:20) at beginWork (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11400:16) at performUnitOfWork (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:15850:12) at workLoopSync (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:15784:5) at renderRootSync (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:15756:7) at performSyncWorkOnRoot (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:15461:20) at flushSyncCallbacks (../../node_modules/react-test-renderer/cjs/react-test-renderer.development.js:2597:22) at flushActQueue (../../node_modules/react/cjs/react.development.js:2667:24) at act (../../node_modules/react/cjs/react.development.js:2521:11) at actImplementation (../../node_modules/@testing-library/react-native/src/act.ts:30:25) at renderWithAct (../../node_modules/@testing-library/react-native/src/render-act.ts:12:11) at renderInternal (../../node_modules/@testing-library/react-native/src/render.tsx:59:33) at renderInternal (../../node_modules/@testing-library/react-native/src/render.tsx:29:10) at renderWithProviders (src/pages/ListWithVariableSize.test.tsx:16:16) at Object.renderWithProviders (src/pages/ListWithVariableSize.test.tsx:24:20)
return {
transform: [{ rotateZ: `${rotationZ.value}deg` }],
};
});

return (
<SpatialNavigationFocusableView
onSelect={onSelect}
onLongSelect={rotate360}
indexRange={indexRange}
viewProps={{ accessibilityLabel: programInfo.title }}
ref={ref}
>
{({ isFocused, isRootActive }) => (
<Program
isFocused={isFocused && isRootActive}
programInfo={programInfo}
label={label}
variant={variant}
/>
<Animated.View style={animatedStyle}>
<Program
isFocused={isFocused && isRootActive}
programInfo={programInfo}
label={label}
variant={variant}
/>
</Animated.View>
)}
</SpatialNavigationFocusableView>
);
Expand Down
1 change: 0 additions & 1 deletion packages/example/src/pages/GridWithLongNodesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
SpatialNavigationVirtualizedListRef,
} from 'react-tv-space-navigation';
import { Page } from '../components/Page';
import '../components/configureRemoteControl';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wtf 😂 thanks for fixing it!

import { programInfos } from '../modules/program/infra/programInfos';
import styled from '@emotion/native';
import { scaledPixels } from '../design-system/helpers/scaledPixels';
Expand Down
1 change: 0 additions & 1 deletion packages/example/src/pages/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import styled from '@emotion/native';
import { DefaultFocus, SpatialNavigationScrollView } from 'react-tv-space-navigation';
import { Page } from '../components/Page';
import '../components/configureRemoteControl';
import { Box } from '../design-system/components/Box';
import { Spacer } from '../design-system/components/Spacer';
import { Typography } from '../design-system/components/Typography';
Expand Down
1 change: 0 additions & 1 deletion packages/example/src/pages/ProgramGridPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { StyleSheet, View } from 'react-native';
import { DefaultFocus } from 'react-tv-space-navigation';
import { Page } from '../components/Page';
import { VirtualizedSpatialGrid } from '../components/VirtualizedSpatialGrid';
import '../components/configureRemoteControl';
import { scaledPixels } from '../design-system/helpers/scaledPixels';

export const ProgramGridPage = () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"build": "webpack --config webpack.config.js"
},
"dependencies": {
"@bam.tech/lrud": "^8.0.1",
"@bam.tech/lrud": "^8.0.2",
"lodash.uniqueid": "^4.0.1"
}
}
6 changes: 6 additions & 0 deletions packages/lib/src/spatial-navigation/components/Node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type DefaultProps = {
onFocus?: () => void;
onBlur?: () => void;
onSelect?: () => void;
onLongSelect?: () => void;
onActive?: () => void;
onInactive?: () => void;
orientation?: NodeOrientation;
Expand Down Expand Up @@ -101,6 +102,7 @@ export const SpatialNavigationNode = forwardRef<SpatialNavigationNodeRef, Props>
onFocus,
onBlur,
onSelect,
onLongSelect = onSelect,
onActive,
onInactive,
orientation = 'vertical',
Expand Down Expand Up @@ -144,6 +146,9 @@ export const SpatialNavigationNode = forwardRef<SpatialNavigationNodeRef, Props>
const currentOnSelect = useRef<() => void>();
currentOnSelect.current = onSelect;

const currentOnLongSelect = useRef<() => void>();
currentOnLongSelect.current = onLongSelect;

const currentOnFocus = useRef<() => void>();
currentOnFocus.current = () => {
onFocus?.();
Expand Down Expand Up @@ -174,6 +179,7 @@ export const SpatialNavigationNode = forwardRef<SpatialNavigationNodeRef, Props>
setIsFocused(true);
},
onSelect: () => currentOnSelect.current?.(),
onLongSelect: () => currentOnLongSelect.current?.(),
orientation,
isIndexAlign: alignInGrid,
indexRange,
Expand Down
Loading
Loading