NoriginMedia / Norigin-Spatial-Navigation

React Hooks based Spatial Navigation (Key & Remote Control Navigation) / Web Browsers, Smart TVs and Connected TVs
MIT License
330 stars 97 forks source link

Allow for `onArrowPress` to have next focused node as property of KeyPressDetails #117

Open cyan-2048 opened 10 months ago

cyan-2048 commented 10 months ago

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

It's a problem because that would mean I have to figure out myself which element would be focused next (this isn't a problem with https://github.com/luke-chang/js-spatial-navigation, where there's plenty of spatial navigation events present)

Describe the solution you'd like A clear and concise description of what you want to happen.

Just want a way to know what DOM Node or even React Component is about to take the focus

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

in the useFocusable hook, add a custom property that can be passed any object as an argument for onArrowPress


function ComponentA() {
    const divRef = useRef();
    const { ref } = useFocusable({
        willFocus: { divRef, dontFocus: true }
    })

    return <div ref={(e) => { divRef.current = ref.current = e; }}>this element is about to be focused</div>
}

function ComponentB() {
    const { ref } = useFocusable({
        onArrowPress(direction, props, details) {
            // I can do validation and other stuff, like check if the element needs to be scrolled
            return !details.willFocus.dontFocus
        }
    })

    return <div ref={ref}>this element is about to lose focus</div>
} 

Additional context Add any other context or screenshots about the feature request here.

events fired by luke-chang/js-spatial-navigation that I really miss

sn:willunfocus

Fired when an element is about to lose the focus.

nextElement and nextSectionId indicate where the focus will be moved next.

direction is similar to sn:willmove but will be omitted here if this move is not caused by direction-related actions (e.g. by @ syntax or focus() directly).

native indicates whether this event is triggered by native focus-related events or not.

Note: If it is caused by native blur event, SpatialNavigation will try to focus back to the original element when you cancel it (but not guaranteed).

sn:willfocus

Fired when an element is about to get the focus.

sectionId indicates the currently focused section.

previousElement indicates the last focused element before this move.

direction and native are the same as sn:willunfocus.

Note: If it is caused by native focus event, SpatialNavigation will try to blur it immediately when you cancel it (but not guaranteed).

predikament commented 10 months ago

Thanks for the idea @cyan-2048, we'll have a look as soon as we can!

predikament commented 1 month ago

Sorry for not getting back to you on this, we've had our hands full.

I will do my best to make sure the team looks into this one way or another, as it's a valid suggestion.