diff --git a/src/use-dropdown-menu.ts b/src/use-dropdown-menu.ts index 9e52de5..516984b 100644 --- a/src/use-dropdown-menu.ts +++ b/src/use-dropdown-menu.ts @@ -2,12 +2,12 @@ import React, { useState, useRef, createRef, useEffect, useMemo } from 'react'; // Create interface for button properties -interface ButtonProps +interface ButtonProps extends Pick< - React.DetailedHTMLProps, HTMLButtonElement>, + React.DetailedHTMLProps, ButtonElement>, 'onKeyDown' | 'onClick' | 'tabIndex' | 'role' | 'aria-haspopup' | 'aria-expanded' > { - ref: React.RefObject; + ref: React.RefObject; } // A custom Hook that abstracts away the listeners/controls for dropdown menus @@ -15,8 +15,8 @@ export interface DropdownMenuOptions { disableFocusFirstItemOnClick?: boolean; } -interface DropdownMenuResponse { - readonly buttonProps: ButtonProps; +interface DropdownMenuResponse { + readonly buttonProps: ButtonProps; readonly itemProps: { onKeyDown: (e: React.KeyboardEvent) => void; tabIndex: number; @@ -28,7 +28,10 @@ interface DropdownMenuResponse { readonly moveFocus: (itemIndex: number) => void; } -export default function useDropdownMenu(itemCount: number, options?: DropdownMenuOptions): DropdownMenuResponse { +export default function useDropdownMenu( + itemCount: number, + options?: DropdownMenuOptions +): DropdownMenuResponse { // Use state const [isOpen, setIsOpen] = useState(false); const currentFocusIndex = useRef(null); @@ -36,7 +39,7 @@ export default function useDropdownMenu(itemCount: number, options?: DropdownMen const clickedOpen = useRef(false); // Create refs - const buttonRef = useRef(null); + const buttonRef = useRef(null); const itemRefs = useMemo[]>( () => Array.from({ length: itemCount }, () => createRef()), [itemCount] @@ -216,7 +219,7 @@ export default function useDropdownMenu(itemCount: number, options?: DropdownMen }; // Define spreadable props for button and items - const buttonProps: ButtonProps = { + const buttonProps: ButtonProps = { onKeyDown: buttonListener, onClick: buttonListener, tabIndex: 0, diff --git a/website/docs/design/type-parameters.md b/website/docs/design/type-parameters.md new file mode 100644 index 0000000..4d72d87 --- /dev/null +++ b/website/docs/design/type-parameters.md @@ -0,0 +1,19 @@ +--- +title: Type parameters +--- + +You can customize the behavior with optional type parameters. + +Type constraint | Default | Possible values +:--- | :--- | :--- +`HTMLElement` | `HTMLButtonElement` | Any type that extends `HTMLElement` + +```tsx +const { buttonProps, itemProps, isOpen } = useDropdownMenu(3); +``` + +```tsx + +``` \ No newline at end of file diff --git a/website/sidebars.js b/website/sidebars.js index 65a5011..e1b9c0e 100644 --- a/website/sidebars.js +++ b/website/sidebars.js @@ -1,6 +1,6 @@ module.exports = { default: { 'Getting started': ['getting-started/install', 'getting-started/import', 'getting-started/using'], - Design: ['design/return-object', 'design/accessibility', 'design/options'], + Design: ['design/return-object', 'design/accessibility', 'design/options', 'design/type-parameters'], }, };