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 on arrow release #145

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,10 @@ Receives `direction` (`left`, `right`, `up`, `down`), `extraProps` (see below) a
This callback HAS to return `true` if you want to proceed with the default directional navigation behavior, or `false`
if you want to block the navigation in the specified direction.

##### `onArrowRelease` (function)
Callback that is called when the component is focused and Arrow key is released.
Receives `direction` (`left`, `right`, `up`, `down`), `extraProps` (see below) as argument.

##### `onFocus` (function)
Callback that is called when component gets focus.
Receives `FocusableComponentLayout`, `extraProps` and `FocusDetails` as arguments.
Expand Down
81 changes: 81 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ function Menu({ focusKey: focusKeyParam }: MenuProps) {
onEnterPress: () => {},
onEnterRelease: () => {},
onArrowPress: () => true,
onArrowRelease: () => {},
onFocus: () => {},
onBlur: () => {},
extraProps: { foo: 'bar' }
Expand Down Expand Up @@ -383,6 +384,85 @@ const ScrollingRows = styled.div`
flex-grow: 1;
`;

interface ProgressBarWrapperProps {
focused: boolean;
}

interface ProgressBarProgressProps {
percent: number;
focused: boolean;
}

const ProgressBarWrapper = styled.div<ProgressBarWrapperProps>`
position: absolute;
bottom: 95px;
right: 100px;
width: 540px;
height: 24px;
background-color: gray;
border-radius: 21px;
border-color: white;
border-style: solid;
border-width: ${({ focused }) => (focused ? '6px' : 0)};
box-sizing: border-box;
`;

const ProgressBarProgress = styled.div<ProgressBarProgressProps>`
width: ${({ percent }) => `${percent}%`};
height: 100%;
background-color: ${({ focused }) =>
focused ? 'deepskyblue' : 'dodgerblue'};
border-radius: 21px;
`;

const defaultPercent = 10;
const seekPercent = 10;
const delayedTime = 100;
const DIRECTION_RIGHT = 'right';

function ProgressBar() {
const [percent, setPercent] = useState(defaultPercent);
const timerRef = useRef<NodeJS.Timer | null>(null);
const { ref, focused } = useFocusable({
onArrowPress: (direction: string) => {
if (direction === DIRECTION_RIGHT && timerRef.current === null) {
timerRef.current = setInterval(() => {
setPercent((prevPercent) =>
prevPercent >= 100 ? prevPercent : prevPercent + seekPercent
);
}, delayedTime);
return true;
}
return true;
},
onArrowRelease: (direction: string) => {
if (direction === DIRECTION_RIGHT) {
clearInterval(timerRef.current);
timerRef.current = null;
}
}
});
useEffect(() => {
if (!focused) {
setPercent(defaultPercent);
}
}, [focused]);
useEffect(
() => () => {
if (timerRef.current !== null) {
clearInterval(timerRef.current);
timerRef.current = null;
}
},
[]
);
return (
<ProgressBarWrapper ref={ref} focused={focused}>
<ProgressBarProgress percent={percent} focused={focused} />
</ProgressBarWrapper>
);
}

function Content() {
const { ref, focusKey } = useFocusable();

Expand Down Expand Up @@ -415,6 +495,7 @@ function Content() {
? selectedAsset.title
: 'Press "Enter" to select an asset'}
</SelectedItemTitle>
<ProgressBar />
</SelectedItemWrapper>
<ScrollingRows ref={ref}>
<div>
Expand Down
34 changes: 34 additions & 0 deletions src/SpatialNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ interface FocusableComponent {
onEnterPress: (details?: KeyPressDetails) => void;
onEnterRelease: () => void;
onArrowPress: (direction: string, details: KeyPressDetails) => boolean;
onArrowRelease: (direction: string) => void;
onFocus: (layout: FocusableComponentLayout, details: FocusDetails) => void;
onBlur: (layout: FocusableComponentLayout, details: FocusDetails) => void;
onUpdateFocus: (focused: boolean) => void;
Expand All @@ -106,6 +107,7 @@ interface FocusableComponentUpdatePayload {
onEnterPress: (details?: KeyPressDetails) => void;
onEnterRelease: () => void;
onArrowPress: (direction: string, details: KeyPressDetails) => boolean;
onArrowRelease: (direction: string) => void;
onFocus: (layout: FocusableComponentLayout, details: FocusDetails) => void;
onBlur: (layout: FocusableComponentLayout, details: FocusDetails) => void;
}
Expand Down Expand Up @@ -830,6 +832,14 @@ class SpatialNavigationService {
if (eventType === KEY_ENTER && this.focusKey) {
this.onEnterRelease();
}

if (this.focusKey && (
eventType === DIRECTION_LEFT ||
eventType === DIRECTION_RIGHT ||
eventType === DIRECTION_UP ||
eventType === DIRECTION_DOWN)) {
this.onArrowRelease(eventType)
}
};

window.addEventListener('keyup', this.keyUpEventListener);
Expand Down Expand Up @@ -921,6 +931,28 @@ class SpatialNavigationService {
);
}

onArrowRelease(direction: string) {
const component = this.focusableComponents[this.focusKey];

/* Guard against last-focused component being unmounted at time of onArrowRelease (e.g due to UI fading out) */
if (!component) {
this.log('onArrowRelease', 'noComponent');

return;
}

/* Suppress onArrowRelease if the last-focused item happens to lose its 'focused' status. */
if (!component.focusable) {
this.log('onArrowRelease', 'componentNotFocusable');

return;
}

if (component.onArrowRelease) {
component.onArrowRelease(direction);
}
}

/**
* Move focus by direction, if you can't use buttons or focusing by key.
*
Expand Down Expand Up @@ -1275,6 +1307,7 @@ class SpatialNavigationService {
onEnterPress,
onEnterRelease,
onArrowPress,
onArrowRelease,
onFocus,
onBlur,
saveLastFocusedChild,
Expand All @@ -1295,6 +1328,7 @@ class SpatialNavigationService {
onEnterPress,
onEnterRelease,
onArrowPress,
onArrowRelease,
onFocus,
onBlur,
onUpdateFocus,
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/SpatialNavigation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ describe('SpatialNavigation', () => {
onEnterRelease: () => {},
onFocus: () => {},
onBlur: () => {},
onArrowPress: () => true
onArrowPress: () => true,
onArrowRelease: () => {},
});

SpatialNavigation.navigateByDirection('right', {});
Expand Down
6 changes: 6 additions & 0 deletions src/__tests__/domNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const createRootNode = () => {
onFocus: () => {},
onBlur: () => {},
onArrowPress: () => true,
onArrowRelease: () => {},
onUpdateFocus: () => {},
onUpdateHasFocusedChild: () => {}
});
Expand Down Expand Up @@ -79,6 +80,7 @@ export const createHorizontalLayout = () => {
onFocus: () => {},
onBlur: () => {},
onArrowPress: () => true,
onArrowRelease: () => {},
onUpdateFocus: () => {},
onUpdateHasFocusedChild: () => {}
});
Expand Down Expand Up @@ -118,6 +120,7 @@ export const createHorizontalLayout = () => {
onFocus: () => {},
onBlur: () => {},
onArrowPress: () => true,
onArrowRelease: () => {},
onUpdateFocus: () => {},
onUpdateHasFocusedChild: () => {}
});
Expand Down Expand Up @@ -157,6 +160,7 @@ export const createHorizontalLayout = () => {
onFocus: () => {},
onBlur: () => {},
onArrowPress: () => true,
onArrowRelease: () => {},
onUpdateFocus: () => {},
onUpdateHasFocusedChild: () => {}
});
Expand Down Expand Up @@ -200,6 +204,7 @@ export const createVerticalLayout = () => {
onFocus: () => {},
onBlur: () => {},
onArrowPress: () => true,
onArrowRelease: () => {},
onUpdateFocus: () => {},
onUpdateHasFocusedChild: () => {}
});
Expand Down Expand Up @@ -239,6 +244,7 @@ export const createVerticalLayout = () => {
onFocus: () => {},
onBlur: () => {},
onArrowPress: () => true,
onArrowRelease: () => {},
onUpdateFocus: () => {},
onUpdateHasFocusedChild: () => {}
});
Expand Down
14 changes: 14 additions & 0 deletions src/useFocusable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export type ArrowPressHandler<P = object> = (
details: KeyPressDetails
) => boolean;

export type ArrowReleaseHandler<P = object> = (
direction: string,
props: P,
) => void;

export type FocusHandler<P = object> = (
layout: FocusableComponentLayout,
props: P,
Expand All @@ -55,6 +60,7 @@ export interface UseFocusableConfig<P = object> {
onEnterPress?: EnterPressHandler<P>;
onEnterRelease?: EnterReleaseHandler<P>;
onArrowPress?: ArrowPressHandler<P>;
onArrowRelease?: ArrowReleaseHandler<P>;
onFocus?: FocusHandler<P>;
onBlur?: BlurHandler<P>;
extraProps?: P;
Expand All @@ -81,6 +87,7 @@ const useFocusableHook = <P>({
onEnterPress = noop,
onEnterRelease = noop,
onArrowPress = () => true,
onArrowRelease = noop,
onFocus = noop,
onBlur = noop,
extraProps
Expand All @@ -102,6 +109,10 @@ const useFocusableHook = <P>({
[extraProps, onArrowPress]
);

const onArrowReleaseHandler = useCallback((direction: string) => {
onArrowRelease(direction, extraProps);
}, [onArrowRelease, extraProps])

const onFocusHandler = useCallback(
(layout: FocusableComponentLayout, details: FocusDetails) => {
onFocus(layout, extraProps, details);
Expand Down Expand Up @@ -149,6 +160,7 @@ const useFocusableHook = <P>({
onEnterPress: onEnterPressHandler,
onEnterRelease: onEnterReleaseHandler,
onArrowPress: onArrowPressHandler,
onArrowRelease: onArrowReleaseHandler,
onFocus: onFocusHandler,
onBlur: onBlurHandler,
onUpdateFocus: (isFocused = false) => setFocused(isFocused),
Expand Down Expand Up @@ -182,6 +194,7 @@ const useFocusableHook = <P>({
onEnterPress: onEnterPressHandler,
onEnterRelease: onEnterReleaseHandler,
onArrowPress: onArrowPressHandler,
onArrowRelease: onArrowReleaseHandler,
onFocus: onFocusHandler,
onBlur: onBlurHandler
});
Expand All @@ -194,6 +207,7 @@ const useFocusableHook = <P>({
onEnterPressHandler,
onEnterReleaseHandler,
onArrowPressHandler,
onArrowReleaseHandler,
onFocusHandler,
onBlurHandler
]);
Expand Down