Skip to content

Commit c2860dc

Browse files
committed
fix: use object lookup instead of conditions
1 parent 63483df commit c2860dc

File tree

2 files changed

+37
-46
lines changed

2 files changed

+37
-46
lines changed

src/components/font-family-control/index.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,21 @@ const FontFamilyControl = props => {
4444
id: 'modern-font-stacks',
4545
title: __( 'Modern Font Stacks', i18n ),
4646
options: [
47-
{ label: __( 'System UI', i18n ), value: 'System-UI' },
48-
{ label: __( 'Transitional', i18n ), value: 'Transitional' },
49-
{ label: __( 'Old Style', i18n ), value: 'Old-Style' },
50-
{ label: __( 'Humanist', i18n ), value: 'Humanist' },
51-
{ label: __( 'Geometric Humanist', i18n ), value: 'Geometric-Humanist' },
52-
{ label: __( 'Classical Humanist', i18n ), value: 'Classical-Humanist' },
53-
{ label: __( 'Neo-Grotesque', i18n ), value: 'Neo-Grotesque' },
54-
{ label: __( 'Monospace Slab Serif', i18n ), value: 'Monospace-Slab-Serif' },
55-
{ label: __( 'Monospace Code', i18n ), value: 'Monospace-Code' },
56-
{ label: __( 'Industrial', i18n ), value: 'Industrial' },
57-
{ label: __( 'Rounded Sans', i18n ), value: 'Rounded-Sans' },
58-
{ label: __( 'Slab Serif', i18n ), value: 'Slab-Serif' },
59-
{ label: __( 'Antique', i18n ), value: 'Antique' },
60-
{ label: __( 'Didone', i18n ), value: 'Didone' },
61-
{ label: __( 'Handwritten', i18n ), value: 'Handwritten' },
47+
{ label: __( 'System UI', i18n ), value: 'system-ui' },
48+
{ label: __( 'Transitional', i18n ), value: 'transitional' },
49+
{ label: __( 'Old Style', i18n ), value: 'old-style' },
50+
{ label: __( 'Humanist', i18n ), value: 'humanist' },
51+
{ label: __( 'Geometric Humanist', i18n ), value: 'geometric-humanist' },
52+
{ label: __( 'Classical Humanist', i18n ), value: 'classical-humanist' },
53+
{ label: __( 'Neo-Grotesque', i18n ), value: 'neo-grotesque' },
54+
{ label: __( 'Monospace Slab Serif', i18n ), value: 'monospace-slab-serif' },
55+
{ label: __( 'Monospace Code', i18n ), value: 'monospace-code' },
56+
{ label: __( 'Industrial', i18n ), value: 'industrial' },
57+
{ label: __( 'Rounded Sans', i18n ), value: 'rounded-sans' },
58+
{ label: __( 'Slab Serif', i18n ), value: 'slab-serif' },
59+
{ label: __( 'Antique', i18n ), value: 'antique' },
60+
{ label: __( 'Didone', i18n ), value: 'didone' },
61+
{ label: __( 'Handwritten', i18n ), value: 'handwritten' },
6262
],
6363
},
6464
{

src/util/font.js

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,25 @@ const subsets = { /* eslint-disable quote-props */
1616
'pl_PL': 'latin-ext',
1717
}
1818

19+
// See https://github.com/system-fonts/modern-font-stacks
20+
const MODERN_FONTS = {
21+
'system-ui': 'system-ui, sans-serif',
22+
'transitional': "Charter, 'Bitstream Charter', 'Sitka Text', Cambria, serif",
23+
'old-style': "'Iowan Old Style', 'Palatino Linotype', 'URW Palladio L', P052, serif",
24+
'humanist': "Seravek, 'Gill Sans Nova', Ubuntu, Calibri, 'DejaVu Sans', source-sans-pro, sans-serif",
25+
'geometric-humanist': "Avenir, Montserrat, Corbel, 'URW Gothic', source-sans-pro, sans-serif",
26+
'classical-humanist': "Optima, Candara, 'Noto Sans', source-sans-pro, sans-serif",
27+
'neo-grotesque': "Inter, Roboto, 'Helvetica Neue', 'Arial Nova', 'Nimbus Sans', Arial, sans-serif",
28+
'monospace-slab-serif': "'Nimbus Mono PS', 'Courier New', monospace",
29+
'monospace-code': "ui-monospace, 'Cascadia Code', 'Source Code Pro', Menlo, Consolas, 'DejaVu Sans Mono', monospace",
30+
'industrial': "Bahnschrift, 'DIN Alternate', 'Franklin Gothic Medium', 'Nimbus Sans Narrow', sans-serif-condensed, sans-serif",
31+
'rounded-sans': "ui-rounded, 'Hiragino Maru Gothic ProN', Quicksand, Comfortaa, Manjari, 'Arial Rounded MT', 'Arial Rounded MT Bold', Calibri, source-sans-pro, sans-serif",
32+
'slab-serif': "Rockwell, 'Rockwell Nova', 'Roboto Slab', 'DejaVu Serif', 'Sitka Small', serif",
33+
'antique': "Superclarendon, 'Bookman Old Style', 'URW Bookman', 'URW Bookman L', 'Georgia Pro', Georgia, serif",
34+
'didone': "Didot, 'Bodoni MT', 'Noto Serif Display', 'URW Palladio L', P052, Sylfaen, serif",
35+
'handwritten': "'Segoe Print', 'Bradley Hand', Chilanka, TSCu_Comic, casual, cursive",
36+
}
37+
1938
export const getGoogleFontURL = fontName => {
2039
const family = fontName.replace( / /g, '+' )
2140
const subset = subsets[ locale ] ? `&subset=${ subsets }` : ''
@@ -94,37 +113,9 @@ export const getFontFamily = fontName => {
94113
return fontName
95114
}
96115

97-
// Modern font stacks. See https://github.com/system-fonts/modern-font-stacks
98-
if ( fontName.match( /^system-ui$/i ) ) {
99-
return 'system-ui, sans-serif'
100-
} else if ( fontName.match( /^transitional$/i ) ) {
101-
return "Charter, 'Bitstream Charter', 'Sitka Text', Cambria, serif"
102-
} else if ( fontName.match( /^old-style$/i ) ) {
103-
return "'Iowan Old Style', 'Palatino Linotype', 'URW Palladio L', P052, serif"
104-
} else if ( fontName.match( /^humanist$/i ) ) {
105-
return "Seravek, 'Gill Sans Nova', Ubuntu, Calibri, 'DejaVu Sans', source-sans-pro, sans-serif"
106-
} else if ( fontName.match( /^geometric-humanist$/i ) ) {
107-
return "Avenir, Montserrat, Corbel, 'URW Gothic', source-sans-pro, sans-serif"
108-
} else if ( fontName.match( /^classical-humanist$/i ) ) {
109-
return "Optima, Candara, 'Noto Sans', source-sans-pro, sans-serif"
110-
} else if ( fontName.match( /^neo-grotesque$/i ) ) {
111-
return "Inter, Roboto, 'Helvetica Neue', 'Arial Nova', 'Nimbus Sans', Arial, sans-serif"
112-
} else if ( fontName.match( /^monospace-slab-serif$/i ) ) {
113-
return "'Nimbus Mono PS', 'Courier New', monospace"
114-
} else if ( fontName.match( /^monospace-code$/i ) ) {
115-
return "ui-monospace, 'Cascadia Code', 'Source Code Pro', Menlo, Consolas, 'DejaVu Sans Mono', monospace"
116-
} else if ( fontName.match( /^industrial$/i ) ) {
117-
return "Bahnschrift, 'DIN Alternate', 'Franklin Gothic Medium', 'Nimbus Sans Narrow', sans-serif-condensed, sans-serif"
118-
} else if ( fontName.match( /^rounded-sans$/i ) ) {
119-
return "ui-rounded, 'Hiragino Maru Gothic ProN', Quicksand, Comfortaa, Manjari, 'Arial Rounded MT', 'Arial Rounded MT Bold', Calibri, source-sans-pro, sans-serif"
120-
} else if ( fontName.match( /^slab-serif$/i ) ) {
121-
return "Rockwell, 'Rockwell Nova', 'Roboto Slab', 'DejaVu Serif', 'Sitka Small', serif"
122-
} else if ( fontName.match( /^antique$/i ) ) {
123-
return "Superclarendon, 'Bookman Old Style', 'URW Bookman', 'URW Bookman L', 'Georgia Pro', Georgia, serif"
124-
} else if ( fontName.match( /^didone$/i ) ) {
125-
return "Didot, 'Bodoni MT', 'Noto Serif Display', 'URW Palladio L', P052, Sylfaen, serif"
126-
} else if ( fontName.match( /^handwritten$/i ) ) {
127-
return "'Segoe Print', 'Bradley Hand', Chilanka, TSCu_Comic, casual, cursive"
116+
// Modern font stacks.
117+
if ( fontName.toLowerCase() in MODERN_FONTS ) {
118+
return MODERN_FONTS[ fontName.toLowerCase() ]
128119
}
129120

130121
// Google Font.

0 commit comments

Comments
 (0)