Web 组件。
Web API's turned into React Hooks and Suspense-friendly React components. #useThePlatform
Note: React 16.8+ is required for Hooks.
npm i the-platform --saveyarn add the-platformuseDeviceMotion()Detect and retrieve current device Motion.
import { useDeviceMotion } from 'the-platform';
const Example = () => {
const { acceleration, rotationRate, interval } = useDeviceMotion();
// ...
};useDeviceOrientation()Detect and retrieve current device orientation.
import { useDeviceOrientation } from 'the-platform';
const Example = () => {
const { alpha, beta, gamma, absolute } = useDeviceOrientation();
// ...
};useGeoPosition()Retrieve Geo position from the browser. This will throw a promise (must use with Suspense).
import { useGeoPosition } from 'the-platform';
const Example = () => {
const {
coords: { latitude, longitude },
} = useGeoPosition();
// ...
};useNetworkStatus()Retrieve network status from the browser.
Object containing:
isOnline: boolean: true if the browser has network access. false
otherwise.
offlineAt?: Date: Date when network connection was lost.
import { useNetworkStatus } from 'the-platform';
const Example = () => {
const { isOnline, offlineAt } = useNetworkStatus();
// ...
};useMedia()query: string | object: media query string or object (parsed by json2mq).
defaultMatches: boolean: a boolean providing a default value for matches
match: boolean: true if the media query matches, false otherwise.
import { useMedia } from 'the-platform';
const Example = () => {
const small = useMedia('(min-width: 400px)');
const medium = useMedia({ minWidth: 800 });
// ...
};useScript()This will throw a promise (must use with Suspense).
Object containing:
src: string: The script's URI.import { useScript } from 'the-platform';
const Example = () => {
const _unused = useScript({ src: 'bundle.js' });
// ...
};useStylesheet()This will throw a promise (must use with Suspense).
Object containing:
href: string: The stylesheet's URI.media?: string: Intended destination media for style information.import { useStylesheet } from 'the-platform';
const Example = () => {
const _unused = useStylesheet({ href: 'normalize.css' });
// ...
};useWindowScrollPosition()Object containing:
x: number: Horizontal scroll in pixels (window.pageXOffset).y: number: Vertical scroll in pixels (window.pageYOffset).import { useWindowScrollPosition } from 'the-platform';
const Example = () => {
const { x, y } = useWindowScrollPosition();
// ...
};useWindowSize()Object containing:
width: Width of browser viewport (window.innerWidth)height: Height of browser viewport (window.innerHeight)import { useWindowSize } from 'the-platform';
const Example = () => {
const { width, height } = useWindowSize();
// ...
};src: stringimport React from 'react';
import { Img } from 'the-platform';
function App() {
return (
);
}
export default App;<Script>src: stringchildren?: () => React.ReactNode - This render prop will only execute after the script has loaded.<script> tagimport React from 'react';
import { Script } from 'the-platform';
function App() {
return (
);
}
export default App;<Video>src: string<video> tagimport React from 'react';
import { Video } from 'the-platform';
function App() {
return (
);
}
export default App;<Audio>src: string<audio> tagimport React from 'react';
import { Audio } from 'the-platform';
function App() {
return (
);
}
export default App;<Preload>Preload a resource with <link rel="preload">. For more information check out MDN or the Google Developer Blog.
href: stringas: string - resource typeimport React from 'react';
import { Preload, Script } from 'the-platform';
function App() {
return (
);
}
export default App;<Stylesheet>Lazy load a stylesheet.
href: stringimport React from 'react';
import { Stylesheet } from 'the-platform';
function App() {
return (
);
}
export default App;MIT License
暂无开放 Issues,或尚未同步最近议题。