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

Fix: block manager scrollable UI a11y fix #68800

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
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
59 changes: 59 additions & 0 deletions packages/block-editor/src/components/block-manager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,65 @@ export default function BlockManager( {
};
}, [] );

// Function to determine which sticky element is active in the viewport
const getActiveStickyElement = ( Elements, parentElement ) => {
for ( const Element of Elements ) {
const rect = Element.getBoundingClientRect();

// Check if the sticky element is in the viewport
if ( rect.top < parentElement.clientHeight && rect.bottom > 0 ) {
return Element; // Return the active sticky element
}
}
return null; // No sticky element is active in the viewport
};

useEffect( () => {
const container = document.querySelector(
'.components-modal__content'
);
const stickyElements = document.querySelectorAll(
'.block-editor-block-manager__category-title'
);
let activeStickyElement = null;

if ( ! container || ! stickyElements ) {
return;
}

const handleFocusIn = ( event ) => {
activeStickyElement = getActiveStickyElement(
stickyElements,
container
);
const focusedElement = event.target;

// Check if the focused element is within the container
if ( container.contains( focusedElement ) ) {
const stickyBottom =
activeStickyElement.getBoundingClientRect().bottom;
const focusedRect = focusedElement.getBoundingClientRect();

// Calculate the desired scroll position
if ( focusedRect.top < stickyBottom ) {
const offset =
container.scrollTop - activeStickyElement.offsetHeight;
container.scrollTo( {
top: offset,
behavior: 'smooth',
} );
}
}
};

container.addEventListener( 'focusin', handleFocusIn );

// Cleanup the event listener
return () => {
container.removeEventListener( 'focusin', handleFocusIn );
};
}, [] );

function enableAllBlockTypes() {
onChange( blockTypes );
}
Expand Down
Loading