PluginProbe
Code Snippets / trunk
Code Snippets vtrunk
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / js / components / InsightsMenu / InsightsChartViewToggle.tsx

InsightsChartViewToggle.tsx in Code Snippets trunk, at js/components/InsightsMenu/InsightsChartViewToggle.tsx

67 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { __, sprintf } from '@wordpress/i18n'
2 import classnames from 'classnames'
3 import React from 'react'
4 import type { InsightsChartView } from '../../types/Insights'
5
6 interface ViewToggleButtonProps<View extends InsightsChartView> extends InsightsChartViewToggleProps<View> {
7 icon: string
8 label: string
9 currentView: View
10 }
11
12 const ViewToggleButton = <View extends InsightsChartView,>(
13 { icon, title, label, view, setView, currentView }: ViewToggleButtonProps<View>
14 ) =>
15 <button
16 type="button"
17 className={classnames('insights-chart-view-toggle-option', { 'active-view': currentView === view })}
18 aria-pressed={currentView === view}
19 title={title}
20 onClick={() => setView(view)}
21 >
22 <span className={`dashicons dashicons-${icon}`} aria-hidden="true" />
23 <span className="screen-reader-text">{label}</span>
24 </button>
25
26 export interface InsightsChartViewToggleProps<View extends InsightsChartView> {
27 title: string
28 view: View
29 setView: (view: View) => void
30 views: readonly View[]
31 }
32
33 const VIEW_OPTIONS: Readonly<Record<InsightsChartView, { icon: string, label: string, title: string }>> = {
34 pie: {
35 icon: 'chart-pie',
36 label: __('Chart view', 'code-snippets'),
37 title: __('Switch to chart view', 'code-snippets')
38 },
39 bar: {
40 icon: 'chart-bar',
41 label: __('List view', 'code-snippets'),
42 title: __('Switch to list view', 'code-snippets')
43 },
44 cloud: {
45 icon: 'cloud',
46 label: __('Tags cloud view', 'code-snippets'),
47 title: __('Switch to tags cloud view', 'code-snippets')
48 }
49 }
50
51 export const InsightsChartViewToggle = <View extends InsightsChartView,>(
52 { title, view, setView, views }: InsightsChartViewToggleProps<View>
53 ) =>
54 <div
55 className="insights-chart-view-toggle"
56 role="group"
57 aria-label={sprintf(__('%s chart view', 'code-snippets'), title)}
58 >
59 {views.map(option => <ViewToggleButton
60 key={option}
61 view={option}
62 {...VIEW_OPTIONS[option]}
63 setView={setView}
64 currentView={view}
65 />)}
66 </div>
67