-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
index.js
108 lines (93 loc) · 2.39 KB
/
index.js
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
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useState, Children } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import Modal from '../modal';
import KeyboardShortcuts from '../keyboard-shortcuts';
import Button from '../button';
import PageControl from './page-control';
import { BackButtonIcon, ForwardButtonIcon } from './icons';
import FinishButton from './finish-button';
export default function Guide( { children, className, contentLabel, finishButtonText, onFinish } ) {
const [ currentPage, setCurrentPage ] = useState( 0 );
const numberOfPages = Children.count( children );
const canGoBack = currentPage > 0;
const canGoForward = currentPage < numberOfPages - 1;
const goBack = () => {
if ( canGoBack ) {
setCurrentPage( currentPage - 1 );
}
};
const goForward = () => {
if ( canGoForward ) {
setCurrentPage( currentPage + 1 );
}
};
if ( numberOfPages === 0 ) {
return null;
}
return (
<Modal
className={ classnames( 'components-guide', className ) }
contentLabel={ contentLabel }
onRequestClose={ onFinish }
>
<KeyboardShortcuts key={ currentPage } shortcuts={ {
left: goBack,
right: goForward,
} } />
<div className="components-guide__container">
{ children[ currentPage ] }
{ ! canGoForward && (
<FinishButton
className="components-guide__inline-finish-button"
onClick={ onFinish }
>
{ finishButtonText || __( 'Finish' ) }
</FinishButton>
) }
<div className="components-guide__footer">
{ canGoBack && (
<Button
className="components-guide__back-button"
icon={ <BackButtonIcon /> }
onClick={ goBack }
>
{ __( 'Previous' ) }
</Button>
) }
<PageControl
currentPage={ currentPage }
numberOfPages={ numberOfPages }
setCurrentPage={ setCurrentPage }
/>
{ canGoForward && (
<Button
className="components-guide__forward-button"
icon={ <ForwardButtonIcon /> }
onClick={ goForward }
>
{ __( 'Next' ) }
</Button>
) }
{ ! canGoForward && (
<FinishButton
className="components-guide__finish-button"
onClick={ onFinish }
>
{ finishButtonText || __( 'Finish' ) }
</FinishButton>
) }
</div>
</div>
</Modal>
);
}