• Stars
    star
    171
  • Rank 222,266 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 7 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

✏️ Font selector component for Google Fonts

Font Picker

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

  • Simple setup
  • No dependencies
  • Automatic font download and generation of the required CSS selectors
  • Efficient font previews (full fonts are only downloaded on selection)

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:

  • Using script tags: Download the FontPicker.js file from the releases page and save it in your project. Include the script in your HTML at the end of the document <body>:
<script src="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>
  • Using NPM: If you're using a module bundler like Webpack, you can install the font-picker package using NPM and import it in your code:
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);
  • apiKey (required): Google API key
  • defaultFamily: Font that is selected on initialization. Default: "Open Sans"
  • options: Object with additional optional parameters:
    • pickerId: If you have multiple font pickers on your site, you need to give them unique IDs which must be appended to the pickers' id attributes and the .apply-font class names. Example: If the options object is { pickerId: "main" }, use #font-picker-main and .apply-font-main
    • families: If only specific fonts shall appear in the list, specify their names in an array. Default: All font families
    • categories: Array of font categories to include in the list. Possible values: "sans-serif", "serif", "display", "handwriting", "monospace". Default: All categories
    • scripts: Array of scripts which the fonts must include and which will be downloaded on font selection. Default: ["latin"]. Example: ["latin", "greek", "hebrew"] (see all possible values)
    • variants: Array of variants which the fonts must include and which will be downloaded on font selection. Default: ["regular"]. Example: ["regular", "italic", "700", "700italic"] (see all possible values)
    • filter: Function which must evaluate to true for a font to be included in the list. Default: font => true. Example: If font => font.family.toLowerCase().startsWith("m"), only fonts whose names begin with "M" will be in the list
    • limit: Maximum number of fonts to display in the list (the least popular fonts will be omitted). Default: 50
    • sort: Sorting attribute for the font list. Possible values: "alphabet", "popularity". Default: "alphabet"
  • onChange: Function to execute whenever the active font is changed

Functions

The FontPicker class exposes the following functions:

  • getFonts(): Returns a map of all font names/objects
  • addFont(fontFamily: string, index?: number): Adds the specified font to the font list (at the given index)
  • removeFont(fontFamily: string): Removes the specified font from the font list
  • getActiveFont(): Returns the font object of the currently active font
  • setActiveFont(fontFamily: string): Sets the provided font as the active font
  • setOnChange(onChange: (font: Font) => void): Update the onChange function after the font picker has been initialized

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.

More Repositories

1

mini-diary

📘 Simple and secure journal app
TypeScript
1,031
star
2

glance

🔎 All-in-one Quick Look plugin
Swift
953
star
3

action-electron-builder

:electron: GitHub Action for building and releasing Electron apps
JavaScript
651
star
4

tmignore

🕔 Exclude development files from Time Machine backups
Swift
322
star
5

font-picker-react

✏️ Font selector component for Google Fonts
TypeScript
152
star
6

react-magnifier

🔍 React image zoom component
TypeScript
151
star
7

action-maven-publish

📦 GitHub Action for automatically publishing Maven packages
JavaScript
101
star
8

lyrics-poster

🎤 Create posters of your favorite artists, written in their lyrics
JavaScript
63
star
9

refind-theme-dark

🎨 A simple, dark theme for rEFInd
54
star
10

action-snapcraft

🐦 GitHub Action for setting up Snapcraft
JavaScript
49
star
11

font-manager

🗂 Manages, downloads and applies Google Fonts for picker components
TypeScript
27
star
12

swift-exec

🚀 Simple process execution with Swift
Swift
16
star
13

draft-js-list-plugin

📝 Better lists for Draft.js
TypeScript
14
star
14

python-wikibase

🤖 Wikibase queries and edits made easy
Python
11
star
15

nbtohtml

📈 Convert Jupyter Notebook files to HTML
Go
9
star
16

github-downloads

⬇️ See the number of downloads next to releases on GitHub
JavaScript
8
star
17

alfred-system-theme

🎨 Alfred themes in style of Apple's macOS apps
7
star
18

react-library-boilerplate

🔨 Create a React component library with no configuration
JavaScript
6
star
19

iconsets.org

🌟 Icon set discovery website for designers
JavaScript
5
star
20

wikibase-api

📦 Wrapper library for the Wikibase API
Python
4
star
21

anonymize-ip

👤 Python library for anonymizing IP addresses
Python
4
star
22

strava-bulk-edit

🏃 Edit multiple Strava activities at once
Go
2
star
23

electron-md-to-pdf

:electron: Convert Markdown to PDF in Electron apps
CSS
2
star
24

hugo-starter-theme

🔨 Boilerplate for building a custom Hugo theme
HTML
2
star
25

samuelmeuli.com

👨‍💻 My personal website
CSS
1
star
26

music-notifications

🎵 Notification service for new releases on iTunes
JavaScript
1
star
27

prettier-config

☑️ My Prettier configuration
JavaScript
1
star
28

react-app-boilerplate

🔨 Create a React application with no configuration
JavaScript
1
star