Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit 1094799

Browse files
docs(use-disclosure): update types and JSDoc
1 parent 187fd1e commit 1094799

File tree

1 file changed

+92
-25
lines changed

1 file changed

+92
-25
lines changed

packages/vue-composables/src/use-disclosure.ts

Lines changed: 92 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,101 @@
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"
29
import { useId } from "./use-id"
310

411
export interface UseDisclosureProps {
12+
/**
13+
* Defines open state from outside dynamic state being passed in.
14+
*
15+
* Overrides `defaultIsOpen` prop.
16+
*/
517
isOpen?: boolean
18+
/**
19+
* Default state on render. Overriden by `isOpen` prop dynamically
20+
* if outside state should pass in a truthy value
21+
*/
622
defaultIsOpen?: boolean
23+
/**
24+
* Additional actions to run when the targeted element is closed.
25+
*/
726
onClose?(): void
27+
/**
28+
* Additional actions to run when the targeted element is opened.
29+
*/
830
onOpen?(): void
31+
/**
32+
* Custom id to connect the toggle with the targeted element for accessibility.
33+
*
34+
* @default `disclosure-<uid>`
35+
*/
936
id?: string
1037
}
1138

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+
1291
/**
1392
* Handles common open, close, or toggle scenarios.
93+
*
1494
* It can be used to control feedback components such as `Modal`, `AlertDialog`, `Drawer`, etc.
1595
*/
16-
export function useDisclosure(props: UseDisclosureProps = {}) {
96+
export function useDisclosure(
97+
props: UseDisclosureProps = {}
98+
): ReturnUseDisclosureType {
1799
const {
18100
isOpen: isOpenProp,
19101
onClose: handleClose,
@@ -24,7 +106,7 @@ export function useDisclosure(props: UseDisclosureProps = {}) {
24106

25107
const isOpenState = ref(defaultIsOpen || false)
26108

27-
const isOpen = ref<boolean>(
109+
const isOpen: ReturnUseDisclosureType["isOpen"] = ref(
28110
isOpenProp !== undefined ? isOpenProp : isOpenState.value
29111
)
30112

@@ -49,35 +131,20 @@ export function useDisclosure(props: UseDisclosureProps = {}) {
49131

50132
const toggle = () => (isOpen.value ? close() : open())
51133

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(() => ({
62135
"aria-expanded": isOpen.value,
63136
"aria-controls": id.value,
64137
onClick() {
65138
toggle()
66139
},
67140
}))
68141

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+
)
81148

82149
watchEffect(() => {
83150
isOpen.value = isOpenState.value

0 commit comments

Comments
 (0)