Skip to content

Commit

Permalink
refactor: cleanups suggested by Adam
Browse files Browse the repository at this point in the history
  • Loading branch information
bradenmacdonald committed Dec 16, 2024
1 parent 23ee64b commit 7b95f95
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/hooks/tests/useIndexOfLastVisibleChild.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ window.ResizeObserver = window.ResizeObserver
}));

function TestComponent() {
const [containerElementRef, setContainerElementRef] = React.useState<HTMLDivElement | null>(null);
const overflowElementRef = React.useRef(null);
const indexOfLastVisibleChild = useIndexOfLastVisibleChild(containerElementRef, overflowElementRef.current);
const containerElementRef = React.useRef<HTMLDivElement>(null);
const overflowElementRef = React.useRef<HTMLDivElement>(null);
const indexOfLastVisibleChild = useIndexOfLastVisibleChild(containerElementRef.current, overflowElementRef.current);

return (
<div ref={setContainerElementRef} style={{ display: 'flex' }}>
<div ref={containerElementRef} style={{ display: 'flex' }}>
<div style={{ width: '250px' }} className="element">Element 1</div>
<div style={{ width: '250px' }} className="element">Element 2</div>
<div style={{ width: '250px' }} className="element">Element 3</div>
Expand Down
38 changes: 24 additions & 14 deletions src/hooks/useArrowKeyNavigation.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { useRef, useEffect } from 'react';

/**
* A React hook to enable arrow key navigation on a component.
*/
interface HandleEnterArgs {
event: KeyboardEvent;
currentIndex: number;
activeElement: HTMLElement;
}

function handleEnter(
{ event, currentIndex, activeElement }: { event: KeyboardEvent, currentIndex: number, activeElement: HTMLElement },
) {
function handleEnter({ event, currentIndex, activeElement }: HandleEnterArgs) {
if (currentIndex === -1) { return; }
activeElement.click();
event.preventDefault();
}

function handleArrowKey(
{ event, currentIndex, availableElements }: {
event: KeyboardEvent,
currentIndex: number,
availableElements: NodeListOf<HTMLElement>,
},
) {
interface HandleArrowKeyArgs {
event: KeyboardEvent;
currentIndex: number;
availableElements: NodeListOf<HTMLElement>;
}

function handleArrowKey({ event, currentIndex, availableElements }: HandleArrowKeyArgs) {
// If the focus isn't in the container, focus on the first thing
if (currentIndex === -1) { availableElements[0].focus(); }

Expand All @@ -44,6 +44,13 @@ function handleArrowKey(
event.preventDefault();
}

interface HandleEventsArgs {
event: KeyboardEvent;
ignoredKeys?: string[];
parentNode: HTMLElement | undefined;
selectors?: string;
}

/**
* Implement arrow key navigation for the given parentNode
*/
Expand All @@ -52,7 +59,7 @@ function handleEvents({
ignoredKeys = [],
parentNode,
selectors = 'a,button,input',
}: { event: KeyboardEvent, ignoredKeys?: string[], parentNode: HTMLElement | undefined, selectors?: string }) {
}: HandleEventsArgs) {
if (!parentNode) { return; }

const { key } = event;
Expand Down Expand Up @@ -90,6 +97,9 @@ export interface ArrowKeyNavProps {
ignoredKeys?: string[];
}

/**
* A React hook to enable arrow key navigation on a component.
*/
export default function useArrowKeyNavigation(props: ArrowKeyNavProps = {}) {
const { selectors, ignoredKeys } = props;
const parentNode = useRef<HTMLElement>();
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useIndexOfLastVisibleChild.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { useLayoutEffect, useState } from 'react';
* @param overflowElementRef - overflow element
*/
const useIndexOfLastVisibleChild = (
containerElementRef: Element | null,
overflowElementRef: Element | null,
containerElementRef: HTMLElement | null,
overflowElementRef: HTMLElement | null,
): number => {
const [indexOfLastVisibleChild, setIndexOfLastVisibleChild] = useState(-1);

Expand Down

0 comments on commit 7b95f95

Please sign in to comment.