-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathuseCarouselNav.ts
More file actions
54 lines (47 loc) · 1.68 KB
/
useCarouselNav.ts
File metadata and controls
54 lines (47 loc) · 1.68 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
'use client';
import { getIntrinsicElementProps, slot, useIsomorphicLayoutEffect } from '@fluentui/react-utilities';
import * as React from 'react';
import { useCarouselContext_unstable as useCarouselContext } from '../CarouselContext';
import type { CarouselNavProps, CarouselNavState } from './CarouselNav.types';
import { useControllableState } from '@fluentui/react-utilities';
/**
* 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;
// 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,
appearance,
renderNavButton: props.children,
components: {
root: 'div',
},
root: slot.always(
getIntrinsicElementProps('div', {
ref,
role: 'tablist',
...props,
focusgroup: 'tablist nowrap nomemory',
children: null,
}),
{ elementType: 'div' },
),
};
};