Skip to content

Conversation

@zztnrudzz13
Copy link
Collaborator

@zztnrudzz13 zztnrudzz13 commented Jan 9, 2026

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.

  • Add subscribeKeyboardHeight() function with cleanup support
  • Add comprehensive unit tests

Checklist

  • Did you write the test code?
  • Have you run yarn run fix to format and lint the code and docs?
  • Have you run yarn run test:coverage to make sure there is no uncovered line?
  • Did you write the JSDoc?

Comment on lines 8 to 10
vi.mock('@webview-kit/core', () => ({
subscribeKeyboardHeight: vi.fn(),
}));
Copy link
Collaborator

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 🤣

Copy link
Collaborator Author

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;
Copy link
Collaborator

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.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0e6bf91 DONE!

Comment on lines +68 to +69
visualViewport.addEventListener('resize', handler);
visualViewport.addEventListener('scroll', handler);
Copy link
Collaborator

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.

Copy link
Collaborator

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?

Copy link
Collaborator Author

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 😄

Copy link
Collaborator

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.

Copy link
Collaborator

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?.();
}

Copy link
Collaborator Author

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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👞

Suggested change
return keyboardHeight;
return { keyboardHeight };

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DONE! 246e424

@zztnrudzz13 zztnrudzz13 force-pushed the feat/subscribe-keyboard-height branch from e1b38e7 to 7fef09e Compare January 14, 2026 14:06
Copy link
Collaborator

@kimyouknow kimyouknow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants