X Enterprises
Composables

useFilters

Generic filter composable for menu items and other list data — reads groups from app config, supports custom filter handlers and a shared mobile drawer state.

useFilters

Generic filter composable that powers the menu filter UI. It reads appConfig.xRestaurants.filters for group definitions and shared state, then exposes utilities to update, toggle, check, and apply filters to an items array. Built-in default handlers exist for search, category, dietary, and sort filter IDs; you can register additional custom handlers with registerFilterHandler().

State is held in useState (x-restaurant-filters / x-restaurant-mobile-filters) so multiple components share the same filter selections.

Usage

const {
  config,
  activeFilters,
  hasActiveFilters,
  activeFiltersCount,
  isMobileFiltersOpen,
  updateFilter,
  toggleCheckbox,
  isCheckboxSelected,
  clearAllFilters,
  filterItems,
  registerFilterHandler,
} = useFilters()

// Register a custom filter handler for a custom filter group ID
registerFilterHandler("spiceLevel", (items, value) => {
  const levels = value as number[]
  if (!levels?.length) return items
  return items.filter(item => levels.includes(item.spiceLevel ?? 0))
})

const visible = filterItems(menuItems)

Returns

KeyTypeDescription
configComputedRef<FiltersConfig>Resolved filters config from appConfig.xRestaurants.filters (with defaults: groups: [], showClearAll: true, mobileBreakpoint: 'lg', title: 'Filters', fixed: false).
activeFiltersRef<ActiveFilters>Reactive map of groupId → FilterValue.
hasActiveFiltersComputedRef<boolean>true when any group has a non-empty value.
activeFiltersCountComputedRef<number>Total number of active filter values across all groups.
isMobileFiltersOpenRef<boolean>Shared open/closed state for the mobile filter drawer.
updateFilter(groupId: string, value: FilterValue) => voidReplaces the value for the given group.
toggleCheckbox(groupId: string, value: string | number) => voidAdds/removes value from a checkbox group's array.
isCheckboxSelected(groupId: string, value: string | number) => booleantrue if value is in the group's array.
clearAllFilters() => voidResets activeFilters to {}.
filterItems(items: MenuItem[]) => MenuItem[]Applies active filters (custom handler first, default handler fallback) to the items array.
registerFilterHandler(filterId: string, handler: (items, value) => items) => voidRegisters a custom filter handler for a given filter group ID.

Built-in default handlers

filterIdBehavior
searchCase-insensitive substring match against item.name and item.description.
categoryKeeps items whose category is in the active array.
dietaryKeeps items where any active tag is present in item.tags.
sortSorts by name, price-asc, or price-desc.

AI Context

composable: useFilters
package: "@xenterprises/nuxt-x-restaurants"
use-when: >
  Driving the menu filter UI for menu lists, or any list of items that needs
  the same group/check/range UI. Use registerFilterHandler() to add custom
  group behaviour beyond the built-in search/category/dietary/sort handlers.
  For the restaurants listing page prefer the higher-level
  useRestaurantFilters() composable.
Copyright © 2026