feat: add blur and escape key callbacks to Toggle component#60
Merged
Conversation
Introduces `onBlur` and `onEscPressed` callback options for the Toggle component, enabling custom logic when the toggle loses focus or the Escape key is pressed. This provides enhanced control and accessibility, and also refactors the existing `onClick` callback handling for consistency.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 57ec94d. Configure here.
When the 'closeOnEscPress' option was enabled, the Toggle component would use 'el.click()' to close itself via the Escape key. This had the unintended side effect of triggering the 'onClick' callback, which is intended for explicit user clicks, not programmatic closing via keyboard. This commit replaces 'el.click()' with 'this.close()' in the 'onKeyDown' handler, ensuring that the 'onClick' callback is only fired for actual click events. The tests and example have been updated to confirm this separation of concerns and verify the click counter does not increment on Escape key press.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Introduces
onBlurandonEscPressedcallback options for the Toggle component, enabling custom logic when the toggle loses focus or the Escape key is pressed. This provides enhanced control and accessibility, and also refactors the existingonClickcallback handling for consistency.Note
Low Risk
Localized Toggle API extension and event-listener cleanup; behavior change is intentional (Escape no longer triggers click) and covered by tests.
Overview
Adds
onBlurandonEscPressedoptions toToggle, so consumers can run custom logic when focus leaves the control or when Escape is pressed while content is open. Blur/escape listeners are wired when those callbacks are set or whencloseOnBlur/closeOnEscPressis enabled; closing on blur/escape is no longer tied to firing the new callbacks.Escape handling is refactored: each instance uses a bound
_handleEscPressonwindow(removed ondestroy) instead of a global listener that simulated a button click. Escape now callsclose()whencloseOnEscPressis true and invokesonEscPressedwithout incrementing click side effects.onClickis aligned with the same pattern (boundOnClick, “custom” detected vs default no-ops). Docs, the accessible-toggle example (event status UI), and Playwright coverage for click, blur, and escape are updated.Reviewed by Cursor Bugbot for commit fb27d8e. Bugbot is set up for automated code reviews on this repo. Configure here.