samuelmeuli / font-picker-react

✏️ Font selector component for Google Fonts
https://font-picker.samuelmeuli.com
MIT License
153 stars 71 forks source link
font-picker font-selector fonts google-fonts react react-component

Font Picker for React

A simple, customizable font picker allowing users to preview, select and use Google Fonts on your website.

Demo

This is the React component for the Font Picker library.

Font picker demo

Getting started

To be able to access the API, you'll need to generate a Google Fonts API key.

1. Setup

Install the font-picker-react package using NPM:

npm install font-picker-react

2. Displaying the font picker

Add the FontPicker component to your React code:

import React, { Component } from "react";
import FontPicker from "font-picker-react";

export default class ExampleComponent extends Component {
    constructor(props) {
        super(props);
        this.state = {
            activeFontFamily: "Open Sans",
        };
    }

    render() {
        return (
            <div>
                <FontPicker
                    apiKey="YOUR_API_KEY"
                    activeFontFamily={this.state.activeFontFamily}
                    onChange={(nextFont) =>
                        this.setState({
                            activeFontFamily: nextFont.family,
                        })
                    }
                />
                <p className="apply-font">The font will be applied to this text.</p>
            </div>
        );
    }
}

3. Applying the selected font

Add the class "apply-font" to all JSX elements you want to apply the selected font to.

When the user selects a font, it will automatically be downloaded and applied to all elements with the "apply-font" class.

Props

The following props can be passed to the FontPicker component:

Currently, only the activeFontFamily, onChange and sort props are reactive.

Development

Requirements: Node.js, Yarn

  1. Clone this repository: git clone REPO_URL
  2. Install all dependencies: yarn
  3. Generate the library bundle: yarn start
  4. View the rendered component on localhost:3000

Suggestions and contributions are always welcome! Please discuss larger changes via issue before submitting a pull request.