UIBeginnerSSR-safehooks/ui/useDropdown.ts

useDropdown

Alias of usePopover.

Import

TypeScript
import { useDropdown } from '@/hooks';

API

Signature

useDropdown(options?)

When to use

Use useDropdown instead of re-implementing the same pattern in multiple components.

SSR / Next.js

DOM extras (scroll lock, focus trap) run only in effects.

Live playground

Edit the example and see it run against the real @/hooks package in this app.

Live playgroundEdit · run with real @/hooks · useDropdown
Editor
function Drop() {
  const triggerRef = useRef<HTMLButtonElement>(null);
  const contentRef = useRef<HTMLDivElement>(null);
  const dd = useDropdown({ triggerRef, contentRef });
  return (
    <>
      <button ref={triggerRef} {...dd.getTriggerProps()}>Menu</button>
      {dd.isOpen && <div ref={contentRef}>Item</div>}
    </>
  );
}
Preview

Implementation source

Full library source for useDropdown — copy into your project when you are not installing a package yet.

hooks/ui/useDropdown.ts
import { usePopover, type UsePopoverOptions } from './usePopover';

/** Alias of {@link usePopover} for dropdown menus. */
export function useDropdown(options?: UsePopoverOptions) {
  return usePopover(options);
}

Copy-paste example

useDropdown.example.tsx
'use client';

import { useRef } from 'react';
import { useDropdown } from '@/hooks';

export function Drop() {
  const triggerRef = useRef<HTMLButtonElement>(null);
  const contentRef = useRef<HTMLDivElement>(null);
  const dd = useDropdown({ triggerRef, contentRef });
  return (
    <>
      <button ref={triggerRef} {...dd.getTriggerProps()}>Menu</button>
      {dd.isOpen && <div ref={contentRef}>Item</div>}
    </>
  );
}

Production checklist

  • Called only from Client Components or other custom hooks
  • Type parameters filled in (e.g. useFetch<User[]>)
  • Tip: These are headless — you own the markup and styles.
  • Source of truth: open hooks/ui/useDropdown.ts for implementation details