Skip to content

🛠️ ⸨ plugin ⸩  quickly import Google Fonts with all available styles (i.e. italic & bold weight variants)

License

Notifications You must be signed in to change notification settings

ht-devx/addGoogleFonts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

addGoogleFonts

A plugin that quickly imports all available styles (such as bold and italic variants) of Google Fonts by specifying the font name(s).

Preview / Demo: jsfiddle.net/ht_dev/4n8xk2p3
Author: HT (@ ht-devx)
Release date: 2024-03-08
Last updated: 2024-03-08 3:08PM [GMT-8]


Table of Contents:


How to use:

Include the following after <head>:

<script src="https://cdn.jsdelivr.net/gh/ht-devx/addGoogleFonts/addGoogleFonts.min.js"></script>
<script>
addGoogleFonts("Albert Sans, Bona Nova");
</script>

In this example, Albert Sans and Bona Nova are included, thus they will be added to the project.

To apply the fonts, specify the font name as the font-family of your component in its CSS:

.your-selector {
    font-family: "Albert Sans", sans-serif;
}

.another-example-selector {
    font-family: "Bona Nova", serif;
}

In the above example, sans-serif and serif are fallbacks in case the intended fonts fail to load properly, or are slow to load.

Preview of how it all comes together:
jsfiddle.net/ht_dev/4n8xk2p3

Usage notes:

  • Feel free to incorporate as many fonts as desired, provided that they are available on Google Fonts.
  • Font names should be separated by a comma , and should stay within the quotation marks "".

✅ Correct example:

addGoogleFonts("Albert Sans, Bona Nova");

❌ Incorrect example:

addGoogleFonts("Albert Sans", "Bona Nova");

How it Works:

  1. This plugin checks if fonts.googleapis.com and fonts.gstatic.com Google Fonts stylesheets exist on the page, and adds them if they don't already exist.
  2. Makes a fetch request to fonts.googleapis.com to check if the fonts specified by the user exist.
  3. Once verified, make a fetch request for each font-weight (100, 200, 300, 400, 500, 600, 700, 800, 900), regardless if that particular font has it or not. For example, the Itim font only has 400 available, so it will return a negative value for all other weights it does not have.
  4. If the font only has one font-weight available, add it as well as its italic counterpart.
  5. If the font has multiple font-weight options available, make another fetch request for a variable font weight range.
    — If it succeeds, that font is a variable font. Proceed with the variable range as well as its italic counterpart.
    — If it fails, that font is not a variable font and each font-weight needs to be loaded separately (along with its italic counterpart) rather than specifying a range.
  6. Lastly, combine all fonts' strings together and load them as a single stylesheet.

Attribution:

No visible credit/attribution is required, but please do not remove the credits situated within the JS file(s). A link to this repository would be greatly appreciated!


Troubleshooting:

If you need further assistance, please contact me at: hello.ht.dev@gmail.com


Credits:

Evidently, Google Fonts.

Based on @rachaelthemes' customFonts.js:

A script that takes a provided font (or fonts) and automatically calls a Google Fonts script to load those fonts.

Special thanks to Rachael for giving me the green light for my addGoogleFonts plugin. ⚪ ⚪ 🟢
Rachael's script is tailored toward Tumblr theme users, whilst my addGoogleFonts is for more general use in web projects.

About

🛠️ ⸨ plugin ⸩  quickly import Google Fonts with all available styles (i.e. italic & bold weight variants)

Topics

Resources

License

Stars

Watchers

Forks