/
index.js
152 lines (135 loc) · 4 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
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
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { useInstanceId } from '@wordpress/compose';
/**
* Internal dependencies
*/
import Button from '../button';
import RangeControl from '../range-control';
import CustomSelectControl from '../custom-select-control';
const DEFAULT_FONT_SIZE = 'default';
const CUSTOM_FONT_SIZE = 'custom';
function getSelectValueFromFontSize( fontSizes, value ) {
if ( value ) {
const fontSizeValue = fontSizes.find( ( font ) => font.size === Number( value ) );
return fontSizeValue ? fontSizeValue.slug : CUSTOM_FONT_SIZE;
}
return DEFAULT_FONT_SIZE;
}
function getSelectOptions( optionsArray, disableCustomFontSizes ) {
optionsArray = [
{ slug: DEFAULT_FONT_SIZE, name: __( 'Default' ) },
...optionsArray,
...disableCustomFontSizes ? [] : [ { slug: CUSTOM_FONT_SIZE, name: __( 'Custom' ) } ],
];
return optionsArray.map( ( option ) => (
{
key: option.slug,
name: option.name,
style: { fontSize: option.size },
}
) );
}
export default function FontSizePicker( {
fallbackFontSize,
fontSizes = [],
disableCustomFontSizes = false,
onChange,
value,
withSlider = false,
} ) {
const instanceId = useInstanceId( FontSizePicker );
const [ currentSelectValue, setCurrentSelectValue ] = useState( getSelectValueFromFontSize( fontSizes, value ) );
if ( disableCustomFontSizes && ! fontSizes.length ) {
return null;
}
const setFontSize = ( fontSizeKey, fontSizeValue ) => {
setCurrentSelectValue( fontSizeKey );
if ( fontSizeKey === DEFAULT_FONT_SIZE ) {
onChange( undefined );
return;
}
if ( ! fontSizeValue ) {
return;
}
onChange( Number( fontSizeValue ) );
};
const onChangeValue = ( event ) => {
const newValue = event.target.value;
const key = getSelectValueFromFontSize( fontSizes, newValue );
setFontSize( key, newValue );
};
const onSelectChangeValue = ( { selectedItem } ) => {
const selectedKey = selectedItem.key;
const selectedValue = selectedItem.style && selectedItem.style.fontSize;
setFontSize( selectedKey, selectedValue );
};
const onSliderChangeValue = ( sliderValue ) => {
const sliderKey = getSelectValueFromFontSize( fontSizes, sliderValue );
setFontSize( sliderKey, sliderValue );
};
const reset = () => {
setFontSize( DEFAULT_FONT_SIZE );
};
const options = getSelectOptions( fontSizes, disableCustomFontSizes );
const rangeControlNumberId = `components-range-control__number#${ instanceId }`;
return (
<fieldset className="components-font-size-picker">
<legend className="screen-reader-text">
{ __( 'Font Size' ) }
</legend>
<div className="components-font-size-picker__controls">
{ fontSizes.length > 0 && (
<CustomSelectControl
className={ 'components-font-size-picker__select' }
label={ __( 'Preset Size' ) }
options={ options }
value={
options.find( ( option ) => option.key === currentSelectValue ) ||
options[ 0 ]
}
onChange={ onSelectChangeValue }
/>
) }
{ ( ! withSlider && ! disableCustomFontSizes ) &&
<div className="components-range-control__number-container">
<label htmlFor={ rangeControlNumberId }>{ __( 'Custom' ) }</label>
<input
id={ rangeControlNumberId }
className="components-range-control__number"
type="number"
onChange={ onChangeValue }
aria-label={ __( 'Custom' ) }
value={ value || '' }
/>
</div>
}
<Button
className="components-color-palette__clear"
disabled={ value === undefined }
onClick={ reset }
isSmall
isSecondary
>
{ __( 'Reset' ) }
</Button>
</div>
{ withSlider &&
<RangeControl
className="components-font-size-picker__custom-input"
label={ __( 'Custom Size' ) }
value={ value || '' }
initialPosition={ fallbackFontSize }
onChange={ onSliderChangeValue }
min={ 12 }
max={ 100 }
beforeIcon="editor-textcolor"
afterIcon="editor-textcolor"
/>
}
</fieldset>
);
}