2023-09-14 08:24:46 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import React, { createContext, useState } from '@wordpress/element';
|
|
|
|
import type { ReactNode } from 'react';
|
|
|
|
|
2024-02-29 14:32:44 +00:00
|
|
|
export const HighlightedBlockContext = createContext< {
|
|
|
|
highlightedBlockClientId: string | null;
|
|
|
|
setHighlightedBlockClientId: ( clientId: string | null ) => void;
|
|
|
|
resetHighlightedBlockClientId: () => void;
|
|
|
|
} >( {
|
|
|
|
highlightedBlockClientId: null,
|
2023-09-14 08:24:46 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2024-02-29 14:32:44 +00:00
|
|
|
setHighlightedBlockClientId: ( clientId: string | null ) => {
|
2023-09-14 08:24:46 +00:00
|
|
|
// No op by default.
|
|
|
|
},
|
2024-02-29 14:32:44 +00:00
|
|
|
resetHighlightedBlockClientId: () => {
|
2023-09-14 08:24:46 +00:00
|
|
|
// No op by default.
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
// A Provider that keeps track of which block is "focussed" in the Assembler Hub.
|
|
|
|
// This is used to highlight the block in the BlockEditor currently.
|
|
|
|
export const HighlightedBlockContextProvider = ( {
|
|
|
|
children,
|
|
|
|
}: {
|
|
|
|
children: ReactNode;
|
|
|
|
} ) => {
|
|
|
|
// Create some state
|
2024-02-29 14:32:44 +00:00
|
|
|
const [ highlightedBlockClientId, setHighlightedBlockClientId ] = useState<
|
|
|
|
string | null
|
|
|
|
>( null );
|
2023-09-14 08:24:46 +00:00
|
|
|
|
2024-02-29 14:32:44 +00:00
|
|
|
const resetHighlightedBlockClientId = () => {
|
|
|
|
setHighlightedBlockClientId( null );
|
2023-09-14 08:24:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<HighlightedBlockContext.Provider
|
|
|
|
value={ {
|
2024-02-29 14:32:44 +00:00
|
|
|
highlightedBlockClientId,
|
|
|
|
setHighlightedBlockClientId,
|
|
|
|
resetHighlightedBlockClientId,
|
2023-09-14 08:24:46 +00:00
|
|
|
} }
|
|
|
|
>
|
|
|
|
{ children }
|
|
|
|
</HighlightedBlockContext.Provider>
|
|
|
|
);
|
|
|
|
};
|