Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add dynamic RTL switching function + readme #120

Merged
Merged
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ Manually recalculate all the layouts. Rarely used.
This hook is the main link between the React component (its DOM element) and the navigation service.
It is used to register the component in the service, get its `focusKey`, `focused` state etc.

### `updateRtl` (function)
Manually update the focus behavior (whether RTL or LTR)

```jsx
const {/* hook output */ } = useFocusable({/* hook params */ });
```
Expand Down
12 changes: 11 additions & 1 deletion src/SpatialNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ class SpatialNavigationService {
this.setKeyMap = this.setKeyMap.bind(this);
this.getCurrentFocusKey = this.getCurrentFocusKey.bind(this);
this.doesFocusableExist = this.doesFocusableExist.bind(this);
this.updateRtl = this.updateRtl.bind(this);

this.setFocusDebounced = debounce(this.setFocus, AUTO_RESTORE_FOCUS_DELAY, {
leading: false,
Expand Down Expand Up @@ -1647,6 +1648,14 @@ class SpatialNavigationService {
doesFocusableExist(focusKey: string) {
return !!this.focusableComponents[focusKey];
}

/**
* This function updates the writing direction
* @param rtl whether the writing direction is right-to-left
*/
updateRtl(rtl: boolean) {
this.writingDirection = rtl ? WritingDirection.RTL : WritingDirection.LTR;
}
}

/**
Expand All @@ -1666,5 +1675,6 @@ export const {
resume,
updateAllLayouts,
getCurrentFocusKey,
doesFocusableExist
doesFocusableExist,
updateRtl
} = SpatialNavigation;