{"id":13481569,"url":"https://github.com/sbekrin/react-with-async-fonts","last_synced_at":"2025-04-06T15:32:33.042Z","repository":{"id":57280855,"uuid":"89772043","full_name":"sbekrin/react-with-async-fonts","owner":"sbekrin","description":"🔠 React module for working with custom web fonts","archived":false,"fork":false,"pushed_at":"2019-05-28T20:21:44.000Z","size":697,"stargazers_count":22,"open_issues_count":6,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-05-21T00:59:11.101Z","etag":null,"topics":["fontfaceobserver","fonts","react","react-hoc","render-prop","web-fonts","with-async-fonts"],"latest_commit_sha":null,"homepage":"https://npmjs.com/react-with-async-fonts","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sbekrin.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-29T08:03:11.000Z","updated_at":"2022-07-19T12:03:56.000Z","dependencies_parsed_at":"2022-08-28T03:01:44.001Z","dependency_job_id":null,"html_url":"https://github.com/sbekrin/react-with-async-fonts","commit_stats":null,"previous_names":["sergeybekrin/react-with-async-fonts"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sbekrin%2Freact-with-async-fonts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sbekrin%2Freact-with-async-fonts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sbekrin%2Freact-with-async-fonts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sbekrin%2Freact-with-async-fonts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sbekrin","download_url":"https://codeload.github.com/sbekrin/react-with-async-fonts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247503078,"owners_count":20949377,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fontfaceobserver","fonts","react","react-hoc","render-prop","web-fonts","with-async-fonts"],"created_at":"2024-07-31T17:00:52.916Z","updated_at":"2025-04-06T15:32:32.780Z","avatar_url":"https://github.com/sbekrin.png","language":"TypeScript","funding_links":[],"categories":["Components"],"sub_categories":["Media"],"readme":"# react-with-async-fonts\n\n[![npm Version](https://img.shields.io/npm/v/react-with-async-fonts.svg?maxAge=0)](https://www.npmjs.com/package/react-with-async-fonts)\n[![Build Status](https://img.shields.io/travis/sbekrin/react-with-async-fonts.svg?maxAge=0)](https://travis-ci.org/sbekrin/react-with-async-fonts)\n[![Coverage Status](https://img.shields.io/coveralls/sbekrin/react-with-async-fonts.svg?maxAge=0)](https://coveralls.io/github/sbekrin/react-with-async-fonts?branch=master)\n[![Greenkeeper badge](https://badges.greenkeeper.io/sbekrin/react-with-async-fonts.svg)](https://greenkeeper.io/)\n\nReact module for working with async loaded custom web fonts, based on [`fontfaceobserver`](https://fontfaceobserver.com/).\n\n\u003e Note: version 4.x introduces breaking changes with new API. It addresses bunch\n\u003e of issues, including canceling promises, better performance, and TS typings.\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n\n\n- [Quick Start](#quick-start)\n- [API](#api)\n  - [`FontObserver` component](#fontobserver-component)\n  - [`FontSubscriber` component](#fontsubscriber-component)\n  - [`withFonts` HoC](#withfonts-hoc)\n  - [`Font` type](#font-type)\n- [Examples](#examples)\n  - [Basic with `FontSubscriber`](#basic-with-fontsubscriber)\n  - [Basic with `withFonts`](#basic-with-withfonts)\n  - [With `styled-components`](#with-styled-components)\n  - [Nested `FontObserver`](#nested-fontobserver)\n  - [Custom `fontfaceobserver` options](#custom-fontfaceobserver-options)\n- [License](#license)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n## Quick Start\n\n1. Install `react-with-async-fonts`:\n\nnpm:\n\n```bash\nnpm install --save react-with-async-fonts\n```\n\nyarn:\n\n```bash\nyarn add react-with-async-fonts\n```\n\n2. Wrap your root component with `FontObserver`:\n\nSet prop with font name. You can access it later in `FontSubscriber` to check if\nit's ready.\n\n```js\nimport { FontObserver } from 'react-with-async-fonts';\nimport { render } from 'react-dom';\nimport App from './app';\n\nrender(\n  \u003cFontObserver openSans=\"Open Sans\"\u003e\n    \u003cApp /\u003e\n  \u003c/FontObserver\u003e,\n  document.getElementById('root'),\n);\n```\n\n3. Wrap your target with `FontSubscriber` component:\n\n\u003e Tip: you can also use [`withFonts` API](#withFonts) if you're really into\n\u003e HoCs.\n\nNote that `FontSubscriber` uses children render prop. Provided function would be\ncalled with single argument which is an object with loaded font keys.\n\n```js\nimport { FontSubscriber } from 'react-with-async-fonts';\n\nconst Heading = ({ children }) =\u003e (\n  \u003cFontSubscriber\u003e\n    {fonts =\u003e (\n      \u003ch1 className={fonts.openSans ? 'opens-sans-font' : 'system-font'}\u003e\n        {children}\n      \u003c/h1\u003e\n    )}\n  \u003c/FontSubscriber\u003e\n);\n\nexport default Heading;\n```\n\n## API\n\n### `FontObserver` component\n\n```js\nimport { FontObserver } from 'react-with-async-fonts';\n```\n\n| Prop      | Type            | Description                                          |\n| --------- | --------------- | ---------------------------------------------------- |\n| `text`    | `string`        | `fontfaceobserver`'s `.load` text options            |\n| `timeout` | `number`        | `fontfaceobserver`'s `.load` timeout options         |\n| `[key]`   | `Font \\| string` | Font family string or a [`Font` object](#font-type). |\n\n### `FontSubscriber` component\n\n```js\nimport { FontSubscriber } from 'react-with-async-fonts';\n```\n\n| Prop       | Type                                    | Description                                                                                                                  |\n| ---------- | --------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |\n| `children` | `(fonts: Object) =\u003e React.Element\u003cany\u003e` | Children render function. Accepts object with loaded font. Once ready, it would contain object of [`Font` type](#font-type). |\n\n### `withFonts` HoC\n\n```js\nimport { withFonts } from 'react-with-async-fonts';\n```\n\n| Argument  | Type                       | Description                                         |\n| --------- | -------------------------- | --------------------------------------------------- |\n| component | `React.ComponentType\u003cany\u003e` | Component to wrap with HoC. Injects `fonts` object. |\n\n### `Font` type\n\n```js\ntype Font = {\n  family: String,\n  weight?:\n    | 'normal'\n    | 'bold'\n    | 'bolder'\n    | 'lighter'\n    | '100'\n    | '200'\n    | '300'\n    | '400'\n    | '500'\n    | '600'\n    | '700'\n    | '800'\n    | '900',\n  style?: 'normal' | 'italic' | 'oblique',\n  stretch?:\n    | 'normal'\n    | 'ultra-condensed'\n    | 'extra-condensed'\n    | 'condensed'\n    | 'semi-condensed'\n    | 'semi-expanded'\n    | 'expanded'\n    | 'extra-expanded'\n    | 'ultra-expanded',\n};\n```\n\n## Examples\n\nHeads up! Each example requires wrapping your app with\n[`FontObserver`](#fontobserver-component):\n\n```js\nimport React from 'react';\nimport { render } from 'react-dom';\nimport { FontObserver } from 'react-with-async-fonts';\nimport App from './app';\n\nrender(\n  \u003cFontObserver montserrat=\"Montserrat\"\u003e\n    \u003cApp /\u003e\n  \u003c/FontObserver\u003e,\n  document.getElementById('root'),\n);\n```\n\n### Basic with `FontSubscriber`\n\n```js\nimport React from 'react';\nimport { FontSubscriber } from 'react-with-async-fonts';\n\nconst Heading = ({ children }) =\u003e (\n  \u003cFontSubscriber\u003e\n    {fonts =\u003e (\n      \u003ch1 className={fonts.montserrat \u0026\u0026 'montserrat-font'}\u003e{children}\u003c/h1\u003e\n    )}\n  \u003c/FontSubscriber\u003e\n);\n\nexport default Heading;\n```\n\n### Basic with `withFonts`\n\nYou can use `withFonts` HoC if you want to compose your component. Please note\nit uses same `FontSubscriber` under the hood.\n\n```js\nimport React from 'react';\nimport { withFonts } from 'react-with-async-fonts';\n\nconst Heading = ({ children, fonts }) =\u003e (\n  \u003ch1 className={fonts.montserrat \u0026\u0026 'montserrat-font'}\u003e{children}\u003c/h1\u003e\n);\n\nexport default withFonts(Heading);\n```\n\n### With `styled-components`\n\nMost elegant way of using it with `styled-components` is `withFonts` HoC.\n\n```js\nimport styled from 'styled-components';\nimport { withFonts } from 'react-with-async-fonts';\n\nconst Heading = styled.h2`\n  font-weight: 300;\n  font-family: ${props =\u003e\n    props.fonts.montserrat\n      ? '\"Open Sans\", sans-serif'\n      : 'Helvetica, sans-serif'};\n`;\n\nexport default withFonts(Heading);\n```\n\n### Nested `FontObserver`\n\nYou can nest `FontObserver` to merge fonts. Children instances overrides parent\nif font with same code was defined.\n\n```js\nimport { FontObserver, FontSubscriber } from 'react-with-async-fonts';\n\nconst Article = ({ title, children }) =\u003e (\n  \u003cFontObserver roboto=\"Roboto\"\u003e\n    \u003cFontObserver ptSans=\"PT Sans\"\u003e\n      \u003cFontSubscriber\u003e\n        {fonts =\u003e (\n          \u003carticle\u003e\n            \u003ch1 className={fonts.roboto ? 'roboto' : 'sans-serif'}\u003e{title}\u003c/h1\u003e\n            \u003cp className={fonts.ptSans ? 'ptsans' : 'serif'}\u003e{children}\u003c/p\u003e\n          \u003c/article\u003e\n        )}\n      \u003c/FontSubscriber\u003e\n    \u003c/FontObserver\u003e\n  \u003c/FontObserver\u003e\n);\n\nexport default Article;\n```\n\n### Custom `fontfaceobserver` options\n\nYou can provide `text` and `timeout` options for\n[`fontfaceobserver`'s .load](https://github.com/bramstein/fontfaceobserver#how-to-use)\nmethod with same props.\n\n```js\nimport { FontObserver, FontSubscriber } from 'react-with-async-fonts';\n\nconst Heading = ({ children }) =\u003e (\n  \u003cFontObserver text={children} timeout={2500} roboto=\"Roboto\"\u003e\n    \u003cFontSubscriber\u003e\n      {fonts =\u003e \u003ch1 className={fonts.roboto \u0026\u0026 'roboto'}\u003e{children}\u003c/h1\u003e}\n    \u003c/FontSubscriber\u003e\n  \u003c/FontObserver\u003e\n);\n\nexport default Heading;\n```\n\n## License\n\nMIT \u0026copy; [Sergey Bekrin](http://bekrin.me)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsbekrin%2Freact-with-async-fonts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsbekrin%2Freact-with-async-fonts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsbekrin%2Freact-with-async-fonts/lists"}