Skip to content

Commit 5172f37

Browse files
authored
Merge pull request #480 from Codeinwp/fix/onboarding-hide-single-color-swatch
fix(onboarding): color picker — hide for single palette + sync screenshot to filter
2 parents 2869507 + b3b9663 commit 5172f37

1 file changed

Lines changed: 36 additions & 4 deletions

File tree

onboarding/src/Components/StarterSiteCard.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ const StarterSiteCard = ( {
4747
setSite,
4848
handleNextStep,
4949
trackingId,
50-
editor
50+
editor,
51+
selectedColors = []
5152
} ) => {
5253
const {
5354
upsell,
@@ -93,6 +94,32 @@ const StarterSiteCard = ( {
9394
screenshot: screenshotMap[ color ] || screenshot,
9495
} ) );
9596
}, [ data?.colors, data?.screenshots_by_color, screenshot ] );
97+
// First globally-selected color this card actually has, returned as the
98+
// canonical RAW colorOptions slug so option.slug === activeColor still hits.
99+
// selectedColors are normalized lowercase (Filters.js) while colorOptions
100+
// slugs / screenshots_by_color keys are raw, so match case-insensitively.
101+
const filterColor = useMemo( () => {
102+
if (
103+
! Array.isArray( selectedColors ) ||
104+
! selectedColors.length ||
105+
! colorOptions.length
106+
) {
107+
return '';
108+
}
109+
110+
const normalizedToSlug = new Map(
111+
colorOptions.map( ( option ) => [
112+
String( option.slug || '' ).trim().toLowerCase(),
113+
option.slug,
114+
] )
115+
);
116+
117+
const matched = selectedColors.find( ( color ) =>
118+
normalizedToSlug.has( color )
119+
);
120+
121+
return matched ? normalizedToSlug.get( matched ) : '';
122+
}, [ selectedColors, colorOptions ] );
96123
const previewColors = colorOptions.slice( 0, 2 );
97124
const [ activePage, setActivePage ] = useState( pageShots[0]?.key || 'home' );
98125
const [ activeColor, setActiveColor ] = useState( colorOptions[0]?.slug || '' );
@@ -102,9 +129,12 @@ const StarterSiteCard = ( {
102129
setActivePage( pageShots[0]?.key || 'home' );
103130
}, [ pageShots ] );
104131

132+
// Seed the displayed color from the global filter when this card matches a
133+
// selected color; otherwise fall back to the first palette. Re-runs only on
134+
// filter/colorOptions changes, so manual swatch clicks survive until then.
105135
useEffect( () => {
106-
setActiveColor( colorOptions[0]?.slug || '' );
107-
}, [ colorOptions ] );
136+
setActiveColor( filterColor || colorOptions[0]?.slug || '' );
137+
}, [ colorOptions, filterColor ] );
108138

109139
const activePageShot =
110140
pageShots.find( ( shot ) => shot.key === activePage ) || pageShots[0] || null;
@@ -194,7 +224,7 @@ const StarterSiteCard = ( {
194224
) ) }
195225
</div>
196226
) }
197-
{ colorOptions.length > 0 && (
227+
{ colorOptions.length > 1 && (
198228
<div
199229
className={
200230
isColorExpanded
@@ -290,12 +320,14 @@ export default compose(
290320
getCurrentEditor,
291321
getCurrentCategory,
292322
getSearchQuery,
323+
getSelectedColors,
293324
} = select( 'ti-onboarding' );
294325
return {
295326
trackingId: getTrackingId(),
296327
editor: getCurrentEditor(),
297328
category: getCurrentCategory(),
298329
query: getSearchQuery(),
330+
selectedColors: getSelectedColors() || [],
299331
};
300332
} ),
301333
withDispatch( ( dispatch, { data } ) => {

0 commit comments

Comments
 (0)