-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.native.js
92 lines (79 loc) · 2.09 KB
/
index.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
/**
* External dependencies
*/
import { ScrollView } from 'react-native';
/**
* WordPress dependencies
*/
import { store as blocksStore } from '@wordpress/blocks';
import { useSelect, useDispatch } from '@wordpress/data';
import { useMemo } from '@wordpress/element';
import { _x } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { getActiveStyle, replaceActiveStyle } from './utils';
import StylePreview from './preview';
import containerStyles from './style.scss';
import { store as blockEditorStore } from '../../store';
function BlockStyles( { clientId, url } ) {
const selector = ( select ) => {
const { getBlock } = select( blockEditorStore );
const { getBlockStyles } = select( blocksStore );
const block = getBlock( clientId );
return {
styles: getBlockStyles( block.name ),
className: block.attributes.className || '',
};
};
const { styles, className } = useSelect( selector, [ clientId ] );
const { updateBlockAttributes } = useDispatch( blockEditorStore );
const renderedStyles = styles?.find( ( style ) => style.isDefault )
? styles
: [
{
name: 'default',
label: _x( 'Default', 'block style' ),
isDefault: true,
},
...styles,
];
const mappedRenderedStyles = useMemo( () => {
const activeStyle = getActiveStyle( renderedStyles, className );
return renderedStyles.map( ( style ) => {
const styleClassName = replaceActiveStyle(
className,
activeStyle,
style
);
const isActive = activeStyle === style;
const onStylePress = () => {
updateBlockAttributes( clientId, {
className: styleClassName,
} );
};
return (
<StylePreview
onPress={ onStylePress }
isActive={ isActive }
key={ style.name }
style={ style }
url={ url }
/>
);
} );
}, [ renderedStyles, className, clientId ] );
if ( ! styles || styles.length === 0 ) {
return null;
}
return (
<ScrollView
horizontal
showsHorizontalScrollIndicator={ false }
contentContainerStyle={ containerStyles.content }
>
{ mappedRenderedStyles }
</ScrollView>
);
}
export default BlockStyles;