react-fns is a collection of imperative Browser API's turned into declarative React components and higher-order components for lots of common situations.
There's a lot more to do. The goal is to standardize almost every Web API on MDN.
Table of Contents
- API Reference
When possible, each component (e.g. <Thing/>
) in react-fns also exports a higher-order component with identical functionality (e.g. withThing()
.
Every render prop'd component shares the same three rendering methods:
<Thing render={props => <Inner />}>
<Thing component={Inner}>
<Thing>{props => <Inner />}</Thing>>
All HoC's will pass through any and all additional props through to the inner component in addition to the props that they inject.
Detect and retrieve current device Motion.
acceleration: DeviceMotionEvent.acceleration
accelerationIncludingGravity: DeviceMotionEvent.accelerationIncludingGravity
rotationRate: DeviceMotionEvent.rotationRate
interval: DeviceMotionEvent.interval
For more information about the DeviceMotion API, check out the MDN reference
import { DeviceMotion } from 'react-fns'
const Example = () =>
<DeviceMotion
render={({ acceleration, accelerationIncludingGravity, rotationRate, interval }) =>
<pre>
{JSON.stringify({acceleration, accelerationIncludingGravity, rotationRate, interval}, null, 2)}
</pre>
}
/>
export default Example
import { withDeviceMotion } from 'react-fns'
const Inner = ({ acceleration, accelerationIncludingGravity, rotationRate, interval }) =>
<pre>
{JSON.stringify({acceleration, accelerationIncludingGravity, rotationRate, interval}, null, 2)}
</pre>
export default withDeviceMotion(Inner)
Detect and retrieve current device orientation.
alpha: number
: value represents the motion of the device around the z axis, represented in degrees with values ranging from 0 to 360.beta: number
: value represents the motion of the device around the x axis, represented in degrees with values ranging from -180 to 180. This represents a front to back motion of the device.gamma: number
: value represents the motion of the device around the y axis, represented in degrees with values ranging from -90 to 90. This represents a left to right motion of the device.
For more information about the DeviceOrientation API, check out the MDN reference
import { DeviceOrientation } from 'react-fns'
const Example = () =>
<DeviceOrientation
render={({ alpha, beta, gamma, absolute }) =>
<pre>
{JSON.stringify({alpha, beta, gamma, absolute}, null, 2)}
</pre>
}
/>
export default Example
import { withDeviceOrientation } from 'react-fns'
const Inner = ({ alpha, beta, gamma, absolute }) =>
<pre>
{JSON.stringify({alpha, beta, gamma, absolute}, null, 2)}
</pre>
export default withDeviceOrientation(Inner)
Retrieve network access from the browser.
online: boolean
:true
if the browser has network access.false
otherwise.offlineAt?: Date
: Date when network connection was lost.
import { Network } from 'react-fns'
const Example = () =>
<Network
render={({ online, offlineAt }) =>
<div>
{online ? 'Online!' : 'Offline'}
{offlineAt && `Last connected ${offlineAt.toISOString()}`}
</div>
}
/>
export default Example
import { withNetwork } from 'react-fns'
const Inner = ({ online, offlineAt }) =>
<div>
{online ? 'Online!' : 'Offline'}
{offlineAt && `Last connected ${offlineAt.toISOString()}`}
</div>
export default withNetwork(Inner)
Retrieve Geo position from the browser.
isLoading: boolean
:true
request statuscoords?: Position
: Geoposition object. Has keys oflatitude
andlongitude
error?: PositionError
: GeoPosition error. See MDN for shape.
import { GeoPosition } from 'react-fns'
const Example = () =>
<GeoPosition
render={({ isLoading, coords, error }) =>
<div>
{coords && `${coords.longitude},${coords.latitude}`}
</div>
}
/>
export default Example
import { withGeoPosition } from 'react-fns'
const Inner = ({ isLoading, coords, error }) =>
<div>
{coords && `${coords.longitude},${coords.latitude}`}
</div>
export default withGeoPosition(Inner)
Retrieve media query (i.e. window.matchMedia().matches
) from the browser. Note this component is taken from @mjackson's awesome react-media
query: string
: A media query string
matches: boolean
:true
if browser matches the media query
import { Media } from 'react-fns'
const Example = () =>
<Media
query="(min-width: 1000px)"
render={(match) =>
<div>
{match ? 'mobile' : 'desktop'}
</div>
}
/>
export default Example
Not implemented
x
: Horizontal scroll in pixels (window.scrollX
)y
: Vertical scroll in pixels (window.scrollX
)
Returns window.scrollY
and window.scrollX
.
import { Scroll } from 'react-fns'
const Example = () =>
<Scroll
render={({ x, y }) =>
<div>Scroll: {x}, {y}</div>
}
/>
export default Example
Injects window.scrollY
and window.scrollX
as x
and y
props.
import { withScroll } from 'react-fns'
const Inner = ({ x, y }) => <div>Scroll Position: {x}, {y}</div>
export default withScroll(Inner)
width
: Width of browser viewport (window.innerWidth
)height
: Height of browser viewport (window.innerHeight
)
Returns window.innerWidth
and window.innerHeight
.
import { WindowSize } from 'react-fns'
const Example = () =>
<WindowSize
render={({ width, height }) =>
<div>Window size: {width}, {height}</div>
}
/>
export default Example
Injects window.innerWidth
and window.innerHeight
as width
and height
props.
import { withWindowSize } from 'react-fns'
const Inner = ({ width, height }) => <div>Window size: {width}, {height}</div>
export default withWindowSize(Inner)
locales
: The current browser locales (navigator.languages
ornavigator.language
)
Returns canonical navigator.languages
or navigator.language
as locales
.
import { Locales } from 'react-fns'
const Example = () =>
<Locales
render={({ locales }) =>
<span>Right now the time and date is {new Intl.DateTimeFormat(locales).format(new Date())}</span>
}
/>
export default Example
Injects canonical navigator.languages
or navigator.language
as locales
prop.
import { withLocales } from 'react-fns'
const Inner = ({ locales }) => <span>Right now the time and date is {new Intl.DateTimeFormat(locales).format(new
export default withLocales(Inner)
Renders <a href="mailto:..." />
email: string
: Recipient's email addresssubject?: string
: Subject of the emailcc?: string | string[]
: Email address or an array of email addresses to Ccbcc?: string | string[]
: Email address or an array of email addresses to Bcc (blind copy)body?: string
: Body copy of the email
Renders <a href="sms:..." />
phone: string
: Phone numberbody?: string
: Body copy of the text message
- Fetch
- Draggable
- Droppable
- Orderable
- InfiniteList
- Parallax
- Pin (to Top / to Bottom)
See https://developer.mozilla.org/en-US/docs/WebAPI for the full list
- MousePosition
- Viewport
- IntersectionObserver
- Other implementations
- Audio
- MidiAccess
- Battery Status
- Vibration
- Camera
- Measure
- Jared Palmer @jaredpalmer
Thanks goes to these wonderful people (emoji key):
MICHAEL JACKSON 🤔 |
Pavel Prichodko 💻 📖 |
Richard Powell 💻 |
Tim Brown 📖 |
Jack Moore 💻 |
Dayle Rees 📖 |
Thomas Flemming 📖 |
---|---|---|---|---|---|---|
Sam Kvale 🐛 💻 |
Rhys Powell 💻 |
Jeppe Reinhold 📖 |
Macklin Underdown 📖 |
This project follows the all-contributors specification. Contributions of any kind welcome!