Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 140 additions & 1 deletion docs/guides/accessibility-labels.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function Asset({ title }: { title: string }) {
});

return (
<div ref={ref} style={{ outline: focused ? '2px solid white' : 'none' }}>
<div ref={ref} style={{ outline: focused ? '2px solid #42fdac' : 'none' }}>
{title}
</div>
);
Expand All @@ -94,6 +94,145 @@ function ContentRow({ title }: { title: string }) {
}
```

**Try it live:** navigate the assets with the arrow keys. The panel shows exactly what `onUtterText` receives — region labels are announced the first time focus enters a region, then only the leaf label while you stay inside it.

<Sandpack>

```tsx
import React, { useEffect, useState } from 'react';
import { init, setFocus } from '@noriginmedia/norigin-spatial-navigation-core';
import {
useFocusable,
FocusContext
} from '@noriginmedia/norigin-spatial-navigation-react';

// The library calls onUtterText with the string to speak; forward it to React.

let onUtter: ((text: string) => void) | null = null;
init({
debug: false,
visualDebug: false,
onUtterText: (text) => onUtter?.(text)
});

function Asset({ title }: { title: string }) {
const { ref, focused } = useFocusable({ accessibilityLabel: title });
return (
<div
ref={ref}
style={{
width: 130,
height: 70,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
borderRadius: 6,
color: 'white',
textAlign: 'center',
padding: 4,
backgroundColor: focused ? '#7150DA' : '#333',
outline: focused ? '2px solid #42fdac' : 'none'
}}
>
{title}
</div>
);
}

function ContentRow({
title,
rowKey,
assets
}: {
title: string;
rowKey: string;
assets: string[];
}) {
const { ref, focusKey } = useFocusable({
focusKey: rowKey,
accessibilityLabel: title
});
return (
<FocusContext.Provider value={focusKey}>
<section ref={ref} style={{ marginBottom: 20 }}>
<h3 style={{ margin: '0 0 8px', color: '#8a7fb0' }}>{title}</h3>
<div style={{ display: 'flex', gap: 12 }}>
{assets.map((a) => (
<Asset key={a} title={a} />
))}
</div>
</section>
</FocusContext.Provider>
);
}

function Content() {
const { ref, focusKey } = useFocusable({
focusKey: 'CONTENT',
accessibilityLabel: 'Recommended'
});
return (
<FocusContext.Provider value={focusKey}>
<div ref={ref}>
<ContentRow
title="Movies"
rowKey="ROW_MOVIES"
assets={['Inception', 'Interstellar']}
/>
<ContentRow
title="Series"
rowKey="ROW_SERIES"
assets={['Breaking Bad', 'The Wire']}
/>
</div>
</FocusContext.Provider>
);
}

export default function App() {
const [log, setLog] = useState<string[]>([]);

useEffect(() => {
onUtter = (text) => setLog((prev) => [text, ...prev].slice(0, 6));
setFocus('CONTENT'); // initial focus, captured now that onUtter is wired up
return () => {
onUtter = null;
};
}, []);

return (
<div
style={{
padding: 24,
backgroundColor: '#221c35',
minHeight: '100vh',
color: 'white'
}}
>
<Content />
<p style={{ color: '#8a7fb0', margin: '8px 0' }}>
onUtterText output (what a TTS engine would speak):
</p>
<pre
style={{
background: '#1a1228',
color: '#c9a6ff',
padding: 16,
borderRadius: 6,
minHeight: 120,
margin: 0,
whiteSpace: 'pre-wrap'
}}
>
{log.length ? log.join('\n') : '(navigate to hear regions announced)'}
</pre>
</div>
);
}
```

</Sandpack>

### 3. Disable native `aria-*` if needed

If your app previously used `aria-label`, `role="region"`, or similar attributes on focusable elements, the browser (and any native screen reader running over it) may also try to announce them. When routing through `onUtterText`, strip or disable those native attributes on the same elements to avoid double-speaking.
Expand Down
100 changes: 99 additions & 1 deletion docs/guides/event-callbacks.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ function Item({ id, title, isNew }: ItemProps) {
});

return (
<div ref={ref} style={{ outline: focused ? '2px solid white' : 'none' }}>
<div ref={ref} style={{ outline: focused ? '2px solid #42fdac' : 'none' }}>
{title}
</div>
);
Expand All @@ -266,6 +266,104 @@ function Item({ id, title, isNew }: ItemProps) {

Without `extraProps`, you would need to wrap each callback in `useCallback` with the relevant dependencies to avoid stale values.

**Try it live:** use the arrow keys to move between items (fires `onFocus`) and press Enter to select one (fires `onEnterPress`). Each callback is logged on screen, with the latest `extraProps` values.

<Sandpack>

```tsx
import React, { useState, useEffect } from 'react';
import { init } from '@noriginmedia/norigin-spatial-navigation-core';
import {
useFocusable,
FocusContext
} from '@noriginmedia/norigin-spatial-navigation-react';

init({ debug: false, visualDebug: false });

interface ItemProps {
id: string;
title: string;
isNew: boolean;
log: (msg: string) => void;
}

function Item({ id, title, isNew, log }: ItemProps) {
const { ref, focused } = useFocusable<ItemProps>({
extraProps: { id, title, isNew, log },
onFocus: (layout, props) => props.log(`onFocus → ${props.id}`),
onEnterPress: (props) =>
props.log(`onEnterPress → ${props.title} (isNew: ${props.isNew})`)
});

return (
<div
ref={ref}
style={{
padding: '16px 20px',
minWidth: 110,
textAlign: 'center',
borderRadius: 6,
color: 'white',
backgroundColor: focused ? '#7150DA' : '#333',
outline: focused ? '2px solid #42fdac' : 'none'
}}
>
{title}
{isNew ? ' •' : ''}
</div>
);
}

const ITEMS = [
{ id: 'a', title: 'Home', isNew: false },
{ id: 'b', title: 'Movies', isNew: true },
{ id: 'c', title: 'Series', isNew: false }
];

export default function App() {
const [logs, setLogs] = useState<string[]>([]);
const log = (msg: string) => setLogs((prev) => [msg, ...prev].slice(0, 6));

const { ref, focusKey, focusSelf } = useFocusable({ focusKey: 'ROOT' });
useEffect(() => {
focusSelf();
}, [focusSelf]);

return (
<FocusContext.Provider value={focusKey}>
<div
ref={ref}
style={{ padding: 32, backgroundColor: '#221c35', minHeight: '100vh' }}
>
<div style={{ display: 'flex', gap: 16, marginBottom: 24 }}>
{ITEMS.map((it) => (
<Item key={it.id} {...it} log={log} />
))}
</div>
<p style={{ color: '#8a7fb0', margin: '0 0 8px' }}>
Arrow = onFocus, Enter = onEnterPress:
</p>
<pre
style={{
background: '#1a1228',
color: '#c9a6ff',
padding: 16,
borderRadius: 6,
minHeight: 120,
margin: 0,
whiteSpace: 'pre-wrap'
}}
>
{logs.length ? logs.join('\n') : '(events will appear here)'}
</pre>
</div>
</FocusContext.Provider>
);
}
```

</Sandpack>

---

## Cleanup in `onArrowRelease`
Expand Down
Loading
Loading