Skip to content

feat: allow enabled to accept a callback for lazy evaluation #105

Description

@p7gg

Feature Request

Summary

Currently, the enabled option in HotkeyOptions only accepts a boolean. It would be useful to also accept a callback (i.e. () => boolean) to support lazy evaluation.

Motivation

There are cases where the enabled state depends on values that are re-evaluated at hotkey trigger time, not at registration time. Having to re-register the hotkey every time that condition changes is cumbersome.

A callback form would allow users to defer the check to when the hotkey is actually fired:

useHotkeys('mod+s', save, {
  enabled: () => !isReadOnly,
})

Proposed API change

// Current
enabled?: boolean

// Proposed
enabled?: boolean | (() => boolean)

The implementation would just call enabled() instead of reading it directly when it's a function.

Alternatives considered

Passing a reactive enabled value that changes over time works, but requires the caller to manage that reactivity and causes unnecessary re-registrations.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions