2023-10-02 08:28:13 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useEffect, useState } from '@wordpress/element';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2024-05-28 07:32:26 +00:00
|
|
|
import { PatternWithBlocks } from '~/customize-store/types/pattern';
|
2023-10-02 08:28:13 +00:00
|
|
|
|
|
|
|
export const useSelectedPattern = (
|
|
|
|
patternSelector = '.edit-site-sidebar-navigation-screen__content .block-editor-block-patterns-list__item'
|
|
|
|
) => {
|
|
|
|
const [ selectedPattern, setSelectedPattern ] =
|
|
|
|
useState< PatternWithBlocks >();
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
// This is a hack to add the "is-selected" class to the selected pattern
|
|
|
|
const patternElements = document.querySelectorAll( patternSelector );
|
|
|
|
|
|
|
|
patternElements.forEach( ( patternElement ) => {
|
|
|
|
if (
|
|
|
|
patternElement.getAttribute( 'aria-label' ) ===
|
|
|
|
selectedPattern?.title
|
|
|
|
) {
|
|
|
|
patternElement.classList.add( 'is-selected' );
|
|
|
|
} else {
|
|
|
|
patternElement.classList.remove( 'is-selected' );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}, [ selectedPattern, patternSelector ] );
|
|
|
|
|
|
|
|
return {
|
|
|
|
selectedPattern,
|
|
|
|
setSelectedPattern,
|
|
|
|
};
|
|
|
|
};
|