Skip to content

Commit

Permalink
add basic keyboard support
Browse files Browse the repository at this point in the history
  • Loading branch information
gwwar committed Oct 4, 2021
1 parent 1de8ebf commit a0d49a1
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
8 changes: 5 additions & 3 deletions packages/block-editor/src/components/list-view/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default function ListViewBlock( {
path,
isExpanded,
style,
isFocused = false,
} ) {
const cellRef = useRef( null );
const [ isHovered, setIsHovered ] = useState( false );
Expand Down Expand Up @@ -83,9 +84,10 @@ export default function ListViewBlock( {
// try to steal the focus from the editor canvas.
useEffect( () => {
if (
withExperimentalPersistentListViewFeatures &&
! isTreeGridMounted &&
isSelected
( withExperimentalPersistentListViewFeatures &&
! isTreeGridMounted &&
isSelected ) ||
isFocused
) {
cellRef.current.focus();
}
Expand Down
7 changes: 6 additions & 1 deletion packages/block-editor/src/components/list-view/branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default function ListViewBranch( props ) {
expandedState
);
}
const { start, maxVisible } = windowMeasurement;
const { start, maxVisible, focus } = windowMeasurement;
const end = start + maxVisible;

// Only use windowing for the persistent list view
Expand All @@ -111,6 +111,10 @@ export default function ListViewBranch( props ) {
}
: {} ),
};
const isFocused =
__experimentalPersistentListViewFeatures &&
( ( start === nextPosition && focus === 'start' ) ||
( end === nextPosition && focus === 'end' ) );

const position = index + 1;
const isLastRowAtLevel = rowCount === position;
Expand Down Expand Up @@ -177,6 +181,7 @@ export default function ListViewBranch( props ) {
isExpanded={ isExpanded }
listPosition={ nextPosition }
style={ style }
isFocused={ isFocused }
/>
) }
{ hasNestedBranch && isExpanded && ! isDragged && (
Expand Down
34 changes: 34 additions & 0 deletions packages/block-editor/src/components/list-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ function measureWindow( scrollContainer, setWindowMeasurement ) {
setWindowMeasurement( {
maxVisible: maxVisible + WINDOW_OVERSCAN,
start,
end: start + maxVisible,
focus: null,
} );
}

Expand Down Expand Up @@ -119,6 +121,7 @@ function ListView(
const [ windowMeasurement, setWindowMeasurement ] = useState( {
maxVisible: 30,
start: 0,
end: 30,
} );

useLayoutEffect( () => {
Expand Down Expand Up @@ -172,6 +175,35 @@ function ListView(
collapse( row?.dataset?.block );
};

const moveWindowUp = () => {
setWindowMeasurement( ( lastMeasurement ) => {
const { start, maxVisible } = lastMeasurement;
const nextStart = Math.max( 0, start - 1 );
return {
start: nextStart,
maxVisible,
end: nextStart + maxVisible,
focus: 'start',
};
} );
};

const moveWindowDown = () => {
setWindowMeasurement( ( lastMeasurement ) => {
const { start, maxVisible } = lastMeasurement;
const nextStart = Math.min(
start + 1,
globalBlockCount - maxVisible
);
return {
start: nextStart,
maxVisible,
end: nextStart + maxVisible,
focus: 'end',
};
} );
};

const contextValue = useMemo(
() => ( {
__experimentalFeatures,
Expand Down Expand Up @@ -206,6 +238,8 @@ function ListView(
ref={ treeGridRef }
onCollapseRow={ collapseRow }
onExpandRow={ expandRow }
moveWindowUp={ moveWindowUp }
moveWindowDown={ moveWindowDown }
>
<ListViewContext.Provider value={ contextValue }>
<ListViewBranch
Expand Down
29 changes: 22 additions & 7 deletions packages/components/src/tree-grid/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,23 @@ function getRowFocusables( rowElement ) {
* Renders both a table and tbody element, used to create a tree hierarchy.
*
* @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/components/src/tree-grid/README.md
* @param {Object} props Component props.
* @param {WPElement} props.children Children to be rendered.
* @param {Function} props.onExpandRow Callback to fire when row is expanded.
* @param {Function} props.onCollapseRow Callback to fire when row is collapsed.
* @param {Object} ref A ref to the underlying DOM table element.
* @param {Object} props Component props.
* @param {WPElement} props.children Children to be rendered.
* @param {Function} props.onExpandRow Callback to fire when row is expanded.
* @param {Function} props.onCollapseRow Callback to fire when row is collapsed.
* @param {Function} props.moveWindowUp Callback to fire when pressing up at the top window row
* @param {Function} props.moveWindowDown Callback to fire when pressing down at the bottom window row
* @param {Object} ref A ref to the underlying DOM table element.
*/
function TreeGrid(
{ children, onExpandRow = () => {}, onCollapseRow = () => {}, ...props },
{
children,
onExpandRow = () => {},
onCollapseRow = () => {},
moveWindowUp = () => {},
moveWindowDown = () => {},
...props
},
ref
) {
const onKeyDown = useCallback( ( event ) => {
Expand Down Expand Up @@ -155,8 +164,14 @@ function TreeGrid(
nextRowIndex = Math.min( currentRowIndex + 1, rows.length - 1 );
}

// Focus is either at the top or bottom edge of the grid. Do nothing.
// Focus is either at the top or bottom edge of the grid. Notify callbacks if tree is using windowing,
// otherwise do nothing.
if ( nextRowIndex === currentRowIndex ) {
if ( keyCode === UP ) {
moveWindowUp();
} else {
moveWindowDown();
}
// Prevent key use for anything else. For example, Voiceover
// will start navigating horizontally when reaching the vertical
// bounds of a table.
Expand Down

0 comments on commit a0d49a1

Please sign in to comment.