Skip to content

Added support for the 'text' query string. #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const App = () => (

## Props

The Component takes two props: `fonts` and `subsets`.
The Component takes three props: `fonts`, `subsets` and `text`.

#### `fonts`
`fonts` should be an array of objects describing the fonts you want to load:
Expand All @@ -61,7 +61,10 @@ The Component takes two props: `fonts` and `subsets`.
```

#### `subsets`
`subsets` should be an array of subsets you want to load. **This prop is optional** - if you do not specify a `subsets` prop then the 'subset' query param will be omitted from the URL and only latin will be loaded.
`subsets` should be an array of subsets you want to load. **This prop is optional and shouldn't be used with `text`** - if you do not specify a `subsets` prop then the 'subset' query param will be omitted from the URL and only latin will be loaded.

#### `text`
`text` should be a string containing the characters you wan't the loaded font files to contain. **This prop is optional and shouldn't be used with `subsets`** - if you do not specify a `text` prop then the 'text' query param will be omitted from the URL.

```JavaScript
['cyrillic-ext', 'greek']
Expand Down
3 changes: 1 addition & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ export interface GoogleFontLoaderProps {
fonts: Font[];
subsets?: string[];
display?: 'auto' | 'block' | 'swap' | 'fallback' | 'optional';
text?: string;
}

declare const GoogleFontLoader: React.FC<GoogleFontLoaderProps>;
// declare class GoogleFontLoader extends React.PureComponent<GoogleFontLoaderProps> {};


export default GoogleFontLoader;
22 changes: 17 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect, useState } from 'react';
import PropTypes from 'prop-types';

const createLink = (fonts, subsets, display) => {
const createLink = (fonts, subsets, display, text) => {
const families = fonts.reduce((acc, font) => {
const family = font.font.replace(/ +/g, '+');
const weights = (font.weights || []).join(',');
Expand All @@ -13,9 +13,14 @@ const createLink = (fonts, subsets, display) => {
}, []).join('|');

const link = document.createElement('link');

link.rel = 'stylesheet';
link.href = `https://fonts.googleapis.com/css?family=${families}`;

if (text) {
link.href += `&text=${encodeURIComponent(text)}`;
}

if (subsets && Array.isArray(subsets) && subsets.length > 0) {
link.href += `&subset=${subsets.join(',')}`;
}
Expand All @@ -27,8 +32,14 @@ const createLink = (fonts, subsets, display) => {
return link;
};

const GoogleFontLoader = ({ fonts, subsets, display = null }) => {
const [link, setLink] = useState(createLink(fonts, subsets, display));
const GoogleFontLoader = ({ fonts, subsets, display = null, text = null }) => {
const [link, setLink] = useState(createLink(fonts, subsets, display, text));

useEffect(() => {
if (subsets && text) {
console.warn("You've supplied react-google-font-loader with the props 'text' and 'subsets', this is unnecessary. https://developers.google.com/fonts/docs/getting_started");
}
}, [subsets, text]);

useEffect(() => {
document.head.appendChild(link);
Expand All @@ -37,8 +48,8 @@ const GoogleFontLoader = ({ fonts, subsets, display = null }) => {
}, [link]);

useEffect(() => {
setLink(createLink(fonts, subsets, display));
}, [fonts, subsets, display]);
setLink(createLink(fonts, subsets, display, text));
}, [fonts, subsets, display, text]);

return null;
};
Expand All @@ -55,6 +66,7 @@ GoogleFontLoader.propTypes = {
).isRequired,
subsets: PropTypes.arrayOf(PropTypes.string),
display: PropTypes.string,
text: PropTypes.string,
};

export default GoogleFontLoader;