File size: 2,345 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
import { Icon, category as iconCategory, menu as iconMenu } from '@wordpress/icons';
import { useTranslate } from 'i18n-calypso';
import {
	PatternLibraryToggle,
	PatternLibraryToggleOption,
} from 'calypso/my-sites/patterns/components/toggle';
import { usePatternsContext } from 'calypso/my-sites/patterns/context';
import { QUERY_PARAM_SEARCH } from 'calypso/my-sites/patterns/lib/filter-patterns-by-term';
import { getCategoryUrlPath } from 'calypso/my-sites/patterns/paths';
import { PatternTypeFilter, PatternView } from 'calypso/my-sites/patterns/types';

import './style.scss';

function getViewToggleUrlPath(
	category: string,
	patternTypeFilter: PatternTypeFilter,
	isGridView: boolean,
	searchTerm: string
): string {
	const path = getCategoryUrlPath( category, patternTypeFilter, false );
	const params = new URLSearchParams();

	if ( isGridView ) {
		params.set( 'grid', '1' );
	}

	if ( searchTerm ) {
		params.set( QUERY_PARAM_SEARCH, searchTerm );
	}

	return params.size ? `${ path }?${ params }` : path;
}

type ViewToggleProps = {
	onChange?( value: PatternView ): void;
};

export function ViewToggle( { onChange }: ViewToggleProps ) {
	const translate = useTranslate();
	const { category, isGridView, patternTypeFilter, searchTerm } = usePatternsContext();
	const currentView = isGridView ? 'grid' : 'list';

	const listViewLabel = translate( 'List view', {
		comment: 'Toggle label for view switcher in the Pattern Library',
		textOnly: true,
	} );

	const gridViewLabel = translate( 'Grid view', {
		comment: 'Toggle label for view switcher in the Pattern Library',
		textOnly: true,
	} );

	return (
		<PatternLibraryToggle
			className="pattern-library__view-toggle"
			onChange={ onChange }
			selected={ currentView }
		>
			<PatternLibraryToggleOption
				aria-label={ listViewLabel }
				href={ getViewToggleUrlPath( category, patternTypeFilter, false, searchTerm ) }
				tooltipText={ listViewLabel }
				value="list"
			>
				<Icon icon={ iconMenu } size={ 20 } />
			</PatternLibraryToggleOption>

			<PatternLibraryToggleOption
				aria-label={ gridViewLabel }
				href={ getViewToggleUrlPath( category, patternTypeFilter, true, searchTerm ) }
				tooltipText={ gridViewLabel }
				value="grid"
			>
				<Icon icon={ iconCategory } size={ 20 } />
			</PatternLibraryToggleOption>
		</PatternLibraryToggle>
	);
}