npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-with-async-fonts

v4.0.3

Published

React HoC to handle async loaded fonts

Downloads

159

Readme

react-with-async-fonts

npm Version Build Status Coverage Status dependencies Status devDependencies Status

React module for working with async loaded custom web fonts, based on fontfaceobserver.

Note: version 4.x introduces breaking changes with new API. It addresses bunch of issues, including canceling promises, better performance, and TS typings.

Quick Start

  1. Install react-with-async-fonts:

npm:

npm install --save react-with-async-fonts

yarn:

yarn add react-with-async-fonts
  1. Wrap your root component with FontObserver:

Set prop with font name. You can access it later in FontSubscriber to check if it's ready.

import { FontObserver } from 'react-with-async-fonts';
import { render } from 'react-dom';
import App from './app';

render(
  <FontObserver openSans="Open Sans">
    <App />
  </FontObserver>,
  document.getElementById('root'),
);
  1. Wrap your target with FontSubscriber component:

Tip: you can also use withFonts API if you're really into HoCs.

Note that FontSubscriber uses children render prop. Provided function would be called with single argument which is an object with loaded font keys.

import { FontSubscriber } from 'react-with-async-fonts';

const Heading = ({ children }) => (
  <FontSubscriber>
    {fonts => (
      <h1 className={fonts.openSans ? 'opens-sans-font' : 'system-font'}>
        {children}
      </h1>
    )}
  </FontSubscriber>
);

export default Heading;

API

FontObserver component

import { FontObserver } from 'react-with-async-fonts';

| Prop | Type | Description | | --------- | --------------- | ---------------------------------------------------- | | text | string | fontfaceobserver's .load text options | | timeout | number | fontfaceobserver's .load timeout options | | [key] | Font \| string | Font family string or a Font object. |

FontSubscriber component

import { FontSubscriber } from 'react-with-async-fonts';

| Prop | Type | Description | | ---------- | --------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- | | children | (fonts: Object) => React.Element<any> | Children render function. Accepts object with loaded font. Once ready, it would contain object of Font type. |

withFonts HoC

import { withFonts } from 'react-with-async-fonts';

| Argument | Type | Description | | --------- | -------------------------- | --------------------------------------------------- | | component | React.ComponentType<any> | Component to wrap with HoC. Injects fonts object. |

Font type

type Font = {
  family: String,
  weight?:
    | 'normal'
    | 'bold'
    | 'bolder'
    | 'lighter'
    | '100'
    | '200'
    | '300'
    | '400'
    | '500'
    | '600'
    | '700'
    | '800'
    | '900',
  style?: 'normal' | 'italic' | 'oblique',
  stretch?:
    | 'normal'
    | 'ultra-condensed'
    | 'extra-condensed'
    | 'condensed'
    | 'semi-condensed'
    | 'semi-expanded'
    | 'expanded'
    | 'extra-expanded'
    | 'ultra-expanded',
};

Examples

Heads up! Each example requires wrapping your app with FontObserver:

import React from 'react';
import { render } from 'react-dom';
import { FontObserver } from 'react-with-async-fonts';
import App from './app';

render(
  <FontObserver montserrat="Montserrat">
    <App />
  </FontObserver>,
  document.getElementById('root'),
);

Basic with FontSubscriber

import React from 'react';
import { FontSubscriber } from 'react-with-async-fonts';

const Heading = ({ children }) => (
  <FontSubscriber>
    {fonts => (
      <h1 className={fonts.montserrat && 'montserrat-font'}>{children}</h1>
    )}
  </FontSubscriber>
);

export default Heading;

Basic with withFonts

You can use withFonts HoC if you want to compose your component. Please note it uses same FontSubscriber under the hood.

import React from 'react';
import { withFonts } from 'react-with-async-fonts';

const Heading = ({ children, fonts }) => (
  <h1 className={fonts.montserrat && 'montserrat-font'}>{children}</h1>
);

export default withFonts(Heading);

With styled-components

Most elegant way of using it with styled-components is withFonts HoC.

import styled from 'styled-components';
import { withFonts } from 'react-with-async-fonts';

const Heading = styled.h2`
  font-weight: 300;
  font-family: ${props =>
    props.fonts.montserrat
      ? '"Open Sans", sans-serif'
      : 'Helvetica, sans-serif'};
`;

export default withFonts(Heading);

Nested FontObserver

You can nest FontObserver to merge fonts. Children instances overrides parent if font with same code was defined.

import { FontObserver, FontSubscriber } from 'react-with-async-fonts';

const Article = ({ title, children }) => (
  <FontObserver roboto="Roboto">
    <FontObserver ptSans="PT Sans">
      <FontSubscriber>
        {fonts => (
          <article>
            <h1 className={fonts.roboto ? 'roboto' : 'sans-serif'}>{title}</h1>
            <p className={fonts.ptSans ? 'ptsans' : 'serif'}>{children}</p>
          </article>
        )}
      </FontSubscriber>
    </FontObserver>
  </FontObserver>
);

export default Article;

Custom fontfaceobserver options

You can provide text and timeout options for fontfaceobserver's .load method with same props.

import { FontObserver, FontSubscriber } from 'react-with-async-fonts';

const Heading = ({ children }) => (
  <FontObserver text={children} timeout={2500} roboto="Roboto">
    <FontSubscriber>
      {fonts => <h1 className={fonts.roboto && 'roboto'}>{children}</h1>}
    </FontSubscriber>
  </FontObserver>
);

export default Heading;

License

MIT © Sergey Bekrin