fast-react/packages/react/fast-react-utilities
Chris Holt a508de4e24 Publish
- @microsoft/fast-components-class-name-contracts-base@4.6.5
 - @microsoft/fast-components-class-name-contracts-msft@4.8.5
 - @microsoft/fast-components-foundation-react@3.1.9
 - @microsoft/fast-components-react-base@4.25.11
 - @microsoft/fast-components-react-msft@4.30.11
 - @microsoft/fast-components-styles-msft@4.28.10
 - @microsoft/fast-jss-manager-react@4.7.3
 - @microsoft/fast-jss-manager@4.1.17
 - @microsoft/fast-jss-utilities@4.7.13
 - @microsoft/fast-layouts-react@4.11.10
 - @microsoft/fast-react-utilities@1.1.6
 - @microsoft/fast-storybook-design-system-addon@1.2.35
 - @microsoft/fast-storybook-presets@1.2.3
 - @microsoft/fast-figma-plugin-msft@0.6.3
 - @microsoft/fast-tooling-react@2.1.0
 - @microsoft/fast-tooling@0.4.0
 - @microsoft/fast-animation@4.0.13
 - @microsoft/fast-colors@5.0.9
 - @microsoft/eslint-config-fast-dna@1.1.2
 - @microsoft/fast-web-utilities@4.5.0
 - @microsoft/fast-components-msft@1.0.0
 - @microsoft/fast-components@1.0.0
 - @microsoft/fast-element@0.9.0
 - @microsoft/fast-foundation@1.0.0
 - @microsoft/fast-color-explorer@1.5.30
 - @microsoft/fast-component-explorer@0.8.21
 - @microsoft/fast-creator@0.0.3
 - @microsoft/fast-tooling-examples@0.1.0
 - @microsoft/site-utilities@0.0.2
2020-05-18 14:50:48 -07:00
..
src chore: reorganize repository structure and workspaces (#2927) 2020-04-14 14:48:12 -07:00
.eslintignore chore: reorganize repository structure and workspaces (#2927) 2020-04-14 14:48:12 -07:00
.eslintrc.js chore: reorganize repository structure and workspaces (#2927) 2020-04-14 14:48:12 -07:00
.npmignore chore: reorganize repository structure and workspaces (#2927) 2020-04-14 14:48:12 -07:00
.npmrc chore: reorganize repository structure and workspaces (#2927) 2020-04-14 14:48:12 -07:00
.prettierignore chore: reorganize repository structure and workspaces (#2927) 2020-04-14 14:48:12 -07:00
CHANGELOG.md Publish 2020-05-18 14:50:48 -07:00
README.md chore: add git attributes and normalize line endings (#3072) 2020-05-06 17:03:18 -07:00
babel.config.js chore: reorganize repository structure and workspaces (#2927) 2020-04-14 14:48:12 -07:00
package.json Publish 2020-05-18 14:50:48 -07:00
tsconfig.build.json chore: reorganize repository structure and workspaces (#2927) 2020-04-14 14:48:12 -07:00
tsconfig.json chore: reorganize repository structure and workspaces (#2927) 2020-04-14 14:48:12 -07:00

README.md

FAST React Utilities

A set of general purpose React utilities.

Installation

npm i --save @microsoft/fast-react-utilities

Exports

Hooks

useTimeout(callback: () => void, delay: number | null, dependencies?: any[]): void

A React hook to declaratively invoke a timeout function. The callback be invoked once after delay - measured in milliseconds. Once the timeout is invoked, no other timeout will be registered unless duration changes.

To force a new timeout to be registered with a previous duration, supply new values to the dependencies. This is similar to how React's useEffect works.

Single execution of callback
function FancyButton() {
    // Execute a callback invoked after 200ms. Callback will only be called once (unless duration changes)
    useTimeout(() => {
        alert("I'm a button")
    }, 200);

    return <button>hello world</button>
}
Execute callback whenever prop is changed
function FancyButton() {
    // Execute a callback 200ms after render. A new callback will be registered
    // when props.value changes
    useTimeout((props) => {
        alert("I'm a button")
    }, 200, [props.value]);

    return <button>hello world</button>
}
Execute callback every render
import { useTransitionState, TransitionStates } from "@microsoft/fast-react-utilities";

function FancyButton() {
    // Execute a callback 200ms after every render. If render happens before the delay,
    // the previous render's timeout will be canceled.
    useTimeout(() => {
        alert("I'm a button")
    }, 200, [Symbol()]);

    return <button>hello world</button>
}

useTransitionState(active: boolean, duration: number | [number, number]): TransitionStates

A React hook used to track the state of a transition based on initial and incoming active values. useTransitionState will return a value from the TransitionStates object, representing "inactive", "active", "activating", and "deactivating". When the incoming active argument changes from one render to the next, the return value will change to one of the middle states, "activating" (false->true) or "deactivating" (true->false). After the supplied duration, the hook will invoke a setState and return the state represented by the incoming active value.

When two duration values are provided, the first will be used when activating and the second will be used when deactivating.

import { classNames } from "@microsoft/fast-web-utilities";
import { useTransitionState, TransitionStates } from "@microsoft/fast-react-utilities";

function FancyDialog(props) {
    const state = useTransitionState(props.visible, [300, 400]);

    return (
        <div
            role="dialog"
            className={classNames(
                "dialog",
                ["dialog_inactive", state === TransitionStates.inactive],
                ["dialog_active", state === TransitionStates.active],
                ["dialog_activating", state === TransitionStates.activating],
                ["dialog_deactivating", state === TransitionStates.deactivating]
            )}
            children={props.children}
        />
    );
}