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 / RootResponder.js
Created March 2, 2018 12:07
Root responder for global interception of touch events in RN
/**
* Wrapper for creating Global (RootContainer) PanResponder
* Responder events are bubbled up by the View hierarchy, but sometimes it's needed to "move"
* touch handling to a sibling component. Instead we're creating Root Responder to capture the touch and
* delegate the callbacks to any component that wants to listen to it.
*
* Any component in the App can hook into the Root Responder lifecycle callbacks
* For capturing of the Root Responder the *Capture callbacks should return true
*
* Only one component at a time can be attached to Root Responder
@asgvard
asgvard / FlatSwiper.js
Created November 10, 2017 20:48
Horizontal Paging Swiper based on FlatList
/**
* TODO Finish this for optimized Android swiping
* Inspired by react-native-swiper but based on cross-platform and optimized FlatList
*/
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {
StyleSheet,
FlatList,
@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>