templates-patterns-collection
/
onboarding
/
src
/
Components
/
CustomizeControls
/
TypographyControl.js
FeaturesControl.js
1 year ago
ImportOptionsControl.js
1 year ago
LogoControl.js
1 year ago
PaletteControl.js
1 year ago
SiteNameControl.js
1 year ago
TypographyControl.js
2 months ago
TypographyControl.js
142 lines
| 1 | /* global tiobDash */ |
| 2 | import { __ } from '@wordpress/i18n'; |
| 3 | import { Button } from '@wordpress/components'; |
| 4 | import classnames from 'classnames'; |
| 5 | import SVG from '../../utils/svg'; |
| 6 | import { withDispatch, withSelect } from '@wordpress/data'; |
| 7 | import { compose } from '@wordpress/compose'; |
| 8 | |
| 9 | const FALLBACK_FONT = 'Arial, Helvetica, sans-serif'; |
| 10 | |
| 11 | const getFontPairs = ( importData ) => { |
| 12 | if ( importData && importData.font_pairs && Object.keys( importData.font_pairs ).length ) { |
| 13 | return importData.font_pairs; |
| 14 | } |
| 15 | if ( tiobDash && tiobDash.fontParings ) { |
| 16 | return tiobDash.fontParings; |
| 17 | } |
| 18 | return null; |
| 19 | }; |
| 20 | |
| 21 | const getDefaultFonts = ( importDataDefault ) => { |
| 22 | const themeMods = importDataDefault?.theme_mods; |
| 23 | return { |
| 24 | body: themeMods?.neve_body_font_family || FALLBACK_FONT, |
| 25 | heading: themeMods?.neve_headings_font_family || FALLBACK_FONT, |
| 26 | }; |
| 27 | }; |
| 28 | |
| 29 | const TypographyControl = ( { |
| 30 | siteStyle, |
| 31 | handleFontClick, |
| 32 | importData, |
| 33 | importDataDefault, |
| 34 | } ) => { |
| 35 | const fontPairs = getFontPairs( importData ); |
| 36 | if ( ! fontPairs ) { |
| 37 | return null; |
| 38 | } |
| 39 | |
| 40 | const { body: defaultBodyFont, heading: defaultHeadingsFont } = |
| 41 | getDefaultFonts( importDataDefault ); |
| 42 | const { font } = siteStyle; |
| 43 | |
| 44 | return ( |
| 45 | <div className="ob-ctrl"> |
| 46 | <div className="ob-ctrl-head small-gap"> |
| 47 | <h3>{ __( 'Typography', 'templates-patterns-collection' ) }</h3> |
| 48 | <Button |
| 49 | icon={ SVG.redo } |
| 50 | onClick={ () => handleFontClick( 'default' ) } |
| 51 | /> |
| 52 | </div> |
| 53 | <div className="ob-ctrl-wrap font"> |
| 54 | <Button |
| 55 | className={ classnames( [ |
| 56 | 'ob-font-pair', |
| 57 | 'ob-font-default', |
| 58 | { 'ob-active': 'default' === font }, |
| 59 | ] ) } |
| 60 | title={ `${ defaultHeadingsFont } / ${ defaultBodyFont }` } |
| 61 | onClick={ () => handleFontClick( 'default' ) } |
| 62 | > |
| 63 | { __( 'Default', 'templates-patterns-collection' ) } |
| 64 | </Button> |
| 65 | |
| 66 | { Object.keys( fontPairs ).map( ( slug ) => { |
| 67 | const { headingFont, bodyFont } = fontPairs[ slug ]; |
| 68 | const headingStyle = { |
| 69 | fontFamily: headingFont.font, |
| 70 | }; |
| 71 | |
| 72 | return ( |
| 73 | <Button |
| 74 | key={ slug } |
| 75 | className={ classnames( [ |
| 76 | 'ob-font-pair', |
| 77 | { 'ob-active': slug === font }, |
| 78 | ] ) } |
| 79 | style={ headingStyle } |
| 80 | title={ `${ headingFont.font } / ${ bodyFont.font }` } |
| 81 | onClick={ () => handleFontClick( slug ) } |
| 82 | > |
| 83 | Abc |
| 84 | </Button> |
| 85 | ); |
| 86 | } ) } |
| 87 | </div> |
| 88 | </div> |
| 89 | ); |
| 90 | }; |
| 91 | |
| 92 | export default compose( |
| 93 | withSelect( ( select ) => { |
| 94 | const { getImportData } = select( 'ti-onboarding' ); |
| 95 | return { |
| 96 | importData: getImportData(), |
| 97 | }; |
| 98 | } ), |
| 99 | withDispatch( |
| 100 | ( |
| 101 | dispatch, |
| 102 | { importData, importDataDefault, siteStyle, setSiteStyle } |
| 103 | ) => { |
| 104 | const { setImportData, setRefresh } = dispatch( 'ti-onboarding' ); |
| 105 | const fontPairs = getFontPairs( importData ); |
| 106 | const { body: defaultBodyFont, heading: defaultHeadingsFont } = |
| 107 | getDefaultFonts( importDataDefault ); |
| 108 | |
| 109 | return { |
| 110 | handleFontClick: ( fontKey ) => { |
| 111 | const newStyle = { |
| 112 | ...siteStyle, |
| 113 | font: fontKey, |
| 114 | }; |
| 115 | setSiteStyle( newStyle ); |
| 116 | |
| 117 | const pair = |
| 118 | fontKey !== 'default' && fontPairs && fontPairs[ fontKey ] |
| 119 | ? fontPairs[ fontKey ] |
| 120 | : { |
| 121 | bodyFont: { font: defaultBodyFont }, |
| 122 | headingFont: { font: defaultHeadingsFont }, |
| 123 | }; |
| 124 | |
| 125 | const { bodyFont, headingFont } = pair; |
| 126 | |
| 127 | const newImportData = { |
| 128 | ...importData, |
| 129 | theme_mods: { |
| 130 | ...importData.theme_mods, |
| 131 | neve_body_font_family: bodyFont.font, |
| 132 | neve_headings_font_family: headingFont.font, |
| 133 | }, |
| 134 | }; |
| 135 | setImportData( newImportData ); |
| 136 | setRefresh( true ); |
| 137 | }, |
| 138 | }; |
| 139 | } |
| 140 | ) |
| 141 | )( TypographyControl ); |
| 142 |