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

Site editor: style the selected template pattern #65917

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a1a89e7
Updated code to set active pattern and its styling
benazeer-ben Oct 7, 2024
17cfe90
Fixes for linting issues on updated files
benazeer-ben Oct 8, 2024
a31771b
Updated code based on the first round feedback points
benazeer-ben Oct 10, 2024
2a30d64
Modification to highlight active iitem on focus
benazeer-ben Oct 11, 2024
4b09b2a
Removed local/session storage
benazeer-ben Oct 14, 2024
9cee60c
Changes applied from drafted PR
benazeerhassan1909 Oct 18, 2024
bf02cf7
Changes applied from drafted PR
benazeerhassan1909 Oct 18, 2024
f727ff0
Updated with new style changes
benazeerhassan1909 Oct 22, 2024
71fc3e5
Linting Fix
benazeerhassan1909 Oct 22, 2024
88369cb
Updated code to set active pattern and its styling
benazeer-ben Oct 7, 2024
5ce1c69
Fixes for linting issues on updated files
benazeer-ben Oct 8, 2024
9d1b5f3
Updated code based on the first round feedback points
benazeer-ben Oct 10, 2024
976c3e1
Modification to highlight active iitem on focus
benazeer-ben Oct 11, 2024
e882062
Removed local/session storage
benazeer-ben Oct 14, 2024
f0aa553
Changes applied from drafted PR
benazeerhassan1909 Oct 18, 2024
214d982
Changes applied from drafted PR
benazeerhassan1909 Oct 18, 2024
9cea49b
Updated with new style changes
benazeerhassan1909 Oct 22, 2024
c8eeba5
Linting Fix
benazeerhassan1909 Oct 22, 2024
046d54a
Merge branch 'enhancement/selected-template-style' of https://github.…
benazeerhassan1909 Oct 24, 2024
3edee3f
Merge branch 'trunk' of github.com:benazeer-ben/gutenberg into enhanc…
benazeer-ben Oct 28, 2024
0cc1530
Merge branch 'trunk' into enhancement/selected-template-style
ciampo Nov 27, 2024
d93540f
Fix spacing
ciampo Nov 27, 2024
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
70 changes: 60 additions & 10 deletions packages/block-editor/src/components/block-patterns-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ function BlockPattern( {
showTitle = true,
showTooltip,
category,
isActive, // new prop to manage active state
setActivePattern, // function to set the active pattern
} ) {
const [ isDragging, setIsDragging ] = useState( false );
const { blocks, viewportWidth } = pattern;
Expand Down Expand Up @@ -77,7 +79,12 @@ function BlockPattern( {
>
{ ( { draggable, onDragStart, onDragEnd } ) => (
<div
className="block-editor-block-patterns-list__list-item"
className={ clsx(
'block-editor-block-patterns-list__list-item',
{ 'is-active': isActive } // Apply 'is-active' class if this pattern is active
benazeer-ben marked this conversation as resolved.
Show resolved Hide resolved
) }
role="button" // Add role to make it behave like a button
tabIndex={ 0 } // Make the element focusable by keyboard
benazeer-ben marked this conversation as resolved.
Show resolved Hide resolved
draggable={ draggable }
onDragStart={ ( event ) => {
setIsDragging( true );
Expand All @@ -92,6 +99,20 @@ function BlockPattern( {
onDragEnd( event );
}
} }
onClick={ () => {
setActivePattern( id ); // Set active pattern when clicked
onClick( pattern, blocks );
onHover?.( null );
} }
onKeyDown={ ( event ) => {
// Simulate button click with Enter or Space key press
if ( event.key === 'Enter' || event.key === ' ' ) {
event.preventDefault(); // Prevent default behavior for spacebar
setActivePattern( id );
onClick( pattern, blocks );
onHover?.( null );
}
} }
benazeer-ben marked this conversation as resolved.
Show resolved Hide resolved
>
<WithToolTip
showTooltip={
Expand Down Expand Up @@ -123,6 +144,7 @@ function BlockPattern( {
}
id={ id }
onClick={ () => {
setActivePattern( id ); // Set active pattern when clicked
onClick( pattern, blocks );
onHover?.( null );
} }
Expand Down Expand Up @@ -199,23 +221,49 @@ function BlockPatternsList(
},
ref
) {
const [ activeCompositeId, setActiveCompositeId ] = useState( undefined );
const [ activePattern, setActivePattern ] = useState( null ); // State to track active pattern

useEffect( () => {
// Reset the active composite item whenever the available patterns change,
// to make sure that Composite widget can receive focus correctly when its
// composite items change. The first composite item will receive focus.
const firstCompositeItemId = blockPatterns.find( ( pattern ) =>
shownPatterns.includes( pattern )
)?.name;
setActiveCompositeId( firstCompositeItemId );
}, [ shownPatterns, blockPatterns ] );
if ( typeof window !== 'undefined' && window.localStorage ) {
// eslint-disable-next-line no-undef
const storedPatternName = localStorage.getItem( 'savedPattern' );

if (
storedPatternName &&
shownPatterns.some(
( pattern ) => pattern.name === storedPatternName
)
) {
// If there's a saved pattern and it exists in shownPatterns, set it as active
setActivePattern( storedPatternName );
} else {
const firstCompositeItemId = blockPatterns.find( ( pattern ) =>
shownPatterns.includes( pattern )
)?.name;

if ( firstCompositeItemId ) {
setActivePattern( firstCompositeItemId );
}
}
}
}, [ shownPatterns, blockPatterns ] );
const handleClickPattern = ( pattern ) => {
// Check if we are in a browser environment and localStorage is available
if ( typeof window !== 'undefined' && window.localStorage ) {
// eslint-disable-next-line no-undef
localStorage.setItem( 'savedPattern', pattern.name ); // Save the selected pattern in localStorage
}
benazeer-ben marked this conversation as resolved.
Show resolved Hide resolved
setActivePattern( pattern.name ); // Set the clicked pattern as active
onClickPattern( pattern ); // Original onClick logic
};
return (
<Composite
orientation={ orientation }
activeId={ activeCompositeId }
setActiveId={ setActiveCompositeId }
activeId={ activePattern }
setActiveId={ setActivePattern }
role="listbox"
className="block-editor-block-patterns-list"
aria-label={ label }
Expand All @@ -228,12 +276,14 @@ function BlockPatternsList(
key={ pattern.name }
id={ pattern.name }
pattern={ pattern }
onClick={ onClickPattern }
onClick={ handleClickPattern }
onHover={ onHover }
isDraggable={ isDraggable }
showTitle={ showTitle }
showTooltip={ showTitlesAsTooltip }
category={ category }
isActive={ activePattern === pattern.name } // Highlight the active pattern
setActivePattern={ setActivePattern } // Function to set the active pattern
/>
) : (
<BlockPatternPlaceholder key={ pattern.name } />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,10 @@
}
}
}
// Solution for "Add "selected" styling to active elements in the design selector."
// https: //github.com/WordPress/gutenberg/issues/65127
.block-editor-block-patterns-list__list-item.is-active {
border: 2px solid #007cba;
/* Custom styles for active item */
background-color: #eaf5ff;
}
benazeer-ben marked this conversation as resolved.
Show resolved Hide resolved
Loading