Skip to content

Instantly share code, notes, and snippets.

@asgvard
Created March 17, 2022 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asgvard/6462ab1177c75037242450502605073b to your computer and use it in GitHub Desktop.
Save asgvard/6462ab1177c75037242450502605073b to your computer and use it in GitHub Desktop.
import React, { useEffect } from 'react';
import ReactDOM from 'react-dom';
import { useFocusable, init, FocusContext } from '@noriginmedia/norigin-spatial-navigation';
init();
const MENU_FOCUS_KEY = 'MENU';
const menuItems = [1, 2, 3, 4, 5];
const rowItems = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
function MenuItem() {
const { ref, focused } = useFocusable();
return (
<div ref={ref} className={focused ? 'menu-item-focused' : 'menu-item'} />
);
}
function Menu() {
const { ref, focusKey } = useFocusable({ focusKey: MENU_FOCUS_KEY });
return (
<FocusContext.Provider value={focusKey}>
<div ref={ref} className="menu">
{menuItems.map((menuItem) => (
<MenuItem key={menuItem} />
))}
</div>
</FocusContext.Provider>
);
}
function GalleryItem() {
const { ref, focused } = useFocusable();
return (
<div
ref={ref}
className={focused ? 'gallery-item-focused' : 'gallery-item'}
/>
);
}
function GalleryRow() {
const { ref, focusKey } = useFocusable();
return (
<FocusContext.Provider value={focusKey}>
<div ref={ref} className="gallery-row">
{rowItems.map((galleryItem) => (
<GalleryItem key={galleryItem} />
))}
</div>
</FocusContext.Provider>
);
}
function App() {
const { ref, setFocus, focusKey } = useFocusable();
useEffect(() => {
setFocus(MENU_FOCUS_KEY);
}, [setFocus]);
return (
<FocusContext.Provider value={focusKey}>
<div ref={ref} className="app">
<Menu />
<GalleryRow />
</div>
</FocusContext.Provider>
);
}
ReactDOM.render(<App />, document.querySelector('#root'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment