feat(theme): add before-theme-switch hook for custom logic before the… #4957
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.
Description
This PR introduces a new injection hook
before-appearance-switch
in the theme context.It allows theme developers to define custom logic (synchronous or asynchronous) that will be executed before switching between dark and light appearance.
If the injected function returns
false
, the theme switch will be canceled.Typical use-cases include custom animations, confirmation dialogs, or delaying the switch for UI synchronization.
For example, if I want to implement a background image switch animation after I click the button—which uses CSS that depends on the presence of
class="dark"
—the class is toggled immediately, so the transition does not have a chance to animate. The browser applies the new class and final styles in the same rendering frame, which causes the background to switch instantly without any smooth animation.With this new
before-appearance-switch
hook, I can delay the actual theme class change until my custom animation or transition has completed. For instance, I can trigger an animation, wait for it to finish (using a Promise or timeout), and then toggle the.dark
class, ensuring that the background image transition is smooth and visually appealing.Example usage:
Why is this needed?
Because toggling the
.dark
class immediately does not allow transitions/animations that depend on the class change to play smoothly. This hook gives developers precise control over when the class is toggled, so they can coordinate it with custom UI effects.Hook Usage example:
In your theme component:
This change is fully backward compatible:
If
before-appearance-switch
is not provided, the theme switch works as before.Linked Issues
No related issues found.
Additional Context
Tip
The author of this PR can publish a preview release by commenting
/publish
below.