Capítulo 274 de 988

Chapter 274: Build a Google Font picker

Core Idea

To show various Google fonts in a font picker and load them once the user selects them, first install @remotion/google-fonts (at least v3. 3.

Key Concepts

  • Show all fonts in a font picker
  • Show only the 250 most popular Google Fonts
  • Show only the 100 most popular Google Fonts
  • Show only the 25 most popular Google Fonts
  • Loading fonts from the server

Code Examples

export const FontPicker: React.FC = () => {
  const newFonts = getAvailableFonts();

  const onChange = useCallback(
    async (e: React.ChangeEvent<HTMLSelectElement>) => {
      const fonts = newFonts[e.target.selectedIndex];

      // Load font information
      const loaded = await fonts.load();

      // Load the font itself
      const {fontFamily, ...otherInfo} = loaded.loadFont();

      // Or get metadata about the font
      const info = loaded.getInfo();
      const styles = Object.keys(info.fonts);
      console.log('Font', info.fontFamily, ' Styles', styles);
      for (const style of styles) {
        const weightObject = info.fonts[style as keyof typeof info.fonts];
        const weights = Object.keys(weightObject);
        console.log('- Style', style, 'supports weights', weights);
        for (const weight of weights) {
          const scripts = Object.keys(weightObject[weight]);
          console.log('-- Weight', weight, 'supports scripts', scripts);
        }
      }
    },
    [newFonts],
  );

  return (
    <div>
      <select onChange={onChange}>
        {newFonts.map((f) => {
          return (
            <option key={f.importName} value={f.importName}>
              {f.fontFamily}
            </option>
          );
        })}
      </select>
    </div>
  );
};
  • What it demonstrates: Usage pattern for Build a Google Font picker from the official docs.

Key Takeaways

  1. Understand show all fonts in a font picker when working with Build a Google Font picker.
  2. Understand show only the 250 most popular google fonts when working with Build a Google Font picker.
  3. Understand show only the 100 most popular google fonts when working with Build a Google Font picker.
  4. Understand show only the 25 most popular google fonts when working with Build a Google Font picker.
  5. Understand loading fonts from the server when working with Build a Google Font picker.

Connects To

  • Cors Issues: related page in the Troubleshooting & Errors section.
  • Enametoolong: related page in the Troubleshooting & Errors section.
  • Null: related page in the Troubleshooting & Errors section.