-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
picker.native.js
102 lines (90 loc) · 2.27 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
/**
* WordPress dependencies
*/
import { useState, useEffect } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
/**
* External dependencies
*/
import { logUserEvent, userEvents } from 'react-native-gutenberg-bridge';
import { Animated } from 'react-native';
/**
* Internal dependencies
*/
import Button from './button';
import Container from './container';
import getDefaultTemplates from './default-templates';
import Preview from './preview';
const PICKER_ANIMATION_DELAY = 500;
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 contentOpacity = new Animated.Value( 1 );
useEffect( () => {
if ( ! visible ) {
onHidePicker();
} else {
setPickerVisible( true );
}
}, [ visible ] );
const onApply = () => {
editPost( {
title: title || templatePreview.name,
blocks: templatePreview.blocks,
} );
logUserEvent( userEvents.editorSessionTemplateApply, {
template: templatePreview.key,
} );
setTemplatePreview( undefined );
};
const onHidePicker = () => {
Animated.timing( contentOpacity, {
toValue: 0,
duration: 300,
delay: PICKER_ANIMATION_DELAY,
} ).start( () => {
setPickerVisible( false );
} );
};
if ( ! pickerVisible ) {
return null;
}
return (
<Animated.View style={ [ { opacity: contentOpacity } ] }>
<Container>
{ templates.map( ( template ) => (
<Button
key={ template.key }
icon={ template.icon }
label={ template.name }
onPress={ () => {
logUserEvent(
userEvents.editorSessionTemplatePreview,
{
template: template.key,
}
);
setTemplatePreview( template );
} }
/>
) ) }
</Container>
<Preview
template={ templatePreview }
onDismiss={ () => setTemplatePreview( undefined ) }
onApply={ onApply }
/>
</Animated.View>
);
};
export default __experimentalPageTemplatePicker;