# use-google-fonts

> A Google Fonts hook for React

Latest version **1.0.2** (published 2020-01-20) · ISC license · 0 weekly downloads

## Install

```sh
npm install use-google-fonts
pnpm add use-google-fonts
yarn add use-google-fonts
bun add use-google-fonts
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2020-01-20 |
| First published | 2020-01-18 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 12.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Andy Mardell |
| Maintainers | andymardell |
| Keywords | react, react-hook, react-hooks, google-fonts, fonts, fontface |

## Links

- npm: https://www.npmjs.com/package/use-google-fonts
- Repository: https://github.com/AndyMardell/use-google-fonts
- Homepage: https://github.com/AndyMardell/use-google-fonts#readme
- Issues: https://github.com/AndyMardell/use-google-fonts/issues
- npm.io page: https://npm.io/package/use-google-fonts

## Dependencies (2)

- [react](https://npm.io/package/react.md) ^16.12.0
- [fontfaceobserver](https://npm.io/package/fontfaceobserver.md) ^2.1.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2020-01-20
- 1.0.1 — 2020-01-19
- 1.0.0 — 2020-01-18

## README

# Use Google Fonts

A React hook for importing Google Fonts. Mostly (completely) borrowed from
shelldandy on [this Next.js GitHub issue](https://github.com/zeit/next.js/issues/512#issuecomment-322026199).

Uses [fontfaceobserver](https://fontfaceobserver.com/) with some optional body
classes.

## Installation

Installing the hook

```bash
npm i use-google-fonts
```

## Usage

Say you want to import Roboto (all weights) and Open Sans (300 only).You can use
the hook as follows:

```js
useGoogleFonts([['Roboto'], ['Open Sans', '300']])
```

Full example:

```js
import React from 'react'
import useGoogleFonts from 'use-google-fonts'

function MyReactComponent() {
  useGoogleFonts([
    ['Roboto'],
    ['Open Sans']
  ])

  return (
    <h1 style={{ fontFamily: "'Roboto', sans-serif" }}>
      Roboto Heading
    </h1>
    <p style={{ fontFamily: "'Open Sans', sans-serif" }}>
      Open Sans body
    </p>
  )
}

export default MyReactComponent
```

## Options

There are only a couple of options:

```js
useGoogleFonts([['Roboto', '400']], {
  addBodyClass: true, // This adds 'roboto' to the body class when loaded
  display: 'swap' // Display mode
})
```

## JSX Component Usage

If you're not into the `useGoogleFonts()` hook, you can place a JSX element
anywhere in your app to import the fonts. Ideally this should be at the 'start'
of your app, E.g. in `app.js`

```js
<GoogleFonts fonts={[['Montserrat', '800'], ['Open Sans']]} />
```

With options:

```js
<GoogleFonts fonts={[['Montserrat']]} options={{ addBodyClass: true }} />
```

Full example using a class component:

```js
import React from 'react'
import { GoogleFonts } from 'use-google-fonts'

class App extends React.Component {
  render() {
    return (
      <div className='App'>
        // 1. Import your font(s)
        <GoogleFonts fonts={[['Montserrat', '800']]} />
        <div className='App-header'>
          <img src={logo} className='App-logo' alt='logo' />
          <h2>Welcome to React</h2>
        </div>
        // 2. Specify your font-family
        <p style={{ fontFamily: "'Montserrat', sans-serif" }}>
          To get started, edit <code>src/App.js</code> and save to reload.
        </p>
      </div>
    )
  }
}
```

## Changelog

### 1.0.2 (20 January 2020)

- Added a JSX component as an alternative to the hook
- Updated this README.md

### 1.0.1 (19 January 2020)

- Initial release

---
_Source: https://npm.io/package/use-google-fonts · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
