-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathuseInlineDrawer.ts
More file actions
114 lines (101 loc) · 3.49 KB
/
useInlineDrawer.ts
File metadata and controls
114 lines (101 loc) · 3.49 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'use client';
import * as React from 'react';
import { PresenceDirection, presenceMotionSlot } from '@fluentui/react-motion';
import { getIntrinsicElementProps, slot } from '@fluentui/react-utilities';
import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';
import { type DrawerMotionParams, InlineDrawerMotion } from '../../shared/drawerMotions';
import { useDrawerDefaultProps } from '../../shared/useDrawerDefaultProps';
import type {
InlineDrawerBaseProps,
InlineDrawerBaseState,
InlineDrawerProps,
InlineDrawerState,
SurfaceMotionSlotProps,
} from './InlineDrawer.types';
const STATIC_MOTION = {
active: true,
canRender: true,
ref: React.createRef<HTMLDivElement>(),
type: 'idle' as const,
};
/**
* Create the base state required to render InlineDrawer without design-specific props.
*
* The returned state can be composed with `useInlineDrawer_unstable` or used directly
* to build custom-styled InlineDrawer variants.
*
* @param props - props from this instance of InlineDrawer (without size, position, separator, surfaceMotion)
* @param ref - reference to root HTMLElement of InlineDrawer
*/
export const useInlineDrawerBase_unstable = (
props: InlineDrawerBaseProps,
ref: React.Ref<HTMLElement>,
): InlineDrawerBaseState => {
const { open = false, unmountOnClose = true } = props;
return {
components: {
root: 'div',
},
root: slot.always(
getIntrinsicElementProps('div', {
...props,
ref,
'aria-hidden': !unmountOnClose && !open ? true : undefined,
}),
{ elementType: 'div' },
),
open,
unmountOnClose,
};
};
/**
* Create the state required to render InlineDrawer.
*
* The returned state can be modified with hooks such as useInlineDrawerStyles_unstable,
* before being passed to renderInlineDrawer_unstable.
*
* @param props - props from this instance of InlineDrawer
* @param ref - reference to root HTMLElement of InlineDrawer
*/
export const useInlineDrawer_unstable = (props: InlineDrawerProps, ref: React.Ref<HTMLElement>): InlineDrawerState => {
const { size, position, open, unmountOnClose } = useDrawerDefaultProps(props);
const { separator = false, surfaceMotion, ...baseProps } = props;
const { dir } = useFluent();
const [animationDirection, setAnimationDirection] = React.useState<PresenceDirection>(open ? 'enter' : 'exit');
const state = useInlineDrawerBase_unstable(baseProps as InlineDrawerBaseProps, ref);
return {
...state,
components: {
root: 'div',
// casting from internal type that has required properties
// to external type that only has optional properties
// converting to unknown first as both Function component signatures are not compatible
surfaceMotion: InlineDrawerMotion as unknown as React.FC<SurfaceMotionSlotProps>,
},
open,
unmountOnClose,
position,
size,
separator,
animationDirection,
surfaceMotion: presenceMotionSlot<DrawerMotionParams>(surfaceMotion, {
elementType: InlineDrawerMotion,
defaultProps: {
position,
size,
dir,
visible: open,
appear: unmountOnClose,
unmountOnExit: unmountOnClose,
onMotionFinish: (_, { direction }) => setAnimationDirection(direction),
onMotionStart: (_, { direction }) => {
if (direction === 'enter') {
setAnimationDirection('enter');
}
},
},
}),
// Deprecated props
motion: STATIC_MOTION,
} satisfies InlineDrawerState;
};