-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
picker.native.js
167 lines (145 loc) · 3.99 KB
/
picker.native.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
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
/**
* WordPress dependencies
*/
import { useState, useEffect, useRef } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import Tooltip from './tooltip';
/**
* External dependencies
*/
import {
logUserEvent,
userEvents,
requestStarterPageTemplatesTooltipShown,
setStarterPageTemplatesTooltipShown,
} from '@wordpress/react-native-bridge';
import { Animated, Dimensions, Keyboard } from 'react-native';
/**
* Internal dependencies
*/
import Button from './button';
import Container from './container';
import getDefaultTemplates from './default-templates';
import ModalPreview from './preview';
// Used to hide the picker if there's no enough space in the window
const PICKER_HEIGHT_OFFSET = 150;
const __experimentalPageTemplatePicker = ( {
templates = getDefaultTemplates(),
visible,
} ) => {
const { editPost } = useDispatch( 'core/editor' );
const { title } = useSelect( ( select ) => {
const { getEditedPostAttribute } = select( 'core/editor' );
return {
title: getEditedPostAttribute( 'title' ),
};
} );
const [ templatePreview, setTemplatePreview ] = useState();
const [ pickerVisible, setPickerVisible ] = useState( visible );
const [ tooltipVisible, setTooltipVisible ] = useState( false );
const contentOpacity = useRef( new Animated.Value( 0 ) ).current;
useEffect( () => {
if ( shouldShowPicker() && visible && ! pickerVisible ) {
setPickerVisible( true );
}
startPickerAnimation( visible );
shouldShowTooltip();
Keyboard.addListener( 'keyboardDidShow', onKeyboardDidShow );
Keyboard.addListener( 'keyboardDidHide', onKeyboardDidHide );
return () => {
Keyboard.removeListener( 'keyboardDidShow', onKeyboardDidShow );
Keyboard.removeListener( 'keyboardDidHide', onKeyboardDidHide );
};
}, [ visible ] );
useEffect( () => {
if ( tooltipVisible && templatePreview ) {
setTooltipVisible( false );
}
}, [ templatePreview ] );
const onKeyboardDidShow = () => {
if ( visible ) {
startPickerAnimation( shouldShowPicker() );
}
};
const onKeyboardDidHide = () => {
if ( visible ) {
setPickerVisible( true );
startPickerAnimation( true );
}
};
const shouldShowPicker = () => {
// On smaller devices on landscape we hide the picker
// so it doesn't overlap with the editor's content
const windowHeight = Dimensions.get( 'window' ).height;
return PICKER_HEIGHT_OFFSET < windowHeight / 3;
};
const shouldShowTooltip = () => {
requestStarterPageTemplatesTooltipShown( ( tooltipShown ) => {
if ( ! tooltipShown ) {
setTooltipVisible( true );
setStarterPageTemplatesTooltipShown( true );
}
} );
};
const onApply = () => {
editPost( {
title: title || templatePreview.name,
blocks: templatePreview.blocks,
} );
logUserEvent( userEvents.editorSessionTemplateApply, {
template: templatePreview.key,
} );
setTemplatePreview( undefined );
};
const onTooltipHidden = () => {
setTooltipVisible( false );
};
const startPickerAnimation = ( isVisible ) => {
Animated.timing( contentOpacity, {
toValue: isVisible ? 1 : 0,
duration: 300,
useNativeDriver: true,
} ).start( () => {
if ( ! isVisible ) {
setPickerVisible( isVisible );
}
} );
};
if ( ! pickerVisible ) {
return null;
}
return (
<Animated.View style={ [ { opacity: contentOpacity } ] }>
{ tooltipVisible && (
<Tooltip onTooltipHidden={ onTooltipHidden } />
) }
<Container>
{ templates.map( ( template ) => (
<Button
key={ template.key }
icon={ template.icon }
label={ template.name }
onPress={ () => {
logUserEvent(
userEvents.editorSessionTemplatePreview,
{
template: template.key,
}
);
setTemplatePreview( template );
} }
/>
) ) }
</Container>
<ModalPreview
template={ templatePreview }
onDismiss={ () => setTemplatePreview( undefined ) }
onApply={ onApply }
/>
</Animated.View>
);
};
export default __experimentalPageTemplatePicker;