| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
const { __ } = wp.i18n; |
| 5 |
const { Component } = wp.element; |
| 6 |
const { |
| 7 |
SelectControl, |
| 8 |
RangeControl, |
| 9 |
Dropdown, |
| 10 |
ButtonGroup, |
| 11 |
Button, |
| 12 |
BaseControl, |
| 13 |
} = wp.components; |
| 14 |
const { DOWN } = wp.keycodes; |
| 15 |
|
| 16 |
/** |
| 17 |
* Internal dependencies |
| 18 |
*/ |
| 19 |
import FontPicker from "./FontPicker"; |
| 20 |
import UnitControl from "../unit-control"; |
| 21 |
import Icon from "./Icon"; |
| 22 |
import { WEIGHTS, TRANSFORMS, FONTS } from "./constants"; |
| 23 |
// import "./style.scss"; |
| 24 |
|
| 25 |
class TypographyControl extends Component { |
| 26 |
onFontChange = (value, onClose) => { |
| 27 |
const { fontFamily, fontWeight } = this.props.attributes; |
| 28 |
|
| 29 |
this.props.setAttributes({ fontFamily: value }); |
| 30 |
|
| 31 |
if ( |
| 32 |
typeof FONTS[value] !== "undefined" && |
| 33 |
typeof FONTS[value].weight !== "undefined" |
| 34 |
) { |
| 35 |
if ( |
| 36 |
fontWeight && |
| 37 |
Object.values(FONTS[fontFamily].weight).indexOf(fontWeight) < 0 |
| 38 |
) { |
| 39 |
this.props.setAttributes({ fontWeight: "" }); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
onClose(); |
| 44 |
}; |
| 45 |
|
| 46 |
render() { |
| 47 |
const { attributes, setAttributes } = this.props; |
| 48 |
|
| 49 |
const { |
| 50 |
fontFamily, |
| 51 |
fontSize, |
| 52 |
fontSizeUnit, |
| 53 |
fontWeight, |
| 54 |
textTransform, |
| 55 |
lineHeight, |
| 56 |
letterSpacing, |
| 57 |
} = attributes; |
| 58 |
|
| 59 |
const SIZE_STEP = fontSizeUnit === "em" ? 0.1 : 1; |
| 60 |
const SIZE_MAX = fontSizeUnit === "em" ? 10 : 100; |
| 61 |
|
| 62 |
return ( |
| 63 |
<Dropdown |
| 64 |
className="components-dropdown-menu components-eb-typography-dropdown" |
| 65 |
position="top right" |
| 66 |
renderToggle={({ isOpen, onToggle }) => { |
| 67 |
const onArrowDown = (event) => { |
| 68 |
if (!isOpen && event.keyCode === DOWN) { |
| 69 |
event.preventDefault(); |
| 70 |
event.stopPropagation(); |
| 71 |
onToggle(); |
| 72 |
} |
| 73 |
}; |
| 74 |
|
| 75 |
return ( |
| 76 |
<Button |
| 77 |
className="components-dropdown-menu__toggle" |
| 78 |
icon={Icon} |
| 79 |
onClick={onToggle} |
| 80 |
onKeyDown={onArrowDown} |
| 81 |
aria-haspopup="true" |
| 82 |
label="Open" |
| 83 |
tooltip="Here it is " |
| 84 |
> |
| 85 |
<span className="components-dropdown-menu__indicator" /> |
| 86 |
</Button> |
| 87 |
); |
| 88 |
}} |
| 89 |
renderContent={({ onClose }) => ( |
| 90 |
<div className="eb-typography-wrapper"> |
| 91 |
<FontPicker |
| 92 |
label={__("Font", "betterdocs")} |
| 93 |
value={fontFamily || null} |
| 94 |
onChange={(nextFontFamily) => |
| 95 |
this.onFontChange(nextFontFamily, onClose) |
| 96 |
} |
| 97 |
className="components-base-control--with-flex" |
| 98 |
/> |
| 99 |
|
| 100 |
<SelectControl |
| 101 |
label={__("Weight", "betterdocs")} |
| 102 |
value={fontWeight || null} |
| 103 |
options={WEIGHTS} |
| 104 |
onChange={(fontWeight) => setAttributes({ fontWeight })} |
| 105 |
/> |
| 106 |
|
| 107 |
<BaseControl label={__("Transform", "betterdocs")}> |
| 108 |
<ButtonGroup> |
| 109 |
{TRANSFORMS.map((item) => ( |
| 110 |
<Button |
| 111 |
isPrimary={textTransform === item.value} |
| 112 |
isSecondary={textTransform !== item.value} |
| 113 |
onClick={() => |
| 114 |
setAttributes({ |
| 115 |
textTransform: item.value, |
| 116 |
}) |
| 117 |
} |
| 118 |
> |
| 119 |
{item.label} |
| 120 |
</Button> |
| 121 |
))} |
| 122 |
</ButtonGroup> |
| 123 |
</BaseControl> |
| 124 |
|
| 125 |
<UnitControl |
| 126 |
selectedUnit={fontSizeUnit} |
| 127 |
unitTypes={[ |
| 128 |
{ label: "px", value: "px" }, |
| 129 |
{ label: "em", value: "em" }, |
| 130 |
{ label: "%", value: "%" }, |
| 131 |
]} |
| 132 |
onClick={(fontSizeUnit) => setAttributes({ fontSizeUnit })} |
| 133 |
/> |
| 134 |
|
| 135 |
<RangeControl |
| 136 |
label={__("Size", "betterdocs")} |
| 137 |
value={fontSize || null} |
| 138 |
onChange={(fontSize) => setAttributes({ fontSize })} |
| 139 |
min={0} |
| 140 |
max={SIZE_MAX} |
| 141 |
step={SIZE_STEP} |
| 142 |
/> |
| 143 |
|
| 144 |
<RangeControl |
| 145 |
label={__("Line Height", "betterdocs")} |
| 146 |
value={lineHeight || null} |
| 147 |
onChange={(lineHeight) => setAttributes({ lineHeight })} |
| 148 |
min={-1} |
| 149 |
max={4} |
| 150 |
step={0.1} |
| 151 |
/> |
| 152 |
|
| 153 |
<RangeControl |
| 154 |
label={__("Letter Spacing", "betterdocs")} |
| 155 |
value={letterSpacing || null} |
| 156 |
onChange={(letterSpacing) => setAttributes({ letterSpacing })} |
| 157 |
min={0} |
| 158 |
max={4} |
| 159 |
step={0.1} |
| 160 |
/> |
| 161 |
</div> |
| 162 |
)} |
| 163 |
/> |
| 164 |
); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
export default TypographyControl; |
| 169 |
|