react-with-async-fonts
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
- Install
react-with-async-fonts
:
npm:
npm install --save react-with-async-fonts
yarn:
yarn add react-with-async-fonts
- Wrap your root component with
FontObserver
:
Set prop with font name. You can access it later in FontSubscriber
to check if
it's ready.
;;; ;
- 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.
; const Heading = <FontSubscriber> <h1 className=fontsopenSans ? 'opens-sans-font' : 'system-font'> children </h1> </FontSubscriber>; ;
API
FontObserver
component
;
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
;
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
;
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
:
;;;; ;
FontSubscriber
Basic with ;; const Heading = <FontSubscriber> <h1 className=fontsmontserrat && 'montserrat-font'>children</h1> </FontSubscriber>; ;
withFonts
Basic with You can use withFonts
HoC if you want to compose your component. Please note
it uses same FontSubscriber
under the hood.
;; const Heading = <h1 className=fontsmontserrat && 'montserrat-font'>children</h1>; Heading;
styled-components
With Most elegant way of using it with styled-components
is withFonts
HoC.
;; const Heading = styledh2` font-weight: 300; font-family: ;`; Heading;
FontObserver
Nested You can nest FontObserver
to merge fonts. Children instances overrides parent
if font with same code was defined.
; const Article = <FontObserver roboto="Roboto"> <FontObserver ptSans="PT Sans"> <FontSubscriber> <article> <h1 className=fontsroboto ? 'roboto' : 'sans-serif'>title</h1> <p className=fontsptSans ? 'ptsans' : 'serif'>children</p> </article> </FontSubscriber> </FontObserver> </FontObserver>; ;
fontfaceobserver
options
Custom You can provide text
and timeout
options for
fontfaceobserver
's .load
method with same props.
; const Heading = <FontObserver text=children timeout=2500 roboto="Roboto"> <FontSubscriber> <h1 className=fontsroboto && 'roboto'>children</h1> </FontSubscriber> </FontObserver>; ;
License
MIT © Sergey Bekrin