-
Notifications
You must be signed in to change notification settings - Fork 0
feat: implement subscribeKeyboardHeight and useKeyboardHeight function #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| vi.mock('@webview-kit/core', () => ({ | ||
| subscribeKeyboardHeight: vi.fn(), | ||
| })); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This mock path @webview-kit/core doesn't match the actual import path used in useKeyboardHeight.ts 🤣
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DONE!
| }: SubscribeKeyboardHeightOptions): SubscribeKeyboardHeightResult { | ||
| const handler = () => callback(getKeyboardHeight()); | ||
|
|
||
| const visualViewport = window.visualViewport; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will throw ReferenceError: window is not defined in SSR environments (Next.js, Remix, etc.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0e6bf91 DONE!
| visualViewport.addEventListener('resize', handler); | ||
| visualViewport.addEventListener('scroll', handler); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The resize and scroll events can fire dozens or even hundreds of times per second. When the keyboard appears with animation, this could lead to unnecessary re-renders or performance issues.
I can think of a few possible approaches here, but it might also be reasonable to treat optimization as a follow-up.
• Provide an optional throttle configuration.
• Wrap the handler with requestAnimationFrame by default.
• Expose a throttle option for more fine-grained control.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since resize and scroll can fire almost simultaneously, the callback may be called twice with the same value. 🤔
I was just thinking out loud.... maybe we could keep the last height in a small internal state and skip the callback when there’s no change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the feedback! I applied the throttle logic and also used a variable to detect if the height has changed from the previous one, in order to block calling multiple callback function 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
similar to the comment about duplicate callbacks, it would be nice to add a test for this as well.
There currently isn’t a test that verifies whether the callback is invoked multiple times when resize and scroll fire at the same time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just an additional idea — what do you think about adding a test case that simulates rapid event firing?
For example, something like this to ensure the callback isn’t called excessively when events occur in quick
// Simulate rapid events
for (let i = 0; i < 100; i++) {
resizeHandler?.();
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
applied test codes, referencing your useScrollDirection hook!
| [immediate] | ||
| ); | ||
|
|
||
| return keyboardHeight; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👞
| return keyboardHeight; | |
| return { keyboardHeight }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DONE! 246e424
e1b38e7 to
7fef09e
Compare
kimyouknow
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Overview
This PR adds the subscribeKeyboardHeight() utility function to react-simplikit, which provides a reactive subscription pattern for listening to on-screen keyboard height changes.
Checklist
yarn run fixto format and lint the code and docs?yarn run test:coverageto make sure there is no uncovered line?