samuelmeuli / font-picker

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

Font Picker

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

Demo

If you use React, see Font Picker for React.

Font picker demo

Getting started

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

1. Setup

You have the following options for installing/using the package:

<script src="https://github.com/samuelmeuli/font-picker/raw/master/path/to/FontPicker.js"></script>
<script>
    const fontPicker = new FontPicker(
        YOUR_API_KEY, // Google API key
        "Open Sans", // Default font
        { limit: 30 }, // Additional options
    );
</script>
npm install font-picker
import FontPicker from "font-picker";

const fontPicker = new FontPicker(
    YOUR_API_KEY, // Google API key
    "Open Sans", // Default font
    { limit: 30 }, // Additional options
);

2. Displaying the font picker

Create an empty <div> with id="font-picker" in your HTML file. This is where the font picker will be generated.

<div id="font-picker"></div>

3. Applying the selected font

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

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

Class names

Customization

Parameters

The following parameters can be passed to the constructor of the FontPicker class:

const fontPicker = new FontPicker(apiKey, defaultFamily, options, onChange);

Functions

The FontPicker class exposes the following functions:

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.