Skip to content

Commit f021337

Browse files
feat: accept url as prop (#67)
* chore: update package structure * chore: bump microbundle-crl from 0.13.10 to 0.13.11 (#41) Bumps [microbundle-crl](https://github.com/developit/microbundle) from 0.13.10 to 0.13.11. - [Release notes](https://github.com/developit/microbundle/releases) - [Commits](https://github.com/developit/microbundle/commits) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> * chore: [security] bump lodash from 4.17.15 to 4.17.20 (#51) Bumps [lodash](https://github.com/lodash/lodash) from 4.17.15 to 4.17.20. **This update includes security fixes.** - [Release notes](https://github.com/lodash/lodash/releases) - [Commits](lodash/lodash@4.17.15...4.17.20) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> * chore: bump prettier from 2.0.5 to 2.1.2 (#58) Bumps [prettier](https://github.com/prettier/prettier) from 2.0.5 to 2.1.2. - [Release notes](https://github.com/prettier/prettier/releases) - [Changelog](https://github.com/prettier/prettier/blob/master/CHANGELOG.md) - [Commits](prettier/prettier@2.0.5...2.1.2) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> * chore: bump eslint-config-prettier from 6.11.0 to 6.12.0 (#62) Bumps [eslint-config-prettier](https://github.com/prettier/eslint-config-prettier) from 6.11.0 to 6.12.0. - [Release notes](https://github.com/prettier/eslint-config-prettier/releases) - [Changelog](https://github.com/prettier/eslint-config-prettier/blob/master/CHANGELOG.md) - [Commits](prettier/eslint-config-prettier@v6.11.0...v6.12.0) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> * chore: bump eslint-plugin-react from 7.20.0 to 7.21.3 (#65) Bumps [eslint-plugin-react](https://github.com/yannickcr/eslint-plugin-react) from 7.20.0 to 7.21.3. - [Release notes](https://github.com/yannickcr/eslint-plugin-react/releases) - [Changelog](https://github.com/yannickcr/eslint-plugin-react/blob/master/CHANGELOG.md) - [Commits](jsx-eslint/eslint-plugin-react@v7.20.0...v7.21.3) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> * chore: bump eslint-plugin-import from 2.21.2 to 2.22.1 (#64) Bumps [eslint-plugin-import](https://github.com/benmosher/eslint-plugin-import) from 2.21.2 to 2.22.1. - [Release notes](https://github.com/benmosher/eslint-plugin-import/releases) - [Changelog](https://github.com/benmosher/eslint-plugin-import/blob/master/CHANGELOG.md) - [Commits](import-js/eslint-plugin-import@v2.21.2...v2.22.1) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> * chore: bump eslint from 6.8.0 to 7.10.0 (#63) Bumps [eslint](https://github.com/eslint/eslint) from 6.8.0 to 7.10.0. - [Release notes](https://github.com/eslint/eslint/releases) - [Changelog](https://github.com/eslint/eslint/blob/master/CHANGELOG.md) - [Commits](eslint/eslint@v6.8.0...v7.10.0) Signed-off-by: dependabot-preview[bot] <support@dependabot.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> * chore: update dependabot config * feat: accept single url as prop This simplifies the api and removes the need to build an object to load a font. As google offers a url already with query params, this hook will just load the font as is. BREAKING CHANGE: removed fonts prop * chore: update docs Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
1 parent 47eaee5 commit f021337

4 files changed

Lines changed: 16 additions & 43 deletions

File tree

.editorconfig

Lines changed: 0 additions & 9 deletions
This file was deleted.

.eslintignore

Lines changed: 0 additions & 5 deletions
This file was deleted.

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import ReactFontLoader from 'react-font-loader'
2020
const App = () => {
2121
return (
2222
<div>
23-
<ReactFontLoader fonts={[{ name: 'Permanent Marker' }]} />
24-
<h1 style={{ fontFamily: 'Permanent Marker' }}>Custom font</h1>
23+
<ReactFontLoader url='https://fonts.googleapis.com/css2?family=Red+Hat+Display:wght@700&display=swap' />
24+
<h1 style={{ fontFamily: 'Red Hat Display' }}>Custom font</h1>
2525
</div>
2626
)
2727
}
@@ -36,11 +36,14 @@ import React from 'react'
3636
import useReactFontLoader from 'react-font-loader'
3737

3838
const App = () => {
39-
useReactFontLoader({ fonts: [{ name: 'Permanent Marker' }] })
39+
useReactFontLoader({
40+
url:
41+
'https://fonts.googleapis.com/css2?family=Red+Hat+Display:wght@700&display=swap',
42+
})
4043

4144
return (
4245
<div>
43-
<h1 style={{ fontFamily: 'Permanent Marker' }}>Custom font</h1>
46+
<h1 style={{ fontFamily: 'Red Hat Display' }}>Custom font</h1>
4447
</div>
4548
)
4649
}

src/index.js

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,24 @@
11
import { useEffect } from 'react'
2-
import { arrayOf, number, oneOfType, shape, string } from 'prop-types'
2+
import { string } from 'prop-types'
33

4-
const createElement = fonts => {
5-
const families = fonts
6-
.reduce((accumulator, font) => {
7-
const family = font.name.replace(' ', '+')
8-
const weight = (font.weight && `:${font.weight.join(',')}`) || ''
9-
return [...accumulator, `${family}${weight}`]
10-
}, [])
11-
.join('|')
12-
13-
const link = document.createElement('link')
14-
link.rel = 'stylesheet'
15-
link.href = `https://fonts.googleapis.com/css?family=${families}`
16-
17-
return link
4+
const createElement = url => {
5+
const element = document.createElement('link')
6+
element.rel = 'stylesheet'
7+
element.href = url
8+
return element
189
}
1910

20-
const ReactFontLoader = ({ fonts }) => {
11+
const ReactFontLoader = ({ url }) => {
2112
useEffect(() => {
22-
const element = createElement(fonts)
13+
const element = createElement(url)
2314
document.head.appendChild(element)
24-
2515
return () => document.head.removeChild(element)
2616
}, [])
27-
2817
return null
2918
}
3019

3120
ReactFontLoader.propTypes = {
32-
fonts: arrayOf(
33-
shape({
34-
name: string.isRequired,
35-
weights: arrayOf(oneOfType([string, number])),
36-
}),
37-
).isRequired,
21+
url: string.isRequired,
3822
}
3923

4024
export default ReactFontLoader

0 commit comments

Comments
 (0)