All Projects β†’ sbekrin β†’ react-with-async-fonts

sbekrin / react-with-async-fonts

Licence: MIT license
πŸ”  React module for working with custom web fonts

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to react-with-async-fonts

fonts
Web fonts that you probably won't find in a CDN
Stars: ✭ 26 (+18.18%)
Mutual labels:  fonts, web-fonts
Fontfaceobserver
Webfont loading. Simple, small, and efficient.
Stars: ✭ 3,948 (+17845.45%)
Mutual labels:  web-fonts, fontfaceobserver
Webfont
Awesome generator of webfont
Stars: ✭ 170 (+672.73%)
Mutual labels:  fonts, web-fonts
typecatcher
Download Google webfonts on the Linux desktop
Stars: ✭ 73 (+231.82%)
Mutual labels:  fonts, web-fonts
Powerline Web Fonts
Powerline Web Fonts for Chromebook
Stars: ✭ 178 (+709.09%)
Mutual labels:  fonts, web-fonts
Woffle
Drag and drop woff and woff2 web font generator for macOS
Stars: ✭ 24 (+9.09%)
Mutual labels:  web-fonts
fdiff
An OpenType table diff tool for fonts. Based on the fontTools TTX format.
Stars: ✭ 33 (+50%)
Mutual labels:  fonts
smufl
Standard Music Font Layout
Stars: ✭ 41 (+86.36%)
Mutual labels:  fonts
Tehreer-Android
Standalone text engine for Android aimed to be free from platform limitations
Stars: ✭ 61 (+177.27%)
Mutual labels:  fonts
figma-linux-font-helper
Figma Linux Font Helper
Stars: ✭ 110 (+400%)
Mutual labels:  fonts
wh40k-icon
Warhammer 40000 icons.
Stars: ✭ 38 (+72.73%)
Mutual labels:  web-fonts
text-style-editor
Text style editor widget for flutter
Stars: ✭ 25 (+13.64%)
Mutual labels:  fonts
awesome-design
A collection of open resources for web designers
Stars: ✭ 87 (+295.45%)
Mutual labels:  fonts
apple-emoji-linux
Apple Color Emoji for Linux
Stars: ✭ 392 (+1681.82%)
Mutual labels:  fonts
typex
Typography web export (css, sass, html, json, ..) plugin for Sketch
Stars: ✭ 42 (+90.91%)
Mutual labels:  fonts
react-callbag-listener
πŸ‘‚ A React render-prop component that listens to values emitted by callbags
Stars: ✭ 21 (-4.55%)
Mutual labels:  render-prop
ark-pixel-font
Open source Pan-CJK pixel font / εΌ€ζΊηš„ζ³›δΈ­ζ—₯ιŸ©εƒη΄ ε­—δ½“
Stars: ✭ 1,767 (+7931.82%)
Mutual labels:  fonts
seed-fonts
WordPress plugin that helps you use web fonts (@font-face) easier. You can use 5 ready-generated Thai-English web fonts, or use your own collection.
Stars: ✭ 20 (-9.09%)
Mutual labels:  fonts
glyphhanger
Your web font utility belt. It can subset web fonts. It can find unicode-ranges for you automatically. It makes julienne fries.
Stars: ✭ 422 (+1818.18%)
Mutual labels:  web-fonts
gatsby-omni-font-loader
Font loader optimized for maximum performance. Removes render-blocking font resources and loads them asynchronusly. Handle FOUT & FOUC with font loading status watcher. Supports both local-hosted fonts and web fonts.
Stars: ✭ 98 (+345.45%)
Mutual labels:  web-fonts

react-with-async-fonts

npm Version Build Status Coverage Status Greenkeeper badge

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].