Skip to content
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

fix(useAdaptivityHasPointer): two pass rendering #5666

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/vkui/src/hooks/useAdaptivityHasPointer.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import { renderHook } from '@testing-library/react-hooks';
import { AdaptivityProvider } from '../components/AdaptivityProvider/AdaptivityProvider';
import { useAdaptivityHasPointer } from './useAdaptivityHasPointer';

describe(useAdaptivityHasPointer, () => {
it('returns on client', () => {
const { result } = renderHook(useAdaptivityHasPointer, {});
expect(result.current).toEqual(true);
});

it('context hasPointer={true}', () => {
const { result } = renderHook(useAdaptivityHasPointer, {
wrapper: ({ children }) => (
<AdaptivityProvider hasPointer={true}>{children}</AdaptivityProvider>
),
});
expect(result.current).toEqual(true);
});

it('context hasPointer={false}', () => {
const { result } = renderHook(useAdaptivityHasPointer, {
wrapper: ({ children }) => (
<AdaptivityProvider hasPointer={false}>{children}</AdaptivityProvider>
),
});
expect(result.current).toEqual(false);
});
});
11 changes: 6 additions & 5 deletions packages/vkui/src/hooks/useAdaptivityHasPointer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ export function useAdaptivityHasPointer(deferDetect?: true): undefined | boolean
export function useAdaptivityHasPointer(deferDetect?: false): boolean;
export function useAdaptivityHasPointer(deferDetect = true): undefined | boolean {
const { hasPointer: hasPointerContext } = React.useContext(AdaptivityContext);
const hasPointer = hasPointerContext === undefined ? hasPointerLib : hasPointerContext;

const isClient = useIsClient(!deferDetect);
if (!isClient) {
return undefined;
const needTwoPassRendering = !deferDetect || hasPointerContext !== undefined;

const isClient = useIsClient(needTwoPassRendering);
if (!isClient || hasPointerContext !== undefined) {
return hasPointerContext;
}

return hasPointer;
return hasPointerLib;
}