File size: 13,562 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
import { isEnabled } from '@automattic/calypso-config';
import page from '@automattic/calypso-router';
import { Button } from '@automattic/components';
import { useLocale, addLocaleToPathLocaleInFront } from '@automattic/i18n-utils';
import styled from '@emotion/styled';
import { Icon, category as iconCategory } from '@wordpress/icons';
import clsx from 'clsx';
import { Substitution, useTranslate } from 'i18n-calypso';
import { useState, useEffect, useRef } from 'react';
import { CategoryPillNavigation } from 'calypso/components/category-pill-navigation';
import { recordTracksEvent } from 'calypso/lib/analytics/tracks';
import { PatternsCopyPasteInfo } from 'calypso/my-sites/patterns/components/copy-paste-info';
import { PatternsGetStarted } from 'calypso/my-sites/patterns/components/get-started';
import { PatternsHeader } from 'calypso/my-sites/patterns/components/header';
import { PatternsPageViewTracker } from 'calypso/my-sites/patterns/components/page-view-tracker';
import { ReadymadeTemplates } from 'calypso/my-sites/patterns/components/readymade-templates';
import { PatternsSearchField } from 'calypso/my-sites/patterns/components/search-field';
import { TypeToggle } from 'calypso/my-sites/patterns/components/type-toggle';
import { ViewToggle } from 'calypso/my-sites/patterns/components/view-toggle';
import { usePatternsContext } from 'calypso/my-sites/patterns/context';
import { usePatternCategories } from 'calypso/my-sites/patterns/hooks/use-pattern-categories';
import { usePatterns } from 'calypso/my-sites/patterns/hooks/use-patterns';
import { useReadymadeTemplates } from 'calypso/my-sites/patterns/hooks/use-readymade-templates';
import { useRecordPatternsEvent } from 'calypso/my-sites/patterns/hooks/use-record-patterns-event';
import { filterPatternsByTerm } from 'calypso/my-sites/patterns/lib/filter-patterns-by-term';
import { filterPatternsByType } from 'calypso/my-sites/patterns/lib/filter-patterns-by-type';
import { getPatternPermalink } from 'calypso/my-sites/patterns/lib/get-pattern-permalink';
import { getTracksPatternType } from 'calypso/my-sites/patterns/lib/get-tracks-pattern-type';
import { getCategoryUrlPath, getOnboardingUrl } from 'calypso/my-sites/patterns/paths';
import {
PatternTypeFilter,
PatternView,
CategoryGalleryFC,
PatternGalleryFC,
} from 'calypso/my-sites/patterns/types';
import { useSelector } from 'calypso/state';
import { isUserLoggedIn } from 'calypso/state/current-user/selectors';
import getUserSetting from 'calypso/state/selectors/get-user-setting';
import './style.scss';
// We use this unstyled Emotion component simply to prevent errors related to the use of Emotion's
// `useCx` hook in `ToggleGroupControl`
const PatternLibraryBody = styled.div``;
export const patternFiltersClassName = 'pattern-library__filters';
// Scroll to anchoring position of category pill navigation element
function scrollToPatternView( stickyFiltersElement: HTMLDivElement, onlyIfBelowThreshold = false ) {
const coords = stickyFiltersElement.getBoundingClientRect();
const style = getComputedStyle( stickyFiltersElement );
const parsedTop = /(\d+(\.\d+)?)px/.exec( style.top );
const topStyle = parseFloat( parsedTop?.[ 1 ] ?? '0' );
if ( onlyIfBelowThreshold && coords.top > topStyle ) {
return;
}
stickyFiltersElement.style.position = 'static';
requestAnimationFrame( () => {
const staticCoords = stickyFiltersElement.getBoundingClientRect();
stickyFiltersElement.style.removeProperty( 'position' );
window.scrollBy( {
behavior: 'smooth',
top: staticCoords.top,
} );
} );
}
function scrollToSection( element: HTMLDivElement, scrollBehavior: ScrollBehavior = 'smooth' ) {
requestAnimationFrame( () => {
element.scrollIntoView( {
behavior: scrollBehavior,
block: 'center',
} );
} );
}
type PatternLibraryProps = {
categoryGallery: CategoryGalleryFC;
patternGallery: PatternGalleryFC;
};
export const PatternLibrary = ( {
categoryGallery: CategoryGallery,
patternGallery: PatternGallery,
}: PatternLibraryProps ) => {
const locale = useLocale();
const translate = useTranslate();
const navRef = useRef< HTMLDivElement >( null );
const readymadeTemplateSectionRef = useRef< HTMLDivElement >( null );
const { recordPatternsEvent } = useRecordPatternsEvent();
const { category, searchTerm, section, isGridView, patternTypeFilter, patternPermalinkId } =
usePatternsContext();
const { data: categories = [] } = usePatternCategories( locale );
const { data: rawPatterns = [], isFetching: isFetchingPatterns } = usePatterns(
locale,
category,
{ enabled: Boolean( category || searchTerm ) }
);
const { data: readymadeTemplates = [] } = useReadymadeTemplates();
const patterns = searchTerm
? filterPatternsByTerm( rawPatterns, searchTerm )
: filterPatternsByType( rawPatterns, patternTypeFilter );
let patternPermalinkName;
if ( patternPermalinkId && ! isFetchingPatterns ) {
patternPermalinkName = patterns.find( ( pattern ) => pattern.ID === patternPermalinkId )?.name;
}
const isLoggedIn = useSelector( isUserLoggedIn );
const isDevAccount = useSelector( ( state ) => getUserSetting( state, 'is_dev_account' ) );
const recordClickEvent = (
tracksEventName: string,
view?: PatternView,
typeFilter?: PatternTypeFilter
) => {
recordTracksEvent( tracksEventName, {
category,
search_term: searchTerm || undefined,
is_logged_in: isLoggedIn,
type: getTracksPatternType( typeFilter ),
user_is_dev_account: isDevAccount ? '1' : '0',
view,
} );
};
const currentView = isGridView ? 'grid' : 'list';
// If the user has scrolled below the anchoring position of the category pill navigation then we
// scroll back up when the category changes
useEffect( () => {
if ( navRef.current ) {
scrollToPatternView( navRef.current, true );
}
}, [ category ] );
// Scroll to anchoring position of category pill navigation when the search form is submitted
useEffect( () => {
if ( navRef.current && searchTerm ) {
scrollToPatternView( navRef.current );
}
}, [ searchTerm ] );
const [ isSticky, setIsSticky ] = useState( false );
const prevNavTopValue = useRef( 0 );
useEffect( () => {
const handleScroll = () => {
if ( ! navRef.current ) {
return;
}
const navbarPosition = navRef.current.getBoundingClientRect().top;
setIsSticky( navbarPosition === prevNavTopValue.current );
prevNavTopValue.current = navbarPosition;
};
window.addEventListener( 'scroll', handleScroll, { passive: true } );
return () => {
window.removeEventListener( 'scroll', handleScroll );
};
}, [] );
useEffect( () => {
if (
'readymade-templates-section' === section &&
readymadeTemplateSectionRef.current &&
! searchTerm
) {
scrollToSection( readymadeTemplateSectionRef.current, 'instant' );
}
}, [ searchTerm, section ] );
// `calypso-router` has trouble with the onboarding URL we use. This code prevents click
// events on onboarding links from propagating to the `calypso-router` event listener,
// which fixes the problem.
useEffect( () => {
const onboardingUrl = getOnboardingUrl( locale, isLoggedIn );
function stopPropagationOnClick( event: MouseEvent ) {
if (
event.target instanceof HTMLAnchorElement &&
event.target.getAttribute( 'href' ) === onboardingUrl
) {
event.stopPropagation();
}
}
document.addEventListener( 'click', stopPropagationOnClick, { capture: true } );
return () => {
document.removeEventListener( 'click', stopPropagationOnClick );
};
}, [ locale, isLoggedIn ] );
const categoryObject = categories?.find( ( { name } ) => name === category );
const shouldDisplayPatternTypeToggle =
category && ! searchTerm && !! categoryObject?.pagePatternCount;
const categoryNavList = categories.map( ( category ) => {
const patternTypeFilterFallback =
category.pagePatternCount === 0 ? PatternTypeFilter.REGULAR : patternTypeFilter;
return {
id: category.name,
label: category.label,
link: getCategoryUrlPath( category.name, patternTypeFilterFallback, false, isGridView ),
};
} );
const isHomePage = ! category && ! searchTerm;
const patternGalleryKey = searchTerm
? `${ searchTerm }-${ category }-${ patternTypeFilter }`
: `${ category }-${ patternTypeFilter }`;
let mainHeading: Substitution = '';
if ( searchTerm && isFetchingPatterns && ! patterns.length ) {
// Non-breaking space
mainHeading = '\u00A0';
} else if ( searchTerm ) {
mainHeading = translate( '%(count)d pattern', '%(count)d patterns', {
count: patterns.length,
args: { count: patterns.length },
} );
} else if ( patternTypeFilter === PatternTypeFilter.PAGES ) {
mainHeading = translate( 'Page Layouts', {
comment: 'Refers to block patterns that contain entire page layouts',
} );
} else if ( patternTypeFilter === PatternTypeFilter.REGULAR ) {
mainHeading = translate( 'Patterns', {
comment: 'Refers to block patterns',
} );
}
return (
<>
{ isHomePage ? (
<PatternsPageViewTracker
patternsCount={ ! isFetchingPatterns ? patterns.length : undefined }
/>
) : (
<PatternsPageViewTracker
patternPermalinkName={ patternPermalinkName }
view={ currentView }
patternsCount={ ! isFetchingPatterns ? patterns.length : undefined }
/>
) }
<PatternsHeader />
<div className="pattern-library__wrapper">
<div
className={ clsx( patternFiltersClassName, {
'pattern-library__filters--sticky': isSticky,
} ) }
ref={ navRef }
>
<div className="pattern-library__filters-inner">
<CategoryPillNavigation
selectedCategoryId={ category }
buttons={ [
{
icon: <Icon icon={ iconCategory } size={ 26 } />,
id: 'all',
label: translate( 'All Categories' ),
link: addLocaleToPathLocaleInFront( '/patterns' ),
isActive: ! category,
},
] }
categories={ categoryNavList }
onSelect={ ( selectedId ) =>
recordPatternsEvent( 'calypso_pattern_library_filter', { category: selectedId } )
}
/>
<div className="pattern-library__body-search">
<PatternsSearchField isCollapsible />
</div>
</div>
</div>
{ isHomePage && (
<CategoryGallery
title={ translate( 'Build anything with patterns', {
comment:
'Heading text for a section in the Pattern Library with links to block pattern categories',
textOnly: true,
} ) }
description={ translate(
'Choose from a library of beautiful, functional design patterns to build exactly the pages you need—or your client needs—in no time.'
) }
categories={ categories }
patternTypeFilter={ PatternTypeFilter.REGULAR }
/>
) }
{ ! isHomePage && (
<PatternLibraryBody className="pattern-library">
<div className="pattern-library__header">
<h1
className={ clsx( 'pattern-library__title', {
'pattern-library__title--search': searchTerm,
} ) }
>
{ mainHeading }
</h1>
{ shouldDisplayPatternTypeToggle && (
<TypeToggle
onChange={ ( type ) => {
recordClickEvent( 'calypso_pattern_library_type_switch', currentView, type );
} }
/>
) }
<ViewToggle
onChange={ ( view ) => {
recordClickEvent(
'calypso_pattern_library_view_switch',
view,
patternTypeFilter
);
} }
/>
</div>
<PatternGallery
category={ category }
displayPlaceholder={ isFetchingPatterns && ! patterns.length }
getPatternPermalink={ ( pattern ) =>
getPatternPermalink( pattern, category, categories )
}
isGridView={ isGridView }
key={ `pattern-gallery-${ patternGalleryKey }` }
patterns={ patterns }
/>
{ searchTerm && ! patterns.length && category && (
<div className="pattern-gallery__body-no-search-results">
<Button
className="pattern-gallery__search-all-categories"
onClick={ () => {
recordClickEvent( 'calypso_pattern_library_search_in_all' );
page( `/patterns${ window.location.search }` );
} }
>
{ translate( 'Search in all categories', {
comment: 'Button to make search of patterns in all categories',
} ) }
</Button>
</div>
) }
</PatternLibraryBody>
) }
{ isHomePage && (
<PatternsCopyPasteInfo
theme={ isEnabled( 'readymade-templates/showcase' ) ? 'gray' : 'dark' }
/>
) }
{ isHomePage && (
<>
<CategoryGallery
title={ translate( 'Beautiful, curated page layouts', {
comment:
'Heading text for a section in the Pattern Library with links to block pattern categories containing page layouts',
textOnly: true,
} ) }
description={ translate(
'Our page layouts are exactly what you need to easily create professional-looking pages using preassembled patterns.'
) }
categories={ categories?.filter( ( c ) => c.pagePatternCount ) }
patternTypeFilter={ PatternTypeFilter.PAGES }
/>
{ isEnabled( 'readymade-templates/showcase' ) && (
<ReadymadeTemplates
readymadeTemplates={ readymadeTemplates }
forwardRef={ readymadeTemplateSectionRef }
/>
) }
</>
) }
</div>
<PatternsGetStarted theme={ isEnabled( 'readymade-templates/showcase' ) ? 'blue' : 'dark' } />
</>
);
};
|