Skip to content

Instantly share code, notes, and snippets.

View asgvard's full-sized avatar

Dmitriy Bryokhin asgvard

View GitHub Profile
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 Button() {
const { ref, focused } = useFocusable();
return (<div ref={ref} className={focused ? 'button-focused' : 'button'}>
Press me
</div>);
}
function GalleryRow({items, focused}) {
const [focusedItemIndex, setFocusedItemIndex] = useState(0);
const onSetFocus = useCallback(({newFocusedIndex}) => {
if (!focused) {
return;
}
setFocusedItemIndex(Math.clamp(newFocusedIndex, 0, items.length - 1));
}, [focused]);
function GalleryRow({items, focused}) {
const [focusedItemIndex, setFocusedItemIndex] = useState(0);
const onKeyPress = useCallback(({keyCode}) => {
if (!focused) {
return;
}
let newIndex;
@asgvard
asgvard / spatial-nav-full-example.js
Last active May 10, 2019 14:56
spatial-nav-full-example
// this example uses react-native-web syntax
import React, {PureComponent} from 'react';
import {View, ScrollView} from 'react-native';
import {initNavigation, withFocusable} from '@noriginmedia/react-spatial-navigation';
initNavigation();
const menuItems = [1, 2, 3, 4, 5];
const rowItems = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
@asgvard
asgvard / spatial-nav-context-example.js
Created April 13, 2019 22:31
spatial-nav-context-example
const SpatialNavigableApp = withSpatialNavigation()(App);
@asgvard
asgvard / focusable-hoc-example.js
Created April 13, 2019 21:58
focusable-hoc-example
const FocusableComponent = withFocusable()(Component);
@asgvard
asgvard / focusable-wrapper-example.js
Created April 13, 2019 21:53
Focusable wrapper example
<Focusable>
<Component />
</Focusable>
const HomeScreen extends PureComponent {
constructor(props) {
this.state = {
focusedKey: null
};
this.onSetFocus = this.onSetFocus.bind(this);
}
componentDidUpdate(oldProps) {
@asgvard
asgvard / spatial-nav-distributed-logic.js
Last active April 13, 2019 19:00
Spatial Navigation article. Distributed logic example
const HomeScreen extends PureComponent {
constructor(props) {
this.state = {
focusedIndex: 0
};
this.onKeyPress = this.onKeyPress.bind(this);
}
componentDidMount() {