2
4 Comments

how can I implement a font pairing feature?

I'm currently building ui.coach and I have feature which generates font pairings and displays them in their respective font family similar to https://fontjoy.com/, but I feel quite lost on what approach to take to implement this feature because I will use google fonts and I assume including a bunch of fonts will slow down the website significantly, I would be very thankful if anyone here can guide me on how to implement this feature (I'm using React)

on October 16, 2020
  1. 2

    How do you generate the pairings? Is that something you do manually that you could hardcode into the application? Does your tool build the pairings on the fly?

    One way could be to load only the fonts you need when you need them. Using react-helmet, your pairing-component can link the exact stylesheet for the combination you’d have to load from Google Fonts with the appropriate family-parameter. If you know which weights you need, you can reduce the time it takes to download and show the fonts even further.

    If you only need to show the names of the fonts without a live preview, you could manually create a bunch of static image files that you then show for each pairing. You’d probably want a large-and-bold and a small-and-medium version of each typeface for headline and paragraph content. This would be less flexible and require a lot of manual preparation, but you wouldn’t need to load any of the typefaces at all.

    I’d be happy to look at some code for this if you show me what you have so far.

    1. 2

      I prototyped the Helmet-version. This assumes a display-component you want to include like so:

      <FontsDemo
        body="Lato"
        heading="Raleway"
      />
      

      The component behind this takes the typefaces from the props you pass to it, links the stylesheet for these fonts from Google Fonts, and shows some text (with the font family appended to the text):

      import React from 'react'
      import { Helmet } from 'react-helmet'
      
      export default ({
        body,
        heading,
      }) => {
        return (
          <React.Fragment>
            <Helmet>
              <link href={`https://fonts.googleapis.com/css2?family=${body.replace(' ', '+')}&family=${heading.replace(' ', '+')}&display=swap`} rel="stylesheet" />
            </Helmet>
      
            <h1 style={{ fontFamily: `${heading}` }}>
              Lorem impsum dolor sit amet ({heading})
            </h1>
      
            <p style={{ fontFamily: `${body}` }}>
              Consectetur adipiscing elit. Aliquam malesuada ex lectus, sit amet vulputate turpis malesuada vel. ({body})
            </p>
          </React.Fragment>
        )
      }
      

      Would that work for you?

      1. 2

        the font pairing will be done manually because I think random fonts won't always make visually pleasing results, and yes this is exactly what I was looking for thank sooooo much I'll start implementing it with react helmet and I'll hit you up on Twitter if I faced any difficulties if you don't mind.

        1. 1

          Sure thing, I’m glad this helped! ui.coach looks neat, looking forward to seeing this in action!