-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #297 from taylrj/add-font-face
Add fontfaces and the use-font-face-observer hook
- Loading branch information
Showing
7 changed files
with
126 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,32 @@ | ||
const merriweather = 'merriweather' | ||
const notoSerifTC = 'Noto Serif TC' | ||
const serif = 'serif' | ||
const rosario = 'rosario' | ||
const notoSansTC = 'Noto Sans TC' | ||
const sansSerif = 'sans-serif' | ||
|
||
const fonts = { | ||
merriweather, | ||
notoSerifTC, | ||
serif, | ||
rosario, | ||
notoSansTC, | ||
sansSerif, | ||
} | ||
|
||
const fontWeight = { | ||
extraLight: '100', | ||
normal: '400', | ||
bold: '700', | ||
} | ||
|
||
const fontFamily = { | ||
title: 'merriweather, Noto Serif TC, serif', | ||
default: 'rosario, Noto Sans TC, sans-serif', | ||
title: `${merriweather}, ${notoSerifTC}, ${serif}`, | ||
default: `${rosario}, ${notoSansTC}, ${sansSerif}`, | ||
// use defaultFallback before ${notoSansTC} is fully loaded | ||
defaultFallback: `${rosario}, ${sansSerif}`, | ||
} | ||
|
||
export default { fontWeight, fontFamily } | ||
export default { fonts, fontWeight, fontFamily } | ||
|
||
export { fontWeight, fontFamily } | ||
export { fonts, fontWeight, fontFamily } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import useOutsideClick from './use-outside-click' | ||
import useFontFaceObserver from './use-font-face-observer' | ||
|
||
export { useOutsideClick } | ||
export { useOutsideClick, useFontFaceObserver } | ||
|
||
export default { | ||
useOutsideClick, | ||
useFontFaceObserver, | ||
} |
36 changes: 36 additions & 0 deletions
36
packages/react-components/src/hook/use-font-face-observer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useState, useEffect } from 'react' | ||
import FontFaceObserver from 'fontfaceobserver-es' | ||
|
||
const useFontFaceObserver = (fontFaces = [], callback) => { | ||
const [isResolved, setIsResolved] = useState(false) | ||
const fontFacesString = JSON.stringify(fontFaces) | ||
|
||
useEffect(() => { | ||
const promises = JSON.parse(fontFacesString).map( | ||
({ family, weight, style, stretch }) => | ||
new FontFaceObserver(family, { | ||
weight, | ||
style, | ||
stretch, | ||
}).load('測試文字') | ||
) | ||
|
||
Promise.all(promises) | ||
.then(() => { | ||
if (typeof callback !== 'function') { | ||
return | ||
} | ||
callback() | ||
return setIsResolved(true) | ||
}) | ||
.catch(e => { | ||
// eslint-disable-next-line no-console | ||
console.error(`An error occurred during font loading`) | ||
console.log(e) | ||
}) | ||
}, [fontFacesString, callback]) | ||
|
||
return isResolved | ||
} | ||
|
||
export default useFontFaceObserver |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { fonts, fontWeight } from '@twreporter/core/lib/constants/font' | ||
// lodash | ||
import forEach from 'lodash/forEach' | ||
import keys from 'lodash/keys' | ||
import reduce from 'lodash/reduce' | ||
|
||
const _ = { | ||
forEach, | ||
keys, | ||
reduce, | ||
} | ||
|
||
const baseGCSDir = 'https://www.twreporter.org/assets/font/' | ||
const fileExt = '.woff2' | ||
|
||
const gcsFontFolder = { | ||
[fonts.notoSansTC]: 'NotoSansTC', | ||
} | ||
|
||
const fontWeightKeys = _.keys(fontWeight) | ||
|
||
// add @font-face to global style to use self-hosted font for performance reasons, to be more precise please check the issue below: | ||
// https://twreporter-org.atlassian.net/browse/TWREPORTER-318?atlOrigin=eyJpIjoiNjg4OTQ2MWU2MGIxNGEzMGE0NDY2ZDNmZGRhOWExZDEiLCJwIjoiaiJ9 | ||
const getFontFaces = ({ font, folder }) => { | ||
const fontFaceCSSTemplate = ({ fontWeightKey }) => ` | ||
@font-face { | ||
font-family: "${font}"; | ||
font-weight: ${fontWeight[fontWeightKey]}; | ||
font-display: swap; | ||
src: url("${baseGCSDir}${folder}/${fontWeightKey}${fileExt}"); | ||
} | ||
` | ||
return _.reduce( | ||
fontWeightKeys, | ||
(fontFaces, fontWeightKey) => { | ||
return fontFaces + fontFaceCSSTemplate({ fontWeightKey }) | ||
}, | ||
'' | ||
) | ||
} | ||
|
||
const fontFaces = { | ||
[fonts.notoSansTC]: getFontFaces({ | ||
font: fonts.notoSansTC, | ||
folder: gcsFontFolder[fonts.notoSansTC], | ||
}), | ||
} | ||
|
||
let fontGCSFiles = [] | ||
|
||
_.forEach(fontFaces, function(fontFace, font) { | ||
_.forEach(fontWeightKeys, fontWeightKey => { | ||
fontGCSFiles.push( | ||
`${baseGCSDir}${gcsFontFolder[font]}/${fontWeightKey}${fileExt}` | ||
) | ||
}) | ||
}) | ||
|
||
export default { fontFaces, fontGCSFiles } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2bbec6a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
react-components – ./
react-components-git-master-twreporter.vercel.app
twreporter-components-storybook.vercel.app
react-components-twreporter.vercel.app