-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathuseCarouselNav.ts
More file actions
83 lines (74 loc) · 2.45 KB
/
useCarouselNav.ts
File metadata and controls
83 lines (74 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use client';
import { useArrowNavigationGroup } from '@fluentui/react-tabster';
import { getIntrinsicElementProps, slot, useIsomorphicLayoutEffect } from '@fluentui/react-utilities';
import * as React from 'react';
import { useCarouselContext_unstable as useCarouselContext } from '../CarouselContext';
import type {
CarouselNavBaseProps,
CarouselNavBaseState,
CarouselNavProps,
CarouselNavState,
} from './CarouselNav.types';
import { useControllableState } from '@fluentui/react-utilities';
/**
* Create the base state required to render CarouselNav, without design-only props.
*
* @param props - props from this instance of CarouselNav (without appearance)
* @param ref - reference to root HTMLDivElement of CarouselNav
*/
export const useCarouselNavBase_unstable = (
props: CarouselNavBaseProps,
ref: React.Ref<HTMLDivElement>,
): CarouselNavBaseState => {
const focusableGroupAttr = useArrowNavigationGroup({
circular: false,
axis: 'horizontal',
memorizeCurrent: false,
// eslint-disable-next-line @typescript-eslint/naming-convention
unstable_hasDefault: true,
});
// Users can choose controlled or uncontrolled, if uncontrolled, the default is initialized by carousel context
const [totalSlides, setTotalSlides] = useControllableState({
state: props.totalSlides,
initialState: 0,
});
const subscribeForValues = useCarouselContext(ctx => ctx.subscribeForValues);
useIsomorphicLayoutEffect(() => {
return subscribeForValues(data => {
setTotalSlides(data.navItemsCount);
});
}, [subscribeForValues, setTotalSlides]);
return {
totalSlides,
renderNavButton: props.children,
components: {
root: 'div',
},
root: slot.always(
getIntrinsicElementProps('div', {
ref,
role: 'tablist',
...props,
...focusableGroupAttr,
children: null,
}),
{ elementType: 'div' },
),
};
};
/**
* Create the state required to render CarouselNav.
*
* The returned state can be modified with hooks such as useCarouselNavStyles_unstable,
* before being passed to renderCarouselNav_unstable.
*
* @param props - props from this instance of CarouselNav
* @param ref - reference to root HTMLDivElement of CarouselNav
*/
export const useCarouselNav_unstable = (props: CarouselNavProps, ref: React.Ref<HTMLDivElement>): CarouselNavState => {
const { appearance } = props;
return {
...useCarouselNavBase_unstable(props, ref),
appearance,
};
};