1
- import { computed , HTMLAttributes , ref , watchEffect } from "vue"
1
+ import {
2
+ computed ,
3
+ ComputedRef ,
4
+ HTMLAttributes ,
5
+ Ref ,
6
+ ref ,
7
+ watchEffect ,
8
+ } from "vue"
2
9
import { useId } from "./use-id"
3
10
4
11
export interface UseDisclosureProps {
12
+ /**
13
+ * Defines open state from outside dynamic state being passed in.
14
+ *
15
+ * Overrides `defaultIsOpen` prop.
16
+ */
5
17
isOpen ?: boolean
18
+ /**
19
+ * Default state on render. Overriden by `isOpen` prop dynamically
20
+ * if outside state should pass in a truthy value
21
+ */
6
22
defaultIsOpen ?: boolean
23
+ /**
24
+ * Additional actions to run when the targeted element is closed.
25
+ */
7
26
onClose ?( ) : void
27
+ /**
28
+ * Additional actions to run when the targeted element is opened.
29
+ */
8
30
onOpen ?( ) : void
31
+ /**
32
+ * Custom id to connect the toggle with the targeted element for accessibility.
33
+ *
34
+ * @default `disclosure-<uid>`
35
+ */
9
36
id ?: string
10
37
}
11
38
39
+ type ReturnUseDisclosureType = {
40
+ /**
41
+ * Returns current state
42
+ *
43
+ * @default false
44
+ */
45
+ isOpen : Ref < boolean >
46
+ /**
47
+ * Actions run when opening targeted element.
48
+ *
49
+ * If target element is uncontrolled, then it includes toggle open.
50
+ */
51
+ open : ( ) => void
52
+ /**
53
+ * Actions run when closing targeted element.
54
+ *
55
+ * If target element is uncontrolled, then it includes toggle closed.
56
+ */
57
+ close : ( ) => void
58
+ /**
59
+ * Actions run when toggling open and closed.
60
+ */
61
+ toggle : ( ) => void
62
+ /**
63
+ * Check if external functionality controls the state of the targeted element
64
+ */
65
+ isControlled : boolean
66
+ /**
67
+ * Computed object of Accessibility attributes and toggling event for the toggling element.
68
+ *
69
+ * `NOTE:` Pass this to the v-bind of the element.
70
+ *
71
+ * i.e. `v-bind='buttonProps'`
72
+ */
73
+ buttonProps : ComputedRef < {
74
+ "aria-expanded" : HTMLAttributes [ "aria-expanded" ]
75
+ "aria-controls" : HTMLAttributes [ "aria-controls" ]
76
+ onClick : HTMLAttributes [ "onClick" ]
77
+ } >
78
+ /**
79
+ * Computed object of Accessibility attributes to show/hide targeted element and for aria controls.
80
+ *
81
+ * `NOTE:` Pass this to the v-bind of the element.
82
+ *
83
+ * i.e. `v-bind='disclosureProps'`
84
+ */
85
+ disclosureProps : ComputedRef < {
86
+ hidden : HTMLAttributes [ "hidden" ]
87
+ id : HTMLAttributes [ "id" ]
88
+ } >
89
+ }
90
+
12
91
/**
13
92
* Handles common open, close, or toggle scenarios.
93
+ *
14
94
* It can be used to control feedback components such as `Modal`, `AlertDialog`, `Drawer`, etc.
15
95
*/
16
- export function useDisclosure ( props : UseDisclosureProps = { } ) {
96
+ export function useDisclosure (
97
+ props : UseDisclosureProps = { }
98
+ ) : ReturnUseDisclosureType {
17
99
const {
18
100
isOpen : isOpenProp ,
19
101
onClose : handleClose ,
@@ -24,7 +106,7 @@ export function useDisclosure(props: UseDisclosureProps = {}) {
24
106
25
107
const isOpenState = ref ( defaultIsOpen || false )
26
108
27
- const isOpen = ref < boolean > (
109
+ const isOpen : ReturnUseDisclosureType [ "isOpen" ] = ref (
28
110
isOpenProp !== undefined ? isOpenProp : isOpenState . value
29
111
)
30
112
@@ -49,35 +131,20 @@ export function useDisclosure(props: UseDisclosureProps = {}) {
49
131
50
132
const toggle = ( ) => ( isOpen . value ? close ( ) : open ( ) )
51
133
52
- /**
53
- * Computed object containing the HTML attributes for the button that
54
- * is triggering the disclosure state
55
- *
56
- * `NOTE:` Pass this to the v-bind of the element.
57
- *
58
- * i.e. `v-bind='buttonProps'`
59
- */
60
-
61
- const buttonProps = computed ( ( ) => ( {
134
+ const buttonProps : ReturnUseDisclosureType [ "buttonProps" ] = computed ( ( ) => ( {
62
135
"aria-expanded" : isOpen . value ,
63
136
"aria-controls" : id . value ,
64
137
onClick ( ) {
65
138
toggle ( )
66
139
} ,
67
140
} ) )
68
141
69
- /**
70
- * Computed object containing the HTML attributes for the element that
71
- * is being effected by the disclosure state.
72
- *
73
- * `NOTE:` Pass this to the v-bind of the element.
74
- *
75
- * i.e. `v-bind='disclosureProps'`
76
- */
77
- const disclosureProps = computed ( ( ) => ( {
78
- hidden : ! isOpen . value ,
79
- id : id . value ,
80
- } ) )
142
+ const disclosureProps : ReturnUseDisclosureType [ "disclosureProps" ] = computed (
143
+ ( ) => ( {
144
+ hidden : ! isOpen . value ,
145
+ id : id . value ,
146
+ } )
147
+ )
81
148
82
149
watchEffect ( ( ) => {
83
150
isOpen . value = isOpenState . value
0 commit comments