|
1 |
| -import { StateService } from '@uirouter/core'; |
| 1 | +import { StateDeclaration } from '@uirouter/core'; |
2 | 2 | import { useEffect, useMemo, useState } from 'react';
|
| 3 | +import { UIRouterReact } from '../core'; |
3 | 4 | import { useDeepObjectDiff } from './useDeepObjectDiff';
|
4 | 5 | import { useOnStateChanged } from './useOnStateChanged';
|
5 | 6 | import { useRouter } from './useRouter';
|
| 7 | +import { useViewContextState } from './useViewContextState'; |
6 | 8 |
|
7 |
| -function checkIfActive(stateService: StateService, stateName: string, params: object, exact: boolean) { |
8 |
| - return exact ? stateService.is(stateName, params) : stateService.includes(stateName, params); |
| 9 | +function checkIfActive( |
| 10 | + router: UIRouterReact, |
| 11 | + stateName: string, |
| 12 | + params: object, |
| 13 | + relative: StateDeclaration, |
| 14 | + exact: boolean |
| 15 | +) { |
| 16 | + return exact |
| 17 | + ? router.stateService.is(stateName, params, { relative }) |
| 18 | + : router.stateService.includes(stateName, params, { relative }); |
9 | 19 | }
|
10 | 20 |
|
11 | 21 | export function useIsActive(stateName: string, params = null, exact = false) {
|
12 |
| - const { stateService } = useRouter(); |
| 22 | + const router = useRouter(); |
| 23 | + const relative = useViewContextState(router); |
13 | 24 | // Don't re-compute initialIsActive on every render
|
14 |
| - const initialIsActive = useMemo(() => checkIfActive(stateService, stateName, params, exact), []); |
| 25 | + const initialIsActive = useMemo(() => checkIfActive(router, stateName, params, relative, exact), []); |
15 | 26 | const [isActive, setIsActive] = useState(initialIsActive);
|
16 | 27 |
|
17 | 28 | const checkIfActiveChanged = () => {
|
18 |
| - const newIsActive = checkIfActive(stateService, stateName, params, exact); |
| 29 | + const newIsActive = checkIfActive(router, stateName, params, relative, exact); |
19 | 30 | if (newIsActive !== isActive) {
|
20 | 31 | setIsActive(newIsActive);
|
21 | 32 | }
|
22 | 33 | };
|
23 | 34 |
|
24 | 35 | useOnStateChanged(checkIfActiveChanged);
|
25 |
| - useEffect(checkIfActiveChanged, [stateService, stateName, useDeepObjectDiff(params), exact]); |
| 36 | + useEffect(checkIfActiveChanged, [router, stateName, useDeepObjectDiff(params), exact]); |
26 | 37 |
|
27 | 38 | return isActive;
|
28 | 39 | }
|
0 commit comments