npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@foundrykit/hooks

v1.0.10

Published

A collection of custom React hooks for common UI patterns and functionality. Provides reusable logic for state management, user interactions, and component behavior.

Downloads

31

Readme

@foundrykit/hooks

A collection of custom React hooks for common UI patterns and functionality. Provides reusable logic for state management, user interactions, and component behavior.

Features

  • Custom hooks - Reusable logic for common UI patterns
  • TypeScript support - Full type safety with comprehensive definitions
  • Performance optimized - Efficient implementations with proper cleanup
  • Accessibility focused - Hooks that support accessibility best practices
  • Framework agnostic - Works with any React setup

Installation

pnpm add @foundrykit/hooks

Available Hooks

State Management

  • useLocalStorage - Persist state in localStorage
  • useDebouncedValue - Debounce values for performance
  • useMediaQuery - React to media query changes
  • useClickOutside - Detect clicks outside elements

Form Hooks

  • useForm - Form state management
  • useField - Individual field management
  • useValidation - Form validation logic

UI Hooks

  • useDialog - Dialog/modal state management
  • usePopover - Popover positioning and state
  • useTooltip - Tooltip positioning and visibility

Utility Hooks

  • usePrevious - Access previous value
  • useInterval - Set up intervals with cleanup
  • useTimeout - Set up timeouts with cleanup

Usage

State Management Hooks

useLocalStorage

Persist state in localStorage with automatic serialization:

import { useLocalStorage } from '@foundrykit/hooks';

function UserPreferences() {
  const [theme, setTheme] = useLocalStorage('theme', 'light');
  const [language, setLanguage] = useLocalStorage('language', 'en');

  return (
    <div>
      <select value={theme} onChange={e => setTheme(e.target.value)}>
        <option value='light'>Light</option>
        <option value='dark'>Dark</option>
      </select>

      <select value={language} onChange={e => setLanguage(e.target.value)}>
        <option value='en'>English</option>
        <option value='es'>Spanish</option>
      </select>
    </div>
  );
}

useDebouncedValue

Debounce values to improve performance:

import { useDebouncedValue } from '@foundrykit/hooks';

function SearchComponent() {
  const [query, setQuery] = useState('');
  const debouncedQuery = useDebouncedValue(query, 300);

  // This effect only runs when debouncedQuery changes
  useEffect(() => {
    if (debouncedQuery) {
      searchAPI(debouncedQuery);
    }
  }, [debouncedQuery]);

  return (
    <input
      value={query}
      onChange={e => setQuery(e.target.value)}
      placeholder='Search...'
    />
  );
}

useMediaQuery

React to media query changes:

import { useMediaQuery } from '@foundrykit/hooks';

function ResponsiveComponent() {
  const isMobile = useMediaQuery('(max-width: 768px)');
  const isTablet = useMediaQuery('(min-width: 769px) and (max-width: 1024px)');
  const isDesktop = useMediaQuery('(min-width: 1025px)');

  return (
    <div>
      {isMobile && <MobileLayout />}
      {isTablet && <TabletLayout />}
      {isDesktop && <DesktopLayout />}
    </div>
  );
}

UI Interaction Hooks

useClickOutside

Detect clicks outside an element:

import { useClickOutside } from '@foundrykit/hooks';

function Dropdown({ isOpen, onClose, children }) {
  const ref = useRef(null);

  useClickOutside(ref, () => {
    if (isOpen) {
      onClose();
    }
  });

  if (!isOpen) return null;

  return (
    <div ref={ref} className='dropdown'>
      {children}
    </div>
  );
}

useDialog

Manage dialog/modal state:

import { useDialog } from '@foundrykit/hooks';

function ModalExample() {
  const { isOpen, open, close, toggle } = useDialog();

  return (
    <div>
      <button onClick={open}>Open Modal</button>

      {isOpen && (
        <div className='modal-overlay' onClick={close}>
          <div className='modal-content' onClick={e => e.stopPropagation()}>
            <h2>Modal Content</h2>
            <button onClick={close}>Close</button>
          </div>
        </div>
      )}
    </div>
  );
}

Form Hooks

useForm

Complete form state management:

import { useForm } from '@foundrykit/hooks';

function ContactForm() {
  const {
    values,
    errors,
    touched,
    handleChange,
    handleBlur,
    handleSubmit,
    isSubmitting,
  } = useForm({
    initialValues: {
      name: '',
      email: '',
      message: '',
    },
    validate: values => {
      const errors = {};
      if (!values.name) errors.name = 'Name is required';
      if (!values.email) errors.email = 'Email is required';
      if (!values.message) errors.message = 'Message is required';
      return errors;
    },
    onSubmit: async values => {
      await submitForm(values);
    },
  });

  return (
    <form onSubmit={handleSubmit}>
      <input
        name='name'
        value={values.name}
        onChange={handleChange}
        onBlur={handleBlur}
        className={touched.name && errors.name ? 'error' : ''}
      />
      {touched.name && errors.name && <span>{errors.name}</span>}

      <input
        name='email'
        value={values.email}
        onChange={handleChange}
        onBlur={handleBlur}
        className={touched.email && errors.email ? 'error' : ''}
      />
      {touched.email && errors.email && <span>{errors.email}</span>}

      <textarea
        name='message'
        value={values.message}
        onChange={handleChange}
        onBlur={handleBlur}
        className={touched.message && errors.message ? 'error' : ''}
      />
      {touched.message && errors.message && <span>{errors.message}</span>}

      <button type='submit' disabled={isSubmitting}>
        {isSubmitting ? 'Sending...' : 'Send Message'}
      </button>
    </form>
  );
}

Utility Hooks

usePrevious

Access the previous value:

import { usePrevious } from '@foundrykit/hooks';

function Counter() {
  const [count, setCount] = useState(0);
  const previousCount = usePrevious(count);

  return (
    <div>
      <p>Current: {count}</p>
      <p>Previous: {previousCount}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}

useInterval

Set up intervals with automatic cleanup:

import { useInterval } from '@foundrykit/hooks';

function Timer() {
  const [seconds, setSeconds] = useState(0);
  const [isRunning, setIsRunning] = useState(false);

  useInterval(() => setSeconds(seconds + 1), isRunning ? 1000 : null);

  return (
    <div>
      <p>Seconds: {seconds}</p>
      <button onClick={() => setIsRunning(!isRunning)}>
        {isRunning ? 'Pause' : 'Start'}
      </button>
    </div>
  );
}

Advanced Usage

Custom Hook Composition

Combine multiple hooks for complex functionality:

import {
  useLocalStorage,
  useDebouncedValue,
  useMediaQuery,
} from '@foundrykit/hooks';

function useResponsiveSearch() {
  const [query, setQuery] = useLocalStorage('search-query', '');
  const debouncedQuery = useDebouncedValue(query, 300);
  const isMobile = useMediaQuery('(max-width: 768px)');

  const searchResults = useMemo(() => {
    if (!debouncedQuery) return [];
    return performSearch(debouncedQuery, { limit: isMobile ? 5 : 10 });
  }, [debouncedQuery, isMobile]);

  return {
    query,
    setQuery,
    debouncedQuery,
    searchResults,
    isMobile,
  };
}

Hook with Dependencies

import { useClickOutside } from '@foundrykit/hooks';

function useDropdown(initialState = false) {
  const [isOpen, setIsOpen] = useState(initialState);
  const ref = useRef(null);

  useClickOutside(ref, () => setIsOpen(false));

  const open = useCallback(() => setIsOpen(true), []);
  const close = useCallback(() => setIsOpen(false), []);
  const toggle = useCallback(() => setIsOpen(!isOpen), [isOpen]);

  return {
    isOpen,
    ref,
    open,
    close,
    toggle,
  };
}

Performance Optimization

import { useDebouncedValue } from '@foundrykit/hooks';

function OptimizedSearch() {
  const [query, setQuery] = useState('');
  const debouncedQuery = useDebouncedValue(query, 500);

  // Memoize expensive operation
  const searchResults = useMemo(() => {
    if (!debouncedQuery) return [];
    return expensiveSearch(debouncedQuery);
  }, [debouncedQuery]);

  return (
    <div>
      <input
        value={query}
        onChange={e => setQuery(e.target.value)}
        placeholder='Search...'
      />
      <SearchResults results={searchResults} />
    </div>
  );
}

TypeScript Support

All hooks include comprehensive TypeScript definitions:

import { useLocalStorage, useDebouncedValue } from '@foundrykit/hooks';

// Type-safe localStorage hook
const [user, setUser] = useLocalStorage<User>('user', null);

// Type-safe debounced value
const [searchTerm, setSearchTerm] = useState<string>('');
const debouncedSearchTerm = useDebouncedValue<string>(searchTerm, 300);

Best Practices

Hook Dependencies

// ✅ Good - Stable callback
const handleClick = useCallback(() => {
  console.log('clicked');
}, []);

// ❌ Avoid - Unstable callback
const handleClick = () => {
  console.log('clicked');
};

Cleanup

// ✅ Good - Proper cleanup
useEffect(() => {
  const timer = setTimeout(() => {
    // do something
  }, 1000);

  return () => clearTimeout(timer);
}, []);

// ❌ Avoid - Missing cleanup
useEffect(() => {
  setTimeout(() => {
    // do something
  }, 1000);
}, []);

Performance

// ✅ Good - Memoized expensive operations
const expensiveValue = useMemo(() => {
  return computeExpensiveValue(data);
}, [data]);

// ❌ Avoid - Recomputing on every render
const expensiveValue = computeExpensiveValue(data);

Contributing

When adding new hooks:

  1. Follow React hooks best practices
  2. Include comprehensive TypeScript definitions
  3. Add JSDoc documentation
  4. Write unit tests
  5. Include usage examples
  6. Update this README

License

MIT