Skip to content

Commit 4f04a8e

Browse files
docs(hooks): Add basic docs and examples for the new hooks api
1 parent 5e7d224 commit 4f04a8e

12 files changed

+209
-29
lines changed

src/hooks/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/**
2+
* ## UI-Router React Hooks
3+
*
4+
* This is a collection of [react hooks](https://reactjs.org/docs/hooks-intro.html)
5+
* that can be used in functional components.
6+
*
7+
* @packageDocumentation @preferred @module react_hooks
8+
*/
19
export { useCurrentStateAndParams } from './useCurrentStateAndParams';
210
export { useOnStateChanged } from './useOnStateChanged';
311
export { useRouter } from './useRouter';

src/hooks/useCurrentStateAndParams.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1+
/** @packageDocumentation @reactapi @module react_hooks */
12
import { useState } from 'react';
23
import { RawParams, StateDeclaration } from '@uirouter/core';
34
import { useOnStateChanged } from './useOnStateChanged';
45
import { useRouter } from './useRouter';
56

67
/**
7-
* Returns the current state and parameter values.
8+
* A hook that returns the current state and parameter values.
89
*
910
* Each time the current state or parameter values change, the component will re-render with the new values.
11+
*
12+
* Example:
13+
* ```jsx
14+
* function CurrentState() {
15+
* const { state, params } = useCurrentStateAndParams();
16+
* return <span>{state.name} ({JSON.stringify(params)})</span>;
17+
* }
18+
* ```
1019
*/
1120
export function useCurrentStateAndParams(): { state: StateDeclaration; params: RawParams } {
1221
const globals = useRouter().globals;

src/hooks/useDeepObjectDiff.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** @packageDocumentation @internalapi @module react_hooks */
12
import { equals } from '@uirouter/core';
23
import { useRef } from 'react';
34

src/hooks/useIsActive.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/** @packageDocumentation @reactapi @module react_hooks */
2+
13
import { StateDeclaration } from '@uirouter/core';
24
import { useEffect, useMemo, useState } from 'react';
35
import { UIRouterReact } from '../core';
@@ -6,6 +8,7 @@ import { useOnStateChanged } from './useOnStateChanged';
68
import { useRouter } from './useRouter';
79
import { useViewContextState } from './useViewContextState';
810

11+
/** @hidden */
912
function checkIfActive(
1013
router: UIRouterReact,
1114
stateName: string,
@@ -18,6 +21,32 @@ function checkIfActive(
1821
: router.stateService.includes(stateName, params, { relative });
1922
}
2023

24+
/**
25+
* A hook that returns true if a given state is active.
26+
*
27+
* Example:
28+
* ```jsx
29+
* function ContactsLabel() {
30+
* const isActive = useIsActive('contacts');
31+
* return <span className={isActive ? 'active' : 'inactive'}>Contacts></span>
32+
* }
33+
* ```
34+
*
35+
* Example:
36+
* ```jsx
37+
* function JoeLabel() {
38+
* const isActive = useIsActive('contacts.contact', { contactId: 'joe' });
39+
* return <span className={isActive ? 'active' : 'inactive'}>Joe></span>
40+
* }
41+
* ```
42+
*
43+
* @param stateName the name of the state to check.
44+
* Relative state names such as '.child' are supported.
45+
* Relative states are resolved relative to the state that rendered the hook.
46+
* @param params if present, the hook will only return true if all the provided parameter values match.
47+
* @param exact when true, the hook returns true only when the state matches exactly.
48+
* when false, returns true if the state matches, or any child state matches.
49+
*/
2150
export function useIsActive(stateName: string, params = null, exact = false) {
2251
const router = useRouter();
2352
const relative = useViewContextState(router);

src/hooks/useOnStateChanged.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
1+
/** @packageDocumentation @reactapi @module react_hooks */
2+
13
import { RawParams, StateDeclaration } from '@uirouter/core';
24
import { useTransitionHook } from './useTransitionHook';
35

6+
/**
7+
* A hook that invokes the provided callback whenever the current state changes.
8+
*
9+
* The callback receives the [[StateDeclaration]] and parameter values of the new current state.
10+
*
11+
* Example:
12+
* ```jsx
13+
* function ShowCurrentState() {
14+
* const [routerState, setRouterState] = useState('');
15+
* useOnStateChanged((state) => setState(state.name);
16+
* return <span>{routerState ? `state changed to ${routerState}` : null}</span>
17+
* }
18+
* ```
19+
*
20+
* @param onStateChangedCallback a callback that receives the new current state and parameter values
21+
*/
422
export function useOnStateChanged(onStateChangedCallback: (state: StateDeclaration, params: RawParams) => void) {
523
useTransitionHook('onSuccess', {}, trans => onStateChangedCallback(trans.to(), trans.params('to')));
624
}

src/hooks/useRouter.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
1+
/** @packageDocumentation @reactapi @module react_hooks */
2+
13
import { useContext } from 'react';
24
import { UIRouterReact } from '../core';
35
import { UIRouterContext } from '../components/UIRouter';
46

57
/** @hidden */
68
export const UIRouterInstanceUndefinedError = `UIRouter instance is undefined. Did you forget to include the <UIRouter> as root component?`;
79

8-
/** Returns the UIRouter object from React Context */
10+
/**
11+
* A hook that returns the UIRouter instance
12+
*
13+
* Example:
14+
* ```jsx
15+
* const FormSubmit() {
16+
* const router = useRouter();
17+
* const form = useContext(FormFromContext);
18+
* function submit() {
19+
* validateForm(form)
20+
* .then(submitForm)
21+
* .then(() => router.stateService.go('home'));
22+
* }
23+
*
24+
* return <button onClick={submit}>Submit form</button>;
25+
* }
26+
* ```
27+
*/
928
export function useRouter(): UIRouterReact {
1029
const router = useContext(UIRouterContext);
1130
if (!router) {

src/hooks/useSref.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/** @packageDocumentation @reactapi @module react_hooks */
2+
13
import * as React from 'react';
24
import { useCallback, useContext, useEffect, useMemo, useState } from 'react';
35
import { isString, StateDeclaration, TransitionOptions } from '@uirouter/core';
@@ -15,15 +17,15 @@ export interface LinkProps {
1517
/** @hidden */
1618
export const IncorrectStateNameTypeError = `The state name passed to useSref must be a string.`;
1719

18-
/** Gets all StateDeclarations that are registered in the StateRegistry. */
20+
/** @hidden Gets all StateDeclarations that are registered in the StateRegistry. */
1921
function useListOfAllStates(router: UIRouterReact) {
2022
const initial = useMemo(() => router.stateRegistry.get(), []);
2123
const [states, setStates] = useState(initial);
2224
useEffect(() => router.stateRegistry.onStatesChanged(() => setStates(router.stateRegistry.get())), []);
2325
return states;
2426
}
2527

26-
/** Gets the StateDeclaration that this sref targets */
28+
/** @hidden Gets the StateDeclaration that this sref targets */
2729
function useTargetState(router: UIRouterReact, stateName: string, context: StateDeclaration): StateDeclaration {
2830
// Whenever any states are added/removed from the registry, get the target state again
2931
const allStates = useListOfAllStates(router);
@@ -33,13 +35,35 @@ function useTargetState(router: UIRouterReact, stateName: string, context: State
3335
}
3436

3537
/**
36-
* A hook that helps create link to a state.
38+
* A hook to create a link to a state.
39+
*
40+
* This hook returns link (anchor tag) props for a given state reference.
41+
* The resulting props can be spread onto an anchor tag.
42+
*
43+
* The props returned from this hook are:
44+
*
45+
* - `href`: the browser URL of the referenced state
46+
* - `onClick`: a mouse event handler that will active the referenced state
47+
*
48+
* Example:
49+
* ```jsx
50+
* function HomeLink() {
51+
* const sref = useSref('home');
52+
* return <a {...sref}>Home</a>
53+
* }
54+
* ```
3755
*
38-
* This hook returns data for an sref (short for state reference)
56+
* Example:
57+
* ```jsx
58+
* function UserLink({ userId, username }) {
59+
* const sref = useSref('users.user', { userId: userId });
60+
* return <a {...sref}>{username}</a>
61+
* }
62+
* ```
3963
*
4064
* @param stateName The name of the state to link to
4165
* @param params Any parameter values
42-
* @param options Transition options
66+
* @param options Transition options used when the onClick handler fires.
4367
*/
4468
export function useSref(stateName: string, params: object = {}, options: TransitionOptions = {}): LinkProps {
4569
if (!isString(stateName)) {

src/hooks/useSrefActive.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/** @packageDocumentation @reactapi @module react_hooks */
2+
13
import { TransitionOptions } from '@uirouter/core';
24
import { LinkProps, useSref } from './useSref';
35
import { useIsActive } from './useIsActive';
@@ -7,26 +9,40 @@ interface ActiveLinkProps extends LinkProps {
79
}
810

911
/**
10-
* A hook that helps create link to a state and tracks if that state is active.
12+
* A hook to create a link to a state and track its active status.
1113
*
12-
* This hook returns an object the following properties of an sref (short for state reference).
14+
* This hook returns link (anchor tag) props for a given state reference.
15+
* The resulting props can be spread onto an anchor tag.
16+
* If the referenced state (and params) is active, then the activeClass is returned as the `className` prop.
1317
*
14-
* - href: the browser URL of the referenced state
15-
* - onClick: a mouse event handler that will active the referenced state
16-
* - className: the activeClass parameter when the state or any child state is active, otherwise an empty string
18+
* The props returned from this hook are:
1719
*
18-
* Example
19-
* ```
20-
* function LinkToHome() {
21-
* const sref = useSrefActive('home', null, 'active');
20+
* - `href`: the browser URL of the referenced state
21+
* - `onClick`: a mouse event handler that will active the referenced state
22+
* - `className`: the activeClass parameter when the state (or any child state) is active, otherwise an empty string
23+
*
24+
* Example:
25+
* ```jsx
26+
* function HomeLink() {
27+
* const sref = useSref('home', null, 'active');
2228
* return <a {...sref}>Home</a>
2329
* }
2430
* ```
2531
*
32+
* Example:
33+
* ```jsx
34+
* function UserLink({ userId, username }) {
35+
* const sref = useSref('users.user', { userId: userId }, 'active');
36+
* return <a {...sref}>{username}</a>
37+
* }
38+
* ```
39+
*
40+
* This hook is a variation of the [[useSref]] hook.
41+
*
2642
* @param stateName The name of the state to link to
2743
* @param params Any parameter values
2844
* @param activeClass A css class string to use when the state is active
29-
* @param options Transition options
45+
* @param options Transition options used when the onClick handler fires.
3046
*/
3147
export function useSrefActive(
3248
stateName: string,

src/hooks/useSrefActiveExact.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/** @packageDocumentation @reactapi @module react_hooks */
2+
13
import { TransitionOptions } from '@uirouter/core';
24
import { LinkProps, useSref } from './useSref';
35
import { useIsActive } from './useIsActive';
@@ -7,26 +9,41 @@ interface ActiveLinkProps extends LinkProps {
79
}
810

911
/**
10-
* A hook that helps create link to a state and tracks if that state is active.
12+
* A hook to create a link to a state and track its active status.
1113
*
12-
* This hook returns an object the following properties of an sref (short for state reference).
14+
* This hook returns link (anchor tag) props for a given state reference.
15+
* The resulting props can be spread onto an anchor tag.
16+
* If the referenced state (and params) is active, then the activeClass is returned as the `className` prop.
1317
*
14-
* - href: the browser URL of the referenced state
15-
* - onClick: a mouse event handler that will active the referenced state
16-
* - className: the activeClass parameter when the state is active, otherwise an empty string
18+
* The props returned from this hook are:
1719
*
18-
* Example
19-
* ```
20-
* function LinkToHome() {
21-
* const sref = useSrefActive('home', null, 'active');
20+
* - `href`: the browser URL of the referenced state
21+
* - `onClick`: a mouse event handler that will active the referenced state
22+
* - `className`: the activeClass parameter when the state is active, otherwise an empty string
23+
*
24+
* Example:
25+
* ```jsx
26+
* function HomeLink() {
27+
* const sref = useSref('home', null, 'active');
2228
* return <a {...sref}>Home</a>
2329
* }
2430
* ```
2531
*
32+
* Example:
33+
* ```jsx
34+
* function UserLink({ userId, username }) {
35+
* const sref = useSref('users.user', { userId: userId }, 'active');
36+
* return <a {...sref}>{username}</a>
37+
* }
38+
* ```
39+
*
40+
* This hook is a variation of the [[useSrefActive]] hook.
41+
* This variation does not render the `activeClass` if a child state is active.
42+
*
2643
* @param stateName The name of the state to link to
2744
* @param params Any parameter values
2845
* @param activeClass A css class string to use when the state is active
29-
* @param options Transition options
46+
* @param options Transition options used when the onClick handler fires.
3047
*/
3148
export function useSrefActiveExact(
3249
stateName: string,

src/hooks/useStableCallback.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/** @packageDocumentation @internalapi @module react_hooks */
2+
13
import { useCallback, useRef } from 'react';
24

35
/**
@@ -7,7 +9,7 @@ import { useCallback, useRef } from 'react';
79
* This can be useful if the callback is being stored long term, such as in the Transition Hook registry.
810
*
911
* Example:
10-
* ```
12+
* ```jsx
1113
* const latestValueFromProps = props.value
1214
* const transitionHook = useStableCallback(() => console.log(latestValueFromProps));
1315
* useEffect(() => {

0 commit comments

Comments
 (0)