PluginProbe
Code Snippets / 4.0.0-beta.3
Code Snippets v4.0.0-beta.3
4.0.0-beta.3 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 All 66 releases
code-snippets / js / components / StatsMenu / StatsChartViewToggle.tsx

StatsChartViewToggle.tsx in Code Snippets 4.0.0-beta.3, at js/components/StatsMenu/StatsChartViewToggle.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 { StatsChartView } from '../../types/Stats'
5
6 interface ViewToggleButtonProps<View extends StatsChartView> extends StatsChartViewToggleProps<View> {
7 icon: string
8 label: string
9 currentView: View
10 }
11
12 const ViewToggleButton = <View extends StatsChartView,>(
13 { icon, title, label, view, setView, currentView }: ViewToggleButtonProps<View>
14 ) =>
15 <button
16 type="button"
17 className={classnames('stats-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 StatsChartViewToggleProps<View extends StatsChartView> {
27 title: string
28 view: View
29 setView: (view: View) => void
30 views: readonly View[]
31 }
32
33 const VIEW_OPTIONS: Readonly<Record<StatsChartView, { 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 StatsChartViewToggle = <View extends StatsChartView,>(
52 { title, view, setView, views }: StatsChartViewToggleProps<View>
53 ) =>
54 <div
55 className="stats-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