feat: allow `enabled` to accept a callback for lazy evaluation
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 70/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Quiet
- Tech stack
- typescript
- Domain
- frontend
Research direction
Start at the HotkeyOptions definition for the enabled option and trace where enabled is checked when a hotkey fires. Update the type and runtime handling so boolean and callback forms are supported, then verify that the callback is evaluated at trigger time and that existing boolean behavior remains unchanged.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- TypeScript
- Stars
- 719
- Forks
- 47
- Avg merge
- 5d 5h
- Merged PRs (30d)
- 1
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from TanStack/hotkeys
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 3/5 1-2 days Newbie friendliness 74/100
-
Difficulty 3/5 1-2 days Newbie friendliness 76/100
-
Difficulty 4/5 3-5 days Newbie friendliness 50/100
All issues in TanStack/hotkeys
Similar issues
-
calcite-components needs triage refactor
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Esri/calcite-design-system#15203 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 91/100
-
community first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
Difficulty 1/5 Under an hour Newbie friendliness 95/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Automattic/studio#4908 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 90/100